C++ Style Guide

Common Rules

The following style guidelines will be followed for both Slice and C++ code:

C1. Braces for compound statements will be on their own lines, at the same indentation level as the statement they are part of. Code and declarations in the brace-enclosed block will be indented one level from the braces. The only exception to this rule is for C++ namespace and Slice module blocks; because the nesting of these scopes could change in the future, if indentation was used it would have to be changed across many source files if the nesting changed. In addition, most code is contained in at least three namespaces/modules, if not more, and indenting the code to represent this would waste a great deal of space and make the code harder to read.
C2. Indentation: 4 spaces per level, no tabs.
C3. Interface, class and structure names will be in UpperCamelCase style.
C4. Function, operation and member variable names will follow lowerCamelCase style.
C5. Comments will follow Doxygen (Javadoc form) guidelines. For Slice this will apply to interface, class and struct definitions, and all operations. For C++, this will apply to classes and operations. All other comments will follow C++ standard convention, which is for "//" comments to be used except for lengthy multi-line comments.
C6. Comment the end of namespace scopes (or any code blocks long enough that would likely not be entirely visible on a typical screen) to improve maintainability / readability.
C7. All comments shall be in the English language.

Slice Style Guide

The following additional style guidelines will be followed in Asterisk SCF Slice definitions:

  1. Empty definitions will be shown by opening and closing braces at the end of the line.
  2. Slice file names will end with "If.ice" (where If is for interface) so that developers in IDEs will be able to tell when they were produced generated code. (The Slice translators use the .ice filename as the basis for the generated C++ and C# filenames.)
  3. #pragma once will be used for multiple-include protection. The Slice translators use the MCPP library for C-style preprocessing, which supports #pragma once. It provides a performance benefit over traditional #ifdef-based include guards in addition to being easier to read and maintain.
Figure 1. Slice Example - TimeIf.ice
C++ Style Guide

To ensure compatibility with GCC, all C++ source and header files must end with a newline.

  1. In pointer and reference declarations, the & and * modifiers should be next to the type, not the variable (Foo& foo)
  2. When declaring variables, modifiers (such as const and auto) should be to written on the left side of the type (const Foo& foo).
  3. Single argument class and struct constructors should always be marked explicit, to keep the compiler from using one in an unexpected way that keeps an error from being detected at compile time.
C++ Header Files

The following additional style guidelines will be followed in C++ header files:

  1. #pragma once will be used for multiple-include protection. #pragma once has been supported in GCC since version 3.4.0, and in Visual Studio since (at least) version 6. It provides a performance benefit over traditional #ifdef-based include guards in addition to being easier to read and maintain.
  2. Forward declarations should be used when the code does not need the entire type declaration; the most common cases of this are pointer and reference variables, which only need the type name declared, but not its complete declaration.
  3. Header files should #include all other headers required to compile them; source files that #include a header file should not be required to know which other headers are required for it. Organize #include statements into blocks based on level. System-level headers first, library headers next (Boost, PJSIP, etc.), Asterisk SCF API headers next, and finally local headers.
  4. Avoid using namespace; in header files, which could pollute the namespace inadvertently for modules which include that header file.
  5. Use lower-case "m" prefix (member) for member variables, to avoid hiding member variables with local variables in methods. This is not done for Slice class member variables, as Slice class members are always public, since Slice classes are more analogous to C++ struct than C++ class.
Figure 1. C++ Header Example
C++ Source Files

The following additional style guidelines will be followed in C++ source (.cpp) files:

  1. Organize #include statements into blocks based on level. System-level headers first, library headers next (Boost, PJSIP, etc.), Asterisk SCF API headers next, and finally local headers.
  2. Flatten namespaces (if desired for convenience) here in the source file, not in header files.
  3. Variables should be initialized using constructor syntax, not assignment syntax, even for Plain-Old-Data types. This is for readability and consistency, not for performance, since modern compilers will compile them to the same object code in nearly all cases. Instead of:
    int x = 1;
    string str = "This is a string.";
    ProxyPtr proxy = 0;
    

    use:

    int x(1);
    string str("This is a string.");
    ProxyPtr proxy(0);
    
  4. Write function calls with no whitespace between the function name and the opening parenthesis, or around the parentheses. The only whitespace should be following any commas that separate arguments, like this:
    doOperation("xyz", 2, fooPtr);
    
  5. Don't format statements (for, if, switch, while, etc.) as if they were function calls; a single space should appear after the keyword, like this:
    if ((x + 1) > 100)
    {
        return y;
    }
    for (x = 0; x++; x < z)
    {
        y *= 3;
    }
    
  6. Compound statements must always be enclosed in braces, even if the block contains only one statement. This rule helps to avoid unexpected execution paths when code is modified for debugging purposes (or any other reason). Instead of this:
    if (x > 5)
        return y;
    while (z > 4)
        z /= 2;
    

    use this:

    if (x > 5)
    {
        return y;
    }
    while (z > 4)
    {
        z /= 2;
    }
    
Figure 1. C++ Source Example
Editor setup
Emacs

Save a copy of the attached file asterisk-scf-style.el into your site-lisp directory. Make sure you have the following in your .emacs files.

Various flavors of Emacs store startup scripts in different places. Check the documentation for your distribution if .emacs doesn't seem quite right for you.
(require 'asterisk-scf-style)

;; Treat .h files as C++ files
(add-to-list 'auto-mode-alist '("//.h//'" . c++-mode))

;; You may want to merge this with existing configuration in your custom-set-variables block
(setq indent-tabs-mode nil
      require-final-newline t
      show-trailing-whitespace t
      c-default-style '((c-mode . "asterisk-scf") (c++-mode . "asterisk-scf"))
      )
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值