nlib
SimpleSingleton.h
Go to the documentation of this file.
1 
2 #pragma once
3 #ifndef INCLUDE_NN_NLIB_SIMPLESINGLETON_H_
4 #define INCLUDE_NN_NLIB_SIMPLESINGLETON_H_
5 
6 #include "nn/nlib/Config.h"
7 
8 NLIB_NAMESPACE_BEGIN
9 
10 template <class T>
12  private:
13  // This ensures Instance() called before main()
14  struct Creator {
15  Creator() { SimpleSingleton<T>::Instance(); }
16  inline void DummyFunc() const {}
17  };
18  static Creator creator;
19 
20  public:
21  static T& Instance() {
22  static T obj;
23 
24  // This ensures 'obj' is already constructed
25  // when called from a static object's constructor.
26  creator.DummyFunc();
27  return obj;
28  }
29  static T& GetInstance() { return Instance(); }
30 
31  private:
34 };
35 
36 template <class T>
38 
39 NLIB_NAMESPACE_END
40 
41 #endif // INCLUDE_NN_NLIB_SIMPLESINGLETON_H_
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
Prohibits use of the copy constructor and assignment operator for the class specified by TypeName...
Definition: Config.h:145
Singleton of the type for implicitly constructing an instance based on a static variable before calli...
static T & Instance()
Gets the singleton instance of T.
A file that contains the configuration information for each development environment.
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
Definition: Config.h:211