diff options
-rw-r--r-- | include/gras/detail/factory.hpp | 44 | ||||
-rw-r--r-- | include/gras/factory.hpp | 258 | ||||
-rw-r--r-- | python/gras/GRAS_Factory.i | 35 | ||||
-rw-r--r-- | python/gras/GRAS_Loader.py | 2 | ||||
-rw-r--r-- | python/gras/__init__.py | 2 | ||||
-rw-r--r-- | tests/example_module.cpp | 7 | ||||
-rw-r--r-- | tests/example_module.py | 2 | ||||
-rw-r--r-- | tests/factory_test.cpp | 8 | ||||
-rw-r--r-- | tests/module_loader_test.py | 4 | ||||
-rw-r--r-- | tmpl/factory.tmpl.hpp | 64 | ||||
-rw-r--r-- | tmpl/factory_detail.tmpl.hpp | 4 |
11 files changed, 283 insertions, 147 deletions
diff --git a/include/gras/detail/factory.hpp b/include/gras/detail/factory.hpp index ac4df6f..d1edf81 100644 --- a/include/gras/detail/factory.hpp +++ b/include/gras/detail/factory.hpp @@ -37,7 +37,7 @@ struct FactoryRegistryEntryImpl0 : FactoryRegistryEntry }; template <typename ReturnType> -void Factory::register_make(const std::string &name, ReturnType(*fcn)()) +void register_make(const std::string &name, ReturnType(*fcn)()) { void *r = new FactoryRegistryEntryImpl0<ReturnType>(fcn); Factory::_register_make(name, r); @@ -61,7 +61,7 @@ struct FactoryRegistryEntryImpl1 : FactoryRegistryEntry }; template <typename ReturnType, typename A0> -void Factory::register_make(const std::string &name, ReturnType(*fcn)(const A0 &)) +void register_make(const std::string &name, ReturnType(*fcn)(const A0 &)) { void *r = new FactoryRegistryEntryImpl1<ReturnType, A0>(fcn); Factory::_register_make(name, r); @@ -85,7 +85,7 @@ struct FactoryRegistryEntryImpl2 : FactoryRegistryEntry }; template <typename ReturnType, typename A0, typename A1> -void Factory::register_make(const std::string &name, ReturnType(*fcn)(const A0 &, const A1 &)) +void register_make(const std::string &name, ReturnType(*fcn)(const A0 &, const A1 &)) { void *r = new FactoryRegistryEntryImpl2<ReturnType, A0, A1>(fcn); Factory::_register_make(name, r); @@ -109,7 +109,7 @@ struct FactoryRegistryEntryImpl3 : FactoryRegistryEntry }; template <typename ReturnType, typename A0, typename A1, typename A2> -void Factory::register_make(const std::string &name, ReturnType(*fcn)(const A0 &, const A1 &, const A2 &)) +void register_make(const std::string &name, ReturnType(*fcn)(const A0 &, const A1 &, const A2 &)) { void *r = new FactoryRegistryEntryImpl3<ReturnType, A0, A1, A2>(fcn); Factory::_register_make(name, r); @@ -133,7 +133,7 @@ struct FactoryRegistryEntryImpl4 : FactoryRegistryEntry }; template <typename ReturnType, typename A0, typename A1, typename A2, typename A3> -void Factory::register_make(const std::string &name, ReturnType(*fcn)(const A0 &, const A1 &, const A2 &, const A3 &)) +void register_make(const std::string &name, ReturnType(*fcn)(const A0 &, const A1 &, const A2 &, const A3 &)) { void *r = new FactoryRegistryEntryImpl4<ReturnType, A0, A1, A2, A3>(fcn); Factory::_register_make(name, r); @@ -157,7 +157,7 @@ struct FactoryRegistryEntryImpl5 : FactoryRegistryEntry }; template <typename ReturnType, typename A0, typename A1, typename A2, typename A3, typename A4> -void Factory::register_make(const std::string &name, ReturnType(*fcn)(const A0 &, const A1 &, const A2 &, const A3 &, const A4 &)) +void register_make(const std::string &name, ReturnType(*fcn)(const A0 &, const A1 &, const A2 &, const A3 &, const A4 &)) { void *r = new FactoryRegistryEntryImpl5<ReturnType, A0, A1, A2, A3, A4>(fcn); Factory::_register_make(name, r); @@ -181,7 +181,7 @@ struct FactoryRegistryEntryImpl6 : FactoryRegistryEntry }; template <typename ReturnType, typename A0, typename A1, typename A2, typename A3, typename A4, typename A5> -void Factory::register_make(const std::string &name, ReturnType(*fcn)(const A0 &, const A1 &, const A2 &, const A3 &, const A4 &, const A5 &)) +void register_make(const std::string &name, ReturnType(*fcn)(const A0 &, const A1 &, const A2 &, const A3 &, const A4 &, const A5 &)) { void *r = new FactoryRegistryEntryImpl6<ReturnType, A0, A1, A2, A3, A4, A5>(fcn); Factory::_register_make(name, r); @@ -205,7 +205,7 @@ struct FactoryRegistryEntryImpl7 : FactoryRegistryEntry }; template <typename ReturnType, typename A0, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6> -void Factory::register_make(const std::string &name, ReturnType(*fcn)(const A0 &, const A1 &, const A2 &, const A3 &, const A4 &, const A5 &, const A6 &)) +void register_make(const std::string &name, ReturnType(*fcn)(const A0 &, const A1 &, const A2 &, const A3 &, const A4 &, const A5 &, const A6 &)) { void *r = new FactoryRegistryEntryImpl7<ReturnType, A0, A1, A2, A3, A4, A5, A6>(fcn); Factory::_register_make(name, r); @@ -229,7 +229,7 @@ struct FactoryRegistryEntryImpl8 : FactoryRegistryEntry }; template <typename ReturnType, typename A0, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7> -void Factory::register_make(const std::string &name, ReturnType(*fcn)(const A0 &, const A1 &, const A2 &, const A3 &, const A4 &, const A5 &, const A6 &, const A7 &)) +void register_make(const std::string &name, ReturnType(*fcn)(const A0 &, const A1 &, const A2 &, const A3 &, const A4 &, const A5 &, const A6 &, const A7 &)) { void *r = new FactoryRegistryEntryImpl8<ReturnType, A0, A1, A2, A3, A4, A5, A6, A7>(fcn); Factory::_register_make(name, r); @@ -253,7 +253,7 @@ struct FactoryRegistryEntryImpl9 : FactoryRegistryEntry }; template <typename ReturnType, typename A0, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8> -void Factory::register_make(const std::string &name, ReturnType(*fcn)(const A0 &, const A1 &, const A2 &, const A3 &, const A4 &, const A5 &, const A6 &, const A7 &, const A8 &)) +void register_make(const std::string &name, ReturnType(*fcn)(const A0 &, const A1 &, const A2 &, const A3 &, const A4 &, const A5 &, const A6 &, const A7 &, const A8 &)) { void *r = new FactoryRegistryEntryImpl9<ReturnType, A0, A1, A2, A3, A4, A5, A6, A7, A8>(fcn); Factory::_register_make(name, r); @@ -277,7 +277,7 @@ struct FactoryRegistryEntryImpl10 : FactoryRegistryEntry }; template <typename ReturnType, typename A0, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8, typename A9> -void Factory::register_make(const std::string &name, ReturnType(*fcn)(const A0 &, const A1 &, const A2 &, const A3 &, const A4 &, const A5 &, const A6 &, const A7 &, const A8 &, const A9 &)) +void register_make(const std::string &name, ReturnType(*fcn)(const A0 &, const A1 &, const A2 &, const A3 &, const A4 &, const A5 &, const A6 &, const A7 &, const A8 &, const A9 &)) { void *r = new FactoryRegistryEntryImpl10<ReturnType, A0, A1, A2, A3, A4, A5, A6, A7, A8, A9>(fcn); Factory::_register_make(name, r); @@ -287,14 +287,14 @@ void Factory::register_make(const std::string &name, ReturnType(*fcn)(const A0 & * Templated make implementations **********************************************************************/ inline -Element *Factory::make(const std::string &name) +Element *make(const std::string &name) { PMCList args(0); return Factory::_handle_make(name, PMC_M(args)); } template <typename A0> -Element *Factory::make(const std::string &name, const A0 &a0) +Element *make(const std::string &name, const A0 &a0) { PMCList args(1); args[0] = PMC_M(a0); @@ -302,7 +302,7 @@ Element *Factory::make(const std::string &name, const A0 &a0) } template <typename A0, typename A1> -Element *Factory::make(const std::string &name, const A0 &a0, const A1 &a1) +Element *make(const std::string &name, const A0 &a0, const A1 &a1) { PMCList args(2); args[0] = PMC_M(a0); @@ -311,7 +311,7 @@ Element *Factory::make(const std::string &name, const A0 &a0, const A1 &a1) } template <typename A0, typename A1, typename A2> -Element *Factory::make(const std::string &name, const A0 &a0, const A1 &a1, const A2 &a2) +Element *make(const std::string &name, const A0 &a0, const A1 &a1, const A2 &a2) { PMCList args(3); args[0] = PMC_M(a0); @@ -321,7 +321,7 @@ Element *Factory::make(const std::string &name, const A0 &a0, const A1 &a1, cons } template <typename A0, typename A1, typename A2, typename A3> -Element *Factory::make(const std::string &name, const A0 &a0, const A1 &a1, const A2 &a2, const A3 &a3) +Element *make(const std::string &name, const A0 &a0, const A1 &a1, const A2 &a2, const A3 &a3) { PMCList args(4); args[0] = PMC_M(a0); @@ -332,7 +332,7 @@ Element *Factory::make(const std::string &name, const A0 &a0, const A1 &a1, cons } template <typename A0, typename A1, typename A2, typename A3, typename A4> -Element *Factory::make(const std::string &name, const A0 &a0, const A1 &a1, const A2 &a2, const A3 &a3, const A4 &a4) +Element *make(const std::string &name, const A0 &a0, const A1 &a1, const A2 &a2, const A3 &a3, const A4 &a4) { PMCList args(5); args[0] = PMC_M(a0); @@ -344,7 +344,7 @@ Element *Factory::make(const std::string &name, const A0 &a0, const A1 &a1, cons } template <typename A0, typename A1, typename A2, typename A3, typename A4, typename A5> -Element *Factory::make(const std::string &name, const A0 &a0, const A1 &a1, const A2 &a2, const A3 &a3, const A4 &a4, const A5 &a5) +Element *make(const std::string &name, const A0 &a0, const A1 &a1, const A2 &a2, const A3 &a3, const A4 &a4, const A5 &a5) { PMCList args(6); args[0] = PMC_M(a0); @@ -357,7 +357,7 @@ Element *Factory::make(const std::string &name, const A0 &a0, const A1 &a1, cons } template <typename A0, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6> -Element *Factory::make(const std::string &name, const A0 &a0, const A1 &a1, const A2 &a2, const A3 &a3, const A4 &a4, const A5 &a5, const A6 &a6) +Element *make(const std::string &name, const A0 &a0, const A1 &a1, const A2 &a2, const A3 &a3, const A4 &a4, const A5 &a5, const A6 &a6) { PMCList args(7); args[0] = PMC_M(a0); @@ -371,7 +371,7 @@ Element *Factory::make(const std::string &name, const A0 &a0, const A1 &a1, cons } template <typename A0, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7> -Element *Factory::make(const std::string &name, const A0 &a0, const A1 &a1, const A2 &a2, const A3 &a3, const A4 &a4, const A5 &a5, const A6 &a6, const A7 &a7) +Element *make(const std::string &name, const A0 &a0, const A1 &a1, const A2 &a2, const A3 &a3, const A4 &a4, const A5 &a5, const A6 &a6, const A7 &a7) { PMCList args(8); args[0] = PMC_M(a0); @@ -386,7 +386,7 @@ Element *Factory::make(const std::string &name, const A0 &a0, const A1 &a1, cons } template <typename A0, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8> -Element *Factory::make(const std::string &name, const A0 &a0, const A1 &a1, const A2 &a2, const A3 &a3, const A4 &a4, const A5 &a5, const A6 &a6, const A7 &a7, const A8 &a8) +Element *make(const std::string &name, const A0 &a0, const A1 &a1, const A2 &a2, const A3 &a3, const A4 &a4, const A5 &a5, const A6 &a6, const A7 &a7, const A8 &a8) { PMCList args(9); args[0] = PMC_M(a0); @@ -402,7 +402,7 @@ Element *Factory::make(const std::string &name, const A0 &a0, const A1 &a1, cons } template <typename A0, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8, typename A9> -Element *Factory::make(const std::string &name, const A0 &a0, const A1 &a1, const A2 &a2, const A3 &a3, const A4 &a4, const A5 &a5, const A6 &a6, const A7 &a7, const A8 &a8, const A9 &a9) +Element *make(const std::string &name, const A0 &a0, const A1 &a1, const A2 &a2, const A3 &a3, const A4 &a4, const A5 &a5, const A6 &a6, const A7 &a7, const A8 &a8, const A9 &a9) { PMCList args(10); args[0] = PMC_M(a0); diff --git a/include/gras/factory.hpp b/include/gras/factory.hpp index 13524a6..9600586 100644 --- a/include/gras/factory.hpp +++ b/include/gras/factory.hpp @@ -8,15 +8,6 @@ #include <PMC/PMC.hpp> #include <string> -/*! - * Register a block's factory function: - * Declare this macro at the global scope in a cpp file. - * The block will register at static initialization time. - */ -#define GRAS_REGISTER_FACTORY(name, fcn) \ - GRAS_STATIC_BLOCK(fcn) \ - {gras::Factory::register_make(name, &fcn);} - namespace gras { @@ -34,86 +25,227 @@ namespace gras struct GRAS_API Factory { /******************************************************************* - * Register API - don't look here, template magic, not helpful + * Private registration hooks ******************************************************************/ - template <typename ReturnType> - static void register_make(const std::string &name, ReturnType(*fcn)()); + static void _register_make(const std::string &, void *); + static Element *_handle_make(const std::string &, const PMCC &); +}; - template <typename ReturnType, typename A0> - static void register_make(const std::string &name, ReturnType(*fcn)(const A0 &)); +/*********************************************************************** + * Register API - don't look here, template magic, not helpful + **********************************************************************/ +template <typename ReturnType> +static void register_make(const std::string &name, ReturnType(*fcn)()); - template <typename ReturnType, typename A0, typename A1> - static void register_make(const std::string &name, ReturnType(*fcn)(const A0 &, const A1 &)); +template <typename ReturnType, typename A0> +static void register_make(const std::string &name, ReturnType(*fcn)(const A0 &)); - template <typename ReturnType, typename A0, typename A1, typename A2> - static void register_make(const std::string &name, ReturnType(*fcn)(const A0 &, const A1 &, const A2 &)); +template <typename ReturnType, typename A0, typename A1> +static void register_make(const std::string &name, ReturnType(*fcn)(const A0 &, const A1 &)); - template <typename ReturnType, typename A0, typename A1, typename A2, typename A3> - static void register_make(const std::string &name, ReturnType(*fcn)(const A0 &, const A1 &, const A2 &, const A3 &)); +template <typename ReturnType, typename A0, typename A1, typename A2> +static void register_make(const std::string &name, ReturnType(*fcn)(const A0 &, const A1 &, const A2 &)); - template <typename ReturnType, typename A0, typename A1, typename A2, typename A3, typename A4> - static void register_make(const std::string &name, ReturnType(*fcn)(const A0 &, const A1 &, const A2 &, const A3 &, const A4 &)); +template <typename ReturnType, typename A0, typename A1, typename A2, typename A3> +static void register_make(const std::string &name, ReturnType(*fcn)(const A0 &, const A1 &, const A2 &, const A3 &)); - template <typename ReturnType, typename A0, typename A1, typename A2, typename A3, typename A4, typename A5> - static void register_make(const std::string &name, ReturnType(*fcn)(const A0 &, const A1 &, const A2 &, const A3 &, const A4 &, const A5 &)); +template <typename ReturnType, typename A0, typename A1, typename A2, typename A3, typename A4> +static void register_make(const std::string &name, ReturnType(*fcn)(const A0 &, const A1 &, const A2 &, const A3 &, const A4 &)); - template <typename ReturnType, typename A0, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6> - static void register_make(const std::string &name, ReturnType(*fcn)(const A0 &, const A1 &, const A2 &, const A3 &, const A4 &, const A5 &, const A6 &)); +template <typename ReturnType, typename A0, typename A1, typename A2, typename A3, typename A4, typename A5> +static void register_make(const std::string &name, ReturnType(*fcn)(const A0 &, const A1 &, const A2 &, const A3 &, const A4 &, const A5 &)); - template <typename ReturnType, typename A0, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7> - static void register_make(const std::string &name, ReturnType(*fcn)(const A0 &, const A1 &, const A2 &, const A3 &, const A4 &, const A5 &, const A6 &, const A7 &)); +template <typename ReturnType, typename A0, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6> +static void register_make(const std::string &name, ReturnType(*fcn)(const A0 &, const A1 &, const A2 &, const A3 &, const A4 &, const A5 &, const A6 &)); - template <typename ReturnType, typename A0, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8> - static void register_make(const std::string &name, ReturnType(*fcn)(const A0 &, const A1 &, const A2 &, const A3 &, const A4 &, const A5 &, const A6 &, const A7 &, const A8 &)); +template <typename ReturnType, typename A0, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7> +static void register_make(const std::string &name, ReturnType(*fcn)(const A0 &, const A1 &, const A2 &, const A3 &, const A4 &, const A5 &, const A6 &, const A7 &)); - template <typename ReturnType, typename A0, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8, typename A9> - static void register_make(const std::string &name, ReturnType(*fcn)(const A0 &, const A1 &, const A2 &, const A3 &, const A4 &, const A5 &, const A6 &, const A7 &, const A8 &, const A9 &)); +template <typename ReturnType, typename A0, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8> +static void register_make(const std::string &name, ReturnType(*fcn)(const A0 &, const A1 &, const A2 &, const A3 &, const A4 &, const A5 &, const A6 &, const A7 &, const A8 &)); - /******************************************************************* - * Make API - don't look here, template magic, not helpful - ******************************************************************/; - inline - static Element *make(const std::string &name); +template <typename ReturnType, typename A0, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8, typename A9> +static void register_make(const std::string &name, ReturnType(*fcn)(const A0 &, const A1 &, const A2 &, const A3 &, const A4 &, const A5 &, const A6 &, const A7 &, const A8 &, const A9 &)); - template <typename A0> - static Element *make(const std::string &name, const A0 &); +/*********************************************************************** + * Make API - don't look here, template magic, not helpful + **********************************************************************/ +inline +static Element *make(const std::string &name); - template <typename A0, typename A1> - static Element *make(const std::string &name, const A0 &, const A1 &); +template <typename A0> +static Element *make(const std::string &name, const A0 &); - template <typename A0, typename A1, typename A2> - static Element *make(const std::string &name, const A0 &, const A1 &, const A2 &); +template <typename A0, typename A1> +static Element *make(const std::string &name, const A0 &, const A1 &); - template <typename A0, typename A1, typename A2, typename A3> - static Element *make(const std::string &name, const A0 &, const A1 &, const A2 &, const A3 &); +template <typename A0, typename A1, typename A2> +static Element *make(const std::string &name, const A0 &, const A1 &, const A2 &); - template <typename A0, typename A1, typename A2, typename A3, typename A4> - static Element *make(const std::string &name, const A0 &, const A1 &, const A2 &, const A3 &, const A4 &); +template <typename A0, typename A1, typename A2, typename A3> +static Element *make(const std::string &name, const A0 &, const A1 &, const A2 &, const A3 &); - template <typename A0, typename A1, typename A2, typename A3, typename A4, typename A5> - static Element *make(const std::string &name, const A0 &, const A1 &, const A2 &, const A3 &, const A4 &, const A5 &); +template <typename A0, typename A1, typename A2, typename A3, typename A4> +static Element *make(const std::string &name, const A0 &, const A1 &, const A2 &, const A3 &, const A4 &); - template <typename A0, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6> - static Element *make(const std::string &name, const A0 &, const A1 &, const A2 &, const A3 &, const A4 &, const A5 &, const A6 &); +template <typename A0, typename A1, typename A2, typename A3, typename A4, typename A5> +static Element *make(const std::string &name, const A0 &, const A1 &, const A2 &, const A3 &, const A4 &, const A5 &); - template <typename A0, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7> - static Element *make(const std::string &name, const A0 &, const A1 &, const A2 &, const A3 &, const A4 &, const A5 &, const A6 &, const A7 &); +template <typename A0, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6> +static Element *make(const std::string &name, const A0 &, const A1 &, const A2 &, const A3 &, const A4 &, const A5 &, const A6 &); - template <typename A0, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8> - static Element *make(const std::string &name, const A0 &, const A1 &, const A2 &, const A3 &, const A4 &, const A5 &, const A6 &, const A7 &, const A8 &); +template <typename A0, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7> +static Element *make(const std::string &name, const A0 &, const A1 &, const A2 &, const A3 &, const A4 &, const A5 &, const A6 &, const A7 &); - template <typename A0, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8, typename A9> - static Element *make(const std::string &name, const A0 &, const A1 &, const A2 &, const A3 &, const A4 &, const A5 &, const A6 &, const A7 &, const A8 &, const A9 &); +template <typename A0, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8> +static Element *make(const std::string &name, const A0 &, const A1 &, const A2 &, const A3 &, const A4 &, const A5 &, const A6 &, const A7 &, const A8 &); - /******************************************************************* - * Private registration hooks - ******************************************************************/ - static void _register_make(const std::string &, void *); - static Element *_handle_make(const std::string &, const PMCC &); -}; +template <typename A0, typename A1, typename A2, typename A3, typename A4, typename A5, typename A6, typename A7, typename A8, typename A9> +static Element *make(const std::string &name, const A0 &, const A1 &, const A2 &, const A3 &, const A4 &, const A5 &, const A6 &, const A7 &, const A8 &, const A9 &); } +/*! + * Register a block's factory function: + * Declare this macro at the global scope in a cpp file. + * The block will register at static initialization time. + */ +#define GRAS_REGISTER_FACTORY(name, fcn) \ + GRAS_STATIC_BLOCK(fcn) \ + {gras::register_make(name, &fcn);} + +/*! + * Register a block's constructor into the factory: + * The arguments to this macro must be the types of each constructor argument. + * Example: GRAS_REGISTER_FACTORY2("/proj/my_block", MyBlock, std::string, size_t) + * Declare this macro at the global scope in a cpp file. + * The block will register at static initialization time. + */ +#define GRAS_REGISTER_FACTORY0(name, type) \ + static gras::Element *make_ ## type() \ + { return new type(); } \ + GRAS_REGISTER_FACTORY(name, make_##type) + +/*! + * Register a block's constructor into the factory: + * The arguments to this macro must be the types of each constructor argument. + * Example: GRAS_REGISTER_FACTORY2("/proj/my_block", MyBlock, std::string, size_t) + * Declare this macro at the global scope in a cpp file. + * The block will register at static initialization time. + */ +#define GRAS_REGISTER_FACTORY1(name, type, A0) \ + static gras::Element *make_ ## type(const A0 &a0) \ + { return new type(a0); } \ + GRAS_REGISTER_FACTORY(name, make_##type) + +/*! + * Register a block's constructor into the factory: + * The arguments to this macro must be the types of each constructor argument. + * Example: GRAS_REGISTER_FACTORY2("/proj/my_block", MyBlock, std::string, size_t) + * Declare this macro at the global scope in a cpp file. + * The block will register at static initialization time. + */ +#define GRAS_REGISTER_FACTORY2(name, type, A0, A1) \ + static gras::Element *make_ ## type(const A0 &a0, const A1 &a1) \ + { return new type(a0, a1); } \ + GRAS_REGISTER_FACTORY(name, make_##type) + +/*! + * Register a block's constructor into the factory: + * The arguments to this macro must be the types of each constructor argument. + * Example: GRAS_REGISTER_FACTORY2("/proj/my_block", MyBlock, std::string, size_t) + * Declare this macro at the global scope in a cpp file. + * The block will register at static initialization time. + */ +#define GRAS_REGISTER_FACTORY3(name, type, A0, A1, A2) \ + static gras::Element *make_ ## type(const A0 &a0, const A1 &a1, const A2 &a2) \ + { return new type(a0, a1, a2); } \ + GRAS_REGISTER_FACTORY(name, make_##type) + +/*! + * Register a block's constructor into the factory: + * The arguments to this macro must be the types of each constructor argument. + * Example: GRAS_REGISTER_FACTORY2("/proj/my_block", MyBlock, std::string, size_t) + * Declare this macro at the global scope in a cpp file. + * The block will register at static initialization time. + */ +#define GRAS_REGISTER_FACTORY4(name, type, A0, A1, A2, A3) \ + static gras::Element *make_ ## type(const A0 &a0, const A1 &a1, const A2 &a2, const A3 &a3) \ + { return new type(a0, a1, a2, a3); } \ + GRAS_REGISTER_FACTORY(name, make_##type) + +/*! + * Register a block's constructor into the factory: + * The arguments to this macro must be the types of each constructor argument. + * Example: GRAS_REGISTER_FACTORY2("/proj/my_block", MyBlock, std::string, size_t) + * Declare this macro at the global scope in a cpp file. + * The block will register at static initialization time. + */ +#define GRAS_REGISTER_FACTORY5(name, type, A0, A1, A2, A3, A4) \ + static gras::Element *make_ ## type(const A0 &a0, const A1 &a1, const A2 &a2, const A3 &a3, const A4 &a4) \ + { return new type(a0, a1, a2, a3, a4); } \ + GRAS_REGISTER_FACTORY(name, make_##type) + +/*! + * Register a block's constructor into the factory: + * The arguments to this macro must be the types of each constructor argument. + * Example: GRAS_REGISTER_FACTORY2("/proj/my_block", MyBlock, std::string, size_t) + * Declare this macro at the global scope in a cpp file. + * The block will register at static initialization time. + */ +#define GRAS_REGISTER_FACTORY6(name, type, A0, A1, A2, A3, A4, A5) \ + static gras::Element *make_ ## type(const A0 &a0, const A1 &a1, const A2 &a2, const A3 &a3, const A4 &a4, const A5 &a5) \ + { return new type(a0, a1, a2, a3, a4, a5); } \ + GRAS_REGISTER_FACTORY(name, make_##type) + +/*! + * Register a block's constructor into the factory: + * The arguments to this macro must be the types of each constructor argument. + * Example: GRAS_REGISTER_FACTORY2("/proj/my_block", MyBlock, std::string, size_t) + * Declare this macro at the global scope in a cpp file. + * The block will register at static initialization time. + */ +#define GRAS_REGISTER_FACTORY7(name, type, A0, A1, A2, A3, A4, A5, A6) \ + static gras::Element *make_ ## type(const A0 &a0, const A1 &a1, const A2 &a2, const A3 &a3, const A4 &a4, const A5 &a5, const A6 &a6) \ + { return new type(a0, a1, a2, a3, a4, a5, a6); } \ + GRAS_REGISTER_FACTORY(name, make_##type) + +/*! + * Register a block's constructor into the factory: + * The arguments to this macro must be the types of each constructor argument. + * Example: GRAS_REGISTER_FACTORY2("/proj/my_block", MyBlock, std::string, size_t) + * Declare this macro at the global scope in a cpp file. + * The block will register at static initialization time. + */ +#define GRAS_REGISTER_FACTORY8(name, type, A0, A1, A2, A3, A4, A5, A6, A7) \ + static gras::Element *make_ ## type(const A0 &a0, const A1 &a1, const A2 &a2, const A3 &a3, const A4 &a4, const A5 &a5, const A6 &a6, const A7 &a7) \ + { return new type(a0, a1, a2, a3, a4, a5, a6, a7); } \ + GRAS_REGISTER_FACTORY(name, make_##type) + +/*! + * Register a block's constructor into the factory: + * The arguments to this macro must be the types of each constructor argument. + * Example: GRAS_REGISTER_FACTORY2("/proj/my_block", MyBlock, std::string, size_t) + * Declare this macro at the global scope in a cpp file. + * The block will register at static initialization time. + */ +#define GRAS_REGISTER_FACTORY9(name, type, A0, A1, A2, A3, A4, A5, A6, A7, A8) \ + static gras::Element *make_ ## type(const A0 &a0, const A1 &a1, const A2 &a2, const A3 &a3, const A4 &a4, const A5 &a5, const A6 &a6, const A7 &a7, const A8 &a8) \ + { return new type(a0, a1, a2, a3, a4, a5, a6, a7, a8); } \ + GRAS_REGISTER_FACTORY(name, make_##type) + +/*! + * Register a block's constructor into the factory: + * The arguments to this macro must be the types of each constructor argument. + * Example: GRAS_REGISTER_FACTORY2("/proj/my_block", MyBlock, std::string, size_t) + * Declare this macro at the global scope in a cpp file. + * The block will register at static initialization time. + */ +#define GRAS_REGISTER_FACTORY10(name, type, A0, A1, A2, A3, A4, A5, A6, A7, A8, A9) \ + static gras::Element *make_ ## type(const A0 &a0, const A1 &a1, const A2 &a2, const A3 &a3, const A4 &a4, const A5 &a5, const A6 &a6, const A7 &a7, const A8 &a8, const A9 &a9) \ + { return new type(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9); } \ + GRAS_REGISTER_FACTORY(name, make_##type) + #include <gras/detail/factory.hpp> #endif /*INCLUDED_GRAS_FACTORY_HPP*/ diff --git a/python/gras/GRAS_Factory.i b/python/gras/GRAS_Factory.i index 1124963..eca9d2e 100644 --- a/python/gras/GRAS_Factory.i +++ b/python/gras/GRAS_Factory.i @@ -30,25 +30,18 @@ namespace gras %pythoncode %{ #TODO we need to register this into the real factory _py_factory = dict() -%} -%extend gras::Factory -{ - %insert("python") - %{ - @staticmethod - def register_make(name, fcn): - #TODO we need to register this into the real factory - _py_factory[name] = fcn - - @staticmethod - def make(name, *args, **kwargs): - - #first try the local to python py factory #TODO real factory - if name in _py_factory: return _py_factory[name](*args, **kwargs) - - from PMC import PMC_M - pmcargs = PMC_M(list(args)) - return Factory._handle_make(name, pmcargs) - %} -} +def register_make(name, fcn): + #TODO we need to register this into the real factory + _py_factory[name] = fcn + +def make(name, *args, **kwargs): + + #first try the local to python py factory #TODO real factory + if name in _py_factory: return _py_factory[name](*args, **kwargs) + + from PMC import PMC_M + pmcargs = PMC_M(list(args)) + return Factory._handle_make(name, pmcargs) + +%} diff --git a/python/gras/GRAS_Loader.py b/python/gras/GRAS_Loader.py index b70bc60..cc73d9b 100644 --- a/python/gras/GRAS_Loader.py +++ b/python/gras/GRAS_Loader.py @@ -2,6 +2,7 @@ import os import sys +import traceback #try to import module #http://effbot.org/zone/import-string.htm @@ -15,6 +16,7 @@ def __try_module_import(filename): module = __import__(module_name) except Exception as ex: print 'Could not import', filename, ex + print traceback.format_exc() finally: sys.path[:] = path # restore diff --git a/python/gras/__init__.py b/python/gras/__init__.py index 6dcd4cd..793eba8 100644 --- a/python/gras/__init__.py +++ b/python/gras/__init__.py @@ -10,7 +10,7 @@ from GRAS_SBuffer import SBufferConfig, SBuffer from GRAS_Tags import Tag, StreamTag, PacketMsg from GRAS_TimeTag import TimeTag from GRAS_Element import Element -from GRAS_Factory import Factory +from GRAS_Factory import make, register_make import GRAS_Block import GRAS_HierBlock import GRAS_TopBlock diff --git a/tests/example_module.cpp b/tests/example_module.cpp index d0316f8..389cf53 100644 --- a/tests/example_module.cpp +++ b/tests/example_module.cpp @@ -26,9 +26,4 @@ struct MyBlock : gras::Block void work(const InputItems &, const OutputItems &){} }; -gras::Block *make_my_block(void) -{ - return new MyBlock(); -} - -GRAS_REGISTER_FACTORY("/tests/my_block0", make_my_block) +GRAS_REGISTER_FACTORY0("/tests/my_block0", MyBlock) diff --git a/tests/example_module.py b/tests/example_module.py index 6c29435..fbf0b45 100644 --- a/tests/example_module.py +++ b/tests/example_module.py @@ -13,4 +13,4 @@ class MyBlock(gras.Block): def get_num(self): return 42 -gras.Factory.register_make("/tests/my_block1", MyBlock) +gras.register_make("/tests/my_block1", MyBlock) diff --git a/tests/factory_test.cpp b/tests/factory_test.cpp index 999e86e..ca8b5ba 100644 --- a/tests/factory_test.cpp +++ b/tests/factory_test.cpp @@ -51,25 +51,25 @@ GRAS_REGISTER_FACTORY("/tests/my_block_args3", make_my_block_args3) BOOST_AUTO_TEST_CASE(test_register_and_make) { - gras::Element *my_block0 = gras::Factory::make("/tests/my_block_args0"); + gras::Element *my_block0 = gras::make("/tests/my_block_args0"); BOOST_CHECK(dynamic_cast<MyBlock *>(my_block0) != NULL); BOOST_CHECK_EQUAL(dynamic_cast<MyBlock *>(my_block0)->a0, 0); BOOST_CHECK_EQUAL(dynamic_cast<MyBlock *>(my_block0)->a1, ""); BOOST_CHECK_EQUAL(dynamic_cast<MyBlock *>(my_block0)->a2, false); - gras::Element *my_block1 = gras::Factory::make("/tests/my_block_args1", 42); + gras::Element *my_block1 = gras::make("/tests/my_block_args1", 42); BOOST_CHECK(dynamic_cast<MyBlock *>(my_block1) != NULL); BOOST_CHECK_EQUAL(dynamic_cast<MyBlock *>(my_block1)->a0, 42); BOOST_CHECK_EQUAL(dynamic_cast<MyBlock *>(my_block1)->a1, ""); BOOST_CHECK_EQUAL(dynamic_cast<MyBlock *>(my_block1)->a2, false); - gras::Element *my_block2 = gras::Factory::make("/tests/my_block_args2", 1, "foo"); + gras::Element *my_block2 = gras::make("/tests/my_block_args2", 1, "foo"); BOOST_CHECK(dynamic_cast<MyBlock *>(my_block2) != NULL); BOOST_CHECK_EQUAL(dynamic_cast<MyBlock *>(my_block2)->a0, 1); BOOST_CHECK_EQUAL(dynamic_cast<MyBlock *>(my_block2)->a1, "foo"); BOOST_CHECK_EQUAL(dynamic_cast<MyBlock *>(my_block2)->a2, false); - gras::Element *my_block3 = gras::Factory::make("/tests/my_block_args3", -2, "bar", true); + gras::Element *my_block3 = gras::make("/tests/my_block_args3", -2, "bar", true); BOOST_CHECK(dynamic_cast<MyBlock *>(my_block3) != NULL); BOOST_CHECK_EQUAL(dynamic_cast<MyBlock *>(my_block3)->a0, -2); BOOST_CHECK_EQUAL(dynamic_cast<MyBlock *>(my_block3)->a1, "bar"); diff --git a/tests/module_loader_test.py b/tests/module_loader_test.py index 4a8d64f..34423fd 100644 --- a/tests/module_loader_test.py +++ b/tests/module_loader_test.py @@ -6,11 +6,11 @@ import gras class ModuleLoaderTest(unittest.TestCase): def test_load_module_cpp(self): - my_block = gras.Factory.make("/tests/my_block0") + my_block = gras.make("/tests/my_block0") self.assertEqual(my_block.get_num(), 42) def test_load_module_py(self): - my_block = gras.Factory.make("/tests/my_block1") + my_block = gras.make("/tests/my_block1") self.assertEqual(my_block.get_num(), 42) if __name__ == '__main__': diff --git a/tmpl/factory.tmpl.hpp b/tmpl/factory.tmpl.hpp index 61a61ba..148a851 100644 --- a/tmpl/factory.tmpl.hpp +++ b/tmpl/factory.tmpl.hpp @@ -8,15 +8,6 @@ #include <PMC/PMC.hpp> #include <string> -/*! - * Register a block's factory function: - * Declare this macro at the global scope in a cpp file. - * The block will register at static initialization time. - */ -#define GRAS_REGISTER_FACTORY(name, fcn) \ - GRAS_STATIC_BLOCK(fcn) \ - {gras::Factory::register_make(name, &fcn);} - namespace gras { @@ -34,30 +25,53 @@ namespace gras struct GRAS_API Factory { /******************************************************************* - * Register API - don't look here, template magic, not helpful - ******************************************************************/ - #for $NARGS in range($MAX_ARGS) - template <typename ReturnType, $expand('typename A%d', $NARGS)> - static void register_make(const std::string &name, ReturnType(*fcn)($expand('const A%d &', $NARGS))); - - #end for - /******************************************************************* - * Make API - don't look here, template magic, not helpful - ******************************************************************/; - #for $NARGS in range($MAX_ARGS) - template <$expand('typename A%d', $NARGS)> - static Element *make(const std::string &name, $expand('const A%d &', $NARGS)); - - #end for - /******************************************************************* * Private registration hooks ******************************************************************/ static void _register_make(const std::string &, void *); static Element *_handle_make(const std::string &, const PMCC &); }; +/*********************************************************************** + * Register API - don't look here, template magic, not helpful + **********************************************************************/ +#for $NARGS in range($MAX_ARGS) +template <typename ReturnType, $expand('typename A%d', $NARGS)> +static void register_make(const std::string &name, ReturnType(*fcn)($expand('const A%d &', $NARGS))); + +#end for +/*********************************************************************** + * Make API - don't look here, template magic, not helpful + **********************************************************************/ +#for $NARGS in range($MAX_ARGS) +template <$expand('typename A%d', $NARGS)> +static Element *make(const std::string &name, $expand('const A%d &', $NARGS)); + +#end for } +/*! + * Register a block's factory function: + * Declare this macro at the global scope in a cpp file. + * The block will register at static initialization time. + */ +#define GRAS_REGISTER_FACTORY(name, fcn) \ + GRAS_STATIC_BLOCK(fcn) \ + {gras::register_make(name, &fcn);} + +#for $NARGS in range($MAX_ARGS) +/*! + * Register a block's constructor into the factory: + * The arguments to this macro must be the types of each constructor argument. + * Example: GRAS_REGISTER_FACTORY2("/proj/my_block", MyBlock, std::string, size_t) + * Declare this macro at the global scope in a cpp file. + * The block will register at static initialization time. + */ +#define GRAS_REGISTER_FACTORY$(NARGS)(name, type, $expand('A%d', $NARGS)) \ + static gras::Element *make_ $('##') type($expand('const A%d &a%d', $NARGS)) \ + { return new type($expand('a%d', $NARGS)); } \ + GRAS_REGISTER_FACTORY(name, make_$('##')type) + +#end for #include <gras/detail/factory.hpp> #endif /*INCLUDED_GRAS_FACTORY_HPP*/ diff --git a/tmpl/factory_detail.tmpl.hpp b/tmpl/factory_detail.tmpl.hpp index ea30235..8712db4 100644 --- a/tmpl/factory_detail.tmpl.hpp +++ b/tmpl/factory_detail.tmpl.hpp @@ -38,7 +38,7 @@ struct FactoryRegistryEntryImpl$(NARGS) : FactoryRegistryEntry }; template <typename ReturnType, $expand('typename A%d', $NARGS)> -void Factory::register_make(const std::string &name, ReturnType(*fcn)($expand('const A%d &', $NARGS))) +void register_make(const std::string &name, ReturnType(*fcn)($expand('const A%d &', $NARGS))) { void *r = new FactoryRegistryEntryImpl$(NARGS)<ReturnType, $expand('A%d', $NARGS)>(fcn); Factory::_register_make(name, r); @@ -50,7 +50,7 @@ void Factory::register_make(const std::string &name, ReturnType(*fcn)($expand('c **********************************************************************/ #for $NARGS in range($MAX_ARGS) template <$expand('typename A%d', $NARGS)> -Element *Factory::make(const std::string &name, $expand('const A%d &a%d', $NARGS)) +Element *make(const std::string &name, $expand('const A%d &a%d', $NARGS)) { PMCList args($NARGS); #for $i in range($NARGS): |