ITK  5.0.0
Insight Segmentation and Registration Toolkit
itkSingleton.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkSingleton_h
19 #define itkSingleton_h
20 
21 #include "itkMacro.h"
22 #include "itkSingletonMacro.h"
23 #include <map>
24 #include <functional>
25 
33 template <typename T>
34 inline void Unused( const T &) {};
35 
36 namespace itk
37 {
44 class ITKCommon_EXPORT SingletonIndex
45 {
46 public:
49  typedef std::map<std::string, std::tuple<void*, std::function<void(void*)>, std::function<void(void)> > > SingletonData;
50 
51  // obtain a global registered in the singleton index under the
52  // globalName, if unknown then nullptr will be returned.
53  template<typename T>
54  T *GetGlobalInstance( const char *globalName )
55  { return static_cast<T*>(this->GetGlobalInstancePrivate(globalName)); }
56 
57 
58  // returns true if the globalName has not been registered yet.
59  //
60  // It is assumed that the global will remain valid until the start
61  // of globals being destroyed.
62  template<typename T>
63  bool SetGlobalInstance( const char * globalName, T * global, std::function<void(void*)> func, std::function<void(void)> deleteFunc)
64  { return this->SetGlobalInstancePrivate(globalName, global, func, deleteFunc);}
65 
68  static Self* GetInstance();
69  static void SetInstance(Self *SingletonIndex);
70  ~SingletonIndex();
72 
73 private:
74 
75  // may return nullptr if string is not registered already
76  //
77  // access something like a std::map<std::string, void *> or
78  // registered globals, it may be possible to restrict the held
79  // classes to be derived from itk::LightObject, so dynamic cast can
80  // work, and could use some type of Holder<T> class for intrinsic types
81  void * GetGlobalInstancePrivate( const char *globalName );
82  // If globalName is already registered than false is return,
83  // otherwise global is added to the singleton index under globalName
84  bool SetGlobalInstancePrivate( const char * globalName, void * global, std::function<void(void*)> func, std::function<void(void)> deleteFunc);
85 
91  static Self* m_Instance;
92 // static SingletonIndexPrivate * m_GlobalSingleton;
93 };
94 
95 
96 // A wrapper for a global variable registered in the singleton index.
97 template<typename T>
98 T* Singleton(const char *globalName, std::function<void(void*)> func, std::function<void(void)> deleteFunc)
99 {
100  static SingletonIndex * singletonIndex = SingletonIndex::GetInstance();
101  Unused(singletonIndex);
102  T* instance = SingletonIndex::GetInstance()->GetGlobalInstance<T>(globalName);
103  if( instance == nullptr )
104  {
105  instance = new T;
106  if (!SingletonIndex::GetInstance()->SetGlobalInstance<T>(globalName, instance, func, deleteFunc))
107  {
108  delete instance;
109  instance = nullptr;
110  }
111  }
112  return instance;
113 }
114 } // end namespace itk
115 
116 #endif
void Unused(const T &)
A function which does nothing.
Definition: itkSingleton.h:34
static Self * m_Instance
Definition: itkSingleton.h:91
static Self * GetInstance()
T * Singleton(const char *globalName, std::function< void(void *)> func, std::function< void(void)> deleteFunc)
Definition: itkSingleton.h:98
Implementation detail.
Definition: itkSingleton.h:44
SingletonData m_GlobalObjects
Definition: itkSingleton.h:90
std::map< std::string, std::tuple< void *, std::function< void(void *)>, std::function< void(void)> > > SingletonData
Definition: itkSingleton.h:49
T * GetGlobalInstance(const char *globalName)
Definition: itkSingleton.h:54
bool SetGlobalInstance(const char *globalName, T *global, std::function< void(void *)> func, std::function< void(void)> deleteFunc)
Definition: itkSingleton.h:63
SingletonIndex Self
Definition: itkSingleton.h:48