37 #ifndef BOOST_PP_IS_ITERATING
39 #ifndef NEKTAR_LIB_UTILITIES_BASIC_UTILS_NEK_FACTORY_HPP
40 #define NEKTAR_LIB_UTILITIES_BASIC_UTILS_NEK_FACTORY_HPP
42 #include <boost/preprocessor/repetition.hpp>
43 #include <boost/preprocessor/arithmetic/sub.hpp>
44 #include <boost/preprocessor/punctuation/comma_if.hpp>
45 #include <boost/preprocessor/iteration/iterate.hpp>
47 #include <boost/shared_ptr.hpp>
56 #define MAX_PARAM 5 // default maximum number of parameters to support
61 namespace LibUtilities
67 #define FACTORY_print(z, n, data) BOOST_PP_CAT(data, n) = none
109 template <
typename tKey,
159 BOOST_PP_ENUM_BINARY_PARAMS(
MAX_PARAM, tParam, x))
168 if (it->second.m_func)
172 return it->second.m_func(BOOST_PP_ENUM_PARAMS(
MAX_PARAM, x));
174 catch (
const std::string& s)
176 std::stringstream errstr;
177 errstr <<
"Unable to create module: " << idKey <<
"\n";
185 std::stringstream errstr;
186 errstr <<
"No such module: " << idKey << std::endl;
209 getMapFactory()->insert(std::pair<tKey,ModuleEntry>(idKey, e));
235 pOut << std::endl <<
"Available classes: " << std::endl;
239 pOut << std::endl <<
"Available classes: " << std::endl;
243 pOut <<
" " << it->first;
244 if (it->second.m_desc !=
"")
246 pOut <<
":" << std::endl <<
" "
247 << it->second.m_desc << std::endl;
266 if (it->second.m_desc == pDesc)
271 std::string errstr =
"Module '"
272 + boost::lexical_cast<std::string>(pDesc)
286 std::stringstream errstr;
287 errstr <<
"No such module: " << idKey << std::endl;
289 return it->second.m_desc;
312 #define BOOST_PP_ITERATION_LIMITS (0, MAX_PARAM-1)
313 #define BOOST_PP_FILENAME_1 "LibUtilities/BasicUtils/NekFactory.hpp"
314 #include BOOST_PP_ITERATE()
319 #endif // end NEKTAR_LIB_UTILITIES_BASIC_UTILS_NEK_FACTORY_HPP
325 #define n BOOST_PP_ITERATION()
327 #define FACTORY_print(z, n, data) data
329 template <
typename tKey,
330 typename tBase BOOST_PP_COMMA_IF(n)
331 BOOST_PP_ENUM_PARAMS(n, typename tParam) >
332 class NekFactory< tKey, tBase,
333 BOOST_PP_ENUM_PARAMS(n, tParam)BOOST_PP_COMMA_IF(n)
337 typedef std::string tDescription;
338 typedef std::less<tKey> tPredicator;
339 typedef boost::shared_ptr<tBase> tBaseSharedPtr;
340 typedef tBaseSharedPtr (*CreatorFunction) (BOOST_PP_ENUM_PARAMS(n, tParam));
344 ModuleEntry(CreatorFunction pFunc,
const tDescription pDesc)
349 CreatorFunction m_func;
352 typedef std::map<tKey, ModuleEntry, tPredicator> TMapFactory;
357 tBaseSharedPtr CreateInstance(tKey idKey BOOST_PP_COMMA_IF(n)
358 BOOST_PP_ENUM_BINARY_PARAMS(n, tParam, x))
360 TMapFactoryIterator it = getMapFactory()->find(idKey);
361 if (it != getMapFactory()->end())
363 if (it->second.m_func)
367 return it->second.m_func(BOOST_PP_ENUM_PARAMS(n, x));
369 catch (
const std::string& s)
371 std::stringstream errstr;
372 errstr <<
"Unable to create module: " << idKey <<
"\n";
378 std::stringstream errstr;
379 errstr <<
"No such module: " << idKey << std::endl;
380 PrintAvailableClasses(errstr);
382 return tBaseSharedPtr();
385 tKey RegisterCreatorFunction(tKey idKey,
386 CreatorFunction classCreator,
387 tDescription pDesc =
"") {
388 ModuleEntry e(classCreator, pDesc);
389 getMapFactory()->insert(std::pair<tKey,ModuleEntry>(idKey, e));
393 bool ModuleExists(tKey idKey)
396 TMapFactoryIterator it = getMapFactory()->find(idKey);
398 if (it != getMapFactory()->end())
405 void PrintAvailableClasses(std::ostream& pOut = std::cout)
407 pOut << std::endl <<
"Available classes: " << std::endl;
408 TMapFactoryIterator it;
409 for (it = getMapFactory()->begin(); it != getMapFactory()->end(); ++it)
411 pOut <<
" " << it->first;
412 if (it->second.m_desc !=
"")
414 pOut <<
":" << std::endl <<
" "
415 << it->second.m_desc << std::endl;
424 tKey GetKey(tDescription pDesc)
426 TMapFactoryIterator it;
427 for (it = getMapFactory()->begin(); it != getMapFactory()->end(); ++it)
429 if (it->second.m_desc == pDesc)
434 std::string errstr =
"Module '"
435 + boost::lexical_cast<std::string>(pDesc)
440 std::string GetClassDescription(tKey idKey)
443 TMapFactoryIterator it = getMapFactory()->find(idKey);
445 std::stringstream errstr;
446 errstr <<
"No such module: " << idKey << std::endl;
447 ASSERTL0 (it != getMapFactory()->end(), errstr.str());
448 return it->second.m_desc;
452 TMapFactory * getMapFactory() {
457 NekFactory(
const NekFactory& rhs);
458 NekFactory& operator=(
const NekFactory& rhs);
460 TMapFactory mMapFactory;