OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimFactoryListInterface.h
Go to the documentation of this file.
1 //**************************************************************************************************
2 //
3 // License: MIT
4 //
5 // See LICENSE.txt file in the top level directory for more details.
6 //
7 // Description: Class declaration of ossimFactoryListInterface.
8 //
9 //**************************************************************************************************
10 // $Id$
11 #ifndef ossimFactoryListInterface_HEADER
12 #define ossimFactoryListInterface_HEADER
13 #include <vector>
14 #include <ossim/base/ossimRefPtr.h>
15 #include <ossim/base/ossimObject.h>
16 #include <ossim/base/ossimString.h>
18 #include <mutex>
19 
32 template <class T, class NativeType>
34  {
35  public:
36  typedef std::vector<T*> FactoryListType;
37  typedef T FactoryType;
38  typedef NativeType NativeReturnType;
39 
41 
45  void addFactory(T* factory)
46  {
47  registerFactory(factory);
48  }
49 
54  bool isFactoryRegistered(T* factory)const
55  {
56  if(!factory) return false;
57  std::lock_guard<std::mutex> lock(m_factoryListMutex);
58 
59  return findFactory(factory);
60  }
61 
66  void registerFactory(T* factory, bool pushToFrontFlag=false)
67  {
68  if(!factory) return;
69  std::lock_guard<std::mutex> lock(m_factoryListMutex);
70  if(!findFactory(factory))
71  {
72  if (pushToFrontFlag)
73  {
74  m_factoryList.insert(m_factoryList.begin(), factory);
75  }
76  else
77  {
78  m_factoryList.push_back(factory);
79  }
80  }
81  }
85  void unregisterFactory(T* factory)
86  {
87  std::lock_guard<std::mutex> lock(m_factoryListMutex);
88  ossim_uint32 idx = 0;
89  for(idx = 0; idx < m_factoryList.size(); ++idx)
90  {
91  if(factory == m_factoryList[idx])
92  {
93  m_factoryList.erase(m_factoryList.begin() + idx);
94  return;
95  }
96  }
97  }
98 
103  void unregisterFactory(const ossimString& factoryTypeName)
104  {
105  std::lock_guard<std::mutex> lock(m_factoryListMutex);
106  ossim_uint32 idx = 0;
107  for(idx = 0; idx < m_factoryList.size(); ++idx)
108  {
109  if (m_factoryList[idx])
110  {
111  ossimString mangledName (typeid(*(m_factoryList[idx])).name());
112  if (mangledName.contains(factoryTypeName))
113  {
114  m_factoryList.erase(m_factoryList.begin() + idx);
115  return;
116  }
117  }
118  }
119  }
120 
125  {
126  std::lock_guard<std::mutex> lock(m_factoryListMutex);
127  m_factoryList.clear();
128  }
129 
133  void registerFactoryToFront(T* factory)
134  {
135  std::lock_guard<std::mutex> lock(m_factoryListMutex);
136  if(!findFactory(factory))
137  {
138  m_factoryList.insert(m_factoryList.begin(), factory);
139  }
140  }
141 
146  void registerFactoryBefore(T* factory, T* beforeThisFactory)
147  {
148  std::lock_guard<std::mutex> lock(m_factoryListMutex);
149  if(!findFactory(factory))
150  {
151  ossim_uint32 idx = 0;
152  for(idx = 0; idx < m_factoryList.size(); ++idx)
153  {
154  if(beforeThisFactory == m_factoryList[idx])
155  {
156  m_factoryList.insert(m_factoryList.begin() + idx, factory);
157  return;
158  }
159  }
160  m_factoryList.push_back(factory);
161  }
162  }
163 
169  void getAllTypeNamesFromRegistry(std::vector<ossimString>& typeList)const;
170 
175  ossimObject* createObjectFromRegistry(const ossimString& typeName)const;
176 
182  const char* prefix=0)const;
183 
188  NativeType* createNativeObjectFromRegistry(const ossimString& typeName)const;
189 
199  NativeType* createNativeObjectFromRegistry(const ossimKeywordlist& kwl,
200  const char* prefix=0)const;
201  protected:
205  bool findFactory(T* factory)const
206  {
207  if(!factory) return false;
208  ossim_uint32 idx = 0;
209  for(;idx < m_factoryList.size();++idx)
210  {
211  if(m_factoryList[idx] == factory)
212  {
213  return true;
214  }
215  }
216 
217  return false;
218  }
219  mutable std::mutex m_factoryListMutex;
221  };
222 
223 template <class T, class NativeType>
225 {
226  //std::lock_guard<std::mutex> lock(m_factoryListMutex);
227  ossim_uint32 idx = 0;
228  for(; idx<m_factoryList.size(); ++idx)
229  {
230  m_factoryList[idx]->getTypeNameList(typeList);
231  }
232 }
233 template <class T, class NativeType>
235 {
236  //std::lock_guard<std::mutex> lock(m_factoryListMutex);
237  ossimObject* result = 0;
238  ossim_uint32 idx = 0;
239  for(;((idx<m_factoryList.size())&&!result); ++idx)
240  {
241  result = m_factoryList[idx]->createObject(typeName);
242  }
243  return result;
244 }
245 
246 template <class T, class NativeType>
248  const char* prefix)const
249 {
250  // std::lock_guard<std::mutex> lock(m_factoryListMutex);
251  ossimObject* result = 0;
252  ossim_uint32 idx = 0;
253  for(;((idx<m_factoryList.size())&&!result); ++idx)
254  {
255  result = m_factoryList[idx]->createObject(kwl, prefix);
256  }
257  return result;
258 }
259 
260 template <class T, class NativeType>
262 {
263  NativeType* result = 0;
264  ossimRefPtr<ossimObject> tempObject = createObjectFromRegistry(typeName);
265  if(tempObject.valid())
266  {
267  result = dynamic_cast<NativeType*>(tempObject.get());
268  if(result)
269  {
270  tempObject.release();
271  }
272  }
273 
274  return result;
275 }
276 
277 template <class T, class NativeType>
279  const char* prefix)const
280 {
281  NativeType* result = 0;
282  ossimRefPtr<ossimObject> tempObject = createObjectFromRegistry(kwl, prefix);
283  if(tempObject.valid())
284  {
285  result = dynamic_cast<NativeType*>(tempObject.get());
286  if(result)
287  {
288  tempObject.release();
289  }
290  }
291 
292  return result;
293 }
294 
295 #endif
Represents serializable keyword/value map.
bool valid() const
Definition: ossimRefPtr.h:75
bool contains(char aChar) const
Definition: ossimString.h:58
The is a factory list interface that allows registries to be accessed in a common way...
void addFactory(T *factory)
This is for backward compatability and calls registerFactory for simple adds.
void unregisterFactory(T *factory)
Will remove the factory from the registry.
void registerFactoryToFront(T *factory)
Inserts the factory to the front of the list.
unsigned int ossim_uint32
NativeType * createNativeObjectFromRegistry(const ossimString &typeName) const
This is a helper method that calls the createObject and makes sure that the returned object is of the...
T * release()
Definition: ossimRefPtr.h:93
void registerFactory(T *factory, bool pushToFrontFlag=false)
Will register a factory to the factory list.
void registerFactoryBefore(T *factory, T *beforeThisFactory)
Will insert the factory before the beforeThisFactory.
bool isFactoryRegistered(T *factory) const
Public access method to determine if a factory is already registered to this list.
bool findFactory(T *factory) const
Utility to find a factory in the list.
void unregisterAllFactories()
Will remove all factories from the registry.
void getAllTypeNamesFromRegistry(std::vector< ossimString > &typeList) const
Will add all object types the factories can allocate.
ossimObject * createObjectFromRegistry(const ossimString &typeName) const
This is the base object return for all objects in the system.
void unregisterFactory(const ossimString &factoryTypeName)
Will remove the factory from the registry by name.