Pyrogenesis  trunk
Macros
code_generation.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define STMT(STMT_code__)   do { STMT_code__; } while(false)
 package code into a single statement. More...
 
#define ONCE(ONCE_code__)
 execute the code passed as a parameter only the first time this is reached. More...
 
#define ONCE_NOT(ONCE_code__)
 execute the code passed as a parameter except the first time this is reached. More...
 
#define AT_STARTUP(code__)
 execute the code passed as a parameter before main is entered. More...
 
#define SAFE_NEW(type, ptr)
 C++ new wrapper: allocates an instance of the given type and stores a pointer to it. More...
 
#define SAFE_DELETE(p)
 delete memory ensuing from new and set the pointer to zero (thus making double-frees safe / a no-op) More...
 
#define SAFE_ARRAY_DELETE(p)
 delete memory ensuing from new[] and set the pointer to zero (thus making double-frees safe / a no-op) More...
 
#define SAFE_FREE(p)
 free memory ensuing from malloc and set the pointer to zero (thus making double-frees safe / a no-op) More...
 

Macro Definition Documentation

#define AT_STARTUP (   code__)
Value:
namespace { struct UID__\
{\
UID__() { code__; }\
} UID2__; }
#define UID2__
Definition: code_annotation.h:182
#define UID__
Definition: code_annotation.h:181

execute the code passed as a parameter before main is entered.

WARNING: if the source file containing this is not directly referenced from anywhere, linkers may discard that object file (unless linking statically). see http://www.cbloom.com/3d/techdocs/more_coding.txt

#define ONCE (   ONCE_code__)
Value:
static bool ONCE_done__ = false;\
if(!ONCE_done__)\
{\
ONCE_done__ = true;\
ONCE_code__;\
}\
)
#define STMT(STMT_code__)
package code into a single statement.
Definition: code_generation.h:41

execute the code passed as a parameter only the first time this is reached.

may be called at any time (in particular before main), but is not thread-safe. if that's important, use pthread_once() instead.

#define ONCE_NOT (   ONCE_code__)
Value:
static bool ONCE_done__ = false;\
if(!ONCE_done__)\
ONCE_done__ = true;\
else\
ONCE_code__;\
)
#define STMT(STMT_code__)
package code into a single statement.
Definition: code_generation.h:41

execute the code passed as a parameter except the first time this is reached.

may be called at any time (in particular before main), but is not thread-safe.

#define SAFE_ARRAY_DELETE (   p)
Value:
delete[] (p); /* if p == 0, delete is a no-op */ \
(p) = 0;\
)
#define STMT(STMT_code__)
package code into a single statement.
Definition: code_generation.h:41

delete memory ensuing from new[] and set the pointer to zero (thus making double-frees safe / a no-op)

#define SAFE_DELETE (   p)
Value:
delete (p); /* if p == 0, delete is a no-op */ \
(p) = 0;\
)
#define STMT(STMT_code__)
package code into a single statement.
Definition: code_generation.h:41

delete memory ensuing from new and set the pointer to zero (thus making double-frees safe / a no-op)

#define SAFE_FREE (   p)
Value:
free((void*)p); /* if p == 0, free is a no-op */ \
(p) = 0;\
)
#define STMT(STMT_code__)
package code into a single statement.
Definition: code_generation.h:41

free memory ensuing from malloc and set the pointer to zero (thus making double-frees safe / a no-op)

#define SAFE_NEW (   type,
  ptr 
)
Value:
type* ptr;\
try\
{\
ptr = new type();\
}\
catch(std::bad_alloc&)\
{\
ptr = 0;\
}

C++ new wrapper: allocates an instance of the given type and stores a pointer to it.

sets pointer to 0 on allocation failure.

this simplifies application code when on VC6, which may or may not throw/return 0 on failure.

#define STMT (   STMT_code__)    do { STMT_code__; } while(false)

package code into a single statement.

Parameters
STMT_code__code to be bundled. (must be interpretable as a macro argument, i.e. sequence of tokens). the argument name is chosen to avoid conflicts.

notes:

  • for(;;) { break; } and {} don't work because invocations of macros implemented with STMT often end with ";", thus breaking if() expressions.
  • we'd really like to eliminate "conditional expression is constant" warnings. replacing 0 literals with extern volatile variables fools VC7 but isn't guaranteed to be free of overhead. we will just squelch the warning (unfortunately non-portable).