From 1298bcd1e421ccfefafa78c6d33760818902f399 Mon Sep 17 00:00:00 2001 From: Josh Blum Date: Thu, 4 Jul 2013 19:46:58 -0700 Subject: gras: function registry is more flexible --- include/gras/detail/block.hpp | 87 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) (limited to 'include/gras/detail') diff --git a/include/gras/detail/block.hpp b/include/gras/detail/block.hpp index 9dccb22..fcd52e1 100644 --- a/include/gras/detail/block.hpp +++ b/include/gras/detail/block.hpp @@ -8,6 +8,93 @@ namespace gras { +struct GRAS_API FunctionRegistry +{ + FunctionRegistry(void); + virtual ~FunctionRegistry(void); + virtual PMCC call(const std::vector &args) = 0; +}; + +template +class FunctionRegistryImpl : public FunctionRegistry +{ +public: + FunctionRegistryImpl( + ClassType *obj, + ReturnType(ClassType::*fcn0)(void), + ReturnType(ClassType::*fcn1)(const Arg0 &) = NULL, + ReturnType(ClassType::*fcn2)(const Arg0 &, const Arg1 &) = NULL + ): + _obj(obj), + _fcn0(fcn0), + _fcn1(fcn1), + _fcn2(fcn2) + {} + virtual ~FunctionRegistryImpl(void){} + + PMCC call(const std::vector &args) + { + if (_fcn0) return PMC_M((_obj->*_fcn0)()); + if (_fcn1) return PMC_M((_obj->*_fcn1)(args[0].safe_as())); + if (_fcn2) return PMC_M((_obj->*_fcn2)(args[0].safe_as(), args[1].safe_as())); + return PMCC();//should not get here + } + +private: + ClassType *_obj; + ReturnType(ClassType::*_fcn0)(void); + ReturnType(ClassType::*_fcn1)(const Arg0 &); + ReturnType(ClassType::*_fcn2)(const Arg0 &, const Arg1 &); +}; + +template void Block::register_call(const std::string &key, ReturnType(ClassType::*fcn)(void)) +{ + ClassType *obj = dynamic_cast(this); + void *fr = new FunctionRegistryImpl(obj, fcn); + _register_function(key, fr); +} + +template void Block::register_call(const std::string &key, ReturnType(ClassType::*fcn)(const Arg0 &)) +{ + ClassType *obj = dynamic_cast(this); + void *fr = new FunctionRegistryImpl(obj, NULL, fcn); + _register_function(key, fr); +} + +template void Block::register_call(const std::string &key, ReturnType(ClassType::*fcn)(const Arg0 &, const Arg1 &)) +{ + ClassType *obj = dynamic_cast(this); + void *fr = new FunctionRegistryImpl(obj, NULL, NULL, fcn); + _register_function(key, fr); +} + +template ReturnType Block::call(const std::string &key) +{ + std::vector args; + PMCC r = _handle_function_access(key, args); + return r.safe_as(); +} + +template ReturnType Block::call(const std::string &key, const Arg0 &a0) +{ + std::vector args; + args.push_back(PMC_M(a0)); + PMCC r = _handle_function_access(key, args); + return r.safe_as(); +} + +template ReturnType Block::call(const std::string &key, const Arg0 &a0, const Arg1 &a1) +{ + std::vector args; + args.push_back(PMC_M(a0)); + args.push_back(PMC_M(a1)); + PMCC r = _handle_function_access(key, args); + return r.safe_as(); +} + + + + struct GRAS_API PropertyRegistry { PropertyRegistry(void); -- cgit