File: Value.cpp

package info (click to toggle)
odil 0.13.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,476 kB
  • sloc: cpp: 55,982; python: 3,947; javascript: 460; xml: 182; makefile: 99; sh: 36
file content (378 lines) | stat: -rw-r--r-- 12,131 bytes parent folder | download | duplicates (3)
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*************************************************************************
 * odil - Copyright (C) Universite de Strasbourg
 * Distributed under the terms of the CeCILL-B license, as published by
 * the CEA-CNRS-INRIA. Refer to the LICENSE file or to
 * http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
 * for details.
 ************************************************************************/

#include <algorithm>

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/stl_bind.h>

#include "odil/DataSet.h"
#include "odil/Value.h"

#include "opaque_types.h"
#include "type_casters.h"
#include "Value.h"

namespace odil
{

namespace wrappers
{

GetItem
::GetItem(std::size_t index)
: index(index)
{
    // Nothing else.
}
    
GetItem::result_type
GetItem
::operator()(odil::Value::Strings const & value) const
{
    return pybind11::bytes(value[this->index]).cast<pybind11::object>();
}

GetSlice
::GetSlice(std::size_t size, pybind11::slice slice)
{
    // WARNING: pybind11 <= 2.2.4 uses the unsigned size_t, not the signed 
    // ssize_t. Use the Python API directly.
    PySlice_GetIndicesEx(
        slice.ptr(), size, 
        &this->start, &this->stop, &this->step, &this->slice_length);
}

GetSlice::result_type
GetSlice
::operator()(odil::Value::Strings const & value) const
{
    result_type result(this->slice_length);
    std::size_t d = 0;
    for(ssize_t s = this->start; s != this->stop; s += this->step) 
    { 
        result[d++] = pybind11::bytes(value[s]);
    }
    return result;
}

SetItem
::SetItem(std::size_t index, pybind11::object item)
: index(index), item(item)
{
    // Nothing else.
}

Iterate::result_type
Iterate
::operator()(odil::Value::Strings const & value) const
{
    return pybind11::make_iterator<
            pybind11::return_value_policy::reference_internal,
            decltype(value.begin()), decltype(value.end()), pybind11::bytes
        >(value.begin(), value.end());
}

Append
::Append(pybind11::object item)
: item(item) 
{
    // Nothing else.
}

Pickle
::Pickle(odil::Value::Type type)
:type(type)
{
    // Nothing else
}

}

}

void wrap_Value(pybind11::module & m)
{
    using namespace pybind11;
    using namespace odil;
    using namespace odil::wrappers;

    class_<Value> value(m, "Value");
    value
        .def(init<Value::Integers>())
        .def(init<Value::Reals>())
        .def(init<Value::Strings>())
        .def(init<Value::DataSets>())
        .def(init<Value::Binary>())
        // Explicit constructor since Value::XXX are opaque
        // WARNING: define *after* other constructors for correct priority
        .def(
            init([](sequence & source) {
                return convert_sequence<Value>(source);
            }))
        .def("get_type", &Value::get_type)
        .def("empty", &Value::empty)
        .def("size", &Value::size)
        .def(
            "as_integers", (Value::Integers & (Value::*)()) &Value::as_integers,
            return_value_policy::reference_internal)
        .def(
            "as_reals", (Value::Reals & (Value::*)()) &Value::as_reals,
            return_value_policy::reference_internal)
        .def(
            "as_strings", (Value::Strings & (Value::*)()) &Value::as_strings,
            return_value_policy::reference_internal)
        .def(
            "as_data_sets", (Value::DataSets & (Value::*)()) &Value::as_data_sets,
            return_value_policy::reference_internal)
        .def(
            "as_binary", (Value::Binary & (Value::*)()) &Value::as_binary,
            return_value_policy::reference_internal)
        .def(self == self)
        .def(self != self)
        .def("clear", &Value::clear)
        .def("__len__", &Value::size)
        .def(
            "__getitem__", [](Value const & self, ssize_t index) {
                if(index < 0)
                {
                    index += self.size();
                }
                
                if(index >= self.size())
                {
                    throw std::out_of_range("list index out of range");
                }
                
                return apply_visitor(GetItem(index), self);
            })
        .def(
            "__getitem__", [](Value const & self, slice slice_) {
                return apply_visitor(GetSlice(self.size(), slice_), self);
            })
        .def(
            "__setitem__", [](Value & self, ssize_t index, object item) {
                if(index < 0)
                {
                    index += self.size();
                }
                
                if(index >= self.size())
                {
                    throw std::out_of_range("list index out of range");
                }
                
                return apply_visitor(SetItem(index, item), self);
            })
        .def(
            "__iter__", 
            [](Value const & self) 
            { 
                return apply_visitor(Iterate(), self);
            })
        .def(
            "append", [](Value & self, object item) {
                return apply_visitor(Append(item), self);
            })
        .def_property_readonly("type", &Value::get_type)
        .def(pickle(
            [](Value const & value) -> tuple {
                return apply_visitor(Pickle(value.get_type()), value);
            },
            [](tuple pickled) {
                auto const type = pickled[0].cast<Value::Type>();
                if(type == Value::Type::Integers)
                {
                    return Value(pickled[1].cast<Value::Integers>());
                }
                else if(type == Value::Type::Reals)
                {
                    return Value(pickled[1].cast<Value::Reals>());
                }
                else if(type == Value::Type::Strings)
                {
                    return Value(pickled[1].cast<Value::Strings>());
                }
                else if(type == Value::Type::DataSets)
                {
                    return Value(pickled[1].cast<Value::DataSets>());
                }
                else if(type == Value::Type::Binary)
                {
                    return Value(pickled[1].cast<Value::Binary>());
                }
                else
                {
                    throw Exception("Value: invalid pickled state");
                }
            }
        ))
    ;

    enum_<Value::Type>(value, "Type")
        .value("Integers", Value::Type::Integers)
        .value("Reals", Value::Type::Reals)
        .value("Strings", Value::Type::Strings)
        .value("DataSets", Value::Type::DataSets)
        .value("Binary", Value::Type::Binary)
    ;
    
    odil::wrappers::bind_vector<Value::Integers>(value, "Integers")
        .def(pickle(
            &pickle_pod_container<Value::Integers>,
            &unpickle_pod_container<Value::Integers>))
    ;
    odil::wrappers::bind_vector<Value::Reals>(value, "Reals")
        .def(pickle(
            &pickle_pod_container<Value::Reals>,
            &unpickle_pod_container<Value::Reals>))
    ;

    // NOTE Using bind_vector brings back #63.
    // Re-use the code of bind_vector and modify where needed.
    {
        using Vector = Value::Strings;
        std::string const name = "Strings";

        using Class_ = class_<Vector>;
        Class_ cl(value, name.c_str(), module_local(true));
        cl.def(init<>());

        detail::vector_if_copy_constructible<Vector, Class_>(cl);
        detail::vector_if_equal_operator<Vector, Class_>(cl);
        detail::vector_if_insertion_operator<Vector, Class_>(cl);
        detail::vector_modifiers<Vector, Class_>(cl);

        // vector_accessor
        using T = typename Vector::value_type;
        using SizeType = long; //typename Vector::size_type;
        using ItType   = typename Vector::iterator;

        cl.def("__getitem__",
            [](Vector &v, SizeType i) {
                if((i>=0 && i >= SizeType(v.size())) || (i<0 && i < -SizeType(v.size())))
                {
                    throw pybind11::index_error();
                }
                return bytes(v[i>=0?i:v.size()+i]);
            });

        cl.def(
            "__iter__",
            [](Vector &v) {
#if PYBIND11_VERSION_HEX < 0x02080000
                using state = detail::iterator_state<
                    ItType, ItType, false, return_value_policy::copy>;
#else
                using state = detail::iterator_state<
                    detail::iterator_access<ItType>,
                    return_value_policy::copy, ItType, ItType, T>;
#endif
                if (!detail::get_type_info(typeid(state), false))
                {
                    class_<state>(handle(), "iterator", pybind11::module_local())
                        .def("__iter__", [](state &s) -> state& { return s; })
                        .def("__next__", [](state &s) -> bytes {
                            if (!s.first_or_done)
                            {
                                ++s.it;
                            }
                            else
                            {
                                s.first_or_done = false;
                            }
                            if (s.it == s.end)
                            {
                                s.first_or_done = true;
                                throw stop_iteration();
                            }
                            return bytes(*s.it);
                        })
                    ;
                }
                return cast(state{v.begin(), v.end(), true});
            }
        );

        // bind_vector
        cl.def("__bool__",
            [](const Vector &v) -> bool {
                return !v.empty();
            },
            "Check whether the list is nonempty"
        );

        cl.def("__len__", &Vector::size);
        
        cl.def(pickle(
            &pickle_object_container<Value::Strings>,
            &unpickle_object_container<Value::Strings>
        ));
    }

    odil::wrappers::bind_vector<Value::DataSets>(value, "DataSets")
        .def(pickle(
            [](Value::DataSets const & value) {
                tuple pickled(value.size());
                for(std::size_t i=0; i<value.size(); ++i)
                {
                    pickled[i] = value[i];
                }
                return pickled;
            },
            [](tuple pickled) {
                Value::DataSets value(pickled.size());
                for(std::size_t i=0; i<value.size(); ++i)
                {
                    value[i] = std::make_shared<DataSet>(
                        pickled[i].cast<DataSet>());
                }
                return value;
            }
        ))
    ;
    
    odil::wrappers::bind_vector<Value::Binary::value_type>(value, "BinaryItem")
        .def(
            "get_memory_view", 
            [](Value::Binary::value_type const & binary_item)
            {
                Py_buffer buffer;
                PyBuffer_FillInfo(
                    &buffer, nullptr,
                    const_cast<Value::Binary::value_type::value_type*>(&binary_item[0]),
                    binary_item.size(), 1, PyBUF_SIMPLE);
                PyObject * memory_view = PyMemoryView_FromBuffer(&buffer);

                return reinterpret_steal<object>(memory_view);
            })
        .def(pickle(
            [](Value::Binary::value_type b) {
                return make_tuple(
                    bytes(reinterpret_cast<char*>(b.data()), b.size()));
            },
            [](tuple pickled) {
                char * buffer;
                ssize_t length;
                PYBIND11_BYTES_AS_STRING_AND_SIZE(
                    pickled[0].ptr(), &buffer, &length);
                
                return Value::Binary::value_type{
                    reinterpret_cast<unsigned char*>(buffer), 
                    reinterpret_cast<unsigned char*>(buffer)+length};
            }
        ))
    ;
    odil::wrappers::bind_vector<Value::Binary>(value, "Binary")
        .def(pickle(
            &pickle_object_container<Value::Binary>,
            &unpickle_object_container<Value::Binary>
        ))
    ;
}