Initialization
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.
One can simply call global functions in the MAXON_INITIALIZATION() macro.
// called on system start static maxon::Result<void> InitGlobalString() { iferr_scope ;
// 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 ;
// 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 MAXON_INITIALIZATION (// system startup []() -> maxon::Result<void> { iferr_scope ;
// allocate global string g_userName = NewObj ( maxon::String ) iferr_return ;
// 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.