Changes between Version 36 and Version 37 of MapGuideCodingStandards


Ignore:
Timestamp:
Sep 19, 2022, 4:35:29 AM (2 years ago)
Author:
jng
Comment:

Update pointer usage example to use unique_ptr and added some notes about public API design considerations

Legend:

Unmodified
Added
Removed
Modified
  • MapGuideCodingStandards

    v36 v37  
    142142
    143143Many times copy and paste is an easy way to make code work.  The downside is that it inflates the code and there is a risk of obfuscating essential details.  If some code is doing the same thing it's worthwhile trying to extract a helper method so that it can be reused.  This makes the code much easier to read and understand.  In the extreme case copy 'n paste leads to hundreds of lines of identical code with just a few lines of changes inside.  This significantly increases the risk of introducing defects, since when anything changes later in time the code has to be changed in many places instead of one.
     144
     145== Public API design considerations ==
     146
     147We use [https://swig.org/ SWIG] to generate wrappers to MapGuide's C++ public API surface in PHP, Java and .net.
     148
     149When introducing a new API to MapGuide, you should use the least-common-denominator subset of C++ language features/constructs to best facilitate wrapper generation across all 3 managed language targets, **even if this is not the most optimal method** in terms of C++ runtime performance.
     150
     151The following should be used for method parameters and return types:
     152
     153 * Allowed method parameter types
     154   * Basic primitive types `bool/BYTE/INT16/INT32/INT64/float/double`
     155   * Raw pointer to any public MapGuide API class
     156   * For strings, only `std::wstring` (aliased in MapGuide as `STRING`) is permitted and only by const reference (aliased in MapGuide as `CREFSTRING`)
     157   * For arrays/collections, use a raw pointer to any subclass of `MgCollection`
     158   * For byte buffers or binary content, use a raw pointer to `MgByteReader`
     159 * Allowed method return types
     160   * Basic primitive types `bool/BYTE/INT16/INT32/INT64/float/double`
     161   * Raw pointer to any public MapGuide API class
     162   * For strings, only `std::wstring` (aliased in MapGuide as `STRING`) is permitted and only by value
     163   * For byte buffers or binary content, return a raw pointer to `MgByteReader`
     164
     165Do not bother/try making public API methods `const`-correct, it will not affect SWIG interface generation and may even complicate or break it in some cases.
     166
     167Anything not on the above list is something assumed to not be reliably SWIG-wrappable with consistent behavior across our 3 managed language targets and should not be used.
     168
     169When naming methods, use a name that is unlikely to collide with reserved/built-in method names of any language target or methods that would be auto-generated by SWIG for given language target. For example:
     170 * `delete` is a bad name for a new public API method because this is the name of the method SWIG auto-generates for Java proxy classes
     171 * `finalize` is a bad name for a new public API method because this is the name of the built-in method in Java classes that will be called when an object is about to be garbage collected
     172
     173For private/internal methods, you are free to name and define method parameters and return type in any way you see fit for best C++ runtime performance.
    144174
    145175== Object Creation and Runtime Speed ==
     
    684714Bar* Foo::CreateBar()
    685715{
    686     auto_ptr<Bar> bar;
     716    unique_ptr<Bar> bar;
    687717
    688718    MG_TRY()
    689719
    690     bar = auto_ptr<Bar>(new Bar);
     720    bar = unique_ptr<Bar>(new Bar);
    691721    assert(0 != bar.get());
    692722    bar.DoSomething(); // might throw an exception
     
    697727}
    698728}}}
    699  
    700 Standard C++ smart pointer notes:
    701  1. Never use auto_ptr objects as elements of STL containers because auto_ptr does not quite meet the requirements of a type you can put into containers (i.e. copies of auto_ptrs are not equivalent).
    702  1. Never use an array as an auto_ptr argument because auto_ptr's destructor invokes only non-array delete.
     729
     730Some smart pointer notes:
     731
     732 1. Don't use `auto_ptr`. This is deprecated in C++11 and removed in C++17 onwards. Most/all cases where you'd think to use `auto_ptr` you can use `unique_ptr` instead
     733 2. If using `unique_ptr`, you may be tempted to use `std::make_unique`, but this is a C++14 standard library feature and we require code to be compiled in C++11 standards mode. If/when MapGuide/FDO adopts C++14 or higher as the base language standard version, usage of this API will be permitted.
     734
    703735
    704736The following code is a cleaned up version of the above sample code using some helper macros.