Nektar++
Public Types | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
Nektar::LibUtilities::NekManager< KeyType, ValueT, opLessCreator > Class Template Reference

#include <NekManager.hpp>

Public Types

typedef std::shared_ptr< ValueT > ValueType
 
typedef std::function< ValueType(const KeyType &key)> CreateFuncType
 
typedef std::map< KeyType, ValueTypeValueContainer
 
typedef std::shared_ptr< ValueContainerValueContainerShPtr
 
typedef std::map< KeyType, CreateFuncType, opLessCreator > CreateFuncContainer
 
typedef std::map< std::string, std::shared_ptr< ValueContainer > > ValueContainerPool
 
typedef std::shared_ptr< bool > BoolSharedPtr
 
typedef std::map< std::string, BoolSharedPtrFlagContainerPool
 

Public Member Functions

 NekManager (std::string whichPool="")
 
 NekManager (CreateFuncType f, std::string whichPool="")
 
 ~NekManager ()
 
bool RegisterCreator (const KeyType &key, const CreateFuncType &createFunc)
 Register the given function and associate it with the key. The return value is just to facilitate calling statically. More...
 
bool RegisterGlobalCreator (const CreateFuncType &createFunc)
 Register the Global Create Function. The return value is just to facilitate calling statically. More...
 
bool AlreadyCreated (const KeyType &key)
 
ValueType operator[] (const KeyType &key)
 
void DeleteObject (const KeyType &key)
 
size_t PoolCount (std::string whichPool)
 

Static Public Member Functions

static void ClearManager (std::string whichPool="")
 
static bool PoolCreated (std::string whichPool)
 
static void EnableManagement (std::string whichPool="")
 
static void DisableManagement (std::string whichPool="")
 

Private Member Functions

NekManager< KeyType, ValueType, opLessCreator > & operator= (const NekManager< KeyType, ValueType, opLessCreator > &rhs)
 
 NekManager (const NekManager< KeyType, ValueType, opLessCreator > &rhs)
 

Private Attributes

ValueContainerShPtr m_values
 
BoolSharedPtr m_managementEnabled
 
CreateFuncType m_globalCreateFunc
 
CreateFuncContainer m_keySpecificCreateFuncs
 

Static Private Attributes

static ValueContainerPool m_ValueContainerPool
 
static FlagContainerPool m_managementEnabledContainerPool
 

Detailed Description

template<typename KeyType, typename ValueT, typename opLessCreator = defOpLessCreator<KeyType>>
class Nektar::LibUtilities::NekManager< KeyType, ValueT, opLessCreator >

Definition at line 69 of file NekManager.hpp.

Member Typedef Documentation

◆ BoolSharedPtr

template<typename KeyType , typename ValueT , typename opLessCreator = defOpLessCreator<KeyType>>
typedef std::shared_ptr<bool> Nektar::LibUtilities::NekManager< KeyType, ValueT, opLessCreator >::BoolSharedPtr

Definition at line 80 of file NekManager.hpp.

◆ CreateFuncContainer

template<typename KeyType , typename ValueT , typename opLessCreator = defOpLessCreator<KeyType>>
typedef std::map<KeyType, CreateFuncType, opLessCreator> Nektar::LibUtilities::NekManager< KeyType, ValueT, opLessCreator >::CreateFuncContainer

Definition at line 77 of file NekManager.hpp.

◆ CreateFuncType

template<typename KeyType , typename ValueT , typename opLessCreator = defOpLessCreator<KeyType>>
typedef std::function<ValueType(const KeyType &key)> Nektar::LibUtilities::NekManager< KeyType, ValueT, opLessCreator >::CreateFuncType

Definition at line 73 of file NekManager.hpp.

◆ FlagContainerPool

template<typename KeyType , typename ValueT , typename opLessCreator = defOpLessCreator<KeyType>>
typedef std::map<std::string, BoolSharedPtr> Nektar::LibUtilities::NekManager< KeyType, ValueT, opLessCreator >::FlagContainerPool

Definition at line 81 of file NekManager.hpp.

◆ ValueContainer

template<typename KeyType , typename ValueT , typename opLessCreator = defOpLessCreator<KeyType>>
typedef std::map<KeyType, ValueType> Nektar::LibUtilities::NekManager< KeyType, ValueT, opLessCreator >::ValueContainer

