Initialization

Table of Contents

About

The macro MAXON_INITIALIZATION() is used to execute code when the application starts and when it shuts down. This is typically useful to allocate and free global data.

Warning
If the "init" functions of MAXON_INITIALIZATION() returns an error, all components defined in the current source file will not be loaded.

Usage

One can simply call global functions in the MAXON_INITIALIZATION() macro.

Warning
MAXON_INITIALIZATION() is called before the classic core is loaded. This means that classic API functions and classes (e.g. GePrint()) cannot be used in this context.
// This example declares a global string containing the machine name.
// MAXON_INITIALIZATION is used to set the value on start up and free the resource on shut down.
#include "maxon/module.h"
#include "maxon/string.h"
maxon::String* g_machineName = nullptr; // global string containing the machine name
// called on system start
static maxon::Result<void> InitGlobalString()
{
// allocate global string
g_machineName = NewObj(maxon::String) iferr_return;
// get machine data
const maxon::DataDictionary data = maxon::Application::GetMachineInfo();
// get machine name
*g_machineName = data.Get(maxon::MACHINEINFO::COMPUTERNAME) iferr_return;
return maxon::OK;
}
// called on system end
static void ClearGlobalString()
{
// delete global string
if (g_machineName != nullptr)
DeleteObj(g_machineName);
}
MAXON_INITIALIZATION(InitGlobalString, ClearGlobalString);

Or simply use a lambda within the macro itself:

// This example declares a global string containing the user name.
// MAXON_INITIALIZATION is used to set the value on start up and free the resource on shut down.
maxon::String* g_userName = nullptr; // global string containing the user name
// system startup
{
// allocate global string
// get machine data
const maxon::DataDictionary data = maxon::Application::GetMachineInfo();
// get machine name
*g_userName = data.Get(maxon::MACHINEINFO::USERNAME) iferr_return;
return maxon::OK;
},
// system shutdown
[]()
{
// delete global string
DeleteObj(g_userName);
});

If no function should be called nullptr can be set.

Further Reading

maxon::Application::GetMachineInfo
static MAXON_METHOD DataDictionary GetMachineInfo()
DeleteObj
#define DeleteObj(obj)
Definition: newobj.h:159
string.h
module.h
maxon::String
Definition: string.h:1197
maxon::OK
return OK
Definition: apibase.h:2490
iferr_return
#define iferr_return
Definition: resultbase.h:1434
MAXON_INITIALIZATION
#define MAXON_INITIALIZATION(...)
Definition: module.h:729
maxon::Result< void >
iferr_scope
#define iferr_scope
Definition: resultbase.h:1343
machineinfo.h
NewObj
#define NewObj(T,...)
Definition: newobj.h:108
application.h