File: Element.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 (130 lines) | stat: -rw-r--r-- 4,585 bytes parent folder | download | duplicates (4)
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
/*************************************************************************
 * 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 <pybind11/pybind11.h>
#include <pybind11/operators.h>

#include "odil/Element.h"
#include "odil/VR.h"

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

#include "Value.h"

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

    class_<Element>(m, "Element")
        // WARNING: the VR is not handled correctly
        //.def_readwrite("vr", &Element::vr)
        .def_property(
            "vr",
            [](Element const & e) { return e.vr; },
            [](Element & e, VR v) { e.vr = v; })
        .def(init<VR>())
        .def(init<Value const &, VR>())
        .def(
            init([](sequence & source, VR vr) {
                return convert_sequence<Element>(source, vr);
            }),
            "source"_a, "vr"_a=VR::INVALID)
        .def("empty", &Element::empty)
        .def("size", &Element::size)
        .def(
            "get_value", (Value const & (Element::*)() const) &Element::get_value,
            return_value_policy::reference_internal)
        .def("is_int", &Element::is_int)
        .def(
            "as_int", (Value::Integers & (Element::*)()) &Element::as_int,
            return_value_policy::reference_internal)
        .def("is_real", &Element::is_real)
        .def(
            "as_real", (Value::Reals & (Element::*)()) &Element::as_real,
            return_value_policy::reference_internal)
        .def("is_string", &Element::is_string)
        .def(
            "as_string", (Value::Strings & (Element::*)()) &Element::as_string,
            return_value_policy::reference_internal)
        .def("is_data_set", &Element::is_data_set)
        .def(
            "as_data_set", (Value::DataSets & (Element::*)()) &Element::as_data_set,
            return_value_policy::reference_internal)
        .def("is_binary", &Element::is_binary)
        .def(
            "as_binary", (Value::Binary & (Element::*)()) &Element::as_binary,
            return_value_policy::reference_internal)
        .def(self == self)
        .def(self != self)
        .def("__len__", &Element::size)
        .def("clear", &Element::clear)
        .def(
            "__getitem__", [](Element 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.get_value());
            })
        .def(
            "__getitem__", [](Element const & self, slice slice_) {
                return apply_visitor(
                    GetSlice(self.size(), slice_), self.get_value());
            })
        .def(
            "__setitem__", [](Element & 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.get_value());
            })
        .def(
            "__iter__", 
            [](Element const & self) 
            { 
                return apply_visitor(Iterate(), self.get_value());
            })
        .def(
            "append", [](Element & self, object item) {
                return apply_visitor(Append(item), self.get_value());
            })
        .def(pickle(
            [](Element const & element) {
                return make_tuple(element.get_value(), element.vr);
            },
            [](tuple pickled) {
                auto const vr = pickled[1].cast<VR>();
                if(len(pickled[0]) == 0)
                {
                    return Element(vr);
                }
                else
                {
                    auto const value = pickled[0].cast<Value>();
                    return Element(value, vr);
                }
            }
        ))
    ;
}