ITK  5.2.0
Insight Toolkit
itkSingleton.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
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
35 Unused(const T &){};
36 
37 namespace itk
38 {
45 class ITKCommon_EXPORT SingletonIndex
46 {
47 public:
50  using SingletonData =
51  std::map<std::string, std::tuple<void *, std::function<void(void *)>, std::function<void(void)>>>;
52 
53  // obtain a global registered in the singleton index under the
54  // globalName, if unknown then nullptr will be returned.
55  template <typename T>
56  T *
57  GetGlobalInstance(const char * globalName)
58  {
59  return static_cast<T *>(this->GetGlobalInstancePrivate(globalName));
60  }
61 
62 
63  // returns true if the globalName has not been registered yet.
64  //
65  // It is assumed that the global will remain valid until the start
66  // of globals being destroyed.
67  template <typename T>
68  bool
69  SetGlobalInstance(const char * globalName,
70  T * global,
71  std::function<void(void *)> func,
72  std::function<void(void)> deleteFunc)
73  {
74  return this->SetGlobalInstancePrivate(globalName, global, func, deleteFunc);
75  }
76 
79  static Self *
80  GetInstance();
81  static void
82  SetInstance(Self * instance);
83  ~SingletonIndex();
85 
86 private:
87  // may return nullptr if string is not registered already
88  //
89  // access something like a std::map<std::string, void *> or
90  // registered globals, it may be possible to restrict the held
91  // classes to be derived from itk::LightObject, so dynamic cast can
92  // work, and could use some type of Holder<T> class for intrinsic types
93  void *
94  GetGlobalInstancePrivate(const char * globalName);
95  // If globalName is already registered than false is return,
96  // otherwise global is added to the singleton index under globalName
97  bool
98  SetGlobalInstancePrivate(const char * globalName,
99  void * global,
100  std::function<void(void *)> func,
101  std::function<void(void)> deleteFunc);
102 
108  static Self * m_Instance;
109  // static SingletonIndexPrivate * m_GlobalSingleton;
110 };
111 
112 
113 // A wrapper for a global variable registered in the singleton index.
114 template <typename T>
115 T *
116 Singleton(const char * globalName, std::function<void(void *)> func, std::function<void(void)> deleteFunc)
117 {
118  static SingletonIndex * singletonIndex = SingletonIndex::GetInstance();
119  Unused(singletonIndex);
120  T * instance = SingletonIndex::GetInstance()->GetGlobalInstance<T>(globalName);
121  if (instance == nullptr)
122  {
123  instance = new T;
124  if (!SingletonIndex::GetInstance()->SetGlobalInstance<T>(globalName, instance, func, deleteFunc))
125  {
126  delete instance;
127  instance = nullptr;
128  }
129  }
130  return instance;
131 }
132 } // end namespace itk
133 
134 #endif
itk::SingletonIndex::m_Instance
static Self * m_Instance
Definition: itkSingleton.h:108
itk::SingletonIndex::SetGlobalInstance
bool SetGlobalInstance(const char *globalName, T *global, std::function< void(void *)> func, std::function< void(void)> deleteFunc)
Definition: itkSingleton.h:69
itk::SingletonIndex::m_GlobalObjects
SingletonData m_GlobalObjects
Definition: itkSingleton.h:107
itkSingletonMacro.h
Unused
void Unused(const T &)
A function which does nothing.
Definition: itkSingleton.h:35
itkMacro.h
itk::SingletonIndex::GetInstance
static Self * GetInstance()
itk::SingletonIndex
Implementation detail.
Definition: itkSingleton.h:45
itk::Singleton
T * Singleton(const char *globalName, std::function< void(void *)> func, std::function< void(void)> deleteFunc)
Definition: itkSingleton.h:116
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::SingletonIndex::SingletonData
std::map< std::string, std::tuple< void *, std::function< void(void *)>, std::function< void(void)> >> SingletonData
Definition: itkSingleton.h:51
itk::SingletonIndex::GetGlobalInstance
T * GetGlobalInstance(const char *globalName)
Definition: itkSingleton.h:57