Definition at line 74 of file NekManager.hpp.

◆ ValueContainerPool

template<typename KeyType , typename ValueT , typename opLessCreator = defOpLessCreator<KeyType>>
typedef std::map<std::string, std::shared_ptr<ValueContainer> > Nektar::LibUtilities::NekManager< KeyType, ValueT, opLessCreator >::ValueContainerPool

Definition at line 79 of file NekManager.hpp.

◆ ValueContainerShPtr

template<typename KeyType , typename ValueT , typename opLessCreator = defOpLessCreator<KeyType>>
typedef std::shared_ptr<ValueContainer> Nektar::LibUtilities::NekManager< KeyType, ValueT, opLessCreator >::ValueContainerShPtr

Definition at line 75 of file NekManager.hpp.

◆ ValueType

template<typename KeyType , typename ValueT , typename opLessCreator = defOpLessCreator<KeyType>>
typedef std::shared_ptr<ValueT> Nektar::LibUtilities::NekManager< KeyType, ValueT, opLessCreator >::ValueType

Definition at line 72 of file NekManager.hpp.

Constructor & Destructor Documentation

◆ NekManager() [1/3]

template<typename KeyType , typename ValueT , typename opLessCreator = defOpLessCreator<KeyType>>
Nektar::LibUtilities::NekManager< KeyType, ValueT, opLessCreator >::NekManager ( std::string  whichPool = "")
inline

Definition at line 83 of file NekManager.hpp.

