Code for pyboost11#

All code is housed in https://github.com/yungyuc/ynote/tree/master/writing/2021/pyboost11/code/.

item.hpp#

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#pragma once

#include <boost/enable_shared_from_this.hpp>
#include <boost/make_shared.hpp>
#include <boost/shared_ptr.hpp>
#include <memory>

struct ItemBpy;

/* To be wrapped by pybind11 */
struct ItemPyb
{
    ItemPyb(int v) : m_value(v) {}
    ItemPyb() = default;
    ItemPyb(ItemPyb const & other) : m_value(other.m_value) {}
    ItemPyb & operator=(ItemPyb const & other) { m_value = other.m_value; return *this; }

    int value() const { return m_value; }
    void set_value(int v) { m_value = v; }

    intptr_t address() const { return reinterpret_cast<intptr_t>(this); }

    void take_pyb(ItemPyb const & other);
    void take_bpy(ItemBpy const & other);

    ItemPyb make_pyb();
    ItemBpy make_bpy();

private:

    int m_value = 0;
};

/* To be wrapped by boost.python */
struct ItemBpy
{
    ItemBpy(int v) : m_value(v) {}
    ItemBpy() = default;
    ItemBpy(ItemBpy const & other) : m_value(other.m_value) {}
    ItemBpy & operator=(ItemBpy const & other) { m_value = other.m_value; return *this; }

    int value() const { return m_value; }
    void set_value(int v) { m_value = v; }

    intptr_t address() const { return reinterpret_cast<intptr_t>(this); }

    void take_pyb(ItemPyb const & other);
    void take_bpy(ItemBpy const & other);

    ItemPyb make_pyb();
    ItemBpy make_bpy();

private:

    int m_value = 0;
};

inline void ItemBpy::take_pyb(ItemPyb const & other) { m_value = other.value(); }
inline void ItemBpy::take_bpy(ItemBpy const & other) { m_value = other.value(); }

inline ItemPyb ItemBpy::make_pyb() { return ItemPyb(value()); }
inline ItemBpy ItemBpy::make_bpy() { return ItemBpy(value()); }

inline void ItemPyb::take_pyb(ItemPyb const & other) { m_value = other.value(); }
inline void ItemPyb::take_bpy(ItemBpy const & other) { m_value = other.value(); }

inline ItemPyb ItemPyb::make_pyb() { return ItemPyb(value()); }
inline ItemBpy ItemPyb::make_bpy() { return ItemBpy(value()); }

struct ItemBpyBS;

/* To be wrapped by pybind11 */
struct ItemPybSS : std::enable_shared_from_this<ItemPybSS>
{
private:

    struct ctor_passkey {};

public:

    template < typename ... Args >
    static std::shared_ptr<ItemPybSS> make(Args && ... args)
    {
        return std::make_shared<ItemPybSS>(std::forward<Args>(args) ..., ctor_passkey());
    }

    ItemPybSS(int v, ctor_passkey const &) : m_value(v) {}
    ItemPybSS() = delete;
    ItemPybSS(ItemPybSS const &) = delete;
    ItemPybSS & operator=(ItemPybSS const &) = delete;

    int value() const { return m_value; }
    void set_value(int v) { m_value = v; }

    intptr_t address() const { return reinterpret_cast<intptr_t>(this); }

    void take_pybss(ItemPybSS const & other);
    void take_bpybs(ItemBpyBS const & other);

    std::shared_ptr<ItemPybSS> make_pybss();
    boost::shared_ptr<ItemBpyBS> make_bpybs();

private:

    int m_value = 0;
};

/* To be wrapped by boost.python */
struct ItemBpyBS : boost::enable_shared_from_this<ItemBpyBS>
{
private:

    struct ctor_passkey {};

public:

    template < typename ... Args >
    static boost::shared_ptr<ItemBpyBS> make(Args && ... args)
    {
        return boost::make_shared<ItemBpyBS>(std::forward<Args>(args) ..., ctor_passkey());
    }

    ItemBpyBS(int v, ctor_passkey const &) : m_value(v) {}
    ItemBpyBS() = delete;
    ItemBpyBS(ItemBpyBS const &) = delete;
    ItemBpyBS & operator=(ItemBpyBS const &) = delete;

    int value() const { return m_value; }
    void set_value(int v) { m_value = v; }

    intptr_t address() const { return reinterpret_cast<intptr_t>(this); }

    void take_pybss(ItemPybSS const & other);
    void take_bpybs(ItemBpyBS const & other);

    std::shared_ptr<ItemPybSS> make_pybss();
    boost::shared_ptr<ItemBpyBS> make_bpybs();

private:

    int m_value = 0;
};

inline void ItemBpyBS::take_pybss(ItemPybSS const & other) { m_value = other.value(); }
inline void ItemBpyBS::take_bpybs(ItemBpyBS const & other) { m_value = other.value(); }

inline std::shared_ptr<ItemPybSS> ItemBpyBS::make_pybss() { return ItemPybSS::make(value()); }
inline boost::shared_ptr<ItemBpyBS> ItemBpyBS::make_bpybs() { return ItemBpyBS::make(value()); }

inline void ItemPybSS::take_pybss(ItemPybSS const & other) { m_value = other.value(); }
inline void ItemPybSS::take_bpybs(ItemBpyBS const & other) { m_value = other.value(); }

inline std::shared_ptr<ItemPybSS> ItemPybSS::make_pybss() { return ItemPybSS::make(value()); }
inline boost::shared_ptr<ItemBpyBS> ItemPybSS::make_bpybs() { return ItemBpyBS::make(value()); }

/* To be wrapped by pybind11 */
struct ContainerPyb
{
    ContainerPyb
    (
        ItemPyb const & pyb, ItemBpy const & bpy
      , std::shared_ptr<ItemPybSS> const & pybss
      , boost::shared_ptr<ItemBpyBS> const & bpybs
    ) : m_pyb(pyb), m_bpy(bpy), m_pybss(pybss), m_bpybs(bpybs) {}

    ItemPyb const & pyb() const { return m_pyb; }
    ItemBpy const & bpy() const { return m_bpy; }

    std::shared_ptr<ItemPybSS> const & pybss() const { return m_pybss; }
    boost::shared_ptr<ItemBpyBS> const & bpybs() const { return m_bpybs; }

    void take_pyb(ItemPyb const & other) { m_pyb.set_value(other.value()); }
    void take_bpy(ItemBpy const & other) { m_bpy.set_value(other.value()); }

    void set_pybss(std::shared_ptr<ItemPybSS> const & other) { m_pybss = other; }
    void set_bpybs(boost::shared_ptr<ItemBpyBS> const & other) { m_bpybs = other; }

    void set_pybss_ref(ItemPybSS & other) { m_pybss = other.shared_from_this(); }
    void set_bpybs_ref(ItemBpyBS & other) { m_bpybs = other.shared_from_this(); }

    std::string overload_pyb(ItemPyb const &) const { return "overload_pyb_noss"; }
    std::string overload_pyb(std::shared_ptr<ItemPybSS> const &) const { return "overload_pyb_ss"; }

    std::string overload_bpy(ItemBpy const &) const { return "overload_bpy_nobs"; }
    std::string overload_bpy(boost::shared_ptr<ItemBpyBS> const &) const { return "overload_bpy_bs"; }

private:

    ItemPyb m_pyb;
    ItemBpy m_bpy;
    std::shared_ptr<ItemPybSS> m_pybss;
    boost::shared_ptr<ItemBpyBS> m_bpybs;
};

/* To be wrapped by boost.python */
struct ContainerBpy
{
    ContainerBpy
    (
        ItemPyb const & pyb, ItemBpy const & bpy
      , std::shared_ptr<ItemPybSS> const & pybss
      , boost::shared_ptr<ItemBpyBS> const & bpybs
    ) : m_pyb(pyb), m_bpy(bpy), m_pybss(pybss), m_bpybs(bpybs) {}

    ItemPyb const & pyb() const { return m_pyb; }
    ItemBpy const & bpy() const { return m_bpy; }

    std::shared_ptr<ItemPybSS> const & pybss() const { return m_pybss; }
    boost::shared_ptr<ItemBpyBS> const & bpybs() const { return m_bpybs; }

    void take_pyb(ItemPyb const & other) { m_pyb.set_value(other.value()); }
    void take_bpy(ItemBpy const & other) { m_bpy.set_value(other.value()); }

    void set_pybss(std::shared_ptr<ItemPybSS> const & other) { m_pybss = other; }
    void set_bpybs(boost::shared_ptr<ItemBpyBS> const & other) { m_bpybs = other; }

    void set_pybss_ref(ItemPybSS & other) { m_pybss = other.shared_from_this(); }
    void set_bpybs_ref(ItemBpyBS & other) { m_bpybs = other.shared_from_this(); }

    std::string overload_pyb(ItemPyb const &) const { return "overload_pyb_noss"; }
    std::string overload_pyb(std::shared_ptr<ItemPybSS> const &) const { return "overload_pyb_ss"; }

