Skip to content

Commit 617aed2

Browse files
Aidan63Aidan Lee
andauthored
Marshalling Types (#1189)
* Add marshal header for value type interop * finalise boxed objects if the type has a destructor * works with static cast * add value type tests * move more conversion functions to reference class * forward declare everything to avoid order issues * ValueType construction from boxed pointer * ValueType no longer inherits from struct * marshal reference interop with existing pointer types * Fix trying to dereference a null pointer when creating a boxed object from a reference * add compare traits specialisation for reference null check * consistent null checking and throwing * raise exceptions when value types are assigned null * reference equality operations * remove no return * remove some un-needed constructors * Make constructors use more templates and use reinterpret cast * use tagged dispatch * support pointer types being held in value type helper * Add new marshal pointer type * rename marshal types in prep for more pointer stuff * support reinterpreting value type references * seemingly working pointer marshalling types * better null checking with pointer references * add marshalling tests * Add pointer collection tests * Move enums into header files * test abstract around pointers * Pointer type changes * add pointer reference to pointer casting * add null test to pointer access * add test for vector of value types * add tests showing weird null behaviour * move marshalling tests to existing native tests * initial managed extern tests * managed extern local tests for two different naming schemes * reduce test duplication * Add tests for classes holding managed externs * change tests so that static functions are tested * un-private point * let enum abstract include meta handle it * remove analyser meta * Fix test related to recent pointer changes * Add pointer ctor overload * view marshalling type * Change to value semantics * Add view tests * Update some tests * String marshalling functions * Marshalling tests * view based string creation and support char16_t in various hxcpp types * Marshalling string tests * const ref a bit * Don't use byte size in copyTo comparison check * view extension tests * Split across multiple headers * typed read and write helper functions * Add compare function * cstring to view conversion function, and allow null pointers * Add view to pointer conversions * View length is now size_t * Add endian specific read and write marshal functions * Add OOB checks * change the public api to use int64_t over size_t * Fix string from view conversions to properly use the view length * Add OOB exception tests * bound check read and write * make sure view write index is int64_t * add tests for exceptions when reading or writing to small views * guard including new marshal headers * fix dodgy conditional --------- Co-authored-by: Aidan Lee <aidan.lee@evcam.com>
1 parent 646aaee commit 617aed2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+6089
-6
lines changed

include/Array.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ template<> struct ReturnNull<double> { typedef Dynamic type; };
3939
template<> struct ReturnNull<float> { typedef Dynamic type; };
4040
template<> struct ReturnNull<bool> { typedef Dynamic type; };
4141
template<> struct ReturnNull<char> { typedef Dynamic type; };
42+
template<> struct ReturnNull<char16_t> { typedef Dynamic type; };
4243
template<> struct ReturnNull<signed char> { typedef Dynamic type; };
4344
template<> struct ReturnNull<unsigned char> { typedef Dynamic type; };
4445
template<> struct ReturnNull<short> { typedef Dynamic type; };
@@ -431,6 +432,7 @@ template<> struct ArrayClassId<unsigned char> { enum { id=hx::clsIdArrayByte };
431432
template<> struct ArrayClassId<signed char> { enum { id=hx::clsIdArrayByte }; };
432433
template<> struct ArrayClassId<unsigned short> { enum { id=hx::clsIdArrayShort }; };
433434
template<> struct ArrayClassId<signed short> { enum { id=hx::clsIdArrayShort }; };
435+
template<> struct ArrayClassId<char16_t> { enum { id = hx::clsIdArrayShort }; };
434436
template<> struct ArrayClassId<unsigned int> { enum { id=hx::clsIdArrayInt }; };
435437
template<> struct ArrayClassId<signed int> { enum { id=hx::clsIdArrayInt }; };
436438
template<> struct ArrayClassId<float> { enum { id=hx::clsIdArrayFloat32 }; };

include/Dynamic.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class HXCPP_EXTERN_CLASS_ATTRIBUTES Dynamic : public hx::ObjectPtr<hx::Object>
2424
Dynamic(unsigned short inVal);
2525
Dynamic(unsigned char inVal);
2626
Dynamic(signed char inVal);
27+
Dynamic(char16_t inVal);
2728
Dynamic(const cpp::CppInt32__ &inVal);
2829
Dynamic(bool inVal);
2930
Dynamic(double inVal);
@@ -71,6 +72,7 @@ class HXCPP_EXTERN_CLASS_ATTRIBUTES Dynamic : public hx::ObjectPtr<hx::Object>
7172
inline operator unsigned char () const { return mPtr ? mPtr->__ToInt() : 0; }
7273
inline operator char () const { return mPtr ? mPtr->__ToInt() : 0; }
7374
inline operator signed char () const { return mPtr ? mPtr->__ToInt() : 0; }
75+
inline operator char16_t () const { return mPtr ? mPtr->__ToInt() : 0; }
7476
inline operator bool() const { return mPtr && mPtr->__ToInt(); }
7577
inline operator cpp::Int64() const { return mPtr ? mPtr->__ToInt64() : 0; }
7678
inline operator cpp::UInt64() const { return mPtr ? mPtr->__ToInt64() : 0; }
@@ -171,6 +173,7 @@ class HXCPP_EXTERN_CLASS_ATTRIBUTES Dynamic : public hx::ObjectPtr<hx::Object>
171173
bool operator op (unsigned short inRHS) const { return IsNumeric() && ((double)(*this) op (double)inRHS); } \
172174
bool operator op (signed char inRHS) const { return IsNumeric() && ((double)(*this) op (double)inRHS); } \
173175
bool operator op (unsigned char inRHS) const { return IsNumeric() && ((double)(*this) op (double)inRHS); } \
176+
bool operator op (char16_t inRHS) const { return IsNumeric() && ((double)(*this) op (double)inRHS); } \
174177
bool operator op (bool inRHS) const { return IsBool() && ((double)(*this) op (double)inRHS); } \
175178

176179
bool operator != (const String &inRHS) const { return !mPtr || ((String)(*this) != inRHS); }
@@ -184,6 +187,7 @@ class HXCPP_EXTERN_CLASS_ATTRIBUTES Dynamic : public hx::ObjectPtr<hx::Object>
184187
bool operator != (unsigned short inRHS) const { return !IsNumeric() || ((double)(*this) != (double)inRHS); }
185188
bool operator != (signed char inRHS) const { return !IsNumeric() || ((double)(*this) != (double)inRHS); }
186189
bool operator != (unsigned char inRHS) const { return !IsNumeric() || ((double)(*this) != (double)inRHS); }
190+
bool operator != (char16_t inRHS) const { return !IsNumeric() || ((double)(*this) != (double)inRHS); }
187191
bool operator != (bool inRHS) const { return !IsBool() || ((double)(*this) != (double)inRHS); }
188192

189193

@@ -229,6 +233,7 @@ class HXCPP_EXTERN_CLASS_ATTRIBUTES Dynamic : public hx::ObjectPtr<hx::Object>
229233
Dynamic operator+(const unsigned short &i) const;
230234
Dynamic operator+(const signed char &i) const;
231235
Dynamic operator+(const unsigned char &i) const;
236+
Dynamic operator+(const char16_t& i) const;
232237
Dynamic operator+(const double &d) const;
233238
Dynamic operator+(const float &d) const;
234239
Dynamic operator+(const cpp::Variant &d) const;
@@ -270,6 +275,8 @@ class HXCPP_EXTERN_CLASS_ATTRIBUTES Dynamic : public hx::ObjectPtr<hx::Object>
270275
{ return mPtr->__GetType()==vtInt ? Dynamic((int)(*this) op inRHS) : Dynamic((double)(*this) op inRHS); } \
271276
Dynamic operator op (const unsigned char &inRHS) const \
272277
{ return mPtr->__GetType()==vtInt ? Dynamic((int)(*this) op inRHS) : Dynamic((double)(*this) op inRHS); } \
278+
Dynamic operator op (const char16_t &inRHS) const \
279+
{ return mPtr->__GetType()==vtInt ? Dynamic((int)(*this) op inRHS) : Dynamic((double)(*this) op inRHS); } \
273280
Dynamic operator op (const cpp::Int64 &inRHS) const \
274281
{ return Dynamic((double)(*this) op inRHS); } \
275282
Dynamic operator op (const cpp::UInt64 &inRHS) const \
@@ -457,6 +464,7 @@ COMPARE_DYNAMIC_OP( > )
457464
inline double operator op (const unsigned short &inLHS,const Dynamic &inRHS) { return inLHS op (double)inRHS; } \
458465
inline double operator op (const signed char &inLHS,const Dynamic &inRHS) { return inLHS op (double)inRHS; } \
459466
inline double operator op (const unsigned char &inLHS,const Dynamic &inRHS) { return inLHS op (double)inRHS; } \
467+
inline double operator op (const char16_t &inLHS,const Dynamic &inRHS) { return inLHS op (double)inRHS; } \
460468

461469
ARITH_DYNAMIC( - )
462470
ARITH_DYNAMIC( + )

include/cpp/Pointer.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,15 @@ class Struct
131131
// This allows 'StaticCast' to be used from arrays
132132
typedef Dynamic Ptr;
133133

134-
inline Struct( ) { }
134+
inline Struct( ) : value() { }
135135
inline Struct( const T &inRHS ) : value(inRHS) { }
136136
inline Struct( const null &) { value = T(); }
137137
inline Struct( const Reference<T> &);
138138
inline Struct( const Dynamic &inRHS) { fromDynamic(inRHS.mPtr); }
139139

140+
template<class... TArgs>
141+
Struct(TArgs... args) : value(std::forward<TArgs>(args)...) {}
142+
140143
inline Struct<T,HANDLER> &operator=( const T &inRHS ) { value = inRHS; return *this; }
141144
inline Struct<T,HANDLER> &operator=( const null & ) { value = T(); return *this; }
142145
inline Struct<T,HANDLER> &operator=( const Dynamic &inRHS ) { return *this = Struct<T,HANDLER>(inRHS); }
@@ -212,6 +215,7 @@ class Pointer
212215
hx::Object *obj = inVariant.asObject();
213216
ptr = obj ? (T*)inVariant.valObject->__GetHandle() : 0;
214217
}
218+
inline Pointer(const ::cpp::marshal::PointerReference<T>);
215219

216220
template<typename O>
217221
inline Pointer( const O *inValue ) : ptr( (T*) inValue) { }
@@ -509,6 +513,11 @@ class Pointer_obj
509513
}
510514

