From cf8ca0ba62e78e70b7621b0c81bf7c031eabe3ac Mon Sep 17 00:00:00 2001 From: Josh Blum Date: Mon, 29 Jul 2013 22:14:19 -0700 Subject: gras: simplfy factory w/ macros and namespace --- tmpl/factory.tmpl.hpp | 64 +++++++++++++++++++++++++++----------------- tmpl/factory_detail.tmpl.hpp | 4 +-- 2 files changed, 41 insertions(+), 27 deletions(-) (limited to 'tmpl') 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 #include -/*! - * 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 { @@ -33,22 +24,6 @@ namespace gras */ struct GRAS_API Factory { - /******************************************************************* - * Register API - don't look here, template magic, not helpful - ******************************************************************/ - #for $NARGS in range($MAX_ARGS) - template - 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 ******************************************************************/ @@ -56,8 +31,47 @@ struct GRAS_API Factory 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 +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 #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 -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)(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): -- cgit