About
To utilize features of the Maxon API it is typically needed to allocate an object that provides access to data and functions. These objects can either be simple C++ structures and classes or reference classes representing implementations of interfaces.
Object Creation
Simple objects can be allocated using the following memory management tools of the Maxon API. If it is necessary to allocate objects directly, these functions should always be used instead of the standard C++ allocators.
- Note
- If possible, one should always use reference counting. See References.
For allocation of low-level memory see Memory Allocation.
class ExampleClass
{
public:
{
_number = number;
}
{
_number = 0;
}
{
return _number;
}
{
_number = number;
}
private:
};
Int64 Int
signed 32/64 bit int, size depends on the platform
Definition: apibase.h:187
#define MAXON_IMPLICIT
Definition: apibase.h:171
const
maxon::
Int number = exampleObject->GetNumber();
// delete the object
DeleteObj(exampleObject);
#define DiagnosticOutput(formatString,...)
Definition: debugdiagnostics.h:170
maxon::Int Int
Definition: ge_sys_math.h:55
The maxon namespace contains all declarations of the Maxon API.
Definition: autoweight.h:21
#define NewObj(T,...)
Definition: newobj.h:113
#define iferr_return
Definition: resultbase.h:1511
Most complex classes of the Maxon API provide a (static) "Create()" function that can be used to create new instances. It is recommended to add such a "Create()" function also to a custom class if it must be initialized after creation.
class AscendingNumbers
{
private:
{
_numbers.Flush();
{
}
}
private:
public:
{
AscendingNumbersRef ref(numbers);
return ref.Disconnect();
}
{
}
{
if (undershoot || overshoot)
return _numbers[index];
}
{
}
};
Definition: basearray.h:431
MAXON_ATTRIBUTE_FORCE_INLINE Int GetCount() const
Gets the number of array elements.
Definition: basearray.h:600
MAXON_ATTRIBUTE_FORCE_INLINE Result< void > CopyFrom(COLLECTION2 &&other, COLLECTION_RESIZE_FLAGS resizeFlags=COLLECTION_RESIZE_FLAGS::FIT_TO_SIZE)
Definition: collection.h:263
bool Bool
boolean type, possible values are only false/true, 8 bit
Definition: apibase.h:180
const ARG & src
Definition: apibase.h:2736
static auto Create(ARGS &&... args)
Definition: apibase.h:2853
return OK
Definition: apibase.h:2770
Int GetCount(const ITERABLE &iterable)
Definition: collection.h:37
MAXON_ATTRIBUTE_FORCE_INLINE auto end(COLLECTION &&c) -> decltype(c.End())
Definition: foreach.h:366
#define MAXON_SOURCE_LOCATION
Definition: memoryallocationbase.h:69
#define iferr_scope
Definition: resultbase.h:1376
AscendingNumbersRef ref(numbers);
{
}
BaseRef< T, StrongRefHandler > StrongRef
Definition: baseref.h:945
Interface References
A reference object represents an implementation of a given interface. Such a reference object can be created in various ways. For details see Interface Basics and Using Interfaces.
- Note
- References to interfaces with only one implementation can often be allocated on the stack e.g. maxon::String.
This example shows how a reference class instance is created using the maxon::Id of the specific implementation:
const maxon::Id id {
"net.maxonexample.class.somesimpleclass" };
const SimpleClassRef simpleClass = componentClass.Create()
iferr_return;
simpleClass.SetNumber(123);
const maxon::Int number = simpleClass.GetNumber();
Definition: objectbase.h:696
Definition: apibaseid.h:243
This example shows how to obtain a reference class from an implementation that is presented as a published object. See Published Objects Usage.
const SimpleClassRef simpleClass = SomeSimpleClass().Create()
iferr_return;
simpleClass.SetNumber(456);
const maxon::Int number = simpleClass.GetNumber();
Interface implementations can also be registered at registries. From these registries the reference class is obtained using the specific maxon::Id of the registered implementation. See Registry Usage.
const maxon::Id redID {
"net.maxonexample.class.colors.red" };
if (componentClass == nullptr)
const ColorRef color = componentClass->Create()
iferr_return;
Definition: string.h:1287
A color consisting of three components R, G and B.
Definition: col.h:16
Interfaces support inheritance. This means a given reference object of a base class can be cast into the reference class of a derived interface. If the cast is not possible a nullptr is returned.
- maxon::Cast(): Casts the object into the given interface type.
- maxon::AssertCast(): Casts the object into the given interface type. Checks only, if the object is really an instance of the interface in debug mode.
{
const maxon::Error error = res.
GetError();
if (error.IsInstanceOf<maxon::ErrnoError>())
{
const maxon::ErrnoError errnoError = maxon::Cast<maxon::ErrnoError>(error);
const maxon::Int errorCode = errnoError.GetErrorCode();
}
}
SFINAEHelper< Error, RESULT_TYPE >::type GetError() const
Definition: resultbase.h:1012
static const ERROR_FAILED FAILED
Definition: resultbase.h:68
Further Reading