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:188
#define MAXON_IMPLICIT
Definition: apibase.h:172
const
maxon::
Int number = exampleObject->GetNumber();
// delete the object
DeleteObj(exampleObject);
maxon::Int Int
Definition: ge_sys_math.h:64
#define DiagnosticOutput(formatString,...)
Definition: debugdiagnostics.h:176
The maxon namespace contains all declarations of the MAXON API.
Definition: autoweight.h:14
#define NewObj(T,...)
Definition: newobj.h:108
#define iferr_return
Definition: resultbase.h:1465
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)
}
{
}
};
Py_ssize_t i
Definition: abstract.h:645
Definition: basearray.h:412
MAXON_ATTRIBUTE_FORCE_INLINE Int GetCount() const
Definition: basearray.h:573
MAXON_ATTRIBUTE_FORCE_INLINE Result< void > CopyFrom(COLLECTION2 &&other, COLLECTION_RESIZE_FLAGS resizeFlags=COLLECTION_RESIZE_FLAGS::FIT_TO_SIZE)
Definition: collection.h:261
PyObject PyObject Py_ssize_t start
Definition: complexobject.h:62
PyObject PyObject Py_ssize_t Py_ssize_t end
Definition: complexobject.h:63
Py_ssize_t * index
Definition: abstract.h:374
PyObject * src
Definition: abstract.h:305
bool Bool
boolean type, possible values are only false/true, 8 bit
Definition: apibase.h:181
static auto Create(ARGS &&... args)
Definition: apibase.h:2750
return OK
Definition: apibase.h:2667
Int GetCount(const ITERABLE &iterable)
Definition: collection.h:37
#define MAXON_SOURCE_LOCATION
Definition: memoryallocationbase.h:67
#define iferr_scope
Definition: resultbase.h:1374
AscendingNumbersRef ref(numbers);
{
}
Py_ssize_t count
Definition: abstract.h:640
for(i=0;i< length;i++)
Definition: unicodeobject.h:61
BaseRef< T, StrongRefHandler > StrongRef
Definition: baseref.h:956
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:709
Definition: apibaseid.h:253
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:1235
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();
}
}
PyObject * error
Definition: codecs.h:206
Py_UCS4 * res
Definition: unicodeobject.h:1113
static const ERROR_FAILED FAILED
Definition: resultbase.h:68
Further Reading