[MOAB-dev] namespace?

Jason Kraftcheck kraftche at cae.wisc.edu
Tue Mar 2 13:57:17 CST 2010


The question has arisen as to whether or not we should also transition to
using a (or perhaps several) C++ namespaces for MOAB as a part of
restructuring the MOAB source.  A complete conversion would entail dropping
the existing 'MB' prefix from all class names and putting everything in a MB
or MOAB namespace.  For example, MBInterface would become MOAB::Interface
(or perhaps MB::Interface or moab::Interface).

The primary question is should be bother to transition to using a namespaces
at all?  The advantages:
  1) Greater flexibility for users:
      o If the user doesn't want to use a given namespace, they can use
        a 'using' directive.
      o Namespaces can be combined (e.g. namespace Foo { using namespace
        MOAB; } makes everything that is in the MOAB namespace also in
        the 'Foo' namespace.
      o Namespaces can be aliased. For example: "namespace MB = MOAB;"
        allows the user to abbreviate 'MOAB::' as 'MB::'.
  2) Avoids symbol conflicts even for non-API symbols.  For example, there
     are many classes and such in MOAB libmoab.so that have external linkage
     and thus symbol names exposed in the library even though they are not
     part of the API.  They are often not prefixed with 'MB'.
  3) It is trendy to use language features.
  4) Allows some obscure stuff like linking different versions of MOAB into
     the same application by using slightly different namespaces.
The disadvantages:
  1) Code changes to implement
  2) Backwards compatibility (API change)
  3) Influences resolving of overloaded symbols by compiler.

If we do choose to use namespaces with MOAB, then there are a few more open
questions:
 o Which namespace do we use?  MB? MOAB? moab?
 o Should we use more than one namespace?
 o Should we provide backwards compability with the current no-namespace
   API?  This shouldn't be too difficult.  Just a few headers that
   analogous to the following for MBInterface.hpp:
     #include "MOAB/Interface.hpp"
     typedef MOAB::Interface MBInterface;
 o Should we use the namespace to implement symbol versioning?  This amounts
   to something like:
     #define MOAB_NS MOAB_ ## MOAB_VERSION_MAJOR
     namespace MAOB = MOAB_NS;
   Then everywhere in the MOAB source we use MOAB_NS rather than MOAB as
   the namespace for declarations.  The user can use the 'MOAB' alias.
   The result of this is that all the mangled symbols contain in the final
   compiled library contain the major version number, such that linking to
   an version of the library with the wrong major version number won't work.

- jason





More information about the moab-dev mailing list