From 92c8b4646d77d569480d7c3120a00df6a9645aba Mon Sep 17 00:00:00 2001 From: Josh Blum Date: Sat, 13 Jul 2013 11:23:41 -0700 Subject: gras: work on generator scripts for template madness --- tmpl/callable.tmpl.hpp | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 tmpl/callable.tmpl.hpp (limited to 'tmpl/callable.tmpl.hpp') diff --git a/tmpl/callable.tmpl.hpp b/tmpl/callable.tmpl.hpp new file mode 100644 index 0000000..1c73910 --- /dev/null +++ b/tmpl/callable.tmpl.hpp @@ -0,0 +1,94 @@ +// Copyright (C) by Josh Blum. See LICENSE.txt for licensing information. + +#ifndef INCLUDED_GRAS_CALLABLE_HPP +#define INCLUDED_GRAS_CALLABLE_HPP + +#include +#include +#include +#include +#include + +namespace gras +{ + +/*! + * The callable interface allows subclasses to export public methods, + * but without actually exporting traditional library symbols. + * + * Callable handles the template magic so you don't have to: + * - registering subclass methods is simple and easy on the user. + * - users call registered methods with natural code aesthetics. + * + * Register a method (in the constructor of MyClass): + * this->register_call("set_foo", &MyClass::set_foo); + * + * Call a method on a instance of MyClass: + * my_class->x("set_foo", new_foo_val); + * + * Why x for the call method? + * - The "x" is short, one character of screen width. + * - The "x" looks like "*", which is commonly used. + * - The overloaded () operator sucks with pointers. + * - The overloaded () operator has template issues. + */ +class GRAS_API Callable +{ +public: + + //! Default constructor + Callable(void); + + //! Destructor (virtual for subclasses) + virtual ~Callable(void); + + //! Get a list of names for registered calls + std::vector get_registered_names(void) const; + +protected: + /*! + * Unregister a previously registered call. + * Throws if the name is not found in the registry. + */ + void unregister_call(const std::string &name); + + /******************************************************************* + * Register API - don't look here, template magic, not helpful + ******************************************************************/ +protected: + #for $NARGS in range($MAX_ARGS) + template + void register_call(const std::string &name, ReturnType(ClassType::*fcn)($expand('const Arg%d &', $NARGS))); + + template + void register_call(const std::string &name, void(ClassType::*fcn)($expand('const Arg%d &', $NARGS))); + + #end for + /******************************************************************* + * Call API - don't look here, template magic, not helpful + ******************************************************************/ +public: + #for $NARGS in range($MAX_ARGS) + template + ReturnType x(const std::string &name, $expand('const Arg%d &', $NARGS)); + + template <$expand('typename Arg%d', $NARGS)> + void x(const std::string &name, $expand('const Arg%d &', $NARGS)); + + #end for + /******************************************************************* + * Private registration hooks + ******************************************************************/ +protected: + void _register_call(const std::string &, void *); +public: + virtual PMCC _handle_call(const std::string &, const PMCC &); +private: + boost::shared_ptr _call_registry; +}; + +} //namespace gras + +#include + +#endif /*INCLUDED_GRAS_CALLABLE_HPP*/ -- cgit From fc98eaf2a00e000841cb16d962f83c118e4824c2 Mon Sep 17 00:00:00 2001 From: Josh Blum Date: Sat, 13 Jul 2013 12:58:22 -0700 Subject: gras: added comments and renamed tmpl args --- tmpl/callable.tmpl.hpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'tmpl/callable.tmpl.hpp') diff --git a/tmpl/callable.tmpl.hpp b/tmpl/callable.tmpl.hpp index 1c73910..900bae4 100644 --- a/tmpl/callable.tmpl.hpp +++ b/tmpl/callable.tmpl.hpp @@ -57,11 +57,11 @@ protected: ******************************************************************/ protected: #for $NARGS in range($MAX_ARGS) - template - void register_call(const std::string &name, ReturnType(ClassType::*fcn)($expand('const Arg%d &', $NARGS))); + template + void register_call(const std::string &name, ReturnType(ClassType::*fcn)($expand('const A%d &', $NARGS))); - template - void register_call(const std::string &name, void(ClassType::*fcn)($expand('const Arg%d &', $NARGS))); + template + void register_call(const std::string &name, void(ClassType::*fcn)($expand('const A%d &', $NARGS))); #end for /******************************************************************* @@ -69,11 +69,11 @@ protected: ******************************************************************/ public: #for $NARGS in range($MAX_ARGS) - template - ReturnType x(const std::string &name, $expand('const Arg%d &', $NARGS)); + template + ReturnType x(const std::string &name, $expand('const A%d &', $NARGS)); - template <$expand('typename Arg%d', $NARGS)> - void x(const std::string &name, $expand('const Arg%d &', $NARGS)); + template <$expand('typename A%d', $NARGS)> + void x(const std::string &name, $expand('const A%d &', $NARGS)); #end for /******************************************************************* -- cgit