    std::string overload_bpy(ItemBpy const &) const { return "overload_bpy_nobs"; }
    std::string overload_bpy(boost::shared_ptr<ItemBpyBS> const &) const { return "overload_bpy_bs"; }

private:

    ItemPyb m_pyb;
    ItemBpy m_bpy;
    std::shared_ptr<ItemPybSS> m_pybss;
    boost::shared_ptr<ItemBpyBS> m_bpybs;
};

// vim: set ff=unix fenc=utf8 et sw=4 ts=4 sts=4:

pyboost11.hpp#

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
 * Copyright (c) 2021, Yung-Yu Chen <yyc@solvcon.net>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice,
 *   this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 * - Neither the name of the copyright holder nor the names of its contributors
 *   may be used to endorse or promote products derived from this software
 *   without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#pragma once

#include <pybind11/pybind11.h>

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
#pragma GCC diagnostic ignored "-Wunused-parameter"
#include <boost/python.hpp>
#pragma GCC diagnostic pop

namespace pyboost11
{

// Pybind11 cast by using boost.python.
template <typename T> struct caster
{

    caster(pybind11::handle src)
      : obj(boost::python::handle<>(boost::python::borrowed(src.ptr())))
      , ext(obj)
    {}

    bool check() const { return ext.check(); }

    // From-Python conversion.
    operator T() { return ext(); }
    T operator()() { return ext(); }

    // To-Python conversion.
    static pybind11::handle to_python(T & src)
    {
        namespace bpy = boost::python;
        return bpy::incref(bpy::object(src).ptr());
    }

    boost::python::object obj;
    boost::python::extract<T> ext;

};

} // end namespace pyboost11

namespace pybind11
{

namespace detail
{

template <typename type> struct pyboost11_type_caster
{

// Expanded from PYBIND11_TYPE_CASTER.
protected:
    type value;
public:
    template <typename T_, enable_if_t<std::is_same<type, remove_cv_t<T_>>::value, int> = 0>
    static handle cast(T_ *src, return_value_policy policy, handle parent) {
        if (!src) return none().release();
        if (policy == return_value_policy::take_ownership) {
            auto h = cast(std::move(*src), policy, parent); delete src; return h;
        } else {
            return cast(*src, policy, parent);
        }
    }
    operator type*() { return &value; }
    operator type&() { return value; }
    operator type&&() && { return std::move(value); }
    template <typename T_> using cast_op_type = pybind11::detail::movable_cast_op_type<T_>;

    // Boilerplate.
    bool load(handle src, bool)
    {
        if (!src)
        {
            return false;
        }
        pyboost11::caster<type> ext(src);
        if (!ext.check())
        {
            return false;
        }
        value = ext();
        return true;
    }
    static handle cast(type * src, return_value_policy /* policy */, handle /* parent */)
    {
        return pyboost11::caster<type>::to_python(src);
    }
    static handle cast(type src, return_value_policy /* policy */, handle /* parent */)
    {
        return pyboost11::caster<type>::to_python(src);
    }

};

#define PYBOOST11_TYPE_CASTER(type, py_name) \
    template <> struct type_caster<type> : public pyboost11_type_caster<type> \
    { static constexpr auto name = py_name; }

} // end namespace detail

} // end namespace pybind11

namespace pyboost11
{

// Boost.python convert by using pybind11.
template <typename T> struct converter
{

public:

    converter() { init(); }

    void init()
    {
        static bool initialized = false;
        if (!initialized)
        {
            namespace bpy = boost::python;
            // From-Python conversion.
            bpy::converter::registry::push_back
            (
                &convertible
              , &construct
              , bpy::type_id<T>()
            );
            // To-Python conversion.
            bpy::to_python_converter<T, converter>();

            initialized = true;
        }
    }

    // From-Python convertibility.
    static void * convertible(PyObject * objptr)
    {
        namespace pyb = pybind11;
        try
        {
            pyb::handle(objptr).cast<T>();
            return objptr;
        }
        catch (pyb::cast_error const &)
        {
            return nullptr;
        }
    }

    // From-Python conversion.
    static void construct
    (
        PyObject * objptr
      , boost::python::converter::rvalue_from_python_stage1_data * data
    )
    {
        namespace pyb = pybind11;
        void * storage = reinterpret_cast
        <
            boost::python::converter::rvalue_from_python_storage<T> *
        >(data)->storage.bytes;
        new (storage) T(pyb::handle(objptr).cast<T>());
        data->convertible = storage;
    }

    // To-Python conversion.
    static PyObject * convert(T const & t)
    {
        return pybind11::cast(t).inc_ref().ptr();
    }

};

} // end namespace pyboost11

