[C++-sig] wrapping boost::optional
Neal D. Becker
ndbecker2 at verizon.net
Tue Jul 27 22:21:43 CEST 2004
I'm trying to wrap boost::optional<T>. I have an adequate solution here:
#include <boost/optional.hpp>
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/class.hpp>
#include <boost/python/init.hpp>
#include <boost/python/operators.hpp>
using namespace boost::python;
template<typename T>
inline void set (boost::optional<T>& o, T x) {
o = x;
}
template<typename T>
inline T get (boost::optional<T>& o) {
return o.get();
}
#define OPTIONAL(T)\
class_<boost::optional<T> >("opt_"#T) \
.def ("set", &set<T>) \
.def ("get", &get<T>) \
.def (!(self)) \
;
BOOST_PYTHON_MODULE(optional_wrap)
{
OPTIONAL(int)
OPTIONAL(double)
}
What would be better, though, is that if the option is not initialized, it
should return None in response to get. That would be more pythonic, no?
Problem is, I don't know how to do that. Any hints?
More information about the Cplusplus-sig
mailing list