From bc8165bcc3a2703d8fd3d17925b6bcb55ddff6ef Mon Sep 17 00:00:00 2001 From: Josh Blum Date: Fri, 5 Jul 2013 14:08:12 -0700 Subject: gras: save callable work before we tear up more --- include/gras/detail/block.hpp | 39 +++++++++----- include/gras/detail/callable.hpp | 113 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 139 insertions(+), 13 deletions(-) create mode 100644 include/gras/detail/callable.hpp (limited to 'include/gras/detail') diff --git a/include/gras/detail/block.hpp b/include/gras/detail/block.hpp index fcd52e1..e098797 100644 --- a/include/gras/detail/block.hpp +++ b/include/gras/detail/block.hpp @@ -19,16 +19,13 @@ 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) + + typedef ReturnType(ClassType::*Fcn0)(void); + typedef ReturnType(ClassType::*Fcn1)(const Arg0 &); + typedef ReturnType(ClassType::*Fcn2)(const Arg0 &, const Arg1 &); + + FunctionRegistryImpl(ClassType *obj, Fcn0 fcn0, Fcn1 fcn1 = NULL, Fcn2 fcn2 = NULL): + _obj(obj), _fcn0(fcn0), _fcn1(fcn1), _fcn2(fcn2) {} virtual ~FunctionRegistryImpl(void){} @@ -42,9 +39,7 @@ public: private: ClassType *_obj; - ReturnType(ClassType::*_fcn0)(void); - ReturnType(ClassType::*_fcn1)(const Arg0 &); - ReturnType(ClassType::*_fcn2)(const Arg0 &, const Arg1 &); + Fcn0 _fcn0; Fcn1 _fcn1; Fcn2 _fcn2; }; template void Block::register_call(const std::string &key, ReturnType(ClassType::*fcn)(void)) @@ -92,8 +87,26 @@ template ReturnType Block::c return r.safe_as(); } +inline void Block::call(const std::string &key) +{ + std::vector args; + _handle_function_access(key, args); +} +template void Block::call(const std::string &key, const Arg0 &a0) +{ + std::vector args; + args.push_back(PMC_M(a0)); + _handle_function_access(key, args); +} +template void 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)); + _handle_function_access(key, args); +} struct GRAS_API PropertyRegistry { diff --git a/include/gras/detail/callable.hpp b/include/gras/detail/callable.hpp new file mode 100644 index 0000000..86b741d --- /dev/null +++ b/include/gras/detail/callable.hpp @@ -0,0 +1,113 @@ +// Copyright (C) by Josh Blum. See LICENSE.txt for licensing information. + +#ifndef INCLUDED_GRAS_DETAIL_CALLABLE_HPP +#define INCLUDED_GRAS_DETAIL_CALLABLE_HPP + +#include + +namespace gras +{ + +struct GRAS_API CallableRegistry +{ + CallableRegistry(void); + virtual ~CallableRegistry(void); + virtual PMCC call(const std::vector &args) = 0; +}; + +template +class CallableRegistryImpl : public CallableRegistry +{ +public: + + typedef ReturnType(ClassType::*Fcn0)(void); + typedef ReturnType(ClassType::*Fcn1)(const Arg0 &); + typedef ReturnType(ClassType::*Fcn2)(const Arg0 &, const Arg1 &); + + CallableRegistryImpl(ClassType *obj, Fcn0 fcn0, Fcn1 fcn1 = NULL, Fcn2 fcn2 = NULL): + _obj(obj), _fcn0(fcn0), _fcn1(fcn1), _fcn2(fcn2) + {} + virtual ~CallableRegistryImpl(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; + Fcn0 _fcn0; Fcn1 _fcn1; Fcn2 _fcn2; +}; + +template void Callable::register_call(const std::string &key, ReturnType(ClassType::*fcn)(void)) +{ + ClassType *obj = dynamic_cast(this); + void *fr = new CallableRegistryImpl(obj, fcn); + _register_call(key, fr); +} + +template void Callable::register_call(const std::string &key, ReturnType(ClassType::*fcn)(const Arg0 &)) +{ + ClassType *obj = dynamic_cast(this); + void *fr = new CallableRegistryImpl(obj, NULL, fcn); + _register_call(key, fr); +} + +template void Callable::register_call(const std::string &key, ReturnType(ClassType::*fcn)(const Arg0 &, const Arg1 &)) +{ + ClassType *obj = dynamic_cast(this); + void *fr = new CallableRegistryImpl(obj, NULL, NULL, fcn); + _register_call(key, fr); +} + +template ReturnType Callable::call(const std::string &key) +{ + std::vector args; + PMCC r = _handle_call(key, args); + return r.safe_as(); +} + +template ReturnType Callable::call(const std::string &key, const Arg0 &a0) +{ + std::vector args; + args.push_back(PMC_M(a0)); + PMCC r = _handle_call(key, args); + return r.safe_as(); +} + +template ReturnType Callable::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_call(key, args); + return r.safe_as(); +} + +inline void Callable::call(const std::string &key) +{ + std::vector args; + _handle_call(key, args); +} + +template void Callable::call(const std::string &key, const Arg0 &a0) +{ + std::vector args; + args.push_back(PMC_M(a0)); + _handle_call(key, args); +} + +template void Callable::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)); + _handle_call(key, args); +} + +} //namespace gras + +#endif /*INCLUDED_GRAS_DETAIL_CALLABLE_HPP*/ -- cgit