On 29.01.2016 13:15, Liam Herron wrote:
>
> For the following code:
>
> -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
>
> #include <boost/python.hpp>
>
> using namespace boost::python;
>
>  
>
> namespace // unnamed
>
> {
>
>  
>
> class NullableDouble
>
> {
>
> public:
>
>    NullableDouble()
>
>       : isNull_(true)
>
>       , value_(0.0)
>
>    {
>
>    }
>
>  
>
>    NullableDouble(double x)
>
>       : isNull_(false)
>
>       , value_(x)
>
>    {
>
>    }
>
>  
>
>    double value() const
>
>    {
>
>       return value_;  // null check not relevant to this example
>
>    }
>
>  
>
>    bool isNull() const
>
>    {
>
>       return isNull_;
>
>    }
>
>  
>
>    // ... more functions but not needed for this example
>
>  
>
> private:
>
>    bool isNull_;
>
>    double value_;
>
> };
>
>  
>
> } // end namespace unnamed
>
>  
>
> void init_pnic()
>
> {
>
>    class_<NullableDouble>
>
>    ("NullableDouble", "NullableDouble", init<>())
>
>       .def(init<double>())
>
>       .def("value", &NullableDouble::value)
>
>       .def("isNull", &NullableDouble::isNull)
>
>    ;
>
> }
>
>  
>
> BOOST_PYTHON_MODULE(_testPNIC)
>
> {
>
>    init_pnic();
>
> }
>
> -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
>
>  
>
> I would like to have another constructor method in python that takes a
> ‘None’ and returns the same as the no arg constructor ‘NullableDouble()’
>

Just create a 'factory' function that takes a Python object (and which
you then check for None) to instantiate and return a "null". Add that
function to your wrapper like any other method (using '__init__' and
'make_constructor').

HTH,
        Stefan




>  
>
> What is the best way to do this?
>
>  
>
> Thanks,
>
> --Liam
>
>  
>
>  
>

-- 

      ...ich hab' noch einen Koffer in Berlin...

_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig@python.org
https://mail.python.org/mailman/listinfo/cplusplus-sig

Reply via email to