CodingGuidelines.lyx 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. #LyX 2.0 created this file. For more info see http://www.lyx.org/
  2. \lyxformat 413
  3. \begin_document
  4. \begin_header
  5. \textclass article
  6. \begin_preamble
  7. \usepackage{color}
  8. \definecolor{mygreen}{rgb}{0,0.6,0}
  9. \definecolor{mygray}{rgb}{0.5,0.5,0.5}
  10. \definecolor{mymauve}{rgb}{0.58,0,0.82}
  11. \lstset{ %
  12. backgroundcolor=\color{white}, % choose the background color; you must add \usepackage{color} or \usepackage{xcolor}
  13. basicstyle=\footnotesize, % the size of the fonts that are used for the code
  14. breakatwhitespace=false, % sets if automatic breaks should only happen at whitespace
  15. breaklines=true, % sets automatic line breaking
  16. captionpos=b, % sets the caption-position to bottom
  17. commentstyle=\color{mygreen}, % comment style
  18. % deletekeywords={...}, % if you want to delete keywords from the given language
  19. escapeinside={\%*}{*)}, % if you want to add LaTeX within your code
  20. extendedchars=true, % lets you use non-ASCII characters; for 8-bits encodings only, does not work with UTF-8
  21. frame=single, % adds a frame around the code
  22. keepspaces=true, % keeps spaces in text, useful for keeping indentation of code (possibly needs columns=flexible)
  23. keywordstyle=\color{blue}, % keyword style
  24. language=C++, % the language of the code
  25. morekeywords={*,...}, % if you want to add more keywords to the set
  26. numbers=left, % where to put the line-numbers; possible values are (none, left, right)
  27. numbersep=5pt, % how far the line-numbers are from the code
  28. numberstyle=\tiny\color{mygray}, % the style that is used for the line-numbers
  29. rulecolor=\color{black}, % if not set, the frame-color may be changed on line-breaks within not-black text (e.g. comments (green here))
  30. showspaces=false, % show spaces everywhere adding particular underscores; it overrides 'showstringspaces'
  31. showstringspaces=false, % underline spaces within strings only
  32. showtabs=false, % show tabs within strings adding particular underscores
  33. stepnumber=2, % the step between two line-numbers. If it's 1, each line will be numbered
  34. stringstyle=\color{mymauve}, % string literal style
  35. tabsize=2, % sets default tabsize to 2 spaces
  36. title=\lstname % show the filename of files included with \lstinputlisting; also try caption instead of title
  37. }
  38. \end_preamble
  39. \use_default_options true
  40. \maintain_unincluded_children false
  41. \language english
  42. \language_package default
  43. \inputencoding auto
  44. \fontencoding global
  45. \font_roman lmodern
  46. \font_sans lmss
  47. \font_typewriter lmtt
  48. \font_default_family default
  49. \use_non_tex_fonts false
  50. \font_sc false
  51. \font_osf false
  52. \font_sf_scale 100
  53. \font_tt_scale 100
  54. \graphics default
  55. \default_output_format default
  56. \output_sync 0
  57. \bibtex_command default
  58. \index_command default
  59. \paperfontsize default
  60. \spacing single
  61. \use_hyperref false
  62. \papersize default
  63. \use_geometry false
  64. \use_amsmath 1
  65. \use_esint 1
  66. \use_mhchem 1
  67. \use_mathdots 1
  68. \cite_engine basic
  69. \use_bibtopic false
  70. \use_indices false
  71. \paperorientation portrait
  72. \suppress_date false
  73. \use_refstyle 1
  74. \index Index
  75. \shortcut idx
  76. \color #008000
  77. \end_index
  78. \secnumdepth 3
  79. \tocdepth 3
  80. \paragraph_separation indent
  81. \paragraph_indentation default
  82. \quotes_language english
  83. \papercolumns 1
  84. \papersides 1
  85. \paperpagestyle default
  86. \tracking_changes false
  87. \output_changes false
  88. \html_math_output 0
  89. \html_css_as_file 0
  90. \html_be_strict false
  91. \end_header
  92. \begin_body
  93. \begin_layout Section
  94. Template Classes
  95. \end_layout
  96. \begin_layout Standard
  97. Templated classes are great for writing generic code for multiple types
  98. (e.g.
  99. the same elimination algorithm code for symbolic, discrete, and Gaussian
  100. elimination) without the drawbacks of virtual inheritance (which include
  101. rigid class interfaces, downcasting from returned base class pointers,
  102. and additional runtime overhead).
  103. Depending on how they're used, though, templates can result in very slow
  104. compile times, large binary files, and hard-to-use code.
  105. This section describes the
  106. \begin_inset Quotes eld
  107. \end_inset
  108. best practices
  109. \begin_inset Quotes erd
  110. \end_inset
  111. we have developed for gaining the benefits of templates without the drawbacks.
  112. \end_layout
  113. \begin_layout Standard
  114. If you need to write generic code or classes, here are several programming
  115. patterns we have found to work very well:
  116. \end_layout
  117. \begin_layout Subsection
  118. The
  119. \begin_inset Quotes eld
  120. \end_inset
  121. Templated Base, Specialized Derived
  122. \begin_inset Quotes erd
  123. \end_inset
  124. Pattern
  125. \end_layout
  126. \begin_layout Standard
  127. This pattern is for when you have a generic class containing algorithm or
  128. data structure code that will be specialized to several types.
  129. The templated base class should never be used directly, instead only the
  130. specializations should be used.
  131. Some specialized types can be pre-compiled into the library, but the option
  132. remains to specialize new types in external libraries or projects.
  133. \end_layout
  134. \begin_layout Subsubsection
  135. Basic Class Structure
  136. \end_layout
  137. \begin_layout Standard
  138. We'll use
  139. \family typewriter
  140. FactorGraph
  141. \family default
  142. as an example.
  143. It is templated on the factor type stored in it and has several specializations.
  144. The templated base class
  145. \family typewriter
  146. FactorGraph<class FACTOR>
  147. \family default
  148. is divided into a header file (
  149. \family typewriter
  150. .h
  151. \family default
  152. ) and an
  153. \begin_inset Quotes eld
  154. \end_inset
  155. instantiation
  156. \begin_inset Quotes erd
  157. \end_inset
  158. file (
  159. \family typewriter
  160. -inst.h
  161. \family default
  162. ).
  163. The basic class structure is as follows.
  164. \begin_inset listings
  165. lstparams "basicstyle={\scriptsize\ttfamily},language={C++}"
  166. inline false
  167. status open
  168. \begin_layout Plain Layout
  169. // File FactorGraph.h
  170. \end_layout
  171. \begin_layout Plain Layout
  172. \end_layout
  173. \begin_layout Plain Layout
  174. %*
  175. \backslash
  176. bfseries{
  177. \backslash
  178. emph{
  179. \backslash
  180. color{red}{// Include a minimal set of headers.
  181. Do not include any '-inst.h' files (this is the key to fast compiles).}}}*)
  182. \end_layout
  183. \begin_layout Plain Layout
  184. #include <boost/serialization/nvp.hpp>
  185. \end_layout
  186. \begin_layout Plain Layout
  187. ...
  188. \end_layout
  189. \begin_layout Plain Layout
  190. \end_layout
  191. \begin_layout Plain Layout
  192. namespace gtsam {
  193. \end_layout
  194. \begin_layout Plain Layout
  195. /** Class description */
  196. \end_layout
  197. \begin_layout Plain Layout
  198. template<class FACTOR>
  199. \end_layout
  200. \begin_layout Plain Layout
  201. class FactorGraph
  202. \end_layout
  203. \begin_layout Plain Layout
  204. {
  205. \end_layout
  206. \begin_layout Plain Layout
  207. %*
  208. \backslash
  209. bfseries{
  210. \backslash
  211. emph{
  212. \backslash
  213. color{red}{// Make 'private' any typedefs that must be redefined in derived
  214. classes.
  215. E.g.
  216. 'This' in the context of the derived class should refer to the derived
  217. class.
  218. These typedefs will be used only by the generic code in this base class.}}}*)
  219. \end_layout
  220. \begin_layout Plain Layout
  221. private:
  222. \end_layout
  223. \begin_layout Plain Layout
  224. typedef FactorGraph<FACTOR> This; ///< Typedef for this class
  225. \end_layout
  226. \begin_layout Plain Layout
  227. typedef boost::shared_ptr<This> shared_ptr; ///< Shared pointer to
  228. this
  229. \end_layout
  230. \begin_layout Plain Layout
  231. \end_layout
  232. \begin_layout Plain Layout
  233. %*
  234. \backslash
  235. bfseries{
  236. \backslash
  237. emph{
  238. \backslash
  239. color{red}{// Make 'public' the typedefs that will be valid in the derived
  240. class.}}}*)
  241. \end_layout
  242. \begin_layout Plain Layout
  243. public:
  244. \end_layout
  245. \begin_layout Plain Layout
  246. typedef FACTOR FactorType; ///< Factor type stored in this graph
  247. \end_layout
  248. \begin_layout Plain Layout
  249. typedef boost::shared_ptr<FACTOR> sharedFactor; ///< Shared pointer
  250. to a factor
  251. \end_layout
  252. \begin_layout Plain Layout
  253. ...
  254. \end_layout
  255. \begin_layout Plain Layout
  256. \end_layout
  257. \begin_layout Plain Layout
  258. %*
  259. \backslash
  260. bfseries{
  261. \backslash
  262. emph{
  263. \backslash
  264. color{red}{// Normally, data is 'protected' so the derived class can access
  265. it.}}}*)
  266. \end_layout
  267. \begin_layout Plain Layout
  268. protected:
  269. \end_layout
  270. \begin_layout Plain Layout
  271. /** Collection of factors */
  272. \end_layout
  273. \begin_layout Plain Layout
  274. std::vector<sharedFactor> factors_;
  275. \end_layout
  276. \begin_layout Plain Layout
  277. \end_layout
  278. \begin_layout Plain Layout
  279. %*
  280. \backslash
  281. bfseries{
  282. \backslash
  283. emph{
  284. \backslash
  285. color{red}{// Make 'protected' all constructors, named constructors, or
  286. methods returning the base class type.
  287. These are not public - the derived class will call them and properly convert
  288. returned base classes to the derived class.}}}*)
  289. \end_layout
  290. \begin_layout Plain Layout
  291. /// @name Standard Constructors
  292. \end_layout
  293. \begin_layout Plain Layout
  294. /// @{
  295. \end_layout
  296. \begin_layout Plain Layout
  297. \end_layout
  298. \begin_layout Plain Layout
  299. /** Default constructor */
  300. \end_layout
  301. \begin_layout Plain Layout
  302. FactorGraphUnordered() {}
  303. \end_layout
  304. \begin_layout Plain Layout
  305. \end_layout
  306. \begin_layout Plain Layout
  307. /** Named constructor from iterator over factors */
  308. \end_layout
  309. \begin_layout Plain Layout
  310. template<typename ITERATOR>
  311. \end_layout
  312. \begin_layout Plain Layout
  313. static This FromIterator(ITERATOR firstFactor, ITERATOR lastFactor);
  314. \end_layout
  315. \begin_layout Plain Layout
  316. /// @}
  317. \end_layout
  318. \begin_layout Plain Layout
  319. \end_layout
  320. \begin_layout Plain Layout
  321. %*
  322. \backslash
  323. bfseries{
  324. \backslash
  325. emph{
  326. \backslash
  327. color{red}{// Make 'public' standard methods that will be available in the
  328. derived class's API.}}}*)
  329. \end_layout
  330. \begin_layout Plain Layout
  331. public:
  332. \end_layout
  333. \begin_layout Plain Layout
  334. /// @name Adding Factors
  335. \end_layout
  336. \begin_layout Plain Layout
  337. /// @{
  338. \end_layout
  339. \begin_layout Plain Layout
  340. /** ...
  341. */
  342. \end_layout
  343. \begin_layout Plain Layout
  344. void reserve(size_t size);
  345. \end_layout
  346. \begin_layout Plain Layout
  347. ...
  348. \end_layout
  349. \begin_layout Plain Layout
  350. /// @}
  351. \end_layout
  352. \begin_layout Plain Layout
  353. };
  354. \end_layout
  355. \begin_layout Plain Layout
  356. }
  357. \end_layout
  358. \end_inset
  359. \end_layout
  360. \end_body
  361. \end_document