ITK  5.4.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 
51 #ifndef ITK_LEGACY_REMOVE
52  using SingletonData [[deprecated("The internal representation of the singleton data is private, and may not "
53  "correspond with SingletonData anymore.")]] =
54  std::map<std::string, std::tuple<void *, std::function<void(void *)>, std::function<void()>>>;
55 #endif
56 
57  // obtain a global registered in the singleton index under the
58  // globalName, if unknown then nullptr will be returned.
59  template <typename T>
60  T *
61  GetGlobalInstance(const char * globalName)
62  {
63  return static_cast<T *>(this->GetGlobalInstancePrivate(globalName));
64  }
65 
66 
67  // It is assumed that the global will remain valid until the start
68  // of globals being destroyed.
69  template <typename T>
70  void
71  SetGlobalInstance(const char * globalName, T * global, std::function<void()> deleteFunc)
72  {
73  this->SetGlobalInstancePrivate(globalName, global, deleteFunc);
74  }
75 
76 #ifndef ITK_FUTURE_LEGACY_REMOVE
77  template <typename T>
78  [[deprecated("Prefer calling the SetGlobalInstance(globalName, global, deleteFunc) overload (without the unused func "
79  "parameter)!")]] bool
80  SetGlobalInstance(const char * globalName,
81  T * global,
82  std::function<void(void *)> itkNotUsed(func),
83  std::function<void()> deleteFunc)
84  {
85  this->SetGlobalInstance(globalName, global, deleteFunc);
86  // Just returns true for backward compatibility (legacy only).
87  return true;
88  }
89 #endif
90 
93  static Self *
94  GetInstance();
95  static void
96  SetInstance(Self * instance);
97  ~SingletonIndex();
100 private:
101  // may return nullptr if string is not registered already
102  //
103  // access something like a std::map<std::string, void *> or
104  // registered globals, it may be possible to restrict the held
105  // classes to be derived from itk::LightObject, so dynamic cast can
106  // work, and could use some type of Holder<T> class for intrinsic types
107  void *
108  GetGlobalInstancePrivate(const char * globalName);
109 
110  // global is added or set to the singleton index under globalName
111  void
112  SetGlobalInstancePrivate(const char * globalName, void * global, std::function<void()> deleteFunc);
113 
118  std::map<std::string, std::tuple<void *, std::function<void()>>> m_GlobalObjects;
119  static Self * m_Instance;
120  // static SingletonIndexPrivate * m_GlobalSingleton;
121 };
125 // A wrapper for a global variable registered in the singleton index.
126 template <typename T>
127 T *
128 Singleton(const char * globalName, std::function<void()> deleteFunc)
129 {
130  static SingletonIndex * singletonIndex = SingletonIndex::GetInstance();
131  Unused(singletonIndex);
132  T * instance = SingletonIndex::GetInstance()->GetGlobalInstance<T>(globalName);
133  if (instance == nullptr)
134  {
135  instance = new T;
136  SingletonIndex::GetInstance()->SetGlobalInstance<T>(globalName, instance, deleteFunc);
137  }
138  return instance;
139 }
140 
141 
142 #ifndef ITK_FUTURE_LEGACY_REMOVE
143 template <typename T>
144 [[deprecated("Prefer calling the Singleton(globalName, deleteFunc) overload (without the unused func parameter)!")]] T *
145 Singleton(const char * globalName, std::function<void(void *)> itkNotUsed(func), std::function<void()> deleteFunc)
146 {
147  return Singleton<T>(globalName, deleteFunc);
148 }
149 #endif
150 
151 } // end namespace itk
152 
153 #endif
itk::SingletonIndex::m_Instance
static Self * m_Instance
Definition: itkSingleton.h:119
itk::SingletonIndex::m_GlobalObjects
std::map< std::string, std::tuple< void *, std::function< void()> > > m_GlobalObjects
Definition: itkSingleton.h:118
itkSingletonMacro.h
itk::Singleton
T * Singleton(const char *globalName, std::function< void()> deleteFunc)
Definition: itkSingleton.h:128
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
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:61
AddImageFilter
Definition: itkAddImageFilter.h:81
itk::SingletonIndex::SetGlobalInstance
void SetGlobalInstance(const char *globalName, T *global, std::function< void()> deleteFunc)
Definition: itkSingleton.h:71