511515

516+
template<typename T>
517+
inline static Pointer<T> addressOf(const ::cpp::marshal::ValueReference<T>&);
518+
519+
template<typename T>
520+
inline static Pointer<T*> addressOf(const ::cpp::marshal::PointerReference<T>&);
512521

513522
template<typename T>
514523
inline static Pointer<T> addressOf(T &value) { return Pointer<T>(&value); }

include/cpp/Variant.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ namespace cpp
109109
inline operator unsigned char () const { return asInt(); }
110110
inline operator char () const { return asInt(); }
111111
inline operator signed char () const { return asInt(); }
112+
inline operator char16_t() const { return asInt(); }
112113
inline operator cpp::Int64 () const { return asInt64(); }
113114
inline operator cpp::UInt64 () const { return asInt64(); }
114115
inline bool operator !() const { return !asInt(); }
@@ -206,6 +207,7 @@ namespace cpp
206207
inline bool operator op (unsigned short inRHS) const { return isNumeric() && (asDouble() op (double)inRHS); } \
207208
inline bool operator op (signed char inRHS) const { return isNumeric() && (asDouble() op (double)inRHS); } \
208209
inline bool operator op (unsigned char inRHS) const { return isNumeric() && (asDouble() op (double)inRHS); } \
210+
inline bool operator op (char16_t inRHS) const { return isNumeric() && (asDouble() op (double)inRHS); } \
209211
inline bool operator op (bool inRHS) const { return isBool() && (asDouble() op (double)inRHS); } \
210212
inline bool operator op (const Dynamic &inRHS) const { return Compare(inRHS) op 0; } \
211213

