代码规范例子

c语言与c++混合编译注意点

https://blog.csdn.net/u013257088/article/details/52598250    __BEGIN_DECLS与__END_DECLS

#pragma once   --》 防止头文件重复包含

https://www.cnblogs.com/qiang-upc/p/11407364.html      头文件重复包含

在c++中使用c的编译方式

#ifdef __cplusplus
extern "C" {
#endif

代码片段

#ifdef __cplusplus
}
#endif

宏条件判断

#if !defined(__BIONIC__) && !defined(__INTRODUCED_IN)
#define __INTRODUCED_IN(x)
#endif

 

#if !defined(__ANDROID__) || __ANDROID_API__ >= 30

代码段

#endif

宏依赖判断

#if defined(__LP64__)     --》  判断依赖的宏
#define PRIPTR "016" PRIx64
typedef uint64_t word_t;
#else
#define PRIPTR "08" PRIx64
typedef uint32_t word_t;
#endif

使用union的场景?

 

struct BacktraceUnwindError {
  enum BacktraceUnwindErrorCode error_code;

  union {      --》  使用union
    // for BACKTRACE_UNWIND_ERROR_ACCESS_MEM_FAILED
    uint64_t addr;
    // for BACKTRACE_UNWIND_ERROR_ACCESS_REG_FAILED
    uint64_t regno;
  } error_info;

  BacktraceUnwindError() : error_code(BACKTRACE_UNWIND_NO_ERROR) {}
};

使用extern的场景

case   在c++中使用c的编译方式

