guidelines.qbk 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. [/============================================================================
  2. Boost.Geometry (aka GGL, Generic Geometry Library)
  3. Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  4. Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
  5. Copyright (c) 2009-2013 Mateusz Loskot, London, UK.
  6. Use, modification and distribution is subject to the Boost Software License,
  7. Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  8. http://www.boost.org/LICENSE_1_0.txt)
  9. =============================================================================/]
  10. [section:guidelines Guidelines for developers]
  11. This library is maintained by several developers, and in order it to have
  12. a consistent design, look and feel, a few guidelines need to be followed.
  13. Rules of [@boost:/development/requirements.html Boost Library Requirements and Guidelines]
  14. and [@boost:/development/header.html Boost Header Policy] always have highest authority.
  15. Generally, prefer style of modern C++, conventions as used in latest C++ standard
  16. document and C++ Standard Library. Boost.Spirit is a good example of
  17. how to write and format high quality C++ code.
  18. Some guidelines specific to Boost.Geometry library are outlined below.
  19. [heading Code structure]
  20. * Every file shall have header with copyright and license information.
  21. * Do not put any history or revision information in comments in source files.
  22. Log it with VCS used in the Boost project.
  23. * Every header shall have `#include` guard based on header path and file name:
  24. ``
  25. #ifndef BOOST_GEOMETRY_<DIR1>_<DIR2>_<FILE>_HPP
  26. #define BOOST_GEOMETRY_<DIR1>_<DIR2>_<FILE>_HPP
  27. ...
  28. #endif // BOOST_GEOMETRY_<DIR1>_<DIR2>_<FILE>_HPP
  29. ``
  30. * `#include` directives shall be ordered according the most authoritative header:
  31. * C Standard Library (using C++ library names, i.e. `<cstdlib>`)
  32. * C++ Standard Library
  33. * Boost C++ Libraries
  34. * Boost.Geometry headers
  35. * Other 3rd-party headers (only if applicable! in some samples only)
  36. * Header within these sections should be ordered alphabetically, especially if there are many of them included.
  37. * Namespaces don't increase the level of indentation.
  38. In all other cases braces increase the level of indentation.
  39. ``
  40. namespace boost { namespace geometry
  41. {
  42. namespace mynewspace
  43. {
  44. template <typename Point>
  45. struct my_new_model
  46. {
  47. typedef point_type;
  48. }
  49. } // namespace mynewspace
  50. }} // namespace boost::geometry
  51. ``
  52. * Namespace closing brace should have comment with the namespace name.
  53. * All non-public headers should be placed into `boost/geometry/detail` or
  54. `boost/geometry/*/detail` directory, depending on component level.
  55. * All non-public names should reside in the `boost::geometry::detail` or
  56. `boost::geometry::*::detail` namespace, depending on component level.
  57. * All traits should be placed in dedicated `boost::geometry::traits` or
  58. `boost::geometry::*::traits` namespace
  59. * All tag dispatching routines should be placed in dedicated
  60. `boost::geometry::*::dispatch` namespace.
  61. * Access specifiers for class members shall be orderd as public first, then protected and private at the bottom.
  62. The public members define class interface, thus they are of the highest interested for users, so show them first.
  63. * Exceptions to this rule are allowed for typedef aliases required to be defined first.
  64. [heading Code formatting and indentation]
  65. * The code is indented with spaces, 4 spaces per tab.
  66. * The preferred line length is 80 characters, with maximum length of 100.
  67. * The limit is relaxed for very long string literals (e.g. Well-Known Text with data used in tests and examples).
  68. * Member/base initialization list for constructors on the same line,
  69. if it's small (1-2 members) or 1 member/base per line with leading comma on the left:
  70. ```
  71. struct T
  72. {
  73. T(int a, int b)
  74. : a(a)
  75. , b(b)
  76. {}
  77. int a;
  78. int b;
  79. };
  80. ```
  81. * Template declaration with long template parameter list shall be formatted
  82. with one template parameter per line, all parameters indented,
  83. but `<` and `>` brackets not indented:
  84. ```
  85. template
  86. <
  87. typename T,
  88. typename P,
  89. typename C = std::vector<Point>
  90. >
  91. struct polygon
  92. {
  93. typedef typename boost::remove_const
  94. <
  95. typename traits::point_type<T>::type
  96. >::type type
  97. };
  98. ```
  99. * References and pointers should be formatted emphasizing type, not syntax:
  100. ```
  101. T const& t;
  102. T* t;
  103. T* const t;
  104. T const* t;
  105. T const* const t;
  106. ```
  107. * Braces enclosing block of code (if-else, loops) should be placed in separate lines
  108. ```
  109. if (expr)
  110. {
  111. }
  112. ```
  113. * Parentheses around expressions should not be pre/post-fixed with spaces.
  114. [heading Naming conventions]
  115. * All names follow style of the C++ Standard, lowercase with words separated with underscore `_`,
  116. unless otherwise specified (see other rules).
  117. * Template parameters are named in CamelCase.
  118. * Concepts are named in CamelCase.
  119. * Name of a class data member shall start with `m_` prefix.
  120. The Boost sample header gives no prefix or suffix at all.
  121. However, the `m_` prefix is used in some (not many) Boost libraries as well (e.g. math/tools/remez).
  122. * All macro names shall be in upper-case, words separated with underscore `_`.
  123. * All macro names shall start with `BOOST_GEOMETRY_`.
  124. * All non-public macro names should start with `BOOST_GEOMETRY_DETAIL_` (not used often yet, if at all).
  125. * All public names should reside in the `boost::geometry` namespace.
  126. Nested namespaces are also possible.
  127. * Avoid cryptic names and abbreviations for elements used in wider context (e.g. types, functions).
  128. Short names are allowed if context of use is local, narrow and easily tracable
  129. For example, use of `it` for `iterator` in body of a loop in function:
  130. ```
  131. template <typename Range, typename Functor>
  132. static inline void apply(Range& range, Functor& f)
  133. {
  134. for (typename boost::range_iterator<Range>::type it = boost::begin(range);
  135. it != boost::end(range); ++it)
  136. {
  137. f(*it);
  138. }
  139. }
  140. ```
  141. [heading C++ use conventions]
  142. * Keyword struct is preferred either for POD structures, or for classes used at compile-time
  143. like metafunctions, tags, traits, etc.
  144. * Keyword class is preferred for classes meant to produce actual objects, which have methods
  145. and an active role in the runtime functioning of the program.
  146. * In case of a template, prefer use of typename keyword over class.
  147. [heading Specialisations and dispatching conventions]
  148. * Algorithms are free inline functions, taking any geometry. Parameters are often one or two geometries
  149. * There might be an overload for a strategy. The strategy takes, a.o. care of coordinate systems
  150. * The free `inline` function forwards to a dispatch struct, specialized for the geometry type (so for point, polygon, etc.)
  151. * They have an `static` (`inline`) function called apply
  152. * The dispatch struct calls, or is derived from, an struct implemented in namespace detail
  153. * There the same: a `struct` with a `static` (`inline`) function called apply
  154. * This way the implementation structs are building blocks, they can be reused
  155. * In fact they are reused often by the multi-versions of the algorithms
  156. ```
  157. namespace boost { namespace geometry
  158. {
  159. namespace detail { namespace foo
  160. {
  161. template <typename Point>
  162. struct foo_point
  163. {
  164. // template parameters here
  165. static inline int apply(Point const& p)
  166. {
  167. // do something here
  168. return 1;
  169. }
  170. };
  171. }} // namespace detail::foo
  172. namespace dispatch
  173. {
  174. template
  175. <
  176. Geometry,
  177. Tag = typename geometry::tag<Geometry>::type
  178. >
  179. struct foo
  180. {
  181. };
  182. // Specialization for POINT
  183. ...
  184. } // namespace dispatch
  185. template <typename Point>
  186. inline int foo(Point const& point)
  187. {
  188. return dispatch<Point>::apply(point);
  189. }
  190. }} // namespace boost::geometry
  191. ```
  192. [heading Contributing code]
  193. * Create a patch, open a ticket in the Boost Trac with your patch attached.
  194. * Alternatively, post your patch to the Boost.Geometry mailing list.
  195. * If you contribute a code, always try to provide a minimal test for it.
  196. [endsect]