@@ -278,6 +280,7 @@ namespace cpp
278280
inline double operator op (const unsigned int &inLHS,const cpp::Variant &inRHS) { return inLHS op (double)inRHS; } \
279281
inline double operator op (const signed char &inLHS,const cpp::Variant &inRHS) { return inLHS op (double)inRHS; } \
280282
inline double operator op (const unsigned char &inLHS,const cpp::Variant &inRHS) { return inLHS op (double)inRHS; } \
283+
inline double operator op (const char16_t &inLHS,const cpp::Variant &inRHS) { return inLHS op (double)inRHS; } \
281284
inline double operator op (const signed short &inLHS,const cpp::Variant &inRHS) { return inLHS op (double)inRHS; } \
282285
inline double operator op (const unsigned short &inLHS,const cpp::Variant &inRHS) { return inLHS op (double)inRHS; } \
283286
inline double operator op (const cpp::Int64 &inLHS,const cpp::Variant &inRHS) { return inLHS op (double)inRHS; } \
@@ -604,6 +607,7 @@ HX_VARIANT_OP_ISEQ(short)
604607
HX_VARIANT_OP_ISEQ(unsigned short)
605608
HX_VARIANT_OP_ISEQ(signed char)
606609
HX_VARIANT_OP_ISEQ(unsigned char)
610+
HX_VARIANT_OP_ISEQ(char16_t)
607611
HX_VARIANT_OP_ISEQ(bool)
608612

