-
Notifications
You must be signed in to change notification settings - Fork 354
Expand file tree
/
Copy pathcoder.i
More file actions
133 lines (123 loc) · 3.88 KB
/
Copy pathcoder.i
File metadata and controls
133 lines (123 loc) · 3.88 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
//
// Exposes Encoder and Decoder a subset of functionality plus the
// ability to set and get internal buffer. This allows passing them
// into other SWIG'd methods, such as those in the S2 SWIG library.
// For example:
//
// class S2Polygon {
// ...
// void Encode(Encoder* const encoder) const override;
//
// bool Decode(Decoder* const decoder) override;
// };
//
// Usage:
// # data is a bytes object.
// dec = pywrapcoder.Decoder(data)
// polygon = s2.S2Polygon()
// polygon.Decode(dec)
//
// enc = pywrapcoder.Encoder()
// polygon.Encode(enc)
// data = enc.buffer()
//
%{
#include "s2/util/coding/coder.h"
%}
// For Decoder::reset to accept a bytes or bytearray object.
%typemap(in) (const void* buf, size_t maxn) {
if (PyBytes_Check($input)) {
$1 = (void *) PyBytes_AsString($input);
$2 = PyBytes_Size($input);
} else if (PyByteArray_Check($input)) {
$1 = (void *) PyByteArray_AsString($input);
$2 = PyByteArray_Size($input);
} else {
// TODO: When Py_LIMITED_API is raised to 3.13+ (2028), replace
// %S/(PyObject*)Py_TYPE() with %T in PyErr_Format calls for bare type
// names (e.g. "int" vs "<class 'int'>").
PyErr_Format(PyExc_TypeError,
"bytes or bytearray needed, %S found",
(PyObject*)Py_TYPE($input));
return nullptr;
}
};
// For Encoder::reset to accept a bytearray object.
%typemap(in) (void* buf, size_t maxn) {
if (PyByteArray_Check($input)) {
$1 = (void *) PyByteArray_AsString($input);
$2 = PyByteArray_Size($input);
} else {
PyErr_Format(PyExc_TypeError,
"bytearray needed, %S found",
(PyObject*)Py_TYPE($input));
return nullptr;
}
};
// Keep a reference to the object so that outside users don't
// have to keep one, or else it could be released.
// The auto-generated code passes *args to each of the methods, but the use
// cases we support is only when a single arg is passed in.
%pythonprepend Decoder::Decoder %{
if len(args) == 1:
self._data_keepalive = args[0]
%}
%pythonprepend Decoder::reset %{
self._data_keepalive = args[0]
%}
%extend Decoder {
// Allows direct construction with a bytes or bytearray objects.
Decoder(PyObject *obj) {
if (PyBytes_Check(obj)) {
return new Decoder(PyBytes_AsString(obj), PyBytes_Size(obj));
}
if (PyByteArray_Check(obj)) {
return new Decoder(PyByteArray_AsString(obj), PyByteArray_Size(obj));
}
PyErr_Format(PyExc_TypeError,
"bytes or bytearray needed, %S found",
(PyObject*)Py_TYPE(obj));
return nullptr;
}
}
%extend Encoder {
// Returns internal buffer as a bytearray.
PyObject* buffer() {
return PyByteArray_FromStringAndSize($self->base(), $self->length());
}
}
%ignoreall
%unignore Decoder;
%unignore Decoder::Decoder();
%unignore Decoder::Decoder(const void*, size_t);
%unignore Decoder::~Decoder;
%unignore Decoder::avail() const;
%unignore Decoder::get8();
%unignore Decoder::get16();
%unignore Decoder::get32();
%unignore Decoder::get64();
%unignore Decoder::getfloat();
%unignore Decoder::getdouble();
%unignore Decoder::pos() const;
%unignore Decoder::reset(const void*, size_t);
%unignore Encoder;
%unignore Encoder::Encoder();
%unignore Encoder::Encoder(void*, size_t);
%unignore Encoder::~Encoder;
%unignore Encoder::Ensure(size_t);
%unignore Encoder::avail() const;
%unignore Encoder::buffer();
%unignore Encoder::clear();
%unignore Encoder::length() const;
%unignore Encoder::put8(unsigned char);
%unignore Encoder::put16(uint16_t);
%unignore Encoder::put32(uint32_t);
%unignore Encoder::put64(uint64_t);
%unignore Encoder::putdouble(double);
%unignore Encoder::putfloat(float);
%unignore Encoder::reset(void *, size_t);
// Silence SWIG Warning 362: operator= can't be wrapped in Python.
// %ignoreall is active but the warning fires at parse time regardless.
%ignore Encoder::operator=;
%include "s2/util/coding/coder.h"
%unignoreall