-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathInstanceHandle.i
More file actions
218 lines (187 loc) · 7.08 KB
/
InstanceHandle.i
File metadata and controls
218 lines (187 loc) · 7.08 KB
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
// Copyright 2022 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
%{
#include <stdexcept>
#include <vector>
#include "fastdds/rtps/common/InstanceHandle.hpp"
// Define a hash method in global scope for InstanceHandle_t types
// This is necessary if we want other classes to hash an internal InstanceHandle_t
long hash(const eprosima::fastdds::rtps::InstanceHandle_t& handle)
{
long ret = 0;
for (unsigned int i = 0; i < 16; ++i)
{
ret = (ret * 31) ^ handle.value[i];
}
return ret;
}
%}
// SWIG does not support type conversion operators correctly unless converted to a normal method
%rename(get_guid) eprosima::fastdds::rtps::InstanceHandle_t::operator const GUID_t&;
%ignore eprosima::fastdds::rtps::InstanceHandleValue_t::operator [] const;
%ignore eprosima::fastdds::rtps::InstanceHandleValue_t::operator [];
%ignore eprosima::fastdds::rtps::operator <<(std::ostream&, const InstanceHandle_t&);
%ignore eprosima::fastdds::rtps::operator >>(std::istream&, InstanceHandle_t&);
%ignore eprosima::fastdds::rtps::InstanceHandleValue_t::operator const octet* () const;
%ignore eprosima::fastdds::rtps::InstanceHandleValue_t::operator octet* ();
%extend eprosima::fastdds::rtps::InstanceHandleValue_t {
// Constructor from a sequence of 16 bytes (tuple/list/bytes/bytearray)
InstanceHandleValue_t(PyObject* seq) {
eprosima::fastdds::rtps::InstanceHandleValue_t* self = new eprosima::fastdds::rtps::InstanceHandleValue_t();
SWIG_PYTHON_THREAD_BEGIN_BLOCK;
// Fast-path: bytes
if (PyBytes_Check(seq)) {
if (PyBytes_Size(seq) == 16)
{
const char* b = PyBytes_AsString(seq);
for (int i = 0; i < 16; ++i) (*self)[i] = (uint8_t)(unsigned char)b[i];
}
else
{
delete self;
self = nullptr;
PyErr_SetString(PyExc_ValueError, "Expected 16 bytes");
}
}
// Fast-path: bytearray
else if (PyByteArray_Check(seq))
{
if (PyByteArray_Size(seq) == 16)
{
const char* b = PyByteArray_AsString(seq);
for (int i = 0; i < 16; ++i) (*self)[i] = (uint8_t)(unsigned char)b[i];
}
else
{
delete self;
self = nullptr;
PyErr_SetString(PyExc_ValueError, "Expected 16 bytes");
}
}
else
{
// Generic fallback: iterable from 16 ints 0..255
PyObject* it = PyObject_GetIter(seq);
size_t count {0};
if (it)
{
PyObject* item {nullptr};
while ((item = PyIter_Next(it)) && count < 16)
{
long val = PyLong_AsLong(item);
Py_DECREF(item);
if (val == -1 && PyErr_Occurred())
{
delete self;
self = nullptr;
PyErr_SetString(PyExc_TypeError, "Sequence must contain integers");
break;
}
else if (val < 0 || val > 255)
{
delete self;
self = nullptr;
PyErr_SetString(PyExc_ValueError, "Each value must be in 0..255");
break;
}
(*self)[count] = static_cast<uint8_t>(val);
++count;
}
Py_DECREF(it);
if ((nullptr != item || count != 16) && nullptr != self)
{
delete self;
self = nullptr;
PyErr_SetString(PyExc_ValueError, "Expected 16 elements");
}
}
else
{
delete self;
self = nullptr;
PyErr_SetString(PyExc_TypeError, "Expected a sequence of 16 integers (0..255) or 16-byte object");
}
}
SWIG_PYTHON_THREAD_END_BLOCK;
return self;
}
size_t __len__() const { return 16; }
uint8_t __getitem__(size_t i) const {
if (i >= 16) throw std::out_of_range("index out of range");
return $self->operator[](i);
}
void __setitem__(size_t i, uint8_t v) {
if (i >= 16) throw std::out_of_range("index out of range");
$self->operator[](i) = v;
}
}
%ignore eprosima::fastdds::rtps::InstanceHandle_t::value;
// Declare the comparison operators as internal to the class
%extend eprosima::fastdds::rtps::InstanceHandle_t {
bool operator==(const eprosima::fastdds::rtps::InstanceHandle_t& other) const
{
return *$self == other;
}
bool operator!=(const eprosima::fastdds::rtps::InstanceHandle_t& other) const
{
return *$self != other;
}
std::string __str__() const
{
std::ostringstream out;
out << *$self;
return out.str();
}
// Define the hash method using the global one
long __hash__() const
{
return hash(*$self);
}
// Setter from sequence (tuple/list/bytes/bytearray)
void from_sequence(PyObject* seq) {
// Reuse the constructor to validate and copy
eprosima::fastdds::rtps::InstanceHandleValue_t* tmp = new_eprosima_fastdds_rtps_InstanceHandleValue_t(seq);
if (nullptr != tmp)
{
for (int i = 0; i < 16; ++i) $self->value[i] = (*tmp)[i];
delete tmp; // avoid memory leak
}
}
// Getter: return a tuple of 16 ints (0..255)
PyObject* to_sequence() const {
SWIG_PYTHON_THREAD_BEGIN_BLOCK;
PyObject* python_tuple = PyTuple_New(16);
if (python_tuple)
{
for(size_t count = 0; count < 16; ++count)
{
PyTuple_SetItem(python_tuple, count, PyInt_FromLong($self->value[count]));
}
}
SWIG_PYTHON_THREAD_END_BLOCK;
return python_tuple;
}
}
// Template for std::vector<InstanceHandle_t>
%template(InstanceHandleVector) std::vector<eprosima::fastdds::rtps::InstanceHandle_t>;
%typemap(doctype) std::vector<eprosima::fastdds::rtps::InstanceHandle_t>"InstanceHandleVector";
%include "fastdds/rtps/common/InstanceHandle.hpp"
%pythoncode %{
def _ihv_get_value(self):
return self.to_sequence()
def _ihv_set_value(self, seq):
self.from_sequence(seq)
InstanceHandle_t.value = property(_ihv_get_value, _ihv_set_value,
doc="16-byte value as list/tuple/bytes/bytearray")
%}