609613
inline bool operator < (bool inLHS,const cpp::Variant &inRHS) { return false; }
@@ -633,6 +637,8 @@ inline bool operator > (bool inLHS,const cpp::Variant &inRHS) { return false; }
633637
{ return inRHS.isNumeric() && (inLHS op (double)inRHS); } \
634638
inline bool operator op (unsigned char inLHS,const ::cpp::Variant &inRHS) \
635639
{ return inRHS.isNumeric() && (inLHS op (double)inRHS); } \
640+
inline bool operator op (char16_t inLHS,const ::cpp::Variant &inRHS) \
641+
{ return inRHS.isNumeric() && (inLHS op (double)inRHS); } \
636642
inline bool operator op (const null &,const ::cpp::Variant &inRHS) \
637643
{ return false; } \
638644

include/cpp/marshal/Boxed.hpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#pragma once
2+
3+
#include "Definitions.inc"
4+
5+
template<class T>
6+
inline void cpp::marshal::Boxed_obj<T>::finalise(::hx::Object* obj)
7+
{
8+
auto ptr = reinterpret_cast<Boxed_obj<T>*>(obj);
9+
10+
ptr->value.~T();
11+
}
12+
13+
template<class T>
14+
inline void cpp::marshal::Boxed_obj<T>::setFinaliser(std::true_type)
15+
{
16+
::hx::GCSetFinalizer(this, finalise);
17+
}
18+
19+
template<class T>
20+
inline void cpp::marshal::Boxed_obj<T>::setFinaliser(std::false_type) {}
21+
22+
template<class T>
23+
inline cpp::marshal::Boxed_obj<T>::Boxed_obj() : value()
24+
{
25+
setFinaliser(std::is_destructible<T>{});
26+
}
27+
28+
template<class T>
29+
inline cpp::marshal::Boxed_obj<T>::Boxed_obj(T* ptr) : value(*ptr)
30+
{
31+
setFinaliser(std::is_destructible<T>{});
32+
}
33+
34+
template<class T>
35+
template<class ...TArgs>
36+
inline cpp::marshal::Boxed_obj<T>::Boxed_obj(TArgs... args) : value(std::forward<TArgs>(args)...)
37+
{
38+
setFinaliser(std::is_destructible<T>{});
39+
}

0 commit comments

Comments
 (0)