You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

241 lines
8.0 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. # WebRTC coding style guide
  2. ## **General advice**
  3. Some older parts of the code violate the style guide in various ways.
  4. * If making small changes to such code, follow the style guide when
  5. it’s reasonable to do so, but in matters of formatting etc., it is
  6. often better to be consistent with the surrounding code.
  7. * If making large changes to such code, consider first cleaning it up
  8. in a separate CL.
  9. ## **C++**
  10. WebRTC follows the [Chromium][chr-style] and [Google][goog-style] C++
  11. style guides. In cases where they conflict, the Chromium style guide
  12. trumps the Google style guide, and the rules in this file trump them
  13. both.
  14. [chr-style]: https://chromium.googlesource.com/chromium/src/+/HEAD/styleguide/c++/c++.md
  15. [goog-style]: https://google.github.io/styleguide/cppguide.html
  16. ### C++ version
  17. WebRTC is written in C++14, but with some restrictions:
  18. * We only allow the subset of C++14 (language and library) that is not
  19. banned by Chromium; see [this page][chromium-cpp].
  20. * We only allow the subset of C++14 that is also valid C++17;
  21. otherwise, users would not be able to compile WebRTC in C++17 mode.
  22. [chromium-cpp]: https://chromium-cpp.appspot.com/
  23. ### Abseil
  24. You may use a subset of the utilities provided by the [Abseil][abseil]
  25. library when writing WebRTC C++ code. [Details](abseil-in-webrtc.md).
  26. [abseil]: https://abseil.io/about/
  27. ### <a name="h-cc-pairs"></a>`.h` and `.cc` files come in pairs
  28. `.h` and `.cc` files should come in pairs, with the same name (except
  29. for the file type suffix), in the same directory, in the same build
  30. target.
  31. * If a declaration in `path/to/foo.h` has a definition in some `.cc`
  32. file, it should be in `path/to/foo.cc`.
  33. * If a definition in `path/to/foo.cc` file has a declaration in some
  34. `.h` file, it should be in `path/to/foo.h`.
  35. * Omit the `.cc` file if it would have been empty, but still list the
  36. `.h` file in a build target.
  37. * Omit the `.h` file if it would have been empty. (This can happen
  38. with unit test `.cc` files, and with `.cc` files that define
  39. `main`.)
  40. This makes the source code easier to navigate and organize, and
  41. precludes some questionable build system practices such as having
  42. build targets that don’t pull in definitions for everything they
  43. declare.
  44. [Examples and exceptions](style-guide/h-cc-pairs.md).
  45. ### ArrayView
  46. When passing an array of values to a function, use `rtc::ArrayView`
  47. whenever possible—that is, whenever you’re not passing ownership of
  48. the array, and don’t allow the callee to change the array size.
  49. For example,
  50. instead of | use
  51. ------------------------------------|---------------------
  52. `const std::vector<T>&` | `ArrayView<const T>`
  53. `const T* ptr, size_t num_elements` | `ArrayView<const T>`
  54. `T* ptr, size_t num_elements` | `ArrayView<T>`
  55. See [the source](api/array_view.h) for more detailed docs.
  56. ### sigslot
  57. sigslot is a lightweight library that adds a signal/slot language
  58. construct to C++, making it easy to implement the observer pattern
  59. with minimal boilerplate code.
  60. When adding a signal to a pure interface, **prefer to add a pure
  61. virtual method that returns a reference to a signal**:
  62. ```
  63. sigslot::signal<int>& SignalFoo() = 0;
  64. ```
  65. As opposed to making it a public member variable, as a lot of legacy
  66. code does:
  67. ```
  68. sigslot::signal<int> SignalFoo;
  69. ```
  70. The virtual method approach has the advantage that it keeps the
  71. interface stateless, and gives the subclass more flexibility in how it
  72. implements the signal. It may:
  73. * Have its own signal as a member variable.
  74. * Use a `sigslot::repeater`, to repeat a signal of another object:
  75. ```
  76. sigslot::repeater<int> foo_;
  77. /* ... */
  78. foo_.repeat(bar_.SignalFoo());
  79. ```
  80. * Just return another object's signal directly, if the other object's
  81. lifetime is the same as its own.
  82. ```
  83. sigslot::signal<int>& SignalFoo() { return bar_.SignalFoo(); }
  84. ```
  85. ### std::bind
  86. Don’t use `std::bind`—there are pitfalls, and lambdas are almost as
  87. succinct and already familiar to modern C++ programmers.
  88. ### std::function
  89. `std::function` is allowed, but remember that it’s not the right tool
  90. for every occasion. Prefer to use interfaces when that makes sense,
  91. and consider `rtc::FunctionView` for cases where the callee will not
  92. save the function object.
  93. ### Forward declarations
  94. WebRTC follows the [Google][goog-forward-declarations] C++ style guide
  95. with respect to forward declarations. In summary: avoid using forward
  96. declarations where possible; just `#include` the headers you need.
  97. [goog-forward-declarations]: https://google.github.io/styleguide/cppguide.html#Forward_Declarations
  98. ## **C**
  99. There’s a substantial chunk of legacy C code in WebRTC, and a lot of
  100. it is old enough that it violates the parts of the C++ style guide
  101. that also applies to C (naming etc.) for the simple reason that it
  102. pre-dates the use of the current C++ style guide for this code base.
  103. * If making small changes to C code, mimic the style of the
  104. surrounding code.
  105. * If making large changes to C code, consider converting the whole
  106. thing to C++ first.
  107. ## **Java**
  108. WebRTC follows the [Google Java style guide][goog-java-style].
  109. [goog-java-style]: https://google.github.io/styleguide/javaguide.html
  110. ## **Objective-C and Objective-C++**
  111. WebRTC follows the
  112. [Chromium Objective-C and Objective-C++ style guide][chr-objc-style].
  113. [chr-objc-style]: https://chromium.googlesource.com/chromium/src/+/HEAD/styleguide/objective-c/objective-c.md
  114. ## **Python**
  115. WebRTC follows [Chromium’s Python style][chr-py-style].
  116. [chr-py-style]: https://chromium.googlesource.com/chromium/src/+/HEAD/styleguide/styleguide.md#python
  117. ## **Build files**
  118. The WebRTC build files are written in [GN][gn], and we follow
  119. the [Chromium GN style guide][chr-gn-style]. Additionally, there are
  120. some WebRTC-specific rules below; in case of conflict, they trump the
  121. Chromium style guide.
  122. [gn]: https://chromium.googlesource.com/chromium/src/tools/gn/
  123. [chr-gn-style]: https://chromium.googlesource.com/chromium/src/tools/gn/+/HEAD/docs/style_guide.md
  124. ### <a name="webrtc-gn-templates"></a>WebRTC-specific GN templates
  125. Use the following [GN templates][gn-templ] to ensure that all
  126. our [targets][gn-target] are built with the same configuration:
  127. instead of | use
  128. -----------------|---------------------
  129. `executable` | `rtc_executable`
  130. `shared_library` | `rtc_shared_library`
  131. `source_set` | `rtc_source_set`
  132. `static_library` | `rtc_static_library`
  133. `test` | `rtc_test`
  134. [gn-templ]: https://chromium.googlesource.com/chromium/src/tools/gn/+/HEAD/docs/language.md#Templates
  135. [gn-target]: https://chromium.googlesource.com/chromium/src/tools/gn/+/HEAD/docs/language.md#Targets
  136. ### Target visibility and the native API
  137. The [WebRTC-specific GN templates](#webrtc-gn-templates) declare build
  138. targets whose default `visibility` allows all other targets in the
  139. WebRTC tree (and no targets outside the tree) to depend on them.
  140. Prefer to restrict the visibility if possible:
  141. * If a target is used by only one or a tiny number of other targets,
  142. prefer to list them explicitly: `visibility = [ ":foo", ":bar" ]`
  143. * If a target is used only by targets in the same `BUILD.gn` file:
  144. `visibility = [ ":*" ]`.
  145. Setting `visibility = [ "*" ]` means that targets outside the WebRTC
  146. tree can depend on this target; use this only for build targets whose
  147. headers are part of the [native API](native-api.md).
  148. ### Conditional compilation with the C preprocessor
  149. Avoid using the C preprocessor to conditionally enable or disable
  150. pieces of code. But if you can’t avoid it, introduce a GN variable,
  151. and then set a preprocessor constant to either 0 or 1 in the build
  152. targets that need it:
  153. ```
  154. if (apm_debug_dump) {
  155. defines = [ "WEBRTC_APM_DEBUG_DUMP=1" ]
  156. } else {
  157. defines = [ "WEBRTC_APM_DEBUG_DUMP=0" ]
  158. }
  159. ```
  160. In the C, C++, or Objective-C files, use `#if` when testing the flag,
  161. not `#ifdef` or `#if defined()`:
  162. ```
  163. #if WEBRTC_APM_DEBUG_DUMP
  164. // One way.
  165. #else
  166. // Or another.
  167. #endif
  168. ```
  169. When combined with the `-Wundef` compiler option, this produces
  170. compile time warnings if preprocessor symbols are misspelled, or used
  171. without corresponding build rules to set them.