About
To utilize features of the MAXON API ALIASES 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 ALIASES. 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:
};
const
maxon::
Int number = exampleObject->GetNumber();
// delete the object
DeleteObj(exampleObject);
Most complex classes of the MAXON API ALIASES 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];
}
{
}
};
AscendingNumbers*
const numbers = AscendingNumbers::Create(10, 20)
iferr_return;
AscendingNumbersRef ref(numbers);
{
}
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();
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;
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();
}
}
Further Reading