85 
86  {
87  if (!whichPool.empty())
88  {
89  auto iter = m_ValueContainerPool.find(whichPool);
90  if (iter != m_ValueContainerPool.end())
91  {
92  m_values = iter->second;
95  }
96  else
97  {
99  m_ValueContainerPool[whichPool] = m_values;
100  if (m_managementEnabledContainerPool.find(whichPool) ==
102  {
104  BoolSharedPtr(new bool(true));
105  }
108  }
109  }
110  else
111  {
113  m_managementEnabled = BoolSharedPtr(new bool(true));
114  }
115  };
std::shared_ptr< bool > BoolSharedPtr
Definition: NekManager.hpp:80
ValueContainerShPtr m_values
Definition: NekManager.hpp:338
static ValueContainerPool m_ValueContainerPool
Definition: NekManager.hpp:340
std::shared_ptr< ValueContainer > ValueContainerShPtr
Definition: NekManager.hpp:75
static FlagContainerPool m_managementEnabledContainerPool
Definition: NekManager.hpp:341
CreateFuncContainer m_keySpecificCreateFuncs
Definition: NekManager.hpp:343
std::map< KeyType, ValueType > ValueContainer
Definition: NekManager.hpp:74

References Nektar::LibUtilities::NekManager< KeyType, ValueT, opLessCreator >::m_managementEnabled, Nektar::LibUtilities::NekManager< KeyType, ValueT, opLessCreator >::m_managementEnabledContainerPool, Nektar::LibUtilities::NekManager< KeyType, ValueT, opLessCreator >::m_ValueContainerPool, and Nektar::LibUtilities::NekManager< KeyType, ValueT, opLessCreator >::m_values.

◆ NekManager() [2/3]

template<typename KeyType , typename ValueT , typename opLessCreator = defOpLessCreator<KeyType>>
Nektar::LibUtilities::NekManager< KeyType, ValueT, opLessCreator >::NekManager ( CreateFuncType  f,
std::string  whichPool = "" 
)
inlineexplicit

Definition at line 117 of file NekManager.hpp.

119  {
120  if (!whichPool.empty())
121  {
122 #ifdef NEKTAR_USE_THREAD_SAFETY
123  ReadLock v_rlock(m_mutex); // reading static members
124 #endif
125  auto iter = m_ValueContainerPool.find(whichPool);
126  if (iter != m_ValueContainerPool.end())
127  {
128  m_values = iter->second;
131  }
132  else
133  {
134 #ifdef NEKTAR_USE_THREAD_SAFETY
135  v_rlock.unlock();
136  // now writing static members. Apparently
137  // upgrade_lock has less desirable properties than
138  // just dropping read lock, grabbing write lock.
139  // write will block until all reads are done, but
140  // reads cannot be acquired if write lock is
141  // blocking. In this context writes are supposed to
142  // be rare.
143  WriteLock v_wlock(m_mutex);
144 #endif
146  m_ValueContainerPool[whichPool] = m_values;
147  if (m_managementEnabledContainerPool.find(whichPool) ==
149  {
151  BoolSharedPtr(new bool(true));
152  }
155  }
156  }
157  else
158  {
160  m_managementEnabled = BoolSharedPtr(new bool(true));
161  }
162  }
boost::shared_lock< boost::shared_mutex > ReadLock
Definition: Thread.h:380
boost::unique_lock< boost::shared_mutex > WriteLock
Definition: Thread.h:379

References Nektar::LibUtilities::NekManager< KeyType, ValueT, opLessCreator >::m_managementEnabled, Nektar::LibUtilities::NekManager< KeyType, ValueT, opLessCreator >::m_managementEnabledContainerPool, Nektar::LibUtilities::NekManager< KeyType, ValueT, opLessCreator >::m_ValueContainerPool, and Nektar::LibUtilities::NekManager< KeyType, ValueT, opLessCreator >::m_values.

◆ ~NekManager()

template<typename KeyType , typename ValueT , typename opLessCreator = defOpLessCreator<KeyType>>
Nektar::LibUtilities::NekManager< KeyType, ValueT, opLessCreator >::~NekManager ( )
inline

Definition at line 164 of file NekManager.hpp.

165  {
166  }

◆ NekManager() [3/3]

template<typename KeyType , typename ValueT , typename opLessCreator = defOpLessCreator<KeyType>>
Nektar::LibUtilities::NekManager< KeyType, ValueT, opLessCreator >::NekManager ( const NekManager< KeyType, ValueType, opLessCreator > &  rhs)
private

Member Function Documentation

◆ AlreadyCreated()

template<typename KeyType , typename ValueT , typename opLessCreator = defOpLessCreator<KeyType>>
bool Nektar::LibUtilities::NekManager< KeyType, ValueT, opLessCreator >::AlreadyCreated ( const KeyType &  key)
inline

Definition at line 186 of file NekManager.hpp.

187  {
188  bool value = false;
189  auto found = m_values->find(key);
190  if (found != m_values->end())
191  {
192  value = true;
193  }
194 
195  return value;
196  }

References Nektar::LibUtilities::NekManager< KeyType, ValueT, opLessCreator >::m_values.

◆ ClearManager()

template<typename KeyType , typename ValueT , typename opLessCreator = defOpLessCreator<KeyType>>
static void Nektar::LibUtilities::NekManager< KeyType, ValueT, opLessCreator >::ClearManager ( std::string  whichPool = "")
inlinestatic

Definition at line 248 of file NekManager.hpp.

249  {
250  if (!whichPool.empty())
251  {
252 #ifdef NEKTAR_USE_THREAD_SAFETY
253  WriteLock v_wlock(m_mutex);
254 #endif
255  auto x = m_ValueContainerPool.find(whichPool);
256  ASSERTL1(x != m_ValueContainerPool.end(),
257  "Could not find pool " + whichPool);
258  x->second->clear();
259  }
260  else
261  {
262 #ifdef NEKTAR_USE_THREAD_SAFETY
263  WriteLock v_wlock(m_mutex);
264 #endif
265 
266  for (auto &x : m_ValueContainerPool)
267  {
268  x.second->clear();
269  }
270  }
271  }
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode....
Definition: ErrorUtil.hpp:249

References ASSERTL1, and Nektar::LibUtilities::NekManager< KeyType, ValueT, opLessCreator >::m_ValueContainerPool.

◆ DeleteObject()

template<typename KeyType , typename ValueT , typename opLessCreator = defOpLessCreator<KeyType>>
void Nektar::LibUtilities::NekManager< KeyType, ValueT, opLessCreator >::DeleteObject ( const KeyType &  key)
inline

Definition at line 238 of file NekManager.hpp.

239  {
240  auto found = m_values->find(key);
241 
242  if (found != m_values->end())
243  {
244  m_values->erase(found);
245  }
246  }

References Nektar::LibUtilities::NekManager< KeyType, ValueT, opLessCreator >::m_values.

◆ DisableManagement()

template<typename KeyType , typename ValueT , typename opLessCreator = defOpLessCreator<KeyType>>
static void Nektar::LibUtilities::NekManager< KeyType, ValueT, opLessCreator >::DisableManagement ( std::string  whichPool = "")
inlinestatic

Definition at line 313 of file NekManager.hpp.

314  {
315  if (!whichPool.empty())
316  {
317 #ifdef NEKTAR_USE_THREAD_SAFETY
318  WriteLock v_wlock(m_mutex);
319 #endif
320  auto x = m_managementEnabledContainerPool.find(whichPool);
321  if (x != m_managementEnabledContainerPool.end())
322  {
323  (*x->second) = false;
324  }
325  else
326  {
328  BoolSharedPtr(new bool(false));
329  }
330  }
331  }

References Nektar::LibUtilities::NekManager< KeyType, ValueT, opLessCreator >::m_managementEnabledContainerPool.

◆ EnableManagement()

template<typename KeyType , typename ValueT , typename opLessCreator = defOpLessCreator<KeyType>>
static void Nektar::LibUtilities::NekManager< KeyType, ValueT, opLessCreator >::EnableManagement ( std::string  whichPool = "")
inlinestatic

Definition at line 292 of file NekManager.hpp.

293  {
294  if (!whichPool.empty())
295  {
296 #ifdef NEKTAR_USE_THREAD_SAFETY
297  WriteLock v_wlock(m_mutex);
298 #endif
299 
300  auto x = m_managementEnabledContainerPool.find(whichPool);
301  if (x != m_managementEnabledContainerPool.end())
302  {
303  (*x->second) = true;
304  }
305  else
306  {
308  BoolSharedPtr(new bool(true));
309  }
310  }
311  }

References Nektar::LibUtilities::NekManager< KeyType, ValueT, opLessCreator >::m_managementEnabledContainerPool.

◆ operator=()

template<typename KeyType , typename ValueT , typename opLessCreator = defOpLessCreator<KeyType>>
NekManager<KeyType, ValueType, opLessCreator>& Nektar::LibUtilities::NekManager< KeyType, ValueT, opLessCreator >::operator= ( const NekManager< KeyType, ValueType, opLessCreator > &  rhs)
private

◆ operator[]()

template<typename KeyType , typename ValueT , typename opLessCreator = defOpLessCreator<KeyType>>
ValueType Nektar::LibUtilities::NekManager< KeyType, ValueT, opLessCreator >::operator[] ( const KeyType &  key)
inline

Definition at line 198 of file NekManager.hpp.

199  {
200  auto found = m_values->find(key);
201 
202  if (found != m_values->end())
203  {
204  return (*found).second;
205  }
206  else
207  {
208  // No object, create a new one.
210  auto keyFound = m_keySpecificCreateFuncs.find(key);
211  if (keyFound != m_keySpecificCreateFuncs.end())
212  {
213  f = (*keyFound).second;
214  }
215 
216  if (f)
217  {
218  ValueType v = f(key);
219  if (*m_managementEnabled)
220  {
221  (*m_values)[key] = v;
222  }
223  return v;
224  }
225  else
226  {
227  std::stringstream ss;
228  ss << key;
229  std::string message =
230  "No create func found for key " + ss.str();
231  NEKERROR(ErrorUtil::efatal, message.c_str());
232  static ValueType result;
233  return result;
234  }
235  }
236  }
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mode...
Definition: ErrorUtil.hpp:209
std::function< ValueType(const KeyType &key)> CreateFuncType
Definition: NekManager.hpp:73
std::shared_ptr< ValueT > ValueType
Definition: NekManager.hpp:72

References Nektar::ErrorUtil::efatal, Nektar::LibUtilities::NekManager< KeyType, ValueT, opLessCreator >::m_globalCreateFunc, Nektar::LibUtilities::NekManager< KeyType, ValueT, opLessCreator >::m_keySpecificCreateFuncs, Nektar::LibUtilities::NekManager< KeyType, ValueT, opLessCreator >::m_managementEnabled, Nektar::LibUtilities::NekManager< KeyType, ValueT, opLessCreator >::m_values, and NEKERROR.

◆ PoolCount()

template<typename KeyType , typename ValueT , typename opLessCreator = defOpLessCreator<KeyType>>
size_t Nektar::LibUtilities::NekManager< KeyType, ValueT, opLessCreator >::PoolCount ( std::string  whichPool)
inline

Definition at line 284 of file NekManager.hpp.

285  {
286  auto x = m_ValueContainerPool.find(whichPool);
287  ASSERTL1(x != m_ValueContainerPool.end(),
288  "Could not find pool " + whichPool);
289  return x->second->size();
290  }

References ASSERTL1, and Nektar::LibUtilities::NekManager< KeyType, ValueT, opLessCreator >::m_ValueContainerPool.

◆ PoolCreated()

template<typename KeyType , typename ValueT , typename opLessCreator = defOpLessCreator<KeyType>>
static bool Nektar::LibUtilities::NekManager< KeyType, ValueT, opLessCreator >::PoolCreated ( std::string  whichPool)
inlinestatic

Definition at line 273 of file NekManager.hpp.

274  {
275  bool value = false;
276  auto x = m_ValueContainerPool.find(whichPool);
277  if (x != m_ValueContainerPool.end())
278  {
279  value = true;
280  }
281  return value;
282  }

References Nektar::LibUtilities::NekManager< KeyType, ValueT, opLessCreator >::m_ValueContainerPool.

◆ RegisterCreator()

template<typename KeyType , typename ValueT , typename opLessCreator = defOpLessCreator<KeyType>>
bool Nektar::LibUtilities::NekManager< KeyType, ValueT, opLessCreator >::RegisterCreator ( const KeyType &  key,
const CreateFuncType createFunc 
)
inline

Register the given function and associate it with the key. The return value is just to facilitate calling statically.

Definition at line 170 of file NekManager.hpp.

171  {
172  m_keySpecificCreateFuncs[key] = createFunc;
173 
174  return true;
175  }

References Nektar::LibUtilities::NekManager< KeyType, ValueT, opLessCreator >::m_keySpecificCreateFuncs.

◆ RegisterGlobalCreator()

template<typename KeyType , typename ValueT , typename opLessCreator = defOpLessCreator<KeyType>>
bool Nektar::LibUtilities::NekManager< KeyType, ValueT, opLessCreator >::RegisterGlobalCreator ( const CreateFuncType createFunc)
inline

Register the Global Create Function. The return value is just to facilitate calling statically.

Definition at line 179 of file NekManager.hpp.

180  {
181  m_globalCreateFunc = createFunc;
182 
183  return true;
184  }

References Nektar::LibUtilities::NekManager< KeyType, ValueT, opLessCreator >::m_globalCreateFunc.

Member Data Documentation

◆ m_globalCreateFunc

template<typename KeyType , typename ValueT , typename opLessCreator = defOpLessCreator<KeyType>>
CreateFuncType Nektar::LibUtilities::NekManager< KeyType, ValueT, opLessCreator >::m_globalCreateFunc
private

◆ m_keySpecificCreateFuncs

template<typename KeyType , typename ValueT , typename opLessCreator = defOpLessCreator<KeyType>>
CreateFuncContainer Nektar::LibUtilities::NekManager< KeyType, ValueT, opLessCreator >::m_keySpecificCreateFuncs
private

◆ m_managementEnabled

template<typename KeyType , typename ValueT , typename opLessCreator = defOpLessCreator<KeyType>>
BoolSharedPtr Nektar::LibUtilities::NekManager< KeyType, ValueT, opLessCreator >::m_managementEnabled
private

◆ m_managementEnabledContainerPool

template<typename KeyType , typename ValueT , typename opLessCreator >
NekManager< KeyType, ValueT, opLessCreator >::FlagContainerPool Nektar::LibUtilities::NekManager< KeyType, ValueT, opLessCreator >::m_managementEnabledContainerPool
staticprivate

◆ m_ValueContainerPool

template<typename KeyType , typename ValueT , typename opLessCreator >
NekManager< KeyType, ValueT, opLessCreator >::ValueContainerPool Nektar::LibUtilities::NekManager< KeyType, ValueT, opLessCreator >::m_ValueContainerPool
staticprivate

◆ m_values

template<typename KeyType , typename ValueT , typename opLessCreator = defOpLessCreator<KeyType>>
ValueContainerShPtr Nektar::LibUtilities::NekManager< KeyType, ValueT, opLessCreator >::m_values
private