diff options
author | Josh Blum | 2013-07-29 22:14:19 -0700 |
---|---|---|
committer | Josh Blum | 2013-07-29 22:14:19 -0700 |
commit | cf8ca0ba62e78e70b7621b0c81bf7c031eabe3ac (patch) | |
tree | 38013fffdde0f9620bbb6f9dc7a47ba770672e70 /tmpl | |
parent | 311d63e3f0432c0dda45d622b618b303c41ec541 (diff) | |
download | sandhi-cf8ca0ba62e78e70b7621b0c81bf7c031eabe3ac.tar.gz sandhi-cf8ca0ba62e78e70b7621b0c81bf7c031eabe3ac.tar.bz2 sandhi-cf8ca0ba62e78e70b7621b0c81bf7c031eabe3ac.zip |
gras: simplfy factory w/ macros and namespace
Diffstat (limited to 'tmpl')
-rw-r--r-- | tmpl/factory.tmpl.hpp | 64 | ||||
-rw-r--r-- | tmpl/factory_detail.tmpl.hpp | 4 |
2 files changed, 41 insertions, 27 deletions
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): |