diff options
author | Josh Blum | 2013-07-05 14:08:12 -0700 |
---|---|---|
committer | Josh Blum | 2013-07-05 14:08:12 -0700 |
commit | bc8165bcc3a2703d8fd3d17925b6bcb55ddff6ef (patch) | |
tree | c90c0829a173381398bf6a28c8e5780f76de586f /include/gras/detail | |
parent | 1298bcd1e421ccfefafa78c6d33760818902f399 (diff) | |
download | sandhi-bc8165bcc3a2703d8fd3d17925b6bcb55ddff6ef.tar.gz sandhi-bc8165bcc3a2703d8fd3d17925b6bcb55ddff6ef.tar.bz2 sandhi-bc8165bcc3a2703d8fd3d17925b6bcb55ddff6ef.zip |
gras: save callable work before we tear up more
Diffstat (limited to 'include/gras/detail')
-rw-r--r-- | include/gras/detail/block.hpp | 39 | ||||
-rw-r--r-- | include/gras/detail/callable.hpp | 113 |
2 files changed, 139 insertions, 13 deletions
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 <typename ClassType, typename ReturnType, typename Arg0, typename Arg1> 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 <typename ClassType, typename ReturnType> void Block::register_call(const std::string &key, ReturnType(ClassType::*fcn)(void)) @@ -92,8 +87,26 @@ template <typename ReturnType, typename Arg0, typename Arg1> ReturnType Block::c return r.safe_as<ReturnType>(); } +inline void Block::call(const std::string &key) +{ + std::vector<PMCC> args; + _handle_function_access(key, args); +} +template <typename Arg0> void Block::call(const std::string &key, const Arg0 &a0) +{ + std::vector<PMCC> args; + args.push_back(PMC_M(a0)); + _handle_function_access(key, args); +} +template <typename Arg0, typename Arg1> void Block::call(const std::string &key, const Arg0 &a0, const Arg1 &a1) +{ + std::vector<PMCC> 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 <typeinfo> + +namespace gras +{ + +struct GRAS_API CallableRegistry +{ + CallableRegistry(void); + virtual ~CallableRegistry(void); + virtual PMCC call(const std::vector<PMCC> &args) = 0; +}; + +template <typename ClassType, typename ReturnType, typename Arg0, typename Arg1> +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<PMCC> &args) + { + if (_fcn0) return PMC_M((_obj->*_fcn0)()); + if (_fcn1) return PMC_M((_obj->*_fcn1)(args[0].safe_as<Arg0>())); + if (_fcn2) return PMC_M((_obj->*_fcn2)(args[0].safe_as<Arg0>(), args[1].safe_as<Arg1>())); + return PMCC();//should not get here + } + +private: + ClassType *_obj; + Fcn0 _fcn0; Fcn1 _fcn1; Fcn2 _fcn2; +}; + +template <typename ClassType, typename ReturnType> void Callable::register_call(const std::string &key, ReturnType(ClassType::*fcn)(void)) +{ + ClassType *obj = dynamic_cast<ClassType *>(this); + void *fr = new CallableRegistryImpl<ClassType, ReturnType, int, int>(obj, fcn); + _register_call(key, fr); +} + +template <typename ClassType, typename ReturnType, typename Arg0> void Callable::register_call(const std::string &key, ReturnType(ClassType::*fcn)(const Arg0 &)) +{ + ClassType *obj = dynamic_cast<ClassType *>(this); + void *fr = new CallableRegistryImpl<ClassType, ReturnType, Arg0, int>(obj, NULL, fcn); + _register_call(key, fr); +} + +template <typename ClassType, typename ReturnType, typename Arg0, typename Arg1> void Callable::register_call(const std::string &key, ReturnType(ClassType::*fcn)(const Arg0 &, const Arg1 &)) +{ + ClassType *obj = dynamic_cast<ClassType *>(this); + void *fr = new CallableRegistryImpl<ClassType, ReturnType, Arg0, Arg1>(obj, NULL, NULL, fcn); + _register_call(key, fr); +} + +template <typename ReturnType> ReturnType Callable::call(const std::string &key) +{ + std::vector<PMCC> args; + PMCC r = _handle_call(key, args); + return r.safe_as<ReturnType>(); +} + +template <typename ReturnType, typename Arg0> ReturnType Callable::call(const std::string &key, const Arg0 &a0) +{ + std::vector<PMCC> args; + args.push_back(PMC_M(a0)); + PMCC r = _handle_call(key, args); + return r.safe_as<ReturnType>(); +} + +template <typename ReturnType, typename Arg0, typename Arg1> ReturnType Callable::call(const std::string &key, const Arg0 &a0, const Arg1 &a1) +{ + std::vector<PMCC> args; + args.push_back(PMC_M(a0)); + args.push_back(PMC_M(a1)); + PMCC r = _handle_call(key, args); + return r.safe_as<ReturnType>(); +} + +inline void Callable::call(const std::string &key) +{ + std::vector<PMCC> args; + _handle_call(key, args); +} + +template <typename Arg0> void Callable::call(const std::string &key, const Arg0 &a0) +{ + std::vector<PMCC> args; + args.push_back(PMC_M(a0)); + _handle_call(key, args); +} + +template <typename Arg0, typename Arg1> void Callable::call(const std::string &key, const Arg0 &a0, const Arg1 &a1) +{ + std::vector<PMCC> 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*/ |