#ifdef __cplusplus
extern "C" {
#endif

 

#ifdef __cplusplus
extern "C++" {
template <typename... Ts>
constexpr int __fake_use_va_args(Ts...) {
  return 0;
}

case  外部函数使用

extern int __property_get_real(const char *, char *, const char *)
    __asm__(__USER_LABEL_PREFIX__ "property_get");

extern int __fake_use_va_args(int, ...);

extern CL_API_ENTRY cl_int CL_API_CALL
clGetPlatformIDs(cl_uint          num_entries,
                 cl_platform_id * platforms,
                 cl_uint *        num_platforms) CL_API_SUFFIX__VERSION_1_0;

case 外部变量使用

extern "C" const GUID IID_IDirect3D9;

extern const GUID IID_IDirect3D9;

使用static的场景

case 在类中使用static修饰方法

class Backtrace {
 public:
  enum ArchEnum : uint8_t {
    ARCH_ARM,
    ARCH_ARM64,
    ARCH_X86,
    ARCH_X86_64,
  };

  static void SetGlobalElfCache(bool enable);

}

case 在类中使用static修饰成员

class CommandQueue : public detail::Wrapper<cl_command_queue>
{
private:
    static std::once_flag default_initialized_;
    static CommandQueue default_;
    static cl_int default_error_;

case 全局变量修饰static

static constexpr int PROT_DEVICE_MAP = 0x8000;

case 修饰普通函数

static int impl_tss_dtor_register(tss_t key, tss_dtor_t dtor)

{}

case 修饰局部变量

ADDR_E_RETURNCODE Gfx10Lib::HwlComputePipeBankXor(
    const ADDR2_COMPUTE_PIPEBANKXOR_INPUT* pIn,     ///< [in] input structure
    ADDR2_COMPUTE_PIPEBANKXOR_OUTPUT*      pOut     ///< [out] output structure
    ) const
{

        if (bankBits != 0)
        {
            if (blockBits == 16)
            {
                const UINT_32        XorPatternLen = 8;
                static const UINT_32 XorBank1b[XorPatternLen] = {0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80};
                static const UINT_32 XorBank2b[XorPatternLen] = {0x00, 0x80, 0x40, 0xC0, 0x80, 0x00, 0xC0, 0x40};
                static const UINT_32 XorBank3b[XorPatternLen] = {0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0};

           }
        }
    }

    return ADDR_OK;
}

 

源文件.c文件或者.cc文件格式

/******************************************************************************
 *
 *  Copyright 2009-2012 Broadcom Corporation                         --》  版权说明

 ******************************************************************************/

#define LOG_TAG "a2dp_aac"                  --》  每个源文件定义一个log tag,方便宏定义调试

Bta_av_aact.cc (d:\git\bt_p0\bta\av):#define LOG_TAG "bt_bta_av"
Bta_av_act.cc (d:\git\bt_p0\bta\av):#define LOG_TAG "bt_bta_av"
Bta_av_api.cc (d:\git\bt_p0\bta\av):#define LOG_TAG "bt_bta_av"              --》  多个文件可以定义一个tag,表示这些文件的关联性,放在一个目录下
Bta_av_ci.cc (d:\git\bt_p0\bta\av):#define LOG_TAG "bt_bta_av"
Bta_av_main.cc (d:\git\bt_p0\bta\av):#define LOG_TAG "bt_bta_av"
 

不是所有的源文件都需要定义log tag

/*****************************************************************************
 *
 *  Filename:      uipc.cc
 *
 *  Description:   UIPC implementation for fluoride                           --》  文件功能说明
 *
 *****************************************************************************/

包含头文件的区域

/*****************************************************************************
 *  Constants & Macros                                                                        --》 宏定义区域
 *****************************************************************************/

#define 宏

/*****************************************************************************
 *  Local type definitions                                                                           
 *****************************************************************************/

typedef enum {
  UIPC_TASK_FLAG_DISCONNECT_CHAN = 0x1,
} tUIPC_TASK_FLAGS;

/*****************************************************************************
 *  Static functions
 *****************************************************************************/
static int uipc_close_ch_locked(tUIPC_STATE& uipc, tUIPC_CH_ID ch_id);

/*****************************************************************************
 *  Externs
 *****************************************************************************/

/*****************************************************************************
 *   Helper functions
 *****************************************************************************/

/*****************************************************************************
 *  Global data
 ****************************************************************************/

/*******************************************************************************
 *
 * Function         a2dp_set_avdt_sdp_ver
 *
 * Description      This function allows the script wrapper to change the                    --》 每个函数功能说明
 *                  avdt version of a2dp.
 *
 * Returns          None
 *
 ******************************************************************************/
void a2dp_set_avdt_sdp_ver(uint16_t avdt_sdp_ver) {
  a2dp_cb.avdt_sdp_ver = avdt_sdp_ver;
}

 

头文件的格式

例如一个.h文件格式

/*
 * Copyright 2018 The Android Open Source Project     --》  版权说明
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.        --》   license说明
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0                      
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and                      --》 适用范围说明
 * limitations under the License.
 */

 

#ifndef ANDROID_DATA_SPACE_H
#define ANDROID_DATA_SPACE_H

内容主体                                              --》  宏定义

#endif

 

定义一个class的格式

namespace bluetooth {

// This class is representing Bluetooth UUIDs across whole stack.                        --》   类class的说明
// Here are some general endianness rules:
// 1. UUID is internally kept as as Big Endian.                                                     --》  类uuid使用格式大端
// 2. Bytes representing UUID coming from upper layers, Java or Binder, are Big
//    Endian.
// 3. Bytes representing UUID coming from lower layer, HCI packets, are Little
//    Endian.
// 4. UUID in storage is always string.
class Uuid final {
 public:
 公有成员

  Uuid() = default;

  // Creates and returns a random 128-bit UUID.
  static Uuid GetRandom();                                                       --》  方法说明

  // Returns the shortest possible representation of this UUID in bytes. Either
  // kNumBytes16, kNumBytes32, or kNumBytes128
  size_t GetShortestRepresentationSize() const;

 
};
}  // namespace bluetooth

定义一个enum类型的格式

/**
 * Buffer pixel formats.                   --》 枚举的用途说明
 */
enum AHardwareBuffer_Format {
    /**
     * Corresponding formats:
     *   Vulkan: VK_FORMAT_R8G8B8A8_UNORM
     *   OpenGL ES: GL_RGBA8
     */
    AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM           = 1,        --》  每个元素说明

    /**
     * 32 bits per pixel, 8 bits per channel format where alpha values are
     * ignored (always opaque).
     * Corresponding formats:
     *   Vulkan: VK_FORMAT_R8G8B8A8_UNORM
     *   OpenGL ES: GL_RGB8
     */
    AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM           = 2,

  .......
};

使用typedef定义

typedef enum {
    RADIO_CLASS_AM_FM = 0,  /* FM (including HD radio) and AM */
    RADIO_CLASS_SAT   = 1,  /* Satellite Radio */
    RADIO_CLASS_DT    = 2,  /* Digital Radio (DAB) */
} radio_class_t;

定义一个struct的格式

/**
 * Buffer description. Used for allocating new buffers and querying                        --》  结构体说明
 * parameters of existing ones.
 */
typedef struct AHardwareBuffer_Desc {
    uint32_t    width;      ///< Width in pixels.
    uint32_t    height;     ///< Height in pixels.                      --》 变量说明
    /**
     * Number of images in an image array. AHardwareBuffers with one
     * layer correspond to regular 2D textures. AHardwareBuffers with
     * more than layer correspond to texture arrays. If the layer count
     * is a multiple of 6 and the usage flag
     * AHARDWAREBUFFER_USAGE_GPU_CUBE_MAP is present, the buffer is
     * a cube map or a cube map array.
     */
    uint32_t    layers;
    uint32_t    format;     ///< One of AHardwareBuffer_Format.
    uint64_t    usage;      ///< Combination of AHardwareBuffer_UsageFlags.
    uint32_t    stride;     ///< Row stride in pixels, ignored for AHardwareBuffer_allocate()
    uint32_t    rfu0;       ///< Initialize to zero, reserved for future use.
    uint64_t    rfu1;       ///< Initialize to zero, reserved for future use.
} AHardwareBuffer_Desc;

 

注意代码块使用宏包起来

通过宏的定义,可以改变build的bin或者image的大小,

宏的开关可以影响代码段code text的分支与大小,如下

#if (BTA_AV_CO_CP_SCMS_T == TRUE)
  /* Content protection info - support SCMS-T */
  uint8_t* p = p_cfg->protect_info;
  *p++ = AVDT_CP_LOSC;
  UINT16_TO_STREAM(p, AVDT_CP_SCMS_T_ID);
  p_cfg->num_protect = 1;
#endif

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值