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