diff options
author | astrojhgu <astrojhgu@ed2142bd-67ad-457f-ba7c-d818d4011675> | 2011-02-07 18:10:20 +0000 |
---|---|---|
committer | astrojhgu <astrojhgu@ed2142bd-67ad-457f-ba7c-d818d4011675> | 2011-02-07 18:10:20 +0000 |
commit | 42dafd9fa725a232be82f15a53e5ae0520d7bfc1 (patch) | |
tree | a803e7d11d1506a29e2ced338f5f0da83adf56ad /interface | |
parent | 2eec1bc95e708a280cc4f6e7a912b7fa90131c44 (diff) | |
download | opt-utilities-42dafd9fa725a232be82f15a53e5ae0520d7bfc1.tar.bz2 |
git-svn-id: file:///home/svn/opt_utilities@172 ed2142bd-67ad-457f-ba7c-d818d4011675
Diffstat (limited to 'interface')
-rw-r--r-- | interface/pymodel.hpp | 46 |
1 files changed, 45 insertions, 1 deletions
diff --git a/interface/pymodel.hpp b/interface/pymodel.hpp index 8a55839..8165d6d 100644 --- a/interface/pymodel.hpp +++ b/interface/pymodel.hpp @@ -16,6 +16,50 @@ namespace opt_utilities { + template<typename T> + boost::python::object convert_to_object(const T& x) + { + boost::python::list result; + for(size_t i=0;i<get_size(x);++i) + { + result.append(get_element(x,i)); + } + return result; + } + + static inline boost::python::object convert_to_object(double x) + { + return boost::python::object(x); + } + + static inline boost::python::object convert_to_object(float x) + { + return boost::python::object(x); + } + + + template <typename T> + T convert_from_object(const boost::python::object& o,const T&) + { + T result(boost::python::len(o)); + for(int i=0;i<get_size(result);++i) + { + result[i]=boost::python::extract<typename element_type_trait<T>::element_type>(o[i]); + } + return result; + } + + static inline double convert_from_object(const boost::python::object& o,double) + { + return boost::python::extract<double>(o); + } + + static inline double convert_from_object(const boost::python::object& o,float) + { + return boost::python::extract<double>(o); + } + + template <typename Ty,typename Tx,typename Tp> class pymodel :public model<Ty,Tx,Tp,std::string> @@ -100,7 +144,7 @@ namespace opt_utilities { args.append(get_element(p,i)); } - return boost::python::extract<Ty>(pyfunc(x,args)); + return convert_from_object(pyfunc(convert_to_object(x),args),x); } const char* do_get_type_name()const |