HDK
|
#include <staticData.h>
Public Member Functions | |
T * | operator-> () const |
T & | operator* () const |
T * | Get () const |
bool | IsInitialized () const |
Create or return a previously created object instance of global data.
Any form of global data that requires an constructor (even a default constructor) is unsafe to declare as global data. By global data we mean either a variable defined at file-scope (outside of a function) or a static member of a class. This is because the initialization order of globals is undefined across translation units.
The only exceptions are constexpr constructors and "plain old data" types such as integral or float/double type and pointers. In contrast, std::string
requires construction, as do most STL
types, and most user-defined types as well. Note that static local variables in functions are also safe and are initialized in a thread-safe manner the first time they're encountered.
One way to handle this problem is to go the singleton route, which can be done using the TfSingleton
pattern. However, a fair amount of coding is required for this, and at times, something more lightweight is appropriate. For these few cases, the following construct may be employed:
One uses a TfStaticData<T>
as if it were a pointer; upon first use however, the item is initialized to point at a new object of type T
. Note that the type T
must have a default constructor; that is, the newly created object is created by calling "new T"
.
If you have no need to access the data, but need to make sure it has been initialized (for example, if the type's constructor will have some effect that you need to be sure has happened), you can call the Touch() method.
Warning: the TfStaticData
construct relies upon zero-initialization of global data: therefore, you can only use this structure for static data member of classes or variables declare at file-scope. Do not declare a TfStaticData
object as a local variable, as a member of a class or structure, or as a function parameter. Use normal static local variable initialization inside a function.
One can either call member functions using the "->" operator, or use the dereference "*" operator:
Definition at line 113 of file staticData.h.
|
inline |
Return a pointer to the underlying object, creating and initializing it if necessary.
Definition at line 125 of file staticData.h.
|
inline |
Return true if the underlying data object is created and initialized. Return false otherwise.
Definition at line 132 of file staticData.h.
|
inline |
Member lookup. The underlying data object is created and initialized if necessary.
Definition at line 121 of file staticData.h.
|
inline |
Return a pointer to the underlying data object. It is created and initialized if necessary.
Definition at line 117 of file staticData.h.