OGRE Coding Standards

來源:互聯網
上載者:User

This document describes the coding standards all developers are expected to adhere to when writing code for the OGRE project.

Top-level organisation issues
  1. All source files must begin with the standard OGRE copyright statement:

    /*-----------------------------------------------------------------------------This source file is part of OGRE    (Object-oriented Graphics Rendering Engine)For the latest info, see http://www.ogre3d.org/Copyright (c) 2000-2006 Torus Knot Software LtdAlso see acknowledgements in Readme.htmlThis program is free software; you can redistribute it and/or modify it underthe terms of the GNU Lesser General Public License as published by the Free SoftwareFoundation; either version 2 of the License, or (at your option) Any laterversion.This program is distributed in the hope that it will be useful, but WITHOUTAny WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESSFOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.You should have received a copy of the GNU Lesser General Public License along withthis program; if not, write to the Free Software Foundation, Inc., 59 TemplePlace - Suite 330, Boston, MA 02111-1307, USA, or go tohttp://www.gnu.org/copyleft/lesser.txt.You may alternatively use this source under the terms of a specific version ofthe OGRE Unrestricted License provided you have obtained such a license fromTorus Knot Software Ltd.-----------------------------------------------------------------------------*/
  2. All publically visible classes should be declared in their own header file using the .h extension, placed in the 'include' folder of the sub-project in question, and named after the class but prefixed with 'Ogre' e.g. 'OgreMyClass.h'.
    Only very tightly related classes should be declared in the same header file.
  3. Implementations should be placed in a source file called the same name as the class but with an extension of .cpp.
  4. Everything must be declared inside the namespace 'Ogre'.
Portablity
  1. All code must be cross platform, ANSI C++. No dependencies on platform-specific headers and / or types are allowed (the only exception is when dealing with platform-specific features like windowing, which must be implemented
    for each platform separately).
  2. If you serialise / deserialise any data, subclass from Serializer and use its methods, it will insulate you from endianness issues. If you need to serialise any types it doesn't already handle, make sure you deal with endianness
    issues in the same way Serializer does (ie use native endianness by default but detect the inverse on reading and perform flipping if required).
C++ Standards compliance
  1. Always prefer the STL over custom containers / algorithms.
  2. Always prefer C++ techniques over C.
    • Avoid C-strings (char* and functions like sprintf, strcpy, use Ogre::String)
    • Avoid old I/O routines (fopen et al, use <iostream>)
    • Use abstract classes or templates not void*
    • Use overloaded methods not varargs.
  3. Minimum C++ compiler level is MSVC 7.1 or gcc 3.1. Compilers which do not support things like partial template specialisation properly (such as older versions of MSVC) are not supported.
  4. Use the PImpl idiomto reduce dependencies between classes.
  5. Always use const-correctness. Methods taking non-primitive types as parameters should generally take
    them as const references, methods returning non-primitive types should generally return them as const references. Declare all methods that do not modify internal state 'const'. For lazy-update getter methods, declare the internal state which is lazy-updated
    'mutable'.
  6. Prefer 'protected' over 'private' to encourage specialisation where appropriate
  7. Always declare destructors 'virtual' unless the class you are writing should not have any vtable (no other virtual methods).
  8. Avoid non-const by-ref parameters unless you have no other option. We prefer not to have in/our parameters since they are less intuitive.
Naming conventions & Documentation
  1. Classes, types and structures must be title case (MyNewClass).
  2. Methods and local variables must be camel case (myNewMethod).
  3. Member variables should be prefixed with 'm' (mInstanceVar), static member variables should be prefixed 'ms' (msStaticMemberVar). Do not use any other prefixing such as Hungarian notation.
  4. Preprocessor macros must be all upper case and prefixed with OGRE_
  5. Enums should be named in title case, enum values should be all upper case
  6. All classes and methods must be fully documented in English using Doxygen-compatible comments. Use the @param and @returns directives to define inputs and outputs clearly, and @note to indicate points of interest.
  7. Use verbose, descriptive names for classes, methods, variables - everything except trival counters. Code should be self-describing, don't be obtuse.
Memory Management
  1. Full virtual classes should derive from an AllocatedObject template instantiation typedef'ed in OgreMemoryAllocatorConfig.h. This will define custom new/delete operators on the class. Small, non-virtual value classes like
    Vector3 should not.
  2. Never use new/delete directly. 
    1. For classes derived from AllocatedObject, use OGRE_NEW / OGRE_DELETE as drop-in replacements for new/delete
    2. For  other classes which need a constructor / destructor, use OGRE_NEW_T and OGRE_DELETE_T. If you know there is no destructor, you may use OGRE_NEW_T and free with OGRE_FREE for speed
    3. For primitive types, use OGRE_ALLOC_T and OGRE_FREE
  3. Be aware of allocator issues when using SharedPtr
    1. Classes derived from AllocatedObject can just be constructed using OGRE_NEW and wrapped, no special behaviour
    2. Classes constructed using OGRE_NEW_T must be allocated using MEMCATEGORY_GENERAL, and you must set the SharedPtrFreeMethod parameter to SPFM_DELETE_T
    3. Instances constructed using OGRE_ALLOC_T must be allocated using MEMCATEGORY_GENERAL, and you must set the SharedPtrFreeMethod parameter to SPFM_FREE
  4. When defining STL containers, instead of using std::vectoror std::listetc, use the memory-manager enabled versions vector::type and list::type respectively (most containers have this equivalent). This defaults the memory
    manager to the General type, but you can override the last parameter to the template if you want an alternate type.
Style issues
  1. Insert a newline before an open brace (contentious I know!)
  2. Use typedefs to declare template-based types that you use to avoid ugliness e.g. typedef std::list<MyType*> MyTypeList;
  3. Always insert spaces in between operators and operands (x + y, not x+y)
  4. Use parenthesis to make the operator precedence unambiguous, even when it is not required ((x * y) + 1, not x * y + 1)
Error handling
  1. Fatal errors should always be dealt with though exception handling. No return-value error reporting.
  2. Whenever you make an assumption in your code, verify it with an assert().
Design issues
  1. Use existing design patterns and identify them by their well known names. A good starting reference is the 'Gang of Four' book.
  2. Use strong encapsulation. Top-level interfaces should hide implementations and not require the user of the library to understand internals. Avoid public attributes except in structs.
  3. Don't use 'friend' if you can avoid it. Where classes need to collaborate on an internal implementation, prefix the methods they use to communicate with '_' (this is our demarcation for 'recommended for internal use only').
    This can also be used to expose advanced functionality only intended for very skilled users.
Final words

If in doubt, do as the existing code does!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.