C++ Techniques

About

The MAXON API utilizes various standard and modern C++ technologies. Any developer working with the MAXON API should have a working knowledge of these technologies and generally an advanced knowledge of C++ and object orientated programming.

C++ Techniques

Templates are used to avoid duplicated code and to define generic functionality.

// This example creates two new maxon::BaseArray objects and adds some elements.
intArray.Append(1) iferr_return;
intArray.Append(2) iferr_return;
intArray.Append(3) iferr_return;
stringArray.Append("Hello"_s) iferr_return;
stringArray.Append("World"_s) iferr_return;
Definition: basearray.h:412
MAXON_ATTRIBUTE_FORCE_INLINE ResultRef< T > Append(ARG &&x)
Definition: basearray.h:677
#define iferr_return
Definition: resultbase.h:1519

Macros are used to mark certain parts of the code or to automatically generate utility code. Macros are used heavily in the interface and registry system. E.g. interface declarations use these macros: MAXON_INTERFACE_BASES, MAXON_INTERFACE, MAXON_METHOD and MAXON_DECLARATION.

// This example shows the declaration of a simple interface.
// ---------------------------------------------------------------------
// Simple class that stores a maxon::Int number.
// ---------------------------------------------------------------------
class SimpleClassInterface : MAXON_INTERFACE_BASES(maxon::ObjectInterface)
{
MAXON_INTERFACE(SimpleClassInterface, MAXON_REFERENCE_NORMAL, "net.maxonexample.interfaces.simpleclass");
public:
// ---------------------------------------------------------------------
// Sets the number to store.
// ---------------------------------------------------------------------
// ---------------------------------------------------------------------
// Returns the stored number.
// ---------------------------------------------------------------------
};
// This interface is declared in a file named "simpleclass.h". The automatically
// generated files are therefore named "simpleclass1.hxx" and "simpleclass2.hxx"
// The .hxx header files define the reference class "SimpleClassRef".
#include "simpleclass1.hxx"
// declare the published objects "SomeSimpleClass" and "OtherSimpleClass"
// that give access to implementations of SimpleClassInterface
// the declaration must be placed between the two hxx files since "SimpleClassRef" is defined in the first hxx file
MAXON_DECLARATION(maxon::Class<SimpleClassRef>, SomeSimpleClass, "net.maxonexample.somesimpleclass");
MAXON_DECLARATION(SimpleClassRef, OtherSimpleClass, "net.maxonexample.othersimpleclass");
#include "simpleclass2.hxx"
[interfaces_basic_virtual_interface]
Definition: simpleclass.h:15
MAXON_METHOD void SetNumber(maxon::Int number)
MAXON_INTERFACE(SimpleClassInterface, MAXON_REFERENCE_NORMAL, "net.maxonexample.interfaces.simpleclass")
MAXON_METHOD maxon::Int GetNumber() const
Definition: objectbase.h:709
Int64 Int
signed 32/64 bit int, size depends on the platform
Definition: apibase.h:188
#define MAXON_REFERENCE_NORMAL(FREEIMPL)
Definition: interfacebase.h:1173
#define MAXON_DECLARATION(T, Name, id,...)
Definition: module.h:857
#define MAXON_METHOD
Definition: interfacebase.h:1001
#define MAXON_INTERFACE_BASES(...)
Definition: objectbase.h:1062

Some generic macros can be used in any project, see Generic Macros.

Namespaces are used to avoid symbol collisions. The complete MAXON API is defined in the maxon namespace. It is recommended to use custom namespaces as much as possible.

Lambdas are used instead of callback functions. See also maxon::Delegate.

Enumeration classes are used instead of simple enumerations.

Avoided Techniques

The MAXON API does not use exceptions. Instead the custom error system should be used. See Error Handling.

Further Reading