nlib
SimpleSingleton.h
[詳解]
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:
32  SimpleSingleton();
33  NLIB_DISALLOW_COPY_AND_ASSIGN(SimpleSingleton);
34 };
35 
36 template <class T>
37 typename SimpleSingleton<T>::Creator SimpleSingleton<T>::creator;
38 
39 NLIB_NAMESPACE_END
40 
41 #endif // INCLUDE_NN_NLIB_SIMPLESINGLETON_H_
#define NLIB_FINAL
利用可能であればfinalが定義されます。そうでない場合は空文字列です。
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
TypeName で指定されたクラスのコピーコンストラクタと代入演算子を禁止します。
Definition: Config.h:126
main関数の実行前にstatic変数により暗黙的にインスタンスを構築するタイプのシングルトンです。 ...
static T & Instance()
T のシングルトンインスタンスを取得します。
開発環境別の設定が書かれるファイルです。