summaryrefslogtreecommitdiff
path: root/include/gras
diff options
context:
space:
mode:
authorJosh Blum2013-07-05 19:40:26 -0700
committerJosh Blum2013-07-05 19:40:26 -0700
commit66db95c9d57cd0c2a7a9fabe06f7d7d5b3fb8a45 (patch)
tree3a3fcbac80e13971e20ca4c93de51e1c693f4af4 /include/gras
parentfc80d2c0acec4f668b58a05bd5a3a06b0a2a2280 (diff)
downloadsandhi-66db95c9d57cd0c2a7a9fabe06f7d7d5b3fb8a45.tar.gz
sandhi-66db95c9d57cd0c2a7a9fabe06f7d7d5b3fb8a45.tar.bz2
sandhi-66db95c9d57cd0c2a7a9fabe06f7d7d5b3fb8a45.zip
gras: pythonic work on callable interface
Diffstat (limited to 'include/gras')
-rw-r--r--include/gras/block.hpp4
-rw-r--r--include/gras/block.i10
-rw-r--r--include/gras/callable.hpp2
-rw-r--r--include/gras/detail/callable.hpp70
-rw-r--r--include/gras/element.i9
5 files changed, 51 insertions, 44 deletions
diff --git a/include/gras/block.hpp b/include/gras/block.hpp
index 644a73c..05809a1 100644
--- a/include/gras/block.hpp
+++ b/include/gras/block.hpp
@@ -339,8 +339,8 @@ struct GRAS_API Block : Element
/*******************************************************************
* private implementation guts for overloads and template support
******************************************************************/
- PMCC _handle_call(const std::string &, const PMCC *);
- virtual PMCC _handle_call_ts(const std::string &, const PMCC *);
+ virtual PMCC _handle_call(const std::string &, const PMCC &);
+ virtual PMCC _handle_call_ts(const std::string &, const PMCC &);
void _post_output_msg(const size_t which_output, const PMCC &msg);
void _post_input_msg(const size_t which_input, const PMCC &msg);
};
diff --git a/include/gras/block.i b/include/gras/block.i
index 2de2c98..2ba9779 100644
--- a/include/gras/block.i
+++ b/include/gras/block.i
@@ -21,19 +21,10 @@
////////////////////////////////////////////////////////////////////////
// Create pythonic gateway to get and set
////////////////////////////////////////////////////////////////////////
-%pythoncode %{
-from PMC import *
-%}
%extend gras::Block
{
%insert("python")
%{
- def set(self, key, value):
- self._set_property(key, PMC_M(value))
-
- def get(self, key):
- return self._get_property(key)()
-
def post_output_msg(self, which_output, value):
if not isinstance(value, PMCC): value = PMC_M(value)
self._post_output_msg(which_output, value)
@@ -41,7 +32,6 @@ from PMC import *
def post_input_msg(self, which_input, value):
if not isinstance(value, PMCC): value = PMC_M(value)
self._post_input_msg(which_input, value)
-
%}
}
diff --git a/include/gras/callable.hpp b/include/gras/callable.hpp
index 2405b9d..ad74dd7 100644
--- a/include/gras/callable.hpp
+++ b/include/gras/callable.hpp
@@ -97,7 +97,7 @@ struct GRAS_API Callable
* Private registration hooks
******************************************************************/
void _register_call(const std::string &, void *);
- virtual PMCC _handle_call(const std::string &, const PMCC *);
+ virtual PMCC _handle_call(const std::string &, const PMCC &);
boost::shared_ptr<void> _call_registry;
};
diff --git a/include/gras/detail/callable.hpp b/include/gras/detail/callable.hpp
index 3f5252d..ddc8f55 100644
--- a/include/gras/detail/callable.hpp
+++ b/include/gras/detail/callable.hpp
@@ -3,6 +3,8 @@
#ifndef INCLUDED_GRAS_DETAIL_CALLABLE_HPP
#define INCLUDED_GRAS_DETAIL_CALLABLE_HPP
+#include <PMC/Containers.hpp> //PMCList
+
namespace gras
{
@@ -13,7 +15,7 @@ struct GRAS_API CallableRegistryEntry
{
CallableRegistryEntry(void);
virtual ~CallableRegistryEntry(void);
- virtual PMCC call(const PMCC *args) = 0;
+ virtual PMCC call(const PMCC &args) = 0;
};
/***********************************************************************
@@ -25,7 +27,7 @@ struct CallableRegistryEntryImpl0 : CallableRegistryEntry
typedef ReturnType(ClassType::*Fcn)(void);
CallableRegistryEntryImpl0(ClassType *obj, Fcn fcn):
_obj(obj), _fcn(fcn){}
- PMCC call(const PMCC *args)
+ PMCC call(const PMCC &)
{
return PMC_M((_obj->*_fcn)());
}
@@ -46,7 +48,7 @@ struct CallableRegistryEntryImplVoid0 : CallableRegistryEntry
typedef void(ClassType::*Fcn)(void);
CallableRegistryEntryImplVoid0(ClassType *obj, Fcn fcn):
_obj(obj), _fcn(fcn){}
- PMCC call(const PMCC *args)
+ PMCC call(const PMCC &)
{
(_obj->*_fcn)(); return PMCC();
}
@@ -70,9 +72,10 @@ struct CallableRegistryEntryImpl1 : CallableRegistryEntry
typedef ReturnType(ClassType::*Fcn)(const Arg0 &);
CallableRegistryEntryImpl1(ClassType *obj, Fcn fcn):
_obj(obj), _fcn(fcn){}
- PMCC call(const PMCC *args)
+ PMCC call(const PMCC &args)
{
- return PMC_M((_obj->*_fcn)(args[0].safe_as<Arg0>()));
+ const PMCList &a = args.as<PMCList>();
+ return PMC_M((_obj->*_fcn)(a[0].safe_as<Arg0>()));
}
ClassType *_obj; Fcn _fcn;
};
@@ -91,9 +94,10 @@ struct CallableRegistryEntryImplVoid1 : CallableRegistryEntry
typedef void(ClassType::*Fcn)(const Arg0 &);
CallableRegistryEntryImplVoid1(ClassType *obj, Fcn fcn):
_obj(obj), _fcn(fcn){}
- PMCC call(const PMCC *args)
+ PMCC call(const PMCC &args)
{
- (_obj->*_fcn)(args[0].safe_as<Arg0>()); return PMCC();
+ const PMCList &a = args.as<PMCList>();
+ (_obj->*_fcn)(a[0].safe_as<Arg0>()); return PMCC();
}
ClassType *_obj; Fcn _fcn;
};
@@ -115,9 +119,10 @@ struct CallableRegistryEntryImpl2 : CallableRegistryEntry
typedef ReturnType(ClassType::*Fcn)(const Arg0 &, const Arg1 &);
CallableRegistryEntryImpl2(ClassType *obj, Fcn fcn):
_obj(obj), _fcn(fcn){}
- PMCC call(const PMCC *args)
+ PMCC call(const PMCC &args)
{
- return PMC_M((_obj->*_fcn)(args[0].safe_as<Arg0>(), args[1].safe_as<Arg1>()));
+ const PMCList &a = args.as<PMCList>();
+ return PMC_M((_obj->*_fcn)(a[0].safe_as<Arg0>(), a[1].safe_as<Arg1>()));
}
ClassType *_obj; Fcn _fcn;
};
@@ -136,9 +141,10 @@ struct CallableRegistryEntryImplVoid2 : CallableRegistryEntry
typedef void(ClassType::*Fcn)(const Arg0 &, const Arg1 &);
CallableRegistryEntryImplVoid2(ClassType *obj, Fcn fcn):
_obj(obj), _fcn(fcn){}
- PMCC call(const PMCC *args)
+ PMCC call(const PMCC &args)
{
- (_obj->*_fcn)(args[0].safe_as<Arg0>(), args[1].safe_as<Arg1>()); return PMCC();
+ const PMCList &a = args.as<PMCList>();
+ (_obj->*_fcn)(a[0].safe_as<Arg0>(), a[1].safe_as<Arg1>()); return PMCC();
}
ClassType *_obj; Fcn _fcn;
};
@@ -160,9 +166,10 @@ struct CallableRegistryEntryImpl3 : CallableRegistryEntry
typedef ReturnType(ClassType::*Fcn)(const Arg0 &, const Arg1 &, const Arg2 &);
CallableRegistryEntryImpl3(ClassType *obj, Fcn fcn):
_obj(obj), _fcn(fcn){}
- PMCC call(const PMCC *args)
+ PMCC call(const PMCC &args)
{
- return PMC_M((_obj->*_fcn)(args[0].safe_as<Arg0>(), args[1].safe_as<Arg1>(), args[2].safe_as<Arg2>()));
+ const PMCList &a = args.as<PMCList>();
+ return PMC_M((_obj->*_fcn)(a[0].safe_as<Arg0>(), a[1].safe_as<Arg1>(), a[2].safe_as<Arg2>()));
}
ClassType *_obj; Fcn _fcn;
};
@@ -181,9 +188,10 @@ struct CallableRegistryEntryImplVoid3 : CallableRegistryEntry
typedef void(ClassType::*Fcn)(const Arg0 &, const Arg1 &, const Arg2 &);
CallableRegistryEntryImplVoid3(ClassType *obj, Fcn fcn):
_obj(obj), _fcn(fcn){}
- PMCC call(const PMCC *args)
+ PMCC call(const PMCC &args)
{
- (_obj->*_fcn)(args[0].safe_as<Arg0>(), args[1].safe_as<Arg1>(), args[2].safe_as<Arg2>()); return PMCC();
+ const PMCList &a = args.as<PMCList>();
+ (_obj->*_fcn)(a[0].safe_as<Arg0>(), a[1].safe_as<Arg1>(), a[2].safe_as<Arg2>()); return PMCC();
}
ClassType *_obj; Fcn _fcn;
};
@@ -202,16 +210,16 @@ void Callable::register_call(const std::string &key, void(ClassType::*fcn)(const
template <typename ReturnType>
ReturnType Callable::x(const std::string &key)
{
- PMCC args[0];
- PMCC r = _handle_call(key, args);
+ PMCList args(0);
+ PMCC r = _handle_call(key, PMC_M(args));
return r.safe_as<ReturnType>();
}
inline
void Callable::x(const std::string &key)
{
- PMCC args[0];
- _handle_call(key, args);
+ PMCList args(0);
+ _handle_call(key, PMC_M(args));
}
/***********************************************************************
@@ -220,18 +228,18 @@ void Callable::x(const std::string &key)
template <typename ReturnType, typename Arg0>
ReturnType Callable::x(const std::string &key, const Arg0 &a0)
{
- PMCC args[1];
+ PMCList args(1);
args[0] = PMC_M(a0);
- PMCC r = _handle_call(key, args);
+ PMCC r = _handle_call(key, PMC_M(args));
return r.safe_as<ReturnType>();
}
template <typename Arg0>
void Callable::x(const std::string &key, const Arg0 &a0)
{
- PMCC args[1];
+ PMCList args(1);
args[0] = PMC_M(a0);
- _handle_call(key, args);
+ _handle_call(key, PMC_M(args));
}
/***********************************************************************
@@ -240,20 +248,20 @@ void Callable::x(const std::string &key, const Arg0 &a0)
template <typename ReturnType, typename Arg0, typename Arg1>
ReturnType Callable::x(const std::string &key, const Arg0 &a0, const Arg1 &a1)
{
- PMCC args[2];
+ PMCList args(2);
args[0] = PMC_M(a0);
args[1] = PMC_M(a1);
- PMCC r = _handle_call(key, args);
+ PMCC r = _handle_call(key, PMC_M(args));
return r.safe_as<ReturnType>();
}
template <typename Arg0, typename Arg1>
void Callable::x(const std::string &key, const Arg0 &a0, const Arg1 &a1)
{
- PMCC args[2];
+ PMCList args(2);
args[0] = PMC_M(a0);
args[1] = PMC_M(a1);
- _handle_call(key, args);
+ _handle_call(key, PMC_M(args));
}
/***********************************************************************
@@ -262,22 +270,22 @@ void Callable::x(const std::string &key, const Arg0 &a0, const Arg1 &a1)
template <typename ReturnType, typename Arg0, typename Arg1, typename Arg2>
ReturnType Callable::x(const std::string &key, const Arg0 &a0, const Arg1 &a1, const Arg2 &a2)
{
- PMCC args[3];
+ PMCList args(3);
args[0] = PMC_M(a0);
args[1] = PMC_M(a1);
args[2] = PMC_M(a2);
- PMCC r = _handle_call(key, args);
+ PMCC r = _handle_call(key, PMC_M(args));
return r.safe_as<ReturnType>();
}
template <typename Arg0, typename Arg1, typename Arg2>
void Callable::x(const std::string &key, const Arg0 &a0, const Arg1 &a1, const Arg2 &a2)
{
- PMCC args[3];
+ PMCList args(3);
args[0] = PMC_M(a0);
args[1] = PMC_M(a1);
args[2] = PMC_M(a2);
- _handle_call(key, args);
+ _handle_call(key, PMC_M(args));
}
} //namespace gras
diff --git a/include/gras/element.i b/include/gras/element.i
index eb4d039..8fbba00 100644
--- a/include/gras/element.i
+++ b/include/gras/element.i
@@ -25,14 +25,23 @@ namespace gras
%include <std_string.i>
%include <gras/gras.hpp>
%include <gras/element.hpp>
+%import <PMC/PMC.i>
////////////////////////////////////////////////////////////////////////
// Operator overloads for Element
////////////////////////////////////////////////////////////////////////
+%pythoncode %{
+from PMC import *
+%}
%extend gras::Element
{
%insert("python")
%{
+ def x(self, key, *args):
+ pmcargs = PMC_M(list(args))
+ pmcret = self._handle_call(key, pmcargs)
+ return pmcret()
+
def __eq__(self, rhs):
if not isinstance(rhs, Element): return False
return self.equals(rhs)