CELUI Coding Standards
This file represents an extension and an integration of the Celestia standard
coding convention and is used in the CELUI library.
The names of CELUI source files and data files are always lower case.
The extension .cpp is used for all C++ sources files.
The extension .h is used for all the header files.
The prefix "ui" is used fo all the CELUI files. Some examples are:
uimanager.h, uimanager.cpp, uiwidget.h, uiwidget.cpp.
"Guard defines" around include files are used to prevent multiple inclusion of headers. Guard macros must be unique. The scheme for Guard macros is _<subdirectory>_<filename>_H_ Here's an example:
// This header file is uicommon.h in the celui subdirectory. #ifndef _CELUI_UICOMMON_H_ #define _CELUI_UICOMMON_H_ enum uiWidgetStyle { ... }; #endif // _CELUI_UICOMMON_H_
STL
CELUI uses extensively the Standard Template Library. Reference to an STL class or function in a header,
is made using the "std" namespace prefix. Implementation modules aren't forced to import
the entire std namespace just because they include a particular header.
The right way:
#include <string> extern std::string foo;
Strings
CELUI uses C++'s string class rather than C-style zero terminated strings. A call
to string::c_str() for a pointer to a
zero terminated string is used to pass to external API functions that require
C-style zero terminated strings.
Casts
Type casting is avoided as much as possible. However, C++ style casts
(reinterpret_cast, static_cast, and const_cast) are
occasionally used.
Const
Const declaration is the preferred way for method declaration.
All reference parameters to a
function are declared const unless they are intended to be modified
by the function.
All the methods which does not
modify class members are declared as const.
Const iterators are used to browse containers unless they are intended to allow
the modify of the
elements of the container.
Reference Parameters
Wherever possible function and method parameters are passed as reference or as pointers.
I/O
C++ I/O is the standard system used by CELUI. Sometimes, printf is used.
if (x) { cout << "The value of x is: " << x; }
switch (foo) { case 1: x = 1; break; case 2: x = 4; break; default: x = 0; break; }
class Foo { public: Foo(); private: int x; };
// CELUI style x = y + z; x = y || z; a = b->c; // Not CELUI style x=y; x = y+z; a = b -> c;
// CELUI style char* str; str = reinterpret_cast<char*>(p); // Not CELUI style char *str; str = reinterpret_cast<char *>(p);
// CELUI style y = square(x); y = atan2(x, z);
// CELUI type naming example enum uiWidgetFilling { ... }; struct uiWidgetStyle { ... }; class uiButton { ... }; typedef void (*uiCallback)(class uiWidget *);
// CELUI macro naming example #define DPRINTF logger->Log //! \def Default coefficent used for the shading of the uiWidget colors. #define UICOLORPALETTE_DFT_SHADER 0.3f //! \def Default widget Label Position #define UIWIDLABPOS_DEFAULT UIWIDLABPOS_TOP_LEFT
// CELUI enum definition example with comment format suitable for Doxygen elaboration. // uiAnimationType // //! This enumeration defines the types of animations associated to uiWidget shape. //------------------------------------------------------------------------------- enum uiAnimationType { UIANIMT_UNDEF, //!< 0 Undefined. Used as control. UIANIMT_ROLL_X, //!< 1 roll from left to right. UIANIMT_ROLL_Y, //!< 2 roll from top to bottom. UIANIMT_ROLL_XY, //!< 3 roll from top-left to bottom right. UIANIMT_STRETCH_X, //!< 4 stretch horizontally. UIANIMT_STRETCH_Y, //!< 5 stretch vertically. UIANIMT_STRETCH_XY //!< 6 stretch both horizontally and vertically. };
// CELUI method naming example class uiFont { ... //! Prints a unicode string in the specified position. /*! It uses the previously defined colors, style and spacing.*/ inline float Print( const std::wstring& _s, //!< String to be printed. const float& _x, //!< X Rendering coordinate in pixels. const float& _y //!< Y Rendering coordinate in pixels. ) { return f_Print(_s, _x, _y, -1, fScale, fTopColor, fBottomColor); }; ... }
// CELUI variable naming example class uiFont { // Public data members //----------------------------------------------------------------- std::string m_name; //!< Name of the font. ... //! Prints a label unicode string in the specified position. /*! It uses the previously defined colors, style and spacing.*/ inline float PrintLabel ( const std::wstring& s, //!< String to be printed. const float& _x, //!< X Rendering coordinate in pixels. const float& _y, //!< Y Rendering coordinate in pixels. const int& _idx, //!< Index of the letter that has to be drawn underlined. const int& _avail //!< Space in pixels available for printing ) { return fPrint(_s, _x, _y, _idx, _avail, f_Scale, f_TopColor, f_BottomColor); }; ... private: // Private data members //--------------------------------------------------------------------- bool f_ok; //!< The object is correctly inizialized . int f_texHandle; //!< OpenGL handle of the texture. int f_texWidth; //!< Size of the texture. int f_texHeight; //!< size of the texture. int f_charsHeight; //!< height of the characters. int f_charsMaxWidth; //!< maximum width of the chars. float f_scale; //!< current scale factor dft=1. bool f_italic; //!< Show as italic format dft = false float f_itDelta; //!< Pixels of the inclination bool f_underlined; //!< Show as underlined format dft = false float f_spacing; //!< spacing factor between characters dft = 0 uiColor* f_topColor; //!< current top color of the printed text uiColor* f_bottomColor; //!< current bottom color of the printed text uiCharMap f_uiCM; //!< uiChar map uiCharMapI f_uiCMI; //!< uiChar map const iterator ... //! Prints a string with the specified scale, position and colors. /*! Returns the amount of space in pixels used to print the string. float fPrint ( const std::wstring& _s, //!< String to be printed. const float& _x, //!< X Rendering coordinate in pixels. const float& _y, //!< Y Rendering coordinate in pixels. const int& _idx, //!< Index of the letter that has to be drawn underlined. const int& _avail //!< Space in pixels available for printing const float& _scale, //!< Scale factor. const uiColor* _topColor, //!< Top color. const uiColor* _botColor //!< Bottom color. ); }; ... ... // uiFont::fPrint // //! Print a displaced, scaled, colored string //-------------------------------------------------------------------------- float uiFont::f_Print(const std::wstring& _s, const float& _x, const float& _y, int _idx, float _scale, uiColor* _topColor, uiColor* _botColor) { if (!f_ok) return 0.0f; unsigned int t_i; // iterator for the string float t_cursor = _x; // the cursor displacement // Iterate the string. for (t_i = 0; t_i < _s.length(); t_i++) { // Verify if the available space is exausted. Assume that the next char is the largest. if ((_x + _avail - t_cursor - f_charsMaxWidth)) < 0) return t_cursor - _x; // During label printing if the current letter is the underlined one and the style is // not underlined we must force temporarily the style. if (t_i == _idx && !f_underlined) { f_underlined = true; t_cursor += f_Putch(_s[t_i], t_retVal, _y, _scale, _topColor, _botColor); f_underlined = false; } else { t_cursor += f_Putch(_s[t_i], t_retVal, _y, _scale, _topColor, _botColor); } // add character spacing defined actually for the font. t_cursor += f_Spacing; } return t_cursor - x; }