git » swiftpm » main » tree

[main] / Sources / c_snikket / iinclude / cpp / Pointer.h

  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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
#ifndef CPP_POINTER_H
#define CPP_POINTER_H

namespace cpp
{

struct AutoCast
{
   void *value;

   explicit inline AutoCast(void *inValue) : value(inValue) { }
};


struct RawAutoCast
{
   void *value;

   explicit inline RawAutoCast(void *inValue) : value(inValue) { }

   template<typename T>
   operator T*() const { return (T*)value; }
};


Dynamic CreateDynamicPointer(void *inValue);

enum DynamicHandlerOp
{
   dhoGetClassName,
   dhoToString,
   dhoFromDynamic,
   dhoToDynamic,
   dhoIs,
};
typedef void (*DynamicHandlerFunc)(DynamicHandlerOp op, void *ioValue, int inSize, void *outResult);
Dynamic CreateDynamicStruct(const void *inValue, int inSize, DynamicHandlerFunc inFunc);

template<typename T> class Reference;



struct StructHandlerDynamicParams
{
   StructHandlerDynamicParams(hx::Object *data,const char *inName) :
       outProcessed(false), inName(inName), inData(data) { }
   bool outProcessed;
   hx::Object *inData;
   const char *inName;
};


class DefaultStructHandler
{
   public:
      static inline const char *getName() { return "unknown"; }
      static inline String toString( const void *inValue ) { return HX_CSTRING("Struct"); }
      static inline void handler(DynamicHandlerOp op, void *ioValue, int inSize, void *outResult)
      {
         if (op==dhoToString)
            *(String *)outResult = toString(ioValue);
         else if (op==dhoGetClassName)
            *(const char **)outResult = getName();
         else if (op==dhoToDynamic)
         {
            // Handle outsize..
            *(hx::Object **)outResult = 0;
         }
         else if (op==dhoFromDynamic)
         {
            StructHandlerDynamicParams *params = (StructHandlerDynamicParams *)outResult;
            hx::Object *ptr= params->inData;
            void *data = (void *)ptr->__GetHandle();
            int len = ptr->__length();
            if (data && len>=inSize && ptr->__CStr()==params->inName)
            {
               memcpy(ioValue,data,inSize);
               params->outProcessed = true;
            }
         }
         else if (op==dhoIs)
         {
            StructHandlerDynamicParams *params = (StructHandlerDynamicParams *)outResult;
            hx::Object *ptr= params->inData;
            void *data = (void *)ptr->__GetHandle();
            int len = ptr->__length();
            params->outProcessed = data && len>=inSize && ptr->__CStr()==params->inName;
         }
      }
};


class EnumHandler
{
   public:
      static inline const char *getName() { return "enum"; }
      static inline String toString( const void *inValue ) {
         int val = inValue ? *(int *)inValue : 0;
         return HX_CSTRING("enum(") + String(val) + HX_CSTRING(")");
      }

      static inline void handler(DynamicHandlerOp op, void *ioValue, int inSize, void *outResult)
      {
         if (op==dhoToString)
            *(String *)outResult = toString(ioValue);
         else if (op==dhoGetClassName)
            *(const char **)outResult = getName();
         else if (op==dhoFromDynamic)
         {
            StructHandlerDynamicParams *params = (StructHandlerDynamicParams *)outResult;
            if (params->inData->__GetType()==vtInt)
            {
               *(int *)ioValue =  params->inData->__ToInt();
               params->outProcessed = true;
            }
            else
               DefaultStructHandler::handler(op,ioValue, inSize, outResult);
         }
         else
            DefaultStructHandler::handler(op,ioValue, inSize, outResult);
      }
};



template<typename T, typename HANDLER = DefaultStructHandler >
class Struct
{
public:
   T value;
   // This allows 'StaticCast' to be used from arrays
   typedef Dynamic Ptr;

   inline Struct( ) {  }
   inline Struct( const T &inRHS ) : value(inRHS) {  }
   inline Struct( const null &) { value = T(); }
   inline Struct( const Reference<T> &);
   inline Struct( const Dynamic &inRHS) { fromDynamic(inRHS.mPtr); }

   inline Struct<T,HANDLER> &operator=( const T &inRHS ) { value = inRHS; return *this; }
   inline Struct<T,HANDLER> &operator=( const null & ) { value = T(); return *this; }
   inline Struct<T,HANDLER> &operator=( const Dynamic &inRHS ) { return *this = Struct<T,HANDLER>(inRHS); }

   operator Dynamic() const
   {
      hx::Object *result = 0;
      HANDLER::handler(dhoToDynamic, (void *)&value, sizeof(T), &result );
      if (result)
         return result;
      return CreateDynamicStruct( &value, sizeof(T), HANDLER::handler);
   }
   operator String() const { return HANDLER::toString(&value); }

