123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463 |
- /**************************************************************************/
- /* method_ptrcall.h */
- /**************************************************************************/
- /* This file is part of: */
- /* GODOT ENGINE */
- /* https://godotengine.org */
- /**************************************************************************/
- /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
- /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
- /* */
- /* Permission is hereby granted, free of charge, to any person obtaining */
- /* a copy of this software and associated documentation files (the */
- /* "Software"), to deal in the Software without restriction, including */
- /* without limitation the rights to use, copy, modify, merge, publish, */
- /* distribute, sublicense, and/or sell copies of the Software, and to */
- /* permit persons to whom the Software is furnished to do so, subject to */
- /* the following conditions: */
- /* */
- /* The above copyright notice and this permission notice shall be */
- /* included in all copies or substantial portions of the Software. */
- /* */
- /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
- /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
- /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
- /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
- /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
- /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
- /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
- /**************************************************************************/
- #ifndef METHOD_PTRCALL_H
- #define METHOD_PTRCALL_H
- #include "core/object/object_id.h"
- #include "core/typedefs.h"
- #include "core/variant/variant.h"
- template <typename T>
- struct PtrToArg {};
- #define MAKE_PTRARG(m_type) \
- template <> \
- struct PtrToArg<m_type> { \
- _FORCE_INLINE_ static const m_type &convert(const void *p_ptr) { \
- return *reinterpret_cast<const m_type *>(p_ptr); \
- } \
- typedef m_type EncodeT; \
- _FORCE_INLINE_ static void encode(m_type p_val, void *p_ptr) { \
- *((m_type *)p_ptr) = p_val; \
- } \
- }; \
- template <> \
- struct PtrToArg<const m_type &> { \
- _FORCE_INLINE_ static const m_type &convert(const void *p_ptr) { \
- return *reinterpret_cast<const m_type *>(p_ptr); \
- } \
- typedef m_type EncodeT; \
- _FORCE_INLINE_ static void encode(m_type p_val, void *p_ptr) { \
- *((m_type *)p_ptr) = p_val; \
- } \
- }
- #define MAKE_PTRARGCONV(m_type, m_conv) \
- template <> \
- struct PtrToArg<m_type> { \
- _FORCE_INLINE_ static m_type convert(const void *p_ptr) { \
- return static_cast<m_type>(*reinterpret_cast<const m_conv *>(p_ptr)); \
- } \
- typedef m_conv EncodeT; \
- _FORCE_INLINE_ static void encode(m_type p_val, void *p_ptr) { \
- *((m_conv *)p_ptr) = static_cast<m_conv>(p_val); \
- } \
- }; \
- template <> \
- struct PtrToArg<const m_type &> { \
- _FORCE_INLINE_ static m_type convert(const void *p_ptr) { \
- return static_cast<m_type>(*reinterpret_cast<const m_conv *>(p_ptr)); \
- } \
- typedef m_conv EncodeT; \
- _FORCE_INLINE_ static void encode(m_type p_val, void *p_ptr) { \
- *((m_conv *)p_ptr) = static_cast<m_conv>(p_val); \
- } \
- }
- #define MAKE_PTRARG_BY_REFERENCE(m_type) \
- template <> \
- struct PtrToArg<m_type> { \
- _FORCE_INLINE_ static const m_type &convert(const void *p_ptr) { \
- return *reinterpret_cast<const m_type *>(p_ptr); \
- } \
- typedef m_type EncodeT; \
- _FORCE_INLINE_ static void encode(const m_type &p_val, void *p_ptr) { \
- *((m_type *)p_ptr) = p_val; \
- } \
- }; \
- template <> \
- struct PtrToArg<const m_type &> { \
- _FORCE_INLINE_ static const m_type &convert(const void *p_ptr) { \
- return *reinterpret_cast<const m_type *>(p_ptr); \
- } \
- typedef m_type EncodeT; \
- _FORCE_INLINE_ static void encode(const m_type &p_val, void *p_ptr) { \
- *((m_type *)p_ptr) = p_val; \
- } \
- }
- MAKE_PTRARGCONV(bool, uint8_t);
- // Integer types.
- MAKE_PTRARGCONV(uint8_t, int64_t);
- MAKE_PTRARGCONV(int8_t, int64_t);
- MAKE_PTRARGCONV(uint16_t, int64_t);
- MAKE_PTRARGCONV(int16_t, int64_t);
- MAKE_PTRARGCONV(uint32_t, int64_t);
- MAKE_PTRARGCONV(int32_t, int64_t);
- MAKE_PTRARG(int64_t);
- MAKE_PTRARG(uint64_t);
- // Float types
- MAKE_PTRARGCONV(float, double);
- MAKE_PTRARG(double);
- MAKE_PTRARG(String);
- MAKE_PTRARG(Vector2);
- MAKE_PTRARG(Vector2i);
- MAKE_PTRARG(Rect2);
- MAKE_PTRARG(Rect2i);
- MAKE_PTRARG_BY_REFERENCE(Vector3);
- MAKE_PTRARG_BY_REFERENCE(Vector3i);
- MAKE_PTRARG_BY_REFERENCE(Vector4);
- MAKE_PTRARG_BY_REFERENCE(Vector4i);
- MAKE_PTRARG(Transform2D);
- MAKE_PTRARG(Projection);
- MAKE_PTRARG_BY_REFERENCE(Plane);
- MAKE_PTRARG(Quaternion);
- MAKE_PTRARG_BY_REFERENCE(AABB);
- MAKE_PTRARG_BY_REFERENCE(Basis);
- MAKE_PTRARG_BY_REFERENCE(Transform3D);
- MAKE_PTRARG_BY_REFERENCE(Color);
- MAKE_PTRARG(StringName);
- MAKE_PTRARG(NodePath);
- MAKE_PTRARG(RID);
- // Object doesn't need this.
- MAKE_PTRARG(Callable);
- MAKE_PTRARG(Signal);
- MAKE_PTRARG(Dictionary);
- MAKE_PTRARG(Array);
- MAKE_PTRARG(PackedByteArray);
- MAKE_PTRARG(PackedInt32Array);
- MAKE_PTRARG(PackedInt64Array);
- MAKE_PTRARG(PackedFloat32Array);
- MAKE_PTRARG(PackedFloat64Array);
- MAKE_PTRARG(PackedStringArray);
- MAKE_PTRARG(PackedVector2Array);
- MAKE_PTRARG(PackedVector3Array);
- MAKE_PTRARG(PackedColorArray);
- MAKE_PTRARG(PackedVector4Array);
- MAKE_PTRARG_BY_REFERENCE(Variant);
- // This is for Object.
- template <typename T>
- struct PtrToArg<T *> {
- _FORCE_INLINE_ static T *convert(const void *p_ptr) {
- return likely(p_ptr) ? const_cast<T *>(*reinterpret_cast<T *const *>(p_ptr)) : nullptr;
- }
- typedef Object *EncodeT;
- _FORCE_INLINE_ static void encode(T *p_var, void *p_ptr) {
- *((T **)p_ptr) = p_var;
- }
- };
- template <typename T>
- struct PtrToArg<const T *> {
- _FORCE_INLINE_ static const T *convert(const void *p_ptr) {
- return likely(p_ptr) ? *reinterpret_cast<T *const *>(p_ptr) : nullptr;
- }
- typedef const Object *EncodeT;
- _FORCE_INLINE_ static void encode(T *p_var, void *p_ptr) {
- *((T **)p_ptr) = p_var;
- }
- };
- // This is for ObjectID.
- template <>
- struct PtrToArg<ObjectID> {
- _FORCE_INLINE_ static const ObjectID convert(const void *p_ptr) {
- return ObjectID(*reinterpret_cast<const uint64_t *>(p_ptr));
- }
- typedef uint64_t EncodeT;
- _FORCE_INLINE_ static void encode(const ObjectID &p_val, void *p_ptr) {
- *((uint64_t *)p_ptr) = p_val;
- }
- };
- // This is for the special cases used by Variant.
- // No EncodeT because direct pointer conversion not possible.
- #define MAKE_VECARG(m_type) \
- template <> \
- struct PtrToArg<Vector<m_type>> { \
- _FORCE_INLINE_ static Vector<m_type> convert(const void *p_ptr) { \
- const Vector<m_type> *dvs = reinterpret_cast<const Vector<m_type> *>(p_ptr); \
- Vector<m_type> ret; \
- int len = dvs->size(); \
- ret.resize(len); \
- { \
- const m_type *r = dvs->ptr(); \
- for (int i = 0; i < len; i++) { \
- ret.write[i] = r[i]; \
- } \
- } \
- return ret; \
- } \
- _FORCE_INLINE_ static void encode(const Vector<m_type> &p_vec, void *p_ptr) { \
- Vector<m_type> *dv = reinterpret_cast<Vector<m_type> *>(p_ptr); \
- int len = p_vec.size(); \
- dv->resize(len); \
- { \
- m_type *w = dv->ptrw(); \
- for (int i = 0; i < len; i++) { \
- w[i] = p_vec[i]; \
- } \
- } \
- } \
- }; \
- template <> \
- struct PtrToArg<const Vector<m_type> &> { \
- _FORCE_INLINE_ static Vector<m_type> convert(const void *p_ptr) { \
- const Vector<m_type> *dvs = reinterpret_cast<const Vector<m_type> *>(p_ptr); \
- Vector<m_type> ret; \
- int len = dvs->size(); \
- ret.resize(len); \
- { \
- const m_type *r = dvs->ptr(); \
- for (int i = 0; i < len; i++) { \
- ret.write[i] = r[i]; \
- } \
- } \
- return ret; \
- } \
- }
- // No EncodeT because direct pointer conversion not possible.
- #define MAKE_VECARG_ALT(m_type, m_type_alt) \
- template <> \
- struct PtrToArg<Vector<m_type_alt>> { \
- _FORCE_INLINE_ static Vector<m_type_alt> convert(const void *p_ptr) { \
- const Vector<m_type> *dvs = reinterpret_cast<const Vector<m_type> *>(p_ptr); \
- Vector<m_type_alt> ret; \
- int len = dvs->size(); \
- ret.resize(len); \
- { \
- const m_type *r = dvs->ptr(); \
- for (int i = 0; i < len; i++) { \
- ret.write[i] = r[i]; \
- } \
- } \
- return ret; \
- } \
- _FORCE_INLINE_ static void encode(const Vector<m_type_alt> &p_vec, void *p_ptr) { \
- Vector<m_type> *dv = reinterpret_cast<Vector<m_type> *>(p_ptr); \
- int len = p_vec.size(); \
- dv->resize(len); \
- { \
- m_type *w = dv->ptrw(); \
- for (int i = 0; i < len; i++) { \
- w[i] = p_vec[i]; \
- } \
- } \
- } \
- }; \
- template <> \
- struct PtrToArg<const Vector<m_type_alt> &> { \
- _FORCE_INLINE_ static Vector<m_type_alt> convert(const void *p_ptr) { \
- const Vector<m_type> *dvs = reinterpret_cast<const Vector<m_type> *>(p_ptr); \
- Vector<m_type_alt> ret; \
- int len = dvs->size(); \
- ret.resize(len); \
- { \
- const m_type *r = dvs->ptr(); \
- for (int i = 0; i < len; i++) { \
- ret.write[i] = r[i]; \
- } \
- } \
- return ret; \
- } \
- }
- MAKE_VECARG_ALT(String, StringName);
- // For stuff that gets converted to Array vectors.
- // No EncodeT because direct pointer conversion not possible.
- #define MAKE_VECARR(m_type) \
- template <> \
- struct PtrToArg<Vector<m_type>> { \
- _FORCE_INLINE_ static Vector<m_type> convert(const void *p_ptr) { \
- const Array *arr = reinterpret_cast<const Array *>(p_ptr); \
- Vector<m_type> ret; \
- int len = arr->size(); \
- ret.resize(len); \
- for (int i = 0; i < len; i++) { \
- ret.write[i] = (*arr)[i]; \
- } \
- return ret; \
- } \
- _FORCE_INLINE_ static void encode(const Vector<m_type> &p_vec, void *p_ptr) { \
- Array *arr = reinterpret_cast<Array *>(p_ptr); \
- int len = p_vec.size(); \
- arr->resize(len); \
- for (int i = 0; i < len; i++) { \
- (*arr)[i] = p_vec[i]; \
- } \
- } \
- }; \
- template <> \
- struct PtrToArg<const Vector<m_type> &> { \
- _FORCE_INLINE_ static Vector<m_type> convert(const void *p_ptr) { \
- const Array *arr = reinterpret_cast<const Array *>(p_ptr); \
- Vector<m_type> ret; \
- int len = arr->size(); \
- ret.resize(len); \
- for (int i = 0; i < len; i++) { \
- ret.write[i] = (*arr)[i]; \
- } \
- return ret; \
- } \
- }
- MAKE_VECARR(Variant);
- MAKE_VECARR(RID);
- MAKE_VECARR(Plane);
- // No EncodeT because direct pointer conversion not possible.
- #define MAKE_DVECARR(m_type) \
- template <> \
- struct PtrToArg<Vector<m_type>> { \
- _FORCE_INLINE_ static Vector<m_type> convert(const void *p_ptr) { \
- const Array *arr = reinterpret_cast<const Array *>(p_ptr); \
- Vector<m_type> ret; \
- int len = arr->size(); \
- ret.resize(len); \
- { \
- m_type *w = ret.ptrw(); \
- for (int i = 0; i < len; i++) { \
- w[i] = (*arr)[i]; \
- } \
- } \
- return ret; \
- } \
- _FORCE_INLINE_ static void encode(const Vector<m_type> &p_vec, void *p_ptr) { \
- Array *arr = reinterpret_cast<Array *>(p_ptr); \
- int len = p_vec.size(); \
- arr->resize(len); \
- { \
- const m_type *r = p_vec.ptr(); \
- for (int i = 0; i < len; i++) { \
- (*arr)[i] = r[i]; \
- } \
- } \
- } \
- }; \
- template <> \
- struct PtrToArg<const Vector<m_type> &> { \
- _FORCE_INLINE_ static Vector<m_type> convert(const void *p_ptr) { \
- const Array *arr = reinterpret_cast<const Array *>(p_ptr); \
- Vector<m_type> ret; \
- int len = arr->size(); \
- ret.resize(len); \
- { \
- m_type *w = ret.ptrw(); \
- for (int i = 0; i < len; i++) { \
- w[i] = (*arr)[i]; \
- } \
- } \
- return ret; \
- } \
- }
- // Special case for IPAddress.
- // No EncodeT because direct pointer conversion not possible.
- #define MAKE_STRINGCONV_BY_REFERENCE(m_type) \
- template <> \
- struct PtrToArg<m_type> { \
- _FORCE_INLINE_ static m_type convert(const void *p_ptr) { \
- m_type s = *reinterpret_cast<const String *>(p_ptr); \
- return s; \
- } \
- _FORCE_INLINE_ static void encode(const m_type &p_vec, void *p_ptr) { \
- String *arr = reinterpret_cast<String *>(p_ptr); \
- *arr = p_vec; \
- } \
- }; \
- \
- template <> \
- struct PtrToArg<const m_type &> { \
- _FORCE_INLINE_ static m_type convert(const void *p_ptr) { \
- m_type s = *reinterpret_cast<const String *>(p_ptr); \
- return s; \
- } \
- }
- MAKE_STRINGCONV_BY_REFERENCE(IPAddress);
- // No EncodeT because direct pointer conversion not possible.
- template <>
- struct PtrToArg<Vector<Face3>> {
- _FORCE_INLINE_ static Vector<Face3> convert(const void *p_ptr) {
- const Vector<Vector3> *dvs = reinterpret_cast<const Vector<Vector3> *>(p_ptr);
- Vector<Face3> ret;
- int len = dvs->size() / 3;
- ret.resize(len);
- {
- const Vector3 *r = dvs->ptr();
- Face3 *w = ret.ptrw();
- for (int i = 0; i < len; i++) {
- w[i].vertex[0] = r[i * 3 + 0];
- w[i].vertex[1] = r[i * 3 + 1];
- w[i].vertex[2] = r[i * 3 + 2];
- }
- }
- return ret;
- }
- _FORCE_INLINE_ static void encode(const Vector<Face3> &p_vec, void *p_ptr) {
- Vector<Vector3> *arr = reinterpret_cast<Vector<Vector3> *>(p_ptr);
- int len = p_vec.size();
- arr->resize(len * 3);
- {
- const Face3 *r = p_vec.ptr();
- Vector3 *w = arr->ptrw();
- for (int i = 0; i < len; i++) {
- w[i * 3 + 0] = r[i].vertex[0];
- w[i * 3 + 1] = r[i].vertex[1];
- w[i * 3 + 2] = r[i].vertex[2];
- }
- }
- }
- };
- // No EncodeT because direct pointer conversion not possible.
- template <>
- struct PtrToArg<const Vector<Face3> &> {
- _FORCE_INLINE_ static Vector<Face3> convert(const void *p_ptr) {
- const Vector<Vector3> *dvs = reinterpret_cast<const Vector<Vector3> *>(p_ptr);
- Vector<Face3> ret;
- int len = dvs->size() / 3;
- ret.resize(len);
- {
- const Vector3 *r = dvs->ptr();
- Face3 *w = ret.ptrw();
- for (int i = 0; i < len; i++) {
- w[i].vertex[0] = r[i * 3 + 0];
- w[i].vertex[1] = r[i * 3 + 1];
- w[i].vertex[2] = r[i * 3 + 2];
- }
- }
- return ret;
- }
- };
- #endif // METHOD_PTRCALL_H
|