// vim: set ff=unix fenc=utf8 et sw=4 ts=4 sts=4:

modpyb.cpp#

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "pyboost11.hpp"

#include "item.hpp"

namespace pybind11
{

namespace detail
{

PYBOOST11_TYPE_CASTER(ItemBpy, _("ItemBpy"));
PYBOOST11_TYPE_CASTER(boost::shared_ptr<ItemBpyBS>, _("ItemBpyBS"));

} // end namespace detail

} // end namespace pybind11

PYBIND11_MODULE(modpyb, mod)
{

    namespace pyb = pybind11;

    mod.doc() = "extension module by pybind11";

    pyb::class_<ItemPyb>(mod, "ItemPyb")
        .def(pyb::init<>())
        .def(pyb::init<int>())
        .def_property("value", &ItemPyb::value, &ItemPyb::set_value)
        .def_property_readonly("address", &ItemPyb::address)
        .def("take_pyb", &ItemPyb::take_pyb)
        .def("take_bpy", &ItemPyb::take_bpy)
        .def("make_pyb", &ItemPyb::make_pyb)
        .def("make_bpy", &ItemPyb::make_bpy)
    ;

    pyb::class_<ItemPybSS, std::shared_ptr<ItemPybSS>>(mod, "ItemPybSS")
        .def(pyb::init([](int v) { return ItemPybSS::make(v); }))
        .def_property("value", &ItemPybSS::value, &ItemPybSS::set_value)
        .def_property_readonly("address", &ItemPybSS::address)
        .def("take_pybss", &ItemPybSS::take_pybss)
        .def("take_bpybs", &ItemPybSS::take_bpybs)
        .def("make_pybss", &ItemPybSS::make_pybss)
        .def("make_bpybs", &ItemPybSS::make_bpybs)
    ;

    pyb::class_<ContainerPyb>(mod, "ContainerPyb")
        .def
        (
            pyb::init
            <
                ItemPyb const &, ItemBpy const &
              , std::shared_ptr<ItemPybSS> const &
              , boost::shared_ptr<ItemBpyBS> const &
            >()
        )
        .def_property_readonly("pyb", &ContainerPyb::pyb)
        .def_property_readonly("bpy", &ContainerPyb::bpy)
        .def_property_readonly("pybss", &ContainerPyb::pybss)
        .def_property_readonly("bpybs", &ContainerPyb::bpybs)
        .def("take_pyb", &ContainerPyb::take_pyb)
        .def("take_bpy", &ContainerPyb::take_bpy)
        .def("set_pybss", &ContainerPyb::set_pybss)
        .def("set_bpybs", &ContainerPyb::set_bpybs)
        .def("set_pybss_ref", &ContainerPyb::set_pybss_ref)
        .def
        (
            "set_bpybs_ref"
            // FIXME: How to automatically recognize ItemBpyBS &?
          , [](ContainerPyb & self, boost::shared_ptr<ItemBpyBS> const & other)
            { self.set_bpybs_ref(*other); }
        )
        // Overload without argument name.
        .def
        (
            "overload_pyb"
          , static_cast<std::string (ContainerPyb::*)(ItemPyb const &) const>
            (&ContainerPyb::overload_pyb)
        )
        .def
        (
            "overload_pyb"
          , static_cast<std::string (ContainerPyb::*)(std::shared_ptr<ItemPybSS> const &) const>
            (&ContainerPyb::overload_pyb)
        )
        .def
        (
            "overload_bpy"
          , static_cast<std::string (ContainerPyb::*)(ItemBpy const &) const>
            (&ContainerPyb::overload_bpy)
        )
        .def
        (
            "overload_bpy"
          , static_cast<std::string (ContainerPyb::*)(boost::shared_ptr<ItemBpyBS> const &) const>
            (&ContainerPyb::overload_bpy)
        )
        // Overload with argument name.
        .def
        (
            "overload2_pyb"
          , static_cast<std::string (ContainerPyb::*)(ItemPyb const &) const>
            (&ContainerPyb::overload_pyb)
          , pyb::arg("pyb")
        )
        .def
        (
            "overload2_pyb"
          , static_cast<std::string (ContainerPyb::*)(std::shared_ptr<ItemPybSS> const &) const>
            (&ContainerPyb::overload_pyb)
          , pyb::arg("pybss")
        )
        .def
        (
            "overload2_bpy"
          , static_cast<std::string (ContainerPyb::*)(ItemBpy const &) const>
            (&ContainerPyb::overload_bpy)
          , pyb::arg("bpy")
        )
        .def
        (
            "overload2_bpy"
          , static_cast<std::string (ContainerPyb::*)(boost::shared_ptr<ItemBpyBS> const &) const>
            (&ContainerPyb::overload_bpy)
          , pyb::arg("bpybs")
        )
    ;

}

