diff options
author | astrojhgu <astrojhgu@ed2142bd-67ad-457f-ba7c-d818d4011675> | 2010-12-26 08:10:51 +0000 |
---|---|---|
committer | astrojhgu <astrojhgu@ed2142bd-67ad-457f-ba7c-d818d4011675> | 2010-12-26 08:10:51 +0000 |
commit | 2b4f7ecfa282e5bc3dcc893b25e47151f4dbdc8a (patch) | |
tree | e10594f50303018ebfccfbf1e3d94ed4764a9bc4 /interface/pyfunc_obj.hpp | |
parent | f3019d095d5f0b6c67de18124cebe93fd9eb5a69 (diff) | |
download | opt-utilities-2b4f7ecfa282e5bc3dcc893b25e47151f4dbdc8a.tar.bz2 |
git-svn-id: file:///home/svn/opt_utilities@150 ed2142bd-67ad-457f-ba7c-d818d4011675
Diffstat (limited to 'interface/pyfunc_obj.hpp')
-rw-r--r-- | interface/pyfunc_obj.hpp | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/interface/pyfunc_obj.hpp b/interface/pyfunc_obj.hpp new file mode 100644 index 0000000..db2775a --- /dev/null +++ b/interface/pyfunc_obj.hpp @@ -0,0 +1,71 @@ +#ifndef PYFUNC_OBJ_HPP +#define PYFUNC_OBJ_HPP +#include <boost/python.hpp> +#include <core/optimizer.hpp> +#include <core/opt_traits.hpp> + + +namespace opt_utilities +{ + + template <typename Ty,typename Tx> + class pyfunc_obj + :public func_obj<Ty,Tx> + { + private: + boost::python::object pyfunc; + public: + pyfunc_obj() + { + if(!Py_IsInitialized()) + { + Py_Initialize(); + } + } + + pyfunc_obj(const pyfunc_obj& rhs) + :pyfunc(rhs.pyfunc) + {} + + pyfunc_obj& operator=(const pyfunc_obj& rhs) + { + pyfunc=rhs.pyfunc; + } + + ~pyfunc_obj() + {} + + public: + void attach(const std::string module_name, + const std::string func_name) + { + boost::python::object mod(boost::python::import(module_name.c_str())); + pyfunc=mod.attr(func_name.c_str()); + + } + private: + func_obj<Ty,Tx>* do_clone()const + { + return new pyfunc_obj(*this); + } + + void do_destroy() + { + delete this; + } + + Ty do_eval(const Tx& x) + { + boost::python::list args; + for(size_t i=0;i<get_size(x);++i) + { + args.append(get_element(x,i)); + } + return boost::python::extract<Ty>(pyfunc(args)); + } + }; +} + + +#endif +//EOF |