feisty meow concerns codebase  2.140
static_memory_gremlin.h File Reference
#include <basis/contracts.h>
#include <basis/enhance_cpp.h>
#include <basis/mutex.h>
Include dependency graph for static_memory_gremlin.h:

Go to the source code of this file.

Classes

class  structures::static_memory_gremlin
 Holds onto memory chunks that are allocated globally within the program. More...
 

Namespaces

 structures
 A dynamic container class that holds any kind of object via pointers.
 

Macros

#define SAFE_STATIC(type, func_name, parms)    type &func_name() { SAFE_STATIC_IMPLEMENTATION(type, parms, __LINE__); }
 Statically defines a singleton object whose scope is the program's lifetime. More...
 
#define SAFE_STATIC_CONST(type, func_name, parms)
 this version returns a constant object instead. More...
 
#define STATIC_STRING(str)    SAFE_STATIC_IMPLEMENTATION(astring, (str), __LINE__)
 Statically defines a string for the rest of the program's life. More...
 
#define UNIQUE_NAME_BASED_ON_SOURCE(name, linenum)    static const char *name = "file:" __FILE__ ":line:" #linenum
 this macro creates a unique const char pointer based on file location. More...
 
#define SAFE_STATIC_IMPLEMENTATION(type, parms, linenum)
 this blob is just a chunk of macro implementation for SAFE_STATIC... More...
 

Macro Definition Documentation

◆ SAFE_STATIC

#define SAFE_STATIC (   type,
  func_name,
  parms 
)     type &func_name() { SAFE_STATIC_IMPLEMENTATION(type, parms, __LINE__); }

Statically defines a singleton object whose scope is the program's lifetime.

SAFE_STATIC is a macro that dynamically creates a function returning a particular data type. the object is allocated statically, so that the same object will be returned ever after until the program is shut down. the allocation is guarded so that multiple threads cannot create conflicting static objects.

note: in ms-win32, if you use this macro in a static library (rather than a DLL), then the heap context is different. thus you could actually have multiple copies of the underlying object. if the object is supposed to be global and unique, then that's a problem. relocating the definitions to a dll while keeping declarations in the static lib works (see the program wide logger approach in <application/program_wide_logger.h>). "type" is the object class to return. "func_name" is the function to be created. "parms" must be a list of parameters in parentheses or nothing.

example usage:

SAFE_STATIC(connection_table, conntab, (parm1, parm2))
#define SAFE_STATIC(type, func_name, parms)
Statically defines a singleton object whose scope is the program's lifetime.

will define a function: connection_table &conntab() that returns a connection table object which has been created safely, given that the main synchronizer was called from the main thread at least once.

SAFE_STATIC(astring, static_string_doodle, )

will define a static astring object named "static_string_doodle" that uses the default constructor for astrings.

Definition at line 129 of file static_memory_gremlin.h.

◆ SAFE_STATIC_CONST

#define SAFE_STATIC_CONST (   type,
  func_name,
  parms 
)
Value:
const type &func_name() \
{ SAFE_STATIC_IMPLEMENTATION(type, parms, __LINE__); }
#define SAFE_STATIC_IMPLEMENTATION(type, parms, linenum)
this blob is just a chunk of macro implementation for SAFE_STATIC...

this version returns a constant object instead.

Definition at line 133 of file static_memory_gremlin.h.

◆ SAFE_STATIC_IMPLEMENTATION

#define SAFE_STATIC_IMPLEMENTATION (   type,
  parms,
  linenum 
)

this blob is just a chunk of macro implementation for SAFE_STATIC...

if the static object isn't initialized yet, we'll create it and store it in the static_memory_gremlin. we make sure that the program isn't shutting down, because that imposes a new requirement–previously created statics might have already been destroyed. thus, during the program shutdown, we carefully recreate any objects that were already toast.

Definition at line 158 of file static_memory_gremlin.h.

◆ STATIC_STRING

#define STATIC_STRING (   str)     SAFE_STATIC_IMPLEMENTATION(astring, (str), __LINE__)

Statically defines a string for the rest of the program's life.

This macro can be used to make functions returning a string a little simpler. This can only be used when the string is constant. The usual way to use this macro is in a function that returns a constant reference to a string. The macro allocates the string to be returned statically so that all future calls will refer to the stored string rather than recreating it again.

Definition at line 143 of file static_memory_gremlin.h.

◆ UNIQUE_NAME_BASED_ON_SOURCE

#define UNIQUE_NAME_BASED_ON_SOURCE (   name,
  linenum 
)     static const char *name = "file:" __FILE__ ":line:" #linenum

this macro creates a unique const char pointer based on file location.

Definition at line 149 of file static_memory_gremlin.h.