35 #ifndef NEKTAR_LIB_UTILITIES_BASIC_UTILS_NEK_MANAGER_HPP 36 #define NEKTAR_LIB_UTILITIES_BASIC_UTILS_NEK_MANAGER_HPP 43 #ifdef NEKTAR_USE_THREAD_SAFETY 44 #include <boost/thread/shared_mutex.hpp> 45 #include <boost/thread/locks.hpp> 52 namespace LibUtilities
54 #ifdef NEKTAR_USE_THREAD_SAFETY 55 typedef boost::unique_lock<boost::shared_mutex>
WriteLock;
56 typedef boost::shared_lock<boost::shared_mutex>
ReadLock;
59 template <
typename KeyType>
68 template <
typename KeyType,
typename ValueT,
typename opLessCreator = defOpLessCreator<KeyType> >
84 m_keySpecificCreateFuncs()
87 if (!whichPool.empty())
89 auto iter = m_ValueContainerPool.find(whichPool);
90 if (iter != m_ValueContainerPool.end())
92 m_values = iter->second;
93 m_managementEnabled = m_managementEnabledContainerPool[whichPool];
97 m_values = ValueContainerShPtr(
new ValueContainer);
98 m_ValueContainerPool[whichPool] = m_values;
99 if (m_managementEnabledContainerPool.find(whichPool) == m_managementEnabledContainerPool.end())
101 m_managementEnabledContainerPool[whichPool] = BoolSharedPtr(
new bool(
true));
103 m_managementEnabled = m_managementEnabledContainerPool[whichPool];
108 m_values = ValueContainerShPtr(
new ValueContainer);
109 m_managementEnabled = BoolSharedPtr(
new bool(
true));
114 explicit NekManager(CreateFuncType f, std::string whichPool=
"") :
116 m_globalCreateFunc(f),
117 m_keySpecificCreateFuncs()
119 if (!whichPool.empty())
121 #ifdef NEKTAR_USE_THREAD_SAFETY 122 ReadLock v_rlock(m_mutex);
124 auto iter = m_ValueContainerPool.find(whichPool);
125 if (iter != m_ValueContainerPool.end())
127 m_values = iter->second;
128 m_managementEnabled = m_managementEnabledContainerPool[whichPool];
132 #ifdef NEKTAR_USE_THREAD_SAFETY 141 WriteLock v_wlock(m_mutex);
143 m_values = ValueContainerShPtr(
new ValueContainer);
144 m_ValueContainerPool[whichPool] = m_values;
145 if (m_managementEnabledContainerPool.find(whichPool) == m_managementEnabledContainerPool.end())
147 m_managementEnabledContainerPool[whichPool] = BoolSharedPtr(
new bool(
true));
149 m_managementEnabled = m_managementEnabledContainerPool[whichPool];
155 m_values = ValueContainerShPtr(
new ValueContainer);
156 m_managementEnabled = BoolSharedPtr(
new bool(
true));
167 const CreateFuncType& createFunc)
169 m_keySpecificCreateFuncs[key] = createFunc;
178 m_globalCreateFunc = createFunc;
186 auto found = m_values->find(key);
187 if( found != m_values->end() )
197 auto found = m_values->find(key);
199 if( found != m_values->end() )
201 return (*found).second;
206 CreateFuncType f = m_globalCreateFunc;
207 auto keyFound = m_keySpecificCreateFuncs.find(key);
208 if( keyFound != m_keySpecificCreateFuncs.end() )
210 f = (*keyFound).second;
215 ValueType v = f(key);
216 if (*m_managementEnabled)
218 (*m_values)[key] = v;
224 std::stringstream ss;
226 std::string message =
"No create func found for key " + ss.str();
228 static ValueType result;
236 auto found = m_values->find(key);
238 if( found != m_values->end() )
240 m_values->erase(found);
246 if (!whichPool.empty())
248 #ifdef NEKTAR_USE_THREAD_SAFETY 249 WriteLock v_wlock(m_mutex);
251 auto x = m_ValueContainerPool.find(whichPool);
252 ASSERTL1(x != m_ValueContainerPool.end(),
253 "Could not find pool " + whichPool);
258 #ifdef NEKTAR_USE_THREAD_SAFETY 259 WriteLock v_wlock(m_mutex);
262 for (
auto &x : m_ValueContainerPool)
272 auto x = m_ValueContainerPool.find(whichPool);
273 if (x != m_ValueContainerPool.end())
282 if (!whichPool.empty())
284 #ifdef NEKTAR_USE_THREAD_SAFETY 285 WriteLock v_wlock(m_mutex);
288 auto x = m_managementEnabledContainerPool.find(whichPool);
289 if (x != m_managementEnabledContainerPool.end())
295 m_managementEnabledContainerPool[whichPool] = BoolSharedPtr(
new bool(
true));
302 if (!whichPool.empty())
304 #ifdef NEKTAR_USE_THREAD_SAFETY 305 WriteLock v_wlock(m_mutex);
307 auto x = m_managementEnabledContainerPool.find(whichPool);
308 if (x != m_managementEnabledContainerPool.end())
310 (*x->second) =
false;
314 m_managementEnabledContainerPool[whichPool] = BoolSharedPtr(
new bool(
false));
329 #ifdef NEKTAR_USE_THREAD_SAFETY 330 static boost::shared_mutex m_mutex;
335 #ifdef NEKTAR_USE_THREAD_SAFETY 336 template <
typename KeyType,
typename ValueT,
typename opLessCreator>
343 #endif //NEKTAR_LIB_UTILITIES_BASIC_UTILS_NEK_MANAGER_HPP static void DisableManagement(std::string whichPool="")
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mod...
NekManager(CreateFuncType f, std::string whichPool="")
static FlagContainerPool m_managementEnabledContainerPool
NekManager(std::string whichPool="")
std::shared_ptr< ValueContainer > ValueContainerShPtr
static ValueContainerPool m_ValueContainerPool
CreateFuncType m_globalCreateFunc
std::function< ValueType(const KeyType &key)> CreateFuncType
bool AlreadyCreated(const KeyType &key)
static void EnableManagement(std::string whichPool="")
std::shared_ptr< bool > BoolSharedPtr
boost::shared_lock< boost::shared_mutex > ReadLock
std::map< KeyType, ValueType > ValueContainer
boost::unique_lock< boost::shared_mutex > WriteLock
BoolSharedPtr m_managementEnabled
ValueType operator[](const KeyType &key)
std::map< KeyType, CreateFuncType, opLessCreator > CreateFuncContainer
CreateFuncContainer m_keySpecificCreateFuncs
std::shared_ptr< ValueT > ValueType
static void ClearManager(std::string whichPool="")
static bool PoolCreated(std::string whichPool)
void DeleteObject(const KeyType &key)
bool RegisterGlobalCreator(const CreateFuncType &createFunc)
Register the Global Create Function. The return value is just to facilitate calling statically...
ValueContainerShPtr m_values
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 cal...
StandardMatrixTag boost::call_traits< LhsDataType >::const_reference rhs
std::map< std::string, BoolSharedPtr > FlagContainerPool
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode...
std::map< std::string, std::shared_ptr< ValueContainer > > ValueContainerPool
bool operator()(const KeyType &lhs, const KeyType &rhs) const