// vim: set ff=unix fenc=utf8 et sw=4 ts=4 sts=4:

modbpy.cpp#

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "pyboost11.hpp"

#include "item.hpp"

boost::shared_ptr<ItemBpyBS> make_bpybs(int v)
{
    return ItemBpyBS::make(v);
}

void set_pybss_ref(ContainerBpy & self, std::shared_ptr<ItemPybSS> const & other)
{
    self.set_pybss_ref(*other);
}

BOOST_PYTHON_MODULE(modbpy)
{

    namespace bpy = boost::python;

    pyboost11::converter<ItemPyb>();
    pyboost11::converter<std::shared_ptr<ItemPybSS>>();

    bpy::class_<ItemBpy>("ItemBpy")
        .def(bpy::init<>())
        .def(bpy::init<int>())
        .add_property("value", &ItemBpy::value, &ItemBpy::set_value)
        .add_property("address", &ItemBpy::address)
        .def("take_pyb", &ItemBpy::take_pyb)
        .def("take_bpy", &ItemBpy::take_bpy)
        .def("make_pyb", &ItemBpy::make_pyb)
        .def("make_bpy", &ItemBpy::make_bpy)
    ;

    bpy::class_<ItemBpyBS, boost::shared_ptr<ItemBpyBS>, boost::noncopyable>("ItemBpyBS", bpy::no_init)
        .def("__init__", bpy::make_constructor(&make_bpybs))
        .add_property("value", &ItemBpyBS::value, &ItemBpyBS::set_value)
        .add_property("address", &ItemBpyBS::address)
        .def("take_pybss", &ItemBpyBS::take_pybss)
        .def("take_bpybs", &ItemBpyBS::take_bpybs)
        .def("make_pybss", &ItemBpyBS::make_pybss)
        .def("make_bpybs", &ItemBpyBS::make_bpybs)
    ;

    bpy::class_<ContainerBpy>("ContainerBpy", bpy::no_init)
        .def
        (
            bpy::init
            <
                ItemPyb const &, ItemBpy const &
              , std::shared_ptr<ItemPybSS> const &
              , boost::shared_ptr<ItemBpyBS> const &
            >()
        )
        .add_property
        (
            "pyb"
          , bpy::make_function
            (
                &ContainerBpy::pyb
              , bpy::return_value_policy<bpy::copy_const_reference>()
            )
        )
        .add_property
        (
            "bpy"
          , bpy::make_function
            (
                &ContainerBpy::bpy
              , bpy::return_value_policy<bpy::copy_const_reference>()
            )
        )
        .add_property
        (
            "pybss"
          , bpy::make_function
            (
                &ContainerBpy::pybss
              , bpy::return_value_policy<bpy::copy_const_reference>()
            )
        )
        .add_property
        (
            "bpybs"
          , bpy::make_function
            (
                &ContainerBpy::bpybs
              , bpy::return_value_policy<bpy::copy_const_reference>()
            )
        )
        .def("take_pyb", &ContainerBpy::take_pyb)
        .def("take_bpy", &ContainerBpy::take_bpy)
        .def("set_pybss", &ContainerBpy::set_pybss)
        .def("set_bpybs", &ContainerBpy::set_bpybs)
        // FIXME: How to automatically recognize ItemPybSS &?
        .def("set_pybss_ref", &set_pybss_ref)
        .def("set_bpybs_ref", &ContainerBpy::set_bpybs_ref)
        // Overload with argument name.
        .def
        (
            "overload_pyb"
          , static_cast<std::string (ContainerBpy::*)(ItemPyb const &) const>
            (&ContainerBpy::overload_pyb)
        )
        .def
        (
            "overload_pyb"
          , static_cast<std::string (ContainerBpy::*)(std::shared_ptr<ItemPybSS> const &) const>
            (&ContainerBpy::overload_pyb)
        )
        .def
        (
            "overload_bpy"
          , static_cast<std::string (ContainerBpy::*)(ItemBpy const &) const>
            (&ContainerBpy::overload_bpy)
        )
        .def
        (
            "overload_bpy"
          , static_cast<std::string (ContainerBpy::*)(boost::shared_ptr<ItemBpyBS> const &) const>
            (&ContainerBpy::overload_bpy)
        )
        // Overload with argument name.
        .def
        (
            "overload2_pyb"
          , static_cast<std::string (ContainerBpy::*)(ItemPyb const &) const>
            (&ContainerBpy::overload_pyb)
          , bpy::arg("pyb")
        )
        .def
        (
            "overload2_pyb"
          , static_cast<std::string (ContainerBpy::*)(std::shared_ptr<ItemPybSS> const &) const>
            (&ContainerBpy::overload_pyb)
          , bpy::arg("pybss")
        )
        .def
        (
            "overload2_bpy"
          , static_cast<std::string (ContainerBpy::*)(ItemBpy const &) const>
            (&ContainerBpy::overload_bpy)
          , bpy::arg("bpy")
        )
        .def
        (
            "overload2_bpy"
          , static_cast<std::string (ContainerBpy::*)(boost::shared_ptr<ItemBpyBS> const &) const>
            (&ContainerBpy::overload_bpy)
          , bpy::arg("bpybs")
        )
    ;

}

// vim: set ff=unix fenc=utf8 et sw=4 ts=4 sts=4:

test_run.py#

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import unittest

import modpyb
import modbpy


class ItemTest(unittest.TestCase):

    def setUp(self):

        self.ipyb = modpyb.ItemPyb()
        self.ibpy = modbpy.ItemBpy()

    def test_pyb(self):

        # pyb
        self.ipyb.value = 10
        self.ipyb.take_pyb(modpyb.ItemPyb(11))
        self.assertIsInstance(self.ipyb.make_pyb(), modpyb.ItemPyb)
        self.assertEqual(11, self.ipyb.make_pyb().value)

        # bpy
        self.ipyb.value = 10
        self.ipyb.take_bpy(modbpy.ItemBpy(11))
        self.assertIsInstance(self.ipyb.make_bpy(), modbpy.ItemBpy)
        self.assertEqual(11, self.ipyb.value)

    def test_bpy(self):

        # pyb
        self.ibpy.value = 10
        self.ibpy.take_pyb(modpyb.ItemPyb(11))
        self.assertIsInstance(self.ibpy.make_pyb(), modpyb.ItemPyb)
        self.assertEqual(11, self.ibpy.make_pyb().value)

        # bpy
        self.ibpy.value = 10
        self.ibpy.take_bpy(modbpy.ItemBpy(11))
        self.assertIsInstance(self.ibpy.make_bpy(), modbpy.ItemBpy)
        self.assertEqual(11, self.ibpy.make_bpy().value)


class ContainerTest(unittest.TestCase):

    def test_container_pyb(self):

        pyb = modpyb.ItemPyb(100)
        bpy = modbpy.ItemBpy(101)
        pybss = modpyb.ItemPybSS(102)
        bpybs = modbpy.ItemBpyBS(103)
        cpyb = modpyb.ContainerPyb(pyb, bpy, pybss, bpybs)

        self.assertEqual(100, cpyb.pyb.value)
        self.assertEqual(101, cpyb.bpy.value)
        self.assertEqual(102, cpyb.pybss.value)
        self.assertEqual(103, cpyb.bpybs.value)

        self.assertNotEqual(pyb.address, cpyb.pyb.address)
        self.assertNotEqual(bpy.address, cpyb.bpy.address)
        self.assertEqual(pybss.address, cpyb.pybss.address)
        self.assertEqual(bpybs.address, cpyb.bpybs.address)

        cpyb.take_pyb(modpyb.ItemPyb(200))
        cpyb.take_bpy(modbpy.ItemBpy(201))

        pybss2 = modpyb.ItemPybSS(202)
        bpybs2 = modbpy.ItemBpyBS(203)
        cpyb.set_pybss(pybss2)
        cpyb.set_bpybs(bpybs2)

        self.assertEqual(200, cpyb.pyb.value)
        self.assertEqual(201, cpyb.bpy.value)
        self.assertEqual(202, cpyb.pybss.value)
        self.assertEqual(203, cpyb.bpybs.value)

        self.assertNotEqual(pyb.address, cpyb.pyb.address)
        self.assertNotEqual(bpy.address, cpyb.bpy.address)
        self.assertNotEqual(pybss.address, cpyb.pybss.address)
        self.assertNotEqual(bpybs.address, cpyb.bpybs.address)
        self.assertEqual(pybss2.address, cpyb.pybss.address)
        self.assertEqual(bpybs2.address, cpyb.bpybs.address)

        cpyb.set_pybss_ref(pybss)
        cpyb.set_bpybs_ref(bpybs)

        self.assertEqual(pybss.address, cpyb.pybss.address)
        self.assertEqual(bpybs.address, cpyb.bpybs.address)
        self.assertNotEqual(pybss2.address, cpyb.pybss.address)
        self.assertNotEqual(bpybs2.address, cpyb.bpybs.address)

        self.assertEqual("overload_pyb_noss", cpyb.overload_pyb(pyb))
        self.assertEqual("overload_pyb_ss", cpyb.overload_pyb(pybss))
        self.assertEqual("overload_bpy_nobs", cpyb.overload_bpy(bpy))
        self.assertEqual("overload_bpy_bs", cpyb.overload_bpy(bpybs))
        with self.assertRaises(TypeError):
            cpyb.overload_pyb(pyb=pyb)
        with self.assertRaises(TypeError):
            cpyb.overload_pyb(pybss=pybss)
        with self.assertRaises(TypeError):
            cpyb.overload_bpy(bpy=bpy)
        with self.assertRaises(TypeError):
            cpyb.overload_bpy(bpybs=bpybs)

        self.assertEqual("overload_pyb_noss", cpyb.overload2_pyb(pyb))
        self.assertEqual("overload_pyb_ss", cpyb.overload2_pyb(pybss))
        self.assertEqual("overload_bpy_nobs", cpyb.overload2_bpy(bpy))
        self.assertEqual("overload_bpy_bs", cpyb.overload2_bpy(bpybs))
        self.assertEqual("overload_pyb_noss", cpyb.overload2_pyb(pyb=pyb))
        self.assertEqual("overload_pyb_ss", cpyb.overload2_pyb(pybss=pybss))
        self.assertEqual("overload_bpy_nobs", cpyb.overload2_bpy(bpy=bpy))
        self.assertEqual("overload_bpy_bs", cpyb.overload2_bpy(bpybs=bpybs))

    def test_container_bpy(self):

        pyb = modpyb.ItemPyb(100)
        bpy = modbpy.ItemBpy(101)
        pybss = modpyb.ItemPybSS(102)
        bpybs = modbpy.ItemBpyBS(103)
        cbpy = modbpy.ContainerBpy(pyb, bpy, pybss, bpybs)

        self.assertEqual(100, cbpy.pyb.value)
        self.assertEqual(101, cbpy.bpy.value)
        self.assertEqual(102, cbpy.pybss.value)
        self.assertEqual(103, cbpy.bpybs.value)

        self.assertNotEqual(pyb.address, cbpy.pyb.address)
        self.assertNotEqual(bpy.address, cbpy.bpy.address)
        self.assertEqual(pybss.address, cbpy.pybss.address)
        self.assertEqual(bpybs.address, cbpy.bpybs.address)

        cbpy.take_pyb(modpyb.ItemPyb(200))
        cbpy.take_bpy(modbpy.ItemBpy(201))

        pybss2 = modpyb.ItemPybSS(202)
        bpybs2 = modbpy.ItemBpyBS(203)
        cbpy.set_pybss(pybss2)
        cbpy.set_bpybs(bpybs2)

        self.assertEqual(200, cbpy.pyb.value)
        self.assertEqual(201, cbpy.bpy.value)
        self.assertEqual(202, cbpy.pybss.value)
        self.assertEqual(203, cbpy.bpybs.value)

        self.assertNotEqual(pyb.address, cbpy.pyb.address)
        self.assertNotEqual(bpy.address, cbpy.bpy.address)
        self.assertNotEqual(pybss.address, cbpy.pybss.address)
        self.assertNotEqual(bpybs.address, cbpy.bpybs.address)
        self.assertEqual(pybss2.address, cbpy.pybss.address)
        self.assertEqual(bpybs2.address, cbpy.bpybs.address)

        cbpy.set_pybss_ref(pybss)
        cbpy.set_bpybs_ref(bpybs)

        self.assertEqual(pybss.address, cbpy.pybss.address)
        self.assertEqual(bpybs.address, cbpy.bpybs.address)
        self.assertNotEqual(pybss2.address, cbpy.pybss.address)
        self.assertNotEqual(bpybs2.address, cbpy.bpybs.address)

        self.assertEqual("overload_pyb_noss", cbpy.overload_pyb(pyb))
        self.assertEqual("overload_pyb_ss", cbpy.overload_pyb(pybss))
        self.assertEqual("overload_bpy_nobs", cbpy.overload_bpy(bpy))
        self.assertEqual("overload_bpy_bs", cbpy.overload_bpy(bpybs))
        with self.assertRaisesRegex(Exception, "Python argument types in"):
            cbpy.overload_pyb(pyb=pyb)
        with self.assertRaisesRegex(Exception, "Python argument types in"):
            cbpy.overload_pyb(pybss=pybss)
        with self.assertRaisesRegex(Exception, "Python argument types in"):
            cbpy.overload_bpy(bpy=bpy)
        with self.assertRaisesRegex(Exception, "Python argument types in"):
            cbpy.overload_bpy(bpybs=bpybs)

        self.assertEqual("overload_pyb_noss", cbpy.overload2_pyb(pyb))
        self.assertEqual("overload_pyb_ss", cbpy.overload2_pyb(pybss))
        self.assertEqual("overload_bpy_nobs", cbpy.overload2_bpy(bpy))
        self.assertEqual("overload_bpy_bs", cbpy.overload2_bpy(bpybs))
        self.assertEqual("overload_pyb_noss", cbpy.overload2_pyb(pyb=pyb))
        self.assertEqual("overload_pyb_ss", cbpy.overload2_pyb(pybss=pybss))
        self.assertEqual("overload_bpy_nobs", cbpy.overload2_bpy(bpy=bpy))
        self.assertEqual("overload_bpy_bs", cbpy.overload2_bpy(bpybs=bpybs))

# vim: set fenc=utf8 ff=unix et sw=4 ts=4 sts=4:

Makefile#

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
FLAGS ?=
VERBOSE ?=

pyextsuffix := $(shell python3-config --extension-suffix)

HEADERS := item.hpp pyboost11.hpp

.PHONY: default
default: modpyb$(pyextsuffix) modbpy$(pyextsuffix)

modpyb$(pyextsuffix): modpyb.cpp $(HEADERS) CMakeLists.txt
	mkdir -p build ; \
	cd build ; \
	cmake .. -DCMAKE_BUILD_TYPE=Release
	env VERBOSE=$(VERBOSE) make -C build modpyb_copy

modbpy$(pyextsuffix): modbpy.cpp $(HEADERS) CMakeLists.txt
	mkdir -p build ; \
	cd build ; \
	cmake .. -DCMAKE_BUILD_TYPE=Release
	env VERBOSE=$(VERBOSE) make -C build modbpy_copy

%: %.cpp Makefile
	${CXX} ${CXXFLAGS} -o $@ $<

ALL_FILES := Makefile CMakeLists.txt ${HEADERS} modpyb.cpp modbpy.cpp *.py

.PHONY: tar
tar: ${ALL_FILES}
	tar cfz pyboost11_code.tar.gz ${ALL_FILES}

.PHONY: test
test: modpyb$(pyextsuffix) modbpy$(pyextsuffix)
	env PYTHON_PATH=.:${PYTHON_PATH} python3 -m pytest -v -s

.PHONY: clean
clean:
	rm -rf __pycache__ *.pyc *.pyo build *$(pyextsuffix)

CMakeLists.txt#

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
cmake_minimum_required(VERSION 3.9)
project(pyboost11)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake/")

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(BPYVER "python${Python_VERSION_MAJOR}${Python_VERSION_MINOR}")

set(PYBOOST11_CXX_FLAGS
  "-g -Werror -Wall -Wextra -Wno-unused-value -Wno-noexcept-type")
set(CMAKE_CXX_FLAGS "${PYBOOST11_CXX_FLAGS} ${CMAKE_CXX_FLAGS}")

find_package(pybind11 REQUIRED)
find_package(Python3 COMPONENTS Interpreter Development REQUIRED)
find_package(Boost COMPONENTS ${BPYVER} REQUIRED)

################################################################################
# pybind11 "modpyb"
################################################################################

include_directories(${pybind11_INCLUDE_DIRS})

pybind11_add_module(
    modpyb
    MODULE
    modpyb.cpp
)
target_link_libraries(modpyb PRIVATE pybind11::module ${Boost_LIBRARIES})
set_target_properties(modpyb PROPERTIES CXX_VISIBILITY_PRESET "default")

add_custom_target(modpyb_copy
    COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:modpyb> ${PROJECT_SOURCE_DIR}
    DEPENDS modpyb)

################################################################################
# boost.python "modbpy"
################################################################################

add_library(modbpy MODULE modbpy.cpp)
target_link_libraries(modbpy ${Boost_LIBRARIES} ${PYTHON_LIBRARIES})
target_include_directories(modbpy PRIVATE ${PYTHON_INCLUDE_DIRS})
set_target_properties(modbpy PROPERTIES
  CXX_VISIBILITY_PRESET "default"
  PREFIX ""
  SUFFIX ".${Python3_SOABI}.so")

add_custom_target(modbpy_copy
    COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:modbpy> ${PROJECT_SOURCE_DIR}
    DEPENDS modbpy)

# vim: set fenc=utf8 ff=unix et sw=2 ts=2 sts=2: