Skip to content

Commit a5588ed

Browse files
committed
Added the ability to wrap pointers
- Added the ability to get the address of objects
1 parent c386410 commit a5588ed

File tree

3 files changed

+79
-18
lines changed

3 files changed

+79
-18
lines changed

src/core/modules/memory/memory_tools.cpp

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -303,17 +303,4 @@ void CFunction::RemoveHook(DynamicHooks::HookType_t eType, PyObject* pCallable)
303303
return;
304304

305305
g_mapCallbacks[pHook][eType].remove(pCallable);
306-
}
307-
308-
//-----------------------------------------------------------------------------
309-
// Functions
310-
//-----------------------------------------------------------------------------
311-
int GetError()
312-
{
313-
return dcGetError(g_pCallVM);
314-
}
315-
316-
CPointer* Alloc(int iSize)
317-
{
318-
return new CPointer((unsigned long) UTIL_Alloc(iSize));
319306
}

src/core/modules/memory/memory_tools.h

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ class CPointer
248248
unsigned long m_ulAddr;
249249
};
250250

251+
251252
class CFunction: public CPointer
252253
{
253254
public:
@@ -276,7 +277,36 @@ class CFunction: public CPointer
276277
Convention_t m_eConv;
277278
};
278279

279-
int GetError();
280-
CPointer* Alloc(int iSize);
280+
281+
class Wrap
282+
{
283+
public:
284+
template<class T>
285+
static T* WrapIt(object ptr)
286+
{
287+
unsigned long ulPtr = ExtractPyPtr(ptr);
288+
return (T *) ulPtr;
289+
}
290+
};
291+
292+
293+
//-----------------------------------------------------------------------------
294+
// Functions
295+
//-----------------------------------------------------------------------------
296+
inline int GetError()
297+
{
298+
return dcGetError(g_pCallVM);
299+
}
300+
301+
inline CPointer Alloc(int iSize)
302+
{
303+
return CPointer((unsigned long) UTIL_Alloc(iSize));
304+
}
305+
306+
template<class T>
307+
CPointer GetAddress(T* ptr)
308+
{
309+
return CPointer((unsigned long) ptr);
310+
}
281311

282312
#endif // _MEMORY_TOOLS_H

src/core/modules/memory/memory_wrap_python.cpp

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ void export_memtools();
4444
void export_dyncall();
4545
void export_dynamichooks();
4646
void export_callbacks();
47+
void export_get_address();
4748

4849
DECLARE_SP_MODULE(memory_c)
4950
{
@@ -52,6 +53,7 @@ DECLARE_SP_MODULE(memory_c)
5253
export_dyncall();
5354
export_dynamichooks();
5455
export_callbacks();
56+
export_get_address();
5557
}
5658

5759
//-----------------------------------------------------------------------------
@@ -109,7 +111,7 @@ void export_binaryfile()
109111

110112

111113
//-----------------------------------------------------------------------------
112-
// Exposes CPointer
114+
// Exposes CPointer and CFunction
113115
//-----------------------------------------------------------------------------
114116
#define OVERLOAD_GET_TYPE(name, type) \
115117
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(get_##name##_overload, CPointer::Get<type>, 0, 1)
@@ -380,8 +382,7 @@ void export_memtools()
380382
def("alloc",
381383
Alloc,
382384
"Allocates a memory block.",
383-
args("size"),
384-
manage_new_object_policy()
385+
args("size")
385386
);
386387

387388
class_<CFunction, bases<CPointer> >("Function", init<unsigned long, Convention_t, char*>())
@@ -481,6 +482,9 @@ void export_dynamichooks()
481482
;
482483
}
483484

485+
//-----------------------------------------------------------------------------
486+
// Exposes CCallback
487+
//-----------------------------------------------------------------------------
484488
void export_callbacks()
485489
{
486490
class_< CCallback, bases< CFunction > >("Callback", init< object, Convention_t, char * >())
@@ -490,3 +494,43 @@ void export_callbacks()
490494
)
491495
;
492496
}
497+
498+
499+
//-----------------------------------------------------------------------------
500+
// Exposes wrap/get_address functionality
501+
//-----------------------------------------------------------------------------
502+
// Use this macro to add the ability to get the address of an object
503+
#define GET_ADDRESS(type) \
504+
def("get_address", \
505+
&GetAddress<type>, \
506+
"Returns the memory address of the given object.", \
507+
args("object") \
508+
);
509+
510+
// Use this macro if the Python name of the type is different to the C++ name
511+
#define WRAP_ADDRESS0(type, pyname) \
512+
.def(XSTRINGIFY(pyname), \
513+
&Wrap::WrapIt<type>, \
514+
"Wraps the given address.", \
515+
args("pointer"), \
516+
reference_existing_object_policy() \
517+
).staticmethod(XSTRINGIFY(pyname))
518+
519+
// Use this macro if the Python and C++ name of the type are the same
520+
#define WRAP_ADDRESS1(type) \
521+
WRAP_ADDRESS0(type, type)
522+
523+
void export_get_address()
524+
{
525+
// get_address()
526+
GET_ADDRESS(Vector)
527+
GET_ADDRESS(QAngle)
528+
529+
// wrap.<type>()
530+
class_<Wrap>("wrap", no_init)
531+
532+
WRAP_ADDRESS1(Vector)
533+
WRAP_ADDRESS1(QAngle)
534+
535+
;
536+
}

0 commit comments

Comments
 (0)