HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TfStaticData< T, Factory > Class Template Reference

#include <staticData.h>

Public Member Functions

Toperator-> () const
 
Toperator* () const
 
TGet () const
 
bool IsInitialized () const
 

Detailed Description

template<class T, class Factory = Tf_StaticDataDefaultFactory<T>>
class TfStaticData< T, Factory >

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:

// source file:
#include <set>
#include <string>
static TfStaticData<set<string> > Xyz_nameSet;
void XyzAddName(string name) {
Xyz_nameSet->insert(name);
...
}

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:

void Xyz_SetLastName(string s) {
*Xyz_curName = s;
vector<string> v;
v.push_back(*Xyz_curName);
}

Definition at line 114 of file staticData.h.

Member Function Documentation

template<class T, class Factory = Tf_StaticDataDefaultFactory<T>>
T* TfStaticData< T, Factory >::Get ( ) const
inline

Return a pointer to the underlying object, creating and initializing it if necessary.

Definition at line 126 of file staticData.h.

template<class T, class Factory = Tf_StaticDataDefaultFactory<T>>
bool TfStaticData< T, Factory >::IsInitialized ( ) const
inline

Return true if the underlying data object is created and initialized. Return false otherwise.

Definition at line 133 of file staticData.h.

template<class T, class Factory = Tf_StaticDataDefaultFactory<T>>
T& TfStaticData< T, Factory >::operator* ( ) const
inline

Member lookup. The underlying data object is created and initialized if necessary.

Definition at line 122 of file staticData.h.

template<class T, class Factory = Tf_StaticDataDefaultFactory<T>>
T* TfStaticData< T, Factory >::operator-> ( ) const
inline

Return a pointer to the underlying data object. It is created and initialized if necessary.

Definition at line 118 of file staticData.h.


The documentation for this class was generated from the following file: