Generic Macros

内容表

关于

The MAXON API makes heavy use of C++ preprocessor macros. These macros are used to automatically create code or to mark parts of the code (e.g. in interfaces). The macros described on this page are relevant for every kind of development. Other specialised macros exist for special tasks.

Object Use

Under Xcode / GCC the compiler can check if the return value of a function is used or not. If a return value that should be used is not used, a compile error is thrown.

注意
Do not use these macros to mute errors returned with the error system. Instead use iferr_cannot_fail() or iferr_ignore() 。见 Error Handling .
//---------------------------------------------------------------------------------------- // A critical function. Success must always be checked. //---------------------------------------------------------------------------------------- MAXON_WARN_UNUSED static maxon::Bool CriticalFunction();

// This example shows the function call without and with handling the return value.

// this will trigger a compile error CriticalFunction();

// this won't trigger a compile error const maxon::Bool res = CriticalFunction();

// this won't trigger a compile error MAXON_WARN_MUTE_UNUSED CriticalFunction();

// This example shows a simple structure. // It is marked with MAXON_WARN_UNUSED_CLASS, // so when used as return value, this // return value must be handled.

// A CriticalInfo. struct MAXON_WARN_UNUSED_CLASS CriticalInfo { maxon::Int32 _info; };

// This function returns a CriticalInfo. static CriticalInfo GetCriticalInfo();

// This example shows how the return value of the type of a MAXON_WARN_UNUSED_CLASS // marked class must be handled to avoid a compile error.

// this will trigger a compile error GetCriticalInfo();

// this won't trigger a compile error const CriticalInfo info = GetCriticalInfo();

DiagnosticOutput ( "Info: @" , info._info);

If a variable is declared but not used this will trigger a compile warning. This warning can be suppressed with this function:

注意
Using maxon::UseVariable() will not cause any performance penalty.
// This example shows how an unused variable triggers a warning and how this warning is silenced. MAXON_SCOPE { // this will trigger a compile warning that "number" is not used const maxon::Int number = GetNumber(); } MAXON_SCOPE { // this won't trigger a warning const maxon::Int number = GetNumber(); maxon::UseVariable(number); }

Program Flow and Structure

These macros are used to mark certain parts of the source code:

// This example uses the MAXON_SCOPE macro to mark two blocks of code // that use the same variable names. MAXON_SCOPE { const maxon::Int key = 0; maxon::Int number = data.Get(key, 0); number++; data.Set(key, number) iferr_return ; } MAXON_SCOPE { const maxon::Int key = 1; maxon::Int number = data.Get(key, 0); number++; data.Set(key, number) iferr_return ; }

// This example uses the MAXON_UNLIKELY to mark an unlikely case.

if ( MAXON_UNLIKELY (number > 100)) { number = 100; }

Switch-case statements can be improved with:

// This example defines a custom enumeration and checks if all elements are used in a switch-statement. enum class MyEnum { A, B, C }; const MyEnum value = MyEnum::B; MAXON_SWITCH_CHECKALLENUMS_BEGIN ; switch (value) { case MyEnum::A: ApplicationOutput ( "Value A" ); break ; case MyEnum::B: ApplicationOutput ( "Value B" ); break ; default : ApplicationOutput ( "No Value" ); break ; } MAXON_SWITCH_CHECKALLENUMS_END ;

Finally

The code defined in a "finally" block is executed when the program flow leaves the current scope.

// This example uses a "finally" block to ensure the allocated memory is deleted. static maxon::Result<void> HandleMemory() { iferr_scope ; maxon::Char * data = NewMemClear ( maxon::Char , 1024) iferr_return ; finally { // make sure that the allocated data will be deleted in any case maxon::DeleteMem (data); }; ReadData(data) iferr_return ; WriteData(data) iferr_return ; return maxon::OK ; }

Enummerations

// This example shows the declaration of enumeration classes.

//---------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------- enum class PRIMARY_COLOR { RED = 1, GREEN = 2, BLUE = 3 } MAXON_ENUM_LIST (PRIMARY_COLOR);

//---------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------- enum class QUALITY { NONE = 0, A = (1 << 0), B = (1 << 1), C = (1 << 2), D = (1 << 3), } MAXON_ENUM_FLAGS (QUALITY);

// This example shows how enumeration values are used.

// use simple enumeration

// set color const PRIMARY_COLOR color = PRIMARY_COLOR::RED ;

// check color if (color == PRIMARY_COLOR::RED ) DiagnosticOutput ( "Red" ); if (color != PRIMARY_COLOR::BLUE ) DiagnosticOutput ( "Color is not blue." );

// use bit flags

// define qualities const QUALITY qualities = QUALITY::A | QUALITY::B;

// check qualities

if (qualities & QUALITY::A) DiagnosticOutput ( "Qualitiy A set" ); if (!(qualities & QUALITY::D)) DiagnosticOutput ( "Quality D not set" );

类和函数

The following macros are used to declare classes and their functions:

These macros are used to implement certain operators automatically:

// This example shows a custom class using various macros // to improve the class' functionality. class NumberClass { public : MAXON_IMPLICIT NumberClass( maxon::Int v) { _number = v; }

// define copy constructor NumberClass( const NumberClass& src ) : MAXON_COPY_MEMBERS (_number) { }

// define assign operator "=" from copy constructor MAXON_OPERATOR_COPY_ASSIGNMENT (NumberClass);

// define "==", "!=" operators and GetHashCode() MAXON_OPERATOR_EQUALITY_HASHCODE (NumberClass, _number);

// accessors const maxon::Int * GetNumber() const { return &_number; } maxon::Int * GetNumber() { return MAXON_NONCONST_COUNTERPART (GetNumber()); } private : maxon::Int _number; };

// This example uses a class defined with various macros to implement // a non-const access function and GetHashCode(). NumberClass someNumber(1);

// edit maxon::Int * const number = someNumber.GetNumber(); *number = *number + 1;

// print const maxon::Int * const res = someNumber.GetNumber(); DiagnosticOutput ( "Number: @" , *res);

// hash const maxon::UInt hash = someNumber.GetHashCode(); DiagnosticOutput ( "Hash: @" , hash);

// copy NumberClass someOtherNumber(0); someOtherNumber = someNumber;

Utility

Other utility macros exist:

// This example uses various utility macros.

// prints the value of the macro SOME_VALUE const maxon::String value(MAXON_STRINGIFY(SOME_VALUE)); DiagnosticOutput ( "Some Value: @" , value);

// print the hash of the given values const maxon::Int i = 123; const maxon::Float f = 132.0; const maxon::UInt hash = MAXON_HASHCODE (i, f); DiagnosticOutput ( "Hash: @" , hash);

延伸阅读

MAXON_OPERATOR_COPY_ASSIGNMENT
#define MAXON_OPERATOR_COPY_ASSIGNMENT(TypeName)
定义: classhelpers.h:357
MAXON_SWITCH_CHECKALLENUMS_END
#define MAXON_SWITCH_CHECKALLENUMS_END
定义: compilerdetection.h:415
NewMemClear
#define NewMemClear(T, cnt)
定义: defaultallocator.h:205
MAXON_IMPLICIT
#define MAXON_IMPLICIT
定义: apibase.h:168
MAXON_ENUM_FLAGS
#define MAXON_ENUM_FLAGS(E,...)
定义: enumflags.h:159
finally
#define finally
定义: finally.h:66
MAXON_WARN_UNUSED
#define MAXON_WARN_UNUSED
定义: compilerdetection.h:322
maxon::String
定义: string.h:1197
maxon::OK
return OK
定义: apibase.h:2532
BLUE
BLUE
定义: lib_birender.h:67
maxon::Bool
bool Bool
boolean type, possible values are only false/true, 8 bit
定义: apibase.h:177
iferr_return
#define iferr_return
定义: resultbase.h:1434
MAXON_HASHCODE
#define MAXON_HASHCODE(...)
MAXON_HASHCODE computes the hash code of the arguments based on DefaultCompare::GetCombinedHashCode.
定义: classhelpers.h:452
maxon::Float
Float64 Float
定义: apibase.h:193
GREEN
GREEN
定义: lib_birender.h:66
MAXON_OPERATOR_EQUALITY_HASHCODE
#define MAXON_OPERATOR_EQUALITY_HASHCODE(T,...)
定义: classhelpers.h:498
DiagnosticOutput
#define DiagnosticOutput(formatString,...)
定义: debugdiagnostics.h:166
MAXON_UNLIKELY
#define MAXON_UNLIKELY(X)
定义: compilerdetection.h:482
maxon::src
const T & src
定义: apibase.h:2525
maxon::Result< void >
maxon::Int32
int32_t Int32
32 bit signed integer datatype.
定义: apibase.h:172
maxon::Int
Int64 Int
signed 32/64 bit int, size depends on the platform
定义: apibase.h:184
iferr_scope
#define iferr_scope
定义: resultbase.h:1343
maxon::DeleteMem
void DeleteMem(T *&p)
定义: defaultallocator.h:258
RED
RED
定义: lib_birender.h:65
MDDIRTY::NONE
@ NONE
MAXON_SWITCH_CHECKALLENUMS_BEGIN
#define MAXON_SWITCH_CHECKALLENUMS_BEGIN
定义: compilerdetection.h:414
ApplicationOutput
#define ApplicationOutput(formatString,...)
定义: debugdiagnostics.h:207
MAXON_WARN_MUTE_UNUSED
#define MAXON_WARN_MUTE_UNUSED
The MAXON_WARN_MUTE_UNUSED macro is deprecated. Please use iferr_ignore or iferr_cannot_fail and spec...
定义: compilerdetection.h:326
MAXON_WARN_UNUSED_CLASS
#define MAXON_WARN_UNUSED_CLASS
定义: compilerdetection.h:323
MAXON_ENUM_LIST
#define MAXON_ENUM_LIST(E,...)
定义: enumflags.h:104
maxon::Char
char Char
signed 8 bit character
定义: apibase.h:180
maxon::UInt
UInt64 UInt
unsigned 32/64 bit int, size depends on the platform
定义: apibase.h:185
MAXON_COPY_MEMBERS
#define MAXON_COPY_MEMBERS(...)
定义: classhelpers.h:442
MAXON_NONCONST_COUNTERPART
#define MAXON_NONCONST_COUNTERPART(...)
定义: classhelpers.h:562
MAXON_SCOPE
#define MAXON_SCOPE
定义: apibase.h:2645

Copyright  © 2014-2025 乐数软件    

工业和信息化部: 粤ICP备14079481号-1