   inline Struct( const hx::Val &inRHS) { fromDynamic(inRHS.asObject()); }
   operator hx::Val() const { return operator Dynamic(); }

   bool operator==(const Struct<T,HANDLER> &inRHS) const { return value==inRHS.value; }
   bool operator==(const null &inRHS) const { return false; }
   bool operator!=(const null &inRHS) const { return true; }

   // Haxe uses -> notation
   inline T *operator->() { return &value; }

   T &get() { return value; }

   static inline bool is( const Dynamic &inRHS)
   {
      hx::Object *ptr = inRHS.mPtr;
      if (!ptr)
         return false;
      StructHandlerDynamicParams convert(ptr, ptr->__CStr());
      HANDLER::handler(dhoIs, 0, sizeof(T), &convert );
      return convert.outProcessed;
   }


   inline void fromDynamic( hx::Object *ptr)
   {
      if (!ptr)
      {
         value = T();
         return;
      }
      StructHandlerDynamicParams convert(ptr, ptr->__CStr());
      HANDLER::handler(dhoFromDynamic, &value, sizeof(T), &convert );
      if (!convert.outProcessed)
      {
         hx::NullReference("DynamicData", true);
         return;
      }
   }

   inline operator T& () { return value; }
};




template<typename T>
class Pointer
{
public:
   typedef T elementType;

   T *ptr;

   inline Pointer( ) : ptr(0) { }
   inline Pointer( const Pointer &inRHS ) : ptr(inRHS.ptr) {  }
   inline Pointer( const Dynamic &inRHS) { ptr = inRHS==null()?0: (T*)inRHS->__GetHandle(); }
   inline Pointer( const null &inRHS ) : ptr(0) { }
   inline Pointer( const cpp::Variant &inVariant ) {
      hx::Object *obj = inVariant.asObject();
      ptr = obj  ? (T*)inVariant.valObject->__GetHandle() : 0;
   }

   template<typename O>
   inline Pointer( const O *inValue ) : ptr( (T*) inValue) { }
   //inline Pointer( T *inValue ) : ptr(inValue) { }
   inline Pointer( AutoCast inValue ) : ptr( (T*)inValue.value) { }

   template<typename H>
   inline Pointer( const Struct<T,H> &structVal ) : ptr( &structVal.value ) { }

   template<typename O>
   inline void setRaw(const O *inValue ) { ptr =  (T*) inValue; }
   

   inline Pointer operator=( const Pointer &inRHS ) { return ptr = inRHS.ptr; }
   inline Dynamic operator=( Dynamic &inValue )
   {
      ptr = inValue==null() ? 0 : (T*) inValue->__GetHandle();
      return inValue;
   }
   inline Dynamic operator=( null &inValue ) { ptr=0; return inValue; }

   template<typename O>
   inline Pointer operator=( const Pointer<O> &inValue ) { ptr = (T*) inValue.ptr; return *this; }

   template<typename O>
   inline Pointer operator=( const O *inValue ) { ptr = (T*) inValue; return *this; }

   template<typename H>
   inline Pointer operator=( const Struct<T,H> &structVal ) { ptr = &structVal.value; return *this; }



   inline AutoCast reinterpret() { return AutoCast(ptr); }
   inline RawAutoCast rawCast() { return RawAutoCast(ptr); }

   inline bool operator==( const null &inValue ) const { return ptr==0; }
   inline bool operator!=( const null &inValue ) const { return ptr!=0; }

   // Allow '->' syntax
   inline Pointer *operator->() { return this; }
 	inline Pointer inc() { return ++ptr; }
	inline Pointer dec() { return --ptr; }
	inline Pointer add(int inInt) { return ptr+inInt; }
	inline Pointer sub(int inInt) { return ptr-inInt; }
 	inline Pointer incBy(int inDiff) { ptr+=inDiff; return ptr; }
 	inline Pointer decBy(int inDiff) { ptr-=inDiff; return ptr; }
 	inline T &postIncRef() { return *ptr++; }
 	inline T &postIncVal() { return *ptr++; }

   inline T &at(int inIndex) { return ptr[inIndex]; }
   inline void setAt(int inIndex, const T &test) { ptr[inIndex] = test; }

   inline T &__get(int inIndex) { return ptr[inIndex]; }
   inline T &__set(int inIndex, const T &inValue) { T *p = ptr+inIndex; *p = inValue; return *p; }

   inline T &get_value() { return *ptr; }
   inline T &get_ref() { return *ptr; }
   inline T &set_ref(const T &inValue) { return *ptr = inValue;  }

   operator Dynamic () const { return CreateDynamicPointer((void *)ptr); }
   operator cpp::Variant () const { return CreateDynamicPointer((void *)ptr); }

   operator T * () { return ptr; }
   T * get_raw() { return ptr; }
   const T * get_constRaw() { return ptr; }

