代码风格

XWOS的的代码风格

C语言

1.头文件

1.1. #define 防护符

所有头文件都应该用 #define 防护符来防止重复导入。防护符的格式是: _<路径>_<文件名>_h_

为了保证符号的唯一性,防护符的名称应该基于该文件在项目目录中的完整文件路径。 例如, xwos 项目中的文件 xwos/mp/thd.h 应该有如下防护:

#ifndef __xwos_mp_thd_h__
#define __xwos_mp_thd_h__
...
#endif /* xwos/mp/thd.h */

1.2. 作用域

C语言没有 namespace ,为了达到同样的效果,C语言中符号命名采用 前缀_ 的方式。 前缀 就是 namespace

1.3. 命名约定

1.3.1. 通用命名规则

总述

函数命名,变量命名,文件命名要有描述性,少用缩写。 别心疼空间,让代码易于新读者理解更重要。 不要用只有项目开发者能理解的缩写,也不要通过砍掉几个字母来缩写单词。

int price_count_reader;    // 无缩写
int num_errors;            // "num" 是一个常见的写法
int num_dns_connections;   // 人人都知道 "DNS" 是什么
int n;                     // 毫无意义
int nerr;                  // 含糊不清的缩写
int n_comp_conns;          // 含糊不清的缩写
int wgc_connections;       // 只有贵团队知道是什么意思
int pc_reader;             // "pc" 有太多可能的解释,除非在特定的上下文环境下没有歧义可使用
int cstmr_id;              // 删减了若干字母

注意, 一些特定的广为人知的缩写是允许的, 例如用 i 表示迭代变量。

1.3.2. 文件命名

文件名要全部小写, 可以包含下划线 _ 。可接受的文件命名示例:

    my_useful_module.c
    my_useful_module_test.c

C文件要以 .c 结尾,头文件以 .h 结尾。 不要使用已经存在于 /usr/include 下的文件名。 通常应尽量让文件名更加明确。 xwlog.h 就比 log.h 要好。 内联函数定义必须放在 .h 文件中. 如果内联函数比较短,就直接将实现也放在 .h 中。

C++语言

命名约定

class/enum/struct/union

帕斯卡命名法,首字母大写

class FooName {
};
Class成员
  • 成员变量:m+帕斯卡命名法
  • 成员函数:驼峰命名法,动词开头
  • 成员常变量:mk+帕斯卡命名法
  • 静态成员变量:s+帕斯卡命名法
  • 静态成员常变量:sk+帕斯卡命名法
  • 静态成员函数:s+帕斯卡命名法,动词开头
class SThd
{
  private:
    xwos_thd_d mThdDesc;
    struct xwos_thd mThd;
    xwer_t mCtorRc;

  public:
    SThd(const char * name, xwstk_t * stack, xwsz_t stack_size,
         xwsz_t stack_guard_size = XWOS_STACK_GUARD_SIZE_DEFAULT,
         xwpr_t priority = XWOS_SKD_PRIORITY_RT_MIN, bool detached = false,
         bool privileged = true);
    ~SThd();
    xwer_t getCtorRc();
    xwer_t getStackInfo(struct xwos_thd_stack_info * stack);
    xwer_t intr();
    xwer_t quit();
    xwer_t join(xwer_t * trc);
    xwer_t stop(xwer_t * trc);
    xwer_t detach();
    struct xwos_thd * getXwosObj();
    xwer_t grab();
    xwer_t put();

  protected:
    virtual xwer_t thdMainFunction();
    void yield();
    void exit(xwer_t rc);
    bool shouldFreeze();
    bool shouldStop();
    xwer_t sleep(xwtm_t dur);
    xwer_t sleepTo(xwtm_t to);
    xwer_t sleepFrom(xwtm_t * from, xwtm_t dur);
    xwer_t freeze(void);

  private:
    static xwer_t sThdMainFunction(SThd * thd);
    static void * operator new(xwsz_t sz) = delete;
    void operator delete(void * obj) = delete;
};
enum成员

帕斯卡命名法

enum LockMode : xwu32_t {
    MtxUnlock = 0,
    MtxLock,
    MtxLockTimed,
    MtxLockUninterruptable,
    MtxLockTry,
};
struct/union成员
  • 驼峰名法
  • 名字比较短可以全小写
struct CanLPdu {
    xwu32_t canId; /**< CAN ID */
    xwu16_t dlc; /**< 数据数组的长度 */
    atomic_xwu16_t freshness; /**< 新鲜值, 0表示丢失,大于0表示有效 */
    xwu8_t sdu[64]; /**< 数据数组,实际长度由 @ref dlc 定义 */
};

union CanLPduPack {
    struct {
        xwu32_t packId; /**< Pack ID */
        struct CanLPdu lpdu[1]; /**< 变长数组,实际数量参考mempool中定义 */
    } field; /**< 字段 */
    xwu8_t rawData[sizeof(xwu32_t) + sizeof(struct CanLPdu)];
};

Makefile

TODO

Shell

TODO

EmacsLisp

TODO

Rust语言

TODO

Lua语言

TODO