Table of Contents

NRT Style Guide

File Organization

Indentation Style

We use astyle to ensure uniform indentation styles across our various text editors. A style file is included with NRT, and can be found in nrt/scripts/astylerc. To use this style file, invoke astyle using the –options flag, e.g.

  astyle myfile --options=/path/to/nrt/scripts/stylerc

Vim:

"-------------Essential NRT Style Compliance Settings-------------
 
" Disable old-school vi compatability
set nocompatible
 
" Allow plugins to control our indentation
filetype plugin indent on
 
" Set each auto-indent level to equal two spaces
set shiftwidth=2
 
" Let each tab equal two spaces
set tabstop=2
 
" Make sure vim turns all tabs into spaces
set expandtab
 
" Make vim indent our code properly
set smartindent
 
" Make the maximum line length equal 120
set textwidth=120
 
"-------------Other cool vim tricks-------------
 
" Use a cool menu when autocompleting filenames, commands, etc...
set wildmenu
set wildmode=list:longest
 
" Make vim automatically change directories to the directory of any file you open. 
" This means that when you open a file, then want to open another using :tabe, :o, etc,
" you can just type in the relative path from the file you're currently editing.
set autochdir
 
" When editing the NRT library, it is a total pain when you are editing a .H file in nrt/include/whatever/whatever, 
" and then decide you need to edit the source .C file in the nrt/src/whatever/whatever. This little function will 
" automatically back track in the directory tree for you, find the corresponding .C or .H file, and open it in a new
" tab. 
" To use it, just type ,o (that's a comma, and then a lower-case o). 
function! OpenOther()
  if expand("%:e") == "C"
    exe "tabe" fnameescape(expand("%:p:r:s?src?include?").".H")
  elseif expand("%:e") == "H"
    exe "tabe" fnameescape(expand("%:p:r:s?include?src?").".C")
  endif
endfunction
nmap ,o :call OpenOther()<CR>

Emacs:

Add this to your ~/.emacs:

;; Hide the menu bar
(menu-bar-mode)
 
;; set default line wrap len:
(setq default-fill-column 120)
 
;;LINUM MODE: displays the line number in the left side of buffers.  format is set to accept up to 4 digits line numbers
;;and showing a "|" character between the line number and the buffer content.
(setq linum-format "%4d")
(global-linum-mode 1)
 
;; NRT indentation style for C++ and such
(defun my-c-mode-common-hook ()
  (local-set-key "\C-h" 'backward-delete-char)
  ;; this will make sure spaces are used instead of tabs
  (setq tab-width 4 indent-tabs-mode nil)
  (setq indent-tabs-mode 'nil)
  (setq c-basic-offset 2)
  (c-set-offset 'substatement-open 0)
  (c-set-offset 'statement-case-open 0)
  (c-set-offset 'case-label 0)
  (c-set-offset 'brace-list-open 0)
  (c-set-offset 'access-label -2)
  (c-set-offset 'inclass 4)
  (c-set-offset 'member-init-intro 4)
  ;; include possible ! as comment start string so that indentation starts after it
  (setq comment-start-skip "/\\*+!* *\\|//+ *")
 
  ;; type C-c C-s or C-c C-o while editing to see what other rules to add here...
)
 
(add-hook 'c-mode-hook 'my-c-mode-common-hook)
(add-hook 'c++-mode-hook 'my-c-mode-common-hook)
(add-hook 'perl-mode-hook 'my-c-mode-common-hook)
(add-hook 'cperl-mode-hook 'my-c-mode-common-hook)
(add-hook 'emacs-lisp-mode-hook 'my-c-mode-common-hook)
(add-hook 'nroff-mode-hook 'my-c-mode-common-hook)
(add-hook 'tcl-mode-hook 'my-c-mode-common-hook)
(add-hook 'makefile-mode-hook 'my-c-mode-common-hook)

Eclipse:

...

Coding Standards

Const Correctness

//All Wrong...
const int *x;
const int &x;
//All Right...
int const & x;
int const * x;
int * const x;
int const * const x;
void func(float const val)
{
  float const valsquared = val * val;
  // use valsquared (assuming you will not need to modify it)
}
class MyClass {
int const getVal(int const index) const;
};

Caveat: If a member function should semantically be const (e.g., it reads something from your object) but it actually modifies the object, yet not in a significant manner (typical example is that it locks a mutex member variable in your object), then declare the modified variables (e.g., your mutex) as “mutable”, this is preferred to doing a const_cast inside your member function.

void splitRectangle(nrt::Rectangle<int> const & inputrect, nrt::Rectangle<int> & outputleft, nrt::Rectangle<int> & outputright);

Local Variables

//Bad...
for(int i=0; i<c; ++i);
 
//Better...
for(int itemIdx=0; itemIdx<itemCount; ++itemIdx);

Proper use of inline

Example:

  template <class T>
  class Stuff
  {
    public:
      void doit();   // NO INLINE HERE IN DECLARATION
  };

  ...
  
  // IN IMPLEMENTATION FILE:
  template <class T> inline
  Stuff<T>::doit()
  { ... }
  

Proper use of virtual

Capitalization Rules

class MyCoolClass;
class ImageSegmenter;
bool isRunning;
size_t arraySize;
class MyClass
{
  size_t itsCounter;
  std::vector<int> itsStorage;
}
void doSomething(int paramOne);
 
class MyClass
{
  void doSomethingElse(int paramTwo);
}
  typedef nrt::Dims<float> FloatingDims;
  template<class T1, class T2> 
  struct simple_promotion
  {
    // Even though this is a typedef, we use lowercase and an underscore to emphasize
    // that it is part of some fancy metaprogramming
    typedef decltype(*(T1*)nullptr + *(T2*)nullptr)) promoted_type;
  };
 
  template<class PixType>
  {
    // Even though this is just a boolean, we use lowercase and an underscore to emphasize
    // that it is part of some fancy metaprogramming
    static const bool is_multi_channel = (PixType::numChannels > 1) ? true : false;
  };