   inline void destroy() { delete ptr; }
   inline void destroyArray() { delete [] ptr; }

   inline bool lt(Pointer inOther) { return ptr < inOther.ptr; }
   inline bool gt(Pointer inOther) { return ptr > inOther.ptr; }
   inline bool leq(Pointer inOther) { return ptr <= inOther.ptr; }
   inline bool geq(Pointer inOther) { return ptr >= inOther.ptr; }

};





template<>
class Pointer<void>
{
public:
   enum { elementSize = 0 };

   void *ptr;

   inline Pointer( ) : ptr(0) { }
   inline Pointer( const Pointer &inRHS ) : ptr(inRHS.ptr) {  }
   inline Pointer( const Dynamic &inRHS) { ptr = inRHS==null()?0: (void*)inRHS->__GetHandle(); }
   inline Pointer( const null &inRHS ) : ptr(0) { }

   template<typename O>
   inline Pointer( const O *inValue ) : ptr( (void*) inValue) { }
   //inline Pointer( T *inValue ) : ptr(inValue) { }
   inline Pointer( AutoCast inValue ) : ptr( (void*)inValue.value) { }

   inline Pointer operator=( const Pointer &inRHS ) { return ptr = inRHS.ptr; }
   inline Dynamic operator=( Dynamic &inValue )
   {
      ptr = inValue==null() ? 0 : (void*) inValue->__GetHandle();
      return inValue;
   }
   inline Dynamic operator=( null &inValue ) { ptr=0; return inValue; }
   inline AutoCast reinterpret() { return AutoCast(ptr); }
   inline RawAutoCast rawCast() { return RawAutoCast(ptr); }

   inline bool operator==( const null &inValue ) const { return ptr==0; }
   inline bool operator!=( const null &inValue ) const { return ptr!=0; }

   // Allow '->' syntax
   inline Pointer *operator->() { return this; }
 	inline Pointer inc() { return ptr; }
	inline Pointer dec() { return ptr; }
	inline Pointer add(int inInt) { return ptr; }
	inline Pointer sub(int inInt) { return ptr; }
 	inline Pointer incBy(int inDiff) { return ptr; }
 	inline Pointer decBy(int inDiff) { return ptr; }
 	inline void postIncRef() {  }
 	inline void postIncVal() {  }

   inline void at(int inIndex) {  }

   inline void __get(int inIndex) { }

   template<typename O>
   inline void __set(int inIndex, O inValue) { }

   inline void get_value() {  }
   inline void get_ref() {  }
   template<typename O> inline void set_ref(O val) {  }

   operator Dynamic () const { return CreateDynamicPointer(ptr); }
   //operator hx::Val () const { return CreateDynamicPointer((void *)ptr); }
   operator void * () { return ptr; }
   void * get_raw() { return ptr; }
   const void * get_constRaw() { return ptr; }

   inline void destroy() {  }
   inline void destroyArray() {  }

   inline bool lt(Pointer inOther) { return ptr < inOther.ptr; }
   inline bool gt(Pointer inOther) { return ptr > inOther.ptr; }
   inline bool leq(Pointer inOther) { return ptr <= inOther.ptr; }
   inline bool geq(Pointer inOther) { return ptr >= inOther.ptr; }

};




template<typename T>
inline bool operator == (const null &, Pointer<T> inPtr) { return inPtr.ptr==0; }
template<typename T>
inline bool operator != (const null &, Pointer<T> inPtr) { return inPtr.ptr!=0; }



template<typename T>
class Reference : public Pointer<T>
{
public:
   using Pointer<T>::ptr;


   inline Reference( const T &inRHS ) : Pointer<T>(&inRHS) {  }
   inline Reference( T &inRHS ) : Pointer<T>(&inRHS) {  }

   inline Reference( ) : Pointer<T>((T*)0) { }
   inline Reference( const Reference &inRHS ) : Pointer<T>(inRHS.ptr) {  }
   inline Reference( const Dynamic &inRHS) { ptr = inRHS==null()?0: (T*)inRHS->__GetHandle(); }
   inline Reference( const null &inRHS ) : Pointer<T>((T*)0) { }
   inline Reference( const T *inValue ) : Pointer<T>( (T*) inValue) { }
   //inline Reference( T *inValue ) : Pointer(inValue) { }
   inline Reference( AutoCast inValue ) : Pointer<T>( (T*)inValue.value) { }

   template<typename OTHER>
   inline Reference( const Reference<OTHER> &inOther )
   {
      // Allow reinterpret or not?
      ptr = (T*)inOther.ptr;
   }

   template<typename H>
   inline Reference( const Struct<T,H> &structVal ) : Pointer<T>( &structVal.value ) { }

   inline Reference operator=( const Reference &inRHS ) { return ptr = inRHS.ptr; }


   inline T *operator->() const { return ptr; }
   
   inline operator T &() { return *ptr; }

};

template<typename T,typename H>
Struct<T,H>::Struct( const Reference<T> &ref ) : value(*ref.ptr) { };



template<typename T>
class Function
{
public:
   T *call;

   inline Function( ) { }
   inline Function( const Function &inRHS ) : call(inRHS.call) {  }
   inline Function( const Dynamic &inRHS) { call = inRHS==null()?0: (T*)inRHS->__GetHandle(); }
   inline Function( const null &inRHS ) { call = 0; }
   inline Function( T *inValue ) : call((T*)(inValue)) { }
   //inline Function( T *inValue ) : call(inValue) { }
   inline Function( AutoCast inValue ) : call( (T*)inValue.value) { }
   inline Function( const hx::AnyCast &inValue ) : call( (T*)inValue.mPtr) { }

   template<typename FROM>
   inline static Function __new(FROM from)
   {
      return Function(from);
   }

   inline Function operator=( const Function &inRHS ) { return call = inRHS.call; }
   inline Dynamic operator=( Dynamic &inValue )
   {
      call = inValue==null() ? 0 : (T*) inValue->__GetHandle();
      return inValue;
   }
   inline Dynamic operator=( null &inValue ) { call=0; return inValue; }
   inline bool operator==( const null &inValue ) const { return call==0; }
   inline bool operator!=( const null &inValue ) const { return call!=0; }


   operator Dynamic () const { return CreateDynamicPointer((void *)call); }
   //operator hx::Val () const { return CreateDynamicPointer((void *)call); }
   operator T * () { return call; }
   operator void * () { return (void *)call; }

   inline T &get_call() { return *call; }

   inline bool lt(Function inOther) { return call < inOther.call; }
   inline bool gt(Function inOther) { return call > inOther.call; }
   inline bool leq(Function inOther) { return call <= inOther.call; }
   inline bool geq(Function inOther) { return call >= inOther.call; }

};


template<typename T>
inline bool operator == (const null &, Function<T> inPtr) { return inPtr.call==0; }
template<typename T>
inline bool operator != (const null &, Function<T> inPtr) { return inPtr.call!=0; }



class Function_obj
{
public:

	inline static AutoCast getProcAddress(String inLib, String inPrim)
   {
      return AutoCast(__hxcpp_get_proc_address(inLib, inPrim,false));
   }


   template<typename T>
	inline static AutoCast fromStaticFunction(T *inFunction)
   {
      return AutoCast(inFunction);
   }
};


class Pointer_obj
{
public:
   template<typename T>
	inline static AutoCast arrayElem(::Array<T> array, int inIndex)  { return AutoCast(&array[inIndex]); }
	inline static AutoCast arrayElem(Dynamic inVal, int inIndex)
   {
      if (inVal==null() || !inVal->__IsArray())
         return AutoCast(0);
      hx::ArrayBase *base = (hx::ArrayBase *)inVal.GetPtr();
      return AutoCast(base->GetBase() + inIndex*base->GetElementSize());
   }

   template<typename T>
	inline static AutoCast ofArray(::Array<T> array)  { return AutoCast(&array[0]); }
	inline static AutoCast ofArray(Dynamic inVal)
   {
      if (inVal==null() || !inVal->__IsArray())
         return AutoCast(0);
      hx::ArrayBase *base = (hx::ArrayBase *)inVal.GetPtr();
      return AutoCast(base->GetBase());
   }



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

   template<typename T>
	inline static Pointer<void> endOf(hx::ObjectPtr<T> value)  { return (void *)(value.mPtr+1); }

   template<typename T>
	inline static Pointer<T> fromPointer(T *value)  { return Pointer<T>(value); }
   template<typename T>
	inline static Pointer<T> fromPointer(const T *value)  { return Pointer<T>(value); }

   template<typename T>
	inline static Pointer<T> fromRaw(T *value)  { return Pointer<T>(value); }
   template<typename T>
	inline static Pointer<T> fromRaw(const T *value)  { return Pointer<T>(value); }
	inline static Pointer<void> fromRaw(const AutoCast &inAutoCast)  { return Pointer<void>(inAutoCast.value); }
	inline static Pointer<void> fromRaw(const RawAutoCast &inAutoCast)  { return Pointer<void>(inAutoCast.value); }


   inline static AutoCast fromHandle(Dynamic inValue, String inKind)
   {
      if (inValue==null() || (inKind!=null() && inKind!=__hxcpp_get_kind(inValue)))
         return AutoCast(0);
      return AutoCast(inValue->__GetHandle());
   }
};


class Reference_obj
{
public:

};



} // end namespace cpp

namespace hx
{
template <typename T>
T *StarOf(T &x) { return &x; }
}


#endif