Skip to content

Commit 46a5766

Browse files
committed
Added safe item indexer
1 parent c62b376 commit 46a5766

File tree

3 files changed

+30
-25
lines changed

3 files changed

+30
-25
lines changed

src/core/modules/mathlib/mathlib_wrap_python.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,11 @@ void export_vector()
9797
)
9898

9999
.def("__getitem__",
100-
static_cast< float(Vector::*)( int ) const >(&Vector::operator[])
100+
&GetItemIndexer<Vector, float, 0, 2>
101101
)
102102

103103
.def("__setitem__",
104-
&SetItemIndexer<Vector, float>
104+
&SetItemIndexer<Vector, float, 0, 2>
105105
)
106106

107107
.def("as_vector_2D",
@@ -270,11 +270,11 @@ void export_qangle()
270270
)
271271

272272
.def("__getitem__",
273-
static_cast< float(QAngle::*)( int ) const >(&QAngle::operator[])
273+
&GetItemIndexer<QAngle, float, 0, 2>
274274
)
275275

276276
.def("__setitem__",
277-
SetItemIndexer<QAngle, float>
277+
&SetItemIndexer<QAngle, float, 0, 2>
278278
)
279279

280280
.def(self == self)
@@ -326,11 +326,11 @@ void export_quaternion()
326326
.def(self != self)
327327

328328
.def("__getitem__",
329-
static_cast< float(Quaternion::*)( int ) const >(&Quaternion::operator[])
329+
&GetItemIndexer<Quaternion, float, 0, 3>
330330
)
331331

332332
.def("__setitem__",
333-
&SetItemIndexer<Quaternion, float>
333+
&SetItemIndexer<Quaternion, float, 0, 3>
334334
)
335335

336336
.def_readwrite("x",
@@ -406,11 +406,11 @@ void export_radian_euler()
406406
)
407407

408408
.def("__getitem__",
409-
static_cast< float(RadianEuler::*)( int ) const >(&RadianEuler::operator[])
409+
&GetItemIndexer<RadianEuler, float, 0, 2>
410410
)
411411

412412
.def("__setitem__",
413-
&SetItemIndexer<RadianEuler, float>
413+
&SetItemIndexer<RadianEuler, float, 0, 2>
414414
)
415415

416416
.def_readwrite("x",

src/core/modules/usermessage/usermessage_wrap_python.cpp

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -197,27 +197,14 @@ void export_color()
197197
.def(init<int, int, int>())
198198
.def(init<int, int, int, int>())
199199

200-
.def("set_color",
201-
&Color::SetColor,
202-
set_color_overload(
203-
"Sets the color in a RGB(A) format (0-255).",
204-
args("r", "g", "b", "a")
205-
)
206-
)
207-
208-
.def("get_color",
209-
&ColorExt::GetColor,
210-
"Returns the color as a tuple containing the RGBA values."
211-
)
212-
213200
.def("__getitem__",
214-
&GetItemIndexer<Color, unsigned char>,
215-
"Returns the color at the given index (0-3)."
201+
&GetItemIndexer<Color, unsigned char, 0, 3>,
202+
"Returns the color at the given index."
216203
)
217204

218205
.def("__setitem__",
219-
&SetItemIndexer<Color, unsigned char>,
220-
"Sets the color at the given index (0-3)."
206+
&SetItemIndexer<Color, unsigned char, 0, 3>,
207+
"Sets the color at the given index."
221208
)
222209

223210
.def(self == self)

src/core/utility/sp_util.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,30 @@ inline unsigned long ExtractPyPtr(object obj)
6464
//---------------------------------------------------------------------------------
6565
// Helper template methods for __getitem__ and __setitem__
6666
//---------------------------------------------------------------------------------
67+
template<class cls, class return_type, int iMin, int iMax>
68+
return_type GetItemIndexer(cls* self, int iIndex)
69+
{
70+
if ((iIndex < iMin) || (iIndex > iMax))
71+
BOOST_RAISE_EXCEPTION(PyExc_IndexError, "Index out of range.");
72+
73+
return (*self)[iIndex];
74+
}
75+
6776
template<class T, class U>
6877
U GetItemIndexer(const T* self, const int i)
6978
{
7079
return (*self)[i];
7180
}
7281

82+
template<class cls, class value_type, int iMin, int iMax>
83+
void SetItemIndexer(cls* self, int iIndex, value_type value)
84+
{
85+
if ((iIndex < iMin) || (iIndex > iMax))
86+
BOOST_RAISE_EXCEPTION(PyExc_IndexError, "Index out of range.");
87+
88+
(*self)[iIndex] = value;
89+
}
90+
7391
template<class T, class U>
7492
void SetItemIndexer(T* self, const int i, const U& value)
7593
{

0 commit comments

Comments
 (0)