-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlgencall.hpp
executable file
·2016 lines (1866 loc) · 64 KB
/
lgencall.hpp
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************
* Copyright (C) 2011 Olivetti Engineering SA, CH 1400 Yverdon-les-Bains.
* Original author: Patrick Rapin. All rights reserved.
*
* 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.
******************************************************************************/
// Version 2.2.3
#ifndef LUA_CLASSES_BASED_CALL_H
#define LUA_CLASSES_BASED_CALL_H
/* LCBC_USE_WIDESTRING defines the support for wide character (Unicode) strings:
0 : wchar_t and wstring are not supported; to make sure they are #defined to dummy types;
1 : wchar_t and wstring can be used as arguments to Input, Output and Script classes.
You have to call WideString::SetMode<>() template function to set the desired conversion.
The template parameter of SetMode can be one of these:
RawMode : no conversion, wide character strings are pushed verbatim to the stack (raw bytes);
script snippets cannot be wchar_t* (you would get syntax errors looking like Chineese)
LocaleMode : conversions between wchar_t* and char* use wctomb and mbtowc standard functions;
the resulting strings depend on your current locale;
Utf8Mode : conversions between wchar_t* and char* are done by the library, using UTF-8 encoding */
#ifndef LCBC_USE_WIDESTRING
#define LCBC_USE_WIDESTRING 1
#endif
/* LCBC_USE_CSL defines if data containers and some other classes from the
C++ Standard Library should be handled. The supported classes are :
- string, wstring
- vector<T>, map<K,T>, list<T>, set<T>, deque<T>, multiset<T>, multimap<K,T>
- queue<T,C>, priority_queue<T,C>, stack<T,C>
- pair<T1,T2>, valarray<T>, bitset<N>
0: no support;
1: C++ Standard Library data can be exchanged with Lua.
*/
#ifndef LCBC_USE_CSL
#define LCBC_USE_CSL 0
#endif
#ifndef LCBC_USE_QT
#define LCBC_USE_QT 0
#endif
/* LCBC_USE_MFC enables some Microsoft Foundation Classes (MFC)
objects to be directly handled. The supported classes are :
- CObject* : using (de-)serialization in a userdata
- CString (both ANSI and Unicode)
- CArray<T,A>, CTypedPtrArray<B,T>, CByteArray, CDWordArray, CObArray,
CPtrArray, CStringArray, CUIntArray, CWordArray
- CList<T,A>, CTypedPtrList<B,T>, CPtrList, CObList, CStringList
- CMap<K,AK,V,AV>, CTypedPtrMap<B,K,V>, CMapWordToPtr, CMapPtrToWord,
CMapPtrToPtr, CMapWordToOb, CMapStringToPtr, CMapStringToOb, CMapStringToString
- CPoint, CRect, CSize, CTime, CTimeSpan
0: no support
1: Microsoft Foundation Classes data can be exchanged with Lua
*/
#ifndef LCBC_USE_MFC
#define LCBC_USE_MFC 0
#endif
#ifndef LCBC_USE_TINYXML
#define LCBC_USE_TINYXML 0
#endif
/* LCBC_USE_EXCEPTIONS enables use of exceptions to signal Lua error.
On some embedded systems, exceptions are switched off to save code.
*/
#ifndef LCBC_USE_EXCEPTIONS
#define LCBC_USE_EXCEPTIONS 1
#endif
#ifndef LUA_VERSION_MAJOR
extern "C" {
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
}
#endif
#include <cstring>
#include <cstdlib>
#if LCBC_USE_WIDESTRING
#include <cwchar>
#endif
#if LCBC_USE_CSL
#include <string>
#include <vector>
#include <deque>
#include <map>
#include <list>
#include <set>
#include <queue>
#include <stack>
#include <valarray>
#include <bitset>
#endif
#if LCBC_USE_QT
#include <QString>
#include <QLatin1String>
#include <QDate>
#include <QDateTime>
#include <QByteArray>
#include <QTime>
#endif
#if LCBC_USE_MFC
#define _WIN32_WINNT 0x0500
#include <afx.h>
#include <afxtempl.h>
#endif
#if LCBC_USE_TINYXML
#include <tinyxml.h>
#endif
#if (LUA_VERSION_NUM >= 502) && !defined(lua_objlen)
#define lua_objlen(L,i) lua_rawlen(L, (i))
#endif
namespace lua {
using namespace std;
enum eNil { nil };
#if !LCBC_USE_WIDESTRING
enum dummy_wchar_t {};
class dummy_wstring {};
#define wchar_t dummy_wchar_t
#define wstring dummy_wstring
size_t wcslen(const dummy_wchar_t*);
#endif
#if !LCBC_USE_QT
enum dummy_QString {};
#define QString dummy_QString
#endif
enum WideStringMode { RawMode, LocaleMode, Utf8Mode };
class WideString
{
public:
template<WideStringMode mode> static void SetMode(lua_State* L)
{
lua_pushcfunction(L, Push<mode>);
lua_setfield(L, LUA_REGISTRYINDEX, "LuaClassBasedPushWideString");
lua_pushcfunction(L, Get<mode>);
lua_setfield(L, LUA_REGISTRYINDEX, "LuaClassBasedGetWideString");
}
template<WideStringMode input_mode, WideStringMode output_mode> static void SetMode(lua_State* L)
{
lua_pushcfunction(L, Push<input_mode>);
lua_setfield(L, LUA_REGISTRYINDEX, "LuaClassBasedPushWideString");
lua_pushcfunction(L, Get<output_mode>);
lua_setfield(L, LUA_REGISTRYINDEX, "LuaClassBasedGetWideString");
}
static void Push(lua_State* L, const wchar_t* str) { Push(L, str, wcslen(str)); }
static void Push(lua_State* L, const wchar_t* str, size_t len)
{
lua_getfield(L, LUA_REGISTRYINDEX, "LuaClassBasedPushWideString");
lua_pushlstring(L, (const char*)str, len*sizeof(wchar_t));
lua_call(L, 1, 1);
}
static const wchar_t* Get(lua_State* L, int idx) { size_t size; return Get(L, idx, size); }
static const wchar_t* Get(lua_State* L, int idx, size_t& size)
{
lua_getfield(L, LUA_REGISTRYINDEX, "LuaClassBasedGetWideString");
lua_pushvalue(L, idx);
lua_call(L, 1, 1);
lua_replace(L, idx);
const wchar_t* res = (const wchar_t*)lua_tolstring(L, idx, &size);
size /= sizeof(wchar_t);
return res;
}
private:
template<WideStringMode mode> static int Push(lua_State* L);
template<WideStringMode mode> static int Get(lua_State* L);
};
class QtString
{
public:
static QString Get(lua_State* L, int idx) { size_t size; return Get(L, idx, size); }
static QString Get(lua_State* L, int idx, size_t& size);
static void Push(lua_State* L, const QString& str);
};
class Input;
class Registry
{
public:
Registry(const Input& input_) : input(input_) {}
const Input& get() const { return input; }
private:
const Input& input;
};
class Input
{
public:
Input(eNil) { pPush = &Input::PushNil; }
Input(bool value) { pPush = &Input::PushBoolean; BooleanValue = value; }
Input(char value) { pPush = &Input::PushCharacter; CharacterValue = value; }
Input(wchar_t value) { pPush = &Input::PushWideChar; WideCharValue = value; }
Input(lua_CFunction value) { pPush = &Input::PushFunction; FunctionValue = value; Size = 0;}
Input(lua_CFunction value, size_t len) { pPush = &Input::PushFunction; FunctionValue = value; Size=len; }
Input(const Registry& value) { pPush = &Input::PushRegistry; PointerValue = &value; }
template<class T> Input(T value) { pPush = &Input::PushNumber; NumberValue = (lua_Number)value; }
template<class T> Input(T* value) { pPush = &Input::PushValue<T>; PointerValue = value; }
template<class T> Input(const T* value) { pPush = &Input::PushValue<T>; PointerValue = value; }
template<class T> Input(const T* value, size_t size) { pPush = &Input::PushSizedValue<T>; PointerValue = value; Size = size; }
template<class T> Input(size_t len, const T* value) { pPush = &Input::PushArray<T>; PointerValue = value; Size=len; }
template<class T, size_t L2> Input(size_t len1, const T value[][L2]) { pPush = &Input::Push2DArray<T,L2>; PointerValue = value; Size=len1; }
#if LCBC_USE_CSL
Input(const string& value);
Input(const wstring& value);
template<class T1, class T2> Input(const pair<T1,T2>& value) { pPush = &Input::PushPair<pair<T1,T2> >; PointerValue = &value; }
template<class T, class A> Input(const vector<T,A>& value) { pPush = &Input::PushContainer<vector<T,A> >; PointerValue = &value; }
template<class T, class A> Input(const list<T,A>& value) { pPush = &Input::PushContainer<list<T,A> >; PointerValue = &value; }
template<class T, class A> Input(const deque<T,A>& value) { pPush = &Input::PushContainer<deque<T,A> >; PointerValue = &value; }
template<class K, class T, class C, class A> Input(const map<K,T,C,A>& value) { pPush = &Input::PushMap<map<K,T,C,A> >; PointerValue = &value; }
template<class K, class T, class C, class A> Input(const multimap<K,T,C,A>& value) { pPush = &Input::PushContainer<multimap<K,T,C,A> >; PointerValue = &value; }
template<class T, class C, class A> Input(const set<T,C,A>& value) { pPush = &Input::PushSet<set<T,C,A> >; PointerValue = &value; }
template<class T, class C, class A> Input(const multiset<T,C,A>& value) { pPush = &Input::PushSet<multiset<T,C,A> >; PointerValue = &value; }
template<class T, class C> Input(const queue<T,C>& value) { pPush = &Input::PushQueue<queue<T,C> >; PointerValue = &value; }
template<class T, class C> Input(const stack<T,C>& value) { pPush = &Input::PushQueue<stack<T,C> >; PointerValue = &value; }
template<class T, class C, class P> Input(const priority_queue<T,C,P>& value) { pPush = &Input::PushQueue<priority_queue<T,C,P> >; PointerValue = &value; }
template<class T> Input(const valarray<T>& value) { pPush = &Input::PushValArray<valarray<T> >; PointerValue = &value; }
template<size_t N> Input(const bitset<N>& value) { pPush = &Input::PushValArray<bitset<N> >; PointerValue = &value; }
#endif
#if LCBC_USE_MFC
Input(const CStringA& value);
Input(const CStringW& value);
Input(const CPoint& value);
Input(const CRect& value);
Input(const CSize& value);
Input(const CTime& value);
Input(const CTimeSpan& value);
template<class T, class A> Input(const CArray<T,A>& value) { pPush = &Input::PushCArray<CArray<T,A> >; PointerValue = &value; }
template<class B, class T> Input(const CTypedPtrArray<B,T>& value) { pPush = &Input::PushCArray<CTypedPtrArray<B,T> >; PointerValue = &value; }
Input(const CByteArray& value) { pPush = &Input::PushCArray<CByteArray>; PointerValue = &value; }
Input(const CDWordArray& value) { pPush = &Input::PushCArray<CDWordArray>; PointerValue = &value; }
Input(const CObArray& value) { pPush = &Input::PushCArray<CObArray>; PointerValue = &value; }
Input(const CPtrArray& value) { pPush = &Input::PushCArray<CPtrArray>; PointerValue = &value; }
Input(const CStringArray& value) { pPush = &Input::PushCArray<CStringArray>; PointerValue = &value; }
Input(const CUIntArray& value) { pPush = &Input::PushCArray<CUIntArray>; PointerValue = &value; }
Input(const CWordArray& value) { pPush = &Input::PushCArray<CWordArray>; PointerValue = &value; }
template<class T, class A> Input(const CList<T,A>& value) { pPush = &Input::PushCList<CList<T,A> >; PointerValue = &value; }
template<class B, class T> Input(const CTypedPtrList<B,T>& value) { pPush = &Input::PushCList<CTypedPtrList<B,T> >; PointerValue = &value; }
Input(const CPtrList& value) { pPush = &Input::PushCList<CPtrList>; PointerValue = &value; }
Input(const CObList& value) { pPush = &Input::PushCList<CObList>; PointerValue = &value; }
Input(const CStringList& value) { pPush = &Input::PushCList<CStringList>; PointerValue = &value; }
template<class K, class AK, class V, class AV> Input(const CMap<K,AK,V,AV>& value) { pPush = &Input::PushCMap<CMap<K,AK,V,AV>,K,V>; PointerValue = &value; }
template<class B, class K, class V> Input(const CTypedPtrMap<B,K,V>& value) { pPush = &Input::PushCMap<CTypedPtrMap<B,K,V>,K,V>; PointerValue = &value; }
Input(const CMapWordToPtr& value) { pPush = &Input::PushCMap<CMapWordToPtr, WORD, void*>; PointerValue = &value; }
Input(const CMapPtrToWord& value) { pPush = &Input::PushCMap<CMapPtrToWord, void*, WORD>; PointerValue = &value; }
Input(const CMapPtrToPtr& value) { pPush = &Input::PushCMap<CMapPtrToPtr, void*, void*>; PointerValue = &value; }
Input(const CMapWordToOb& value) { pPush = &Input::PushCMap<CMapWordToOb, WORD, CObject*>; PointerValue = &value; }
Input(const CMapStringToPtr& value) { pPush = &Input::PushCMap<CMapStringToPtr, CString, void*>; PointerValue = &value; }
Input(const CMapStringToOb& value) { pPush = &Input::PushCMap<CMapStringToOb, CString, CObject*>; PointerValue = &value; }
Input(const CMapStringToString& value) { pPush = &Input::PushCMap<CMapStringToString, CString, CString>; PointerValue = &value; }
#endif
#if LCBC_USE_QT
Input(const QString& value);
Input(const QLatin1String& value);
Input(const QDate& value);
Input(const QDateTime& value);
Input(const QByteArray& value);
Input(const QTime& value);
Input(const QChar& value);
Input(const QLatin1Char& value);
#endif
#if LCBC_USE_TINYXML
Input(const TiXmlDocument& value) { pPush = &Input::PushTiXmlDocument; PointerValue = &value; }
#endif
void Push(lua_State* L) const { (this->*pPush)(L); }
private:
void PushNil(lua_State* L) const { lua_pushnil(L); }
void PushBoolean(lua_State* L) const { lua_pushboolean(L, BooleanValue); }
void PushCharacter(lua_State* L) const { lua_pushlstring(L, &CharacterValue, 1); }
void PushWideChar(lua_State* L) const { WideString::Push(L, &WideCharValue, 1); }
void PushNumber(lua_State* L) const { lua_pushnumber(L, NumberValue); }
void PushFunction(lua_State* L) const { lua_pushcclosure(L, FunctionValue, (int)Size); }
void PushRegistry(lua_State* L) const { ((const Registry*)PointerValue)->get().Push(L); lua_rawget(L, LUA_REGISTRYINDEX); }
template<class T> void PushNumber(lua_State* L) const { lua_pushnumber(L, lua_Number(*(T*)PointerValue)); }
template<class T> void PushValue(lua_State* L) const;
template<class T> void PushSizedValue(lua_State* L) const;
template<class T> void PushArray(lua_State* L) const;
template<class T, size_t L2> void Push2DArray(lua_State* L) const;
template<class T> void PushContainer(lua_State* L, const T* val) const;
template<class T> void PushContainer(lua_State* L) const { return PushContainer(L, (const T*)PointerValue); }
template<class T> void PushSet(lua_State* L) const;
template<class T> void PushMap(lua_State* L) const;
template<class T> void PushQueue(lua_State* L) const;
template<class T> void PushValArray(lua_State* L) const;
template<class T> void PushPair(lua_State* L) const;
template<class T> void PushCArray(lua_State* L) const;
template<class T> void PushCList(lua_State* L) const;
template<class T, class K, class V> void PushCMap(lua_State* L) const;
template<class T> void PushQObject(lua_State* L) const;
void PushTiXmlDocument(lua_State* L) const;
void (Input::*pPush)(lua_State* L) const;
union
{
bool BooleanValue;
char CharacterValue;
wchar_t WideCharValue;
lua_Number NumberValue;
const void* PointerValue;
lua_CFunction FunctionValue;
};
size_t Size;
};
class SizeRef
{
public:
SizeRef(size_t size) : Size(size) {}
operator size_t&() { return Size; }
private:
size_t Size;
};
class Output
{
public:
Output(eNil) { pGet = &Output::GetNil; }
Output(const Registry& value) { pGet = &Output::GetRegistry; PointerValue = (void*)&value; }
template<class T> Output(T& value) { pGet = &Output::GetValue<T>; PointerValue = &value; }
template<class T> Output(size_t& size, T* value) { memset(value, 0, size*sizeof(T)); pGet = &Output::GetArray<T>; pSize = &size; PointerValue = value; }
template<class T> Output(const T*& value, size_t& size) { pGet = &Output::GetSizedValue<T>; pSize = &size; PointerValue = &value; }
template<class T, size_t L2> Output(size_t& len1, T value[][L2]) {pGet = &Output::Get2DArray<T,L2>; pSize = &len1; PointerValue = value; }
#if LCBC_USE_CSL
template<class T1, class T2> Output(pair<T1,T2>& value) { pGet = &Output::GetPair<pair<T1,T2> >; PointerValue = &value; }
template<class T, class A> Output(vector<T,A>& value) { pGet = &Output::GetContainer<vector<T,A> >; PointerValue = &value; }
template<class T, class A> Output(list<T,A>& value) { pGet = &Output::GetContainer<list<T,A> >; PointerValue = &value; }
template<class T, class A> Output(deque<T,A>& value) { pGet = &Output::GetContainer<deque<T,A> >; PointerValue = &value; }
template<class K, class T, class C, class A> Output(map<K,T,C,A>& value) { pGet = &Output::GetMap<map<K,T,C,A> >; PointerValue = &value; }
template<class K, class T, class C, class A> Output(multimap<K,T,C,A>& value) { pGet = &Output::GetMultiMap<multimap<K,T,C,A> >; PointerValue = &value; }
template<class T, class C, class A> Output(set<T,C,A>& value) { pGet = &Output::GetSet<set<T,C,A> >; PointerValue = &value; }
template<class T, class C, class A> Output(multiset<T,C,A>& value) { pGet = &Output::GetSet<multiset<T,C,A> >; PointerValue = &value; }
template<class T, class C> Output(queue<T,C>& value) { pGet = &Output::GetQueue<queue<T,C> >; PointerValue = &value; }
template<class T, class C> Output(stack<T,C>& value) { pGet = &Output::GetQueue<stack<T,C> >; PointerValue = &value; }
template<class T, class C, class P> Output(priority_queue<T,C,P>& value) { pGet = &Output::GetQueue<priority_queue<T,C,P> >; PointerValue = &value; }
template<class T> Output(valarray<T>& value) { pGet = &Output::GetValArray<valarray<T>,T>; PointerValue = &value; }
template<size_t N> Output(bitset<N>& value) { pGet = &Output::GetBitSet<bitset<N> >; PointerValue = &value; }
#endif
#if LCBC_USE_MFC
template<class T, class A> Output(CArray<T,A>& value) { pGet = &Output::GetCArray<CArray<T,A>,T>; PointerValue = &value; }
template<class B, class T> Output(CTypedPtrArray<B,T>& value) { pGet = &Output::GetCArray<CTypedPtrArray<B,T>,T>; PointerValue = &value; }
Output(CByteArray& value) { pGet = &Output::GetCArray<CByteArray, BYTE>; PointerValue = &value; }
Output(CDWordArray& value) { pGet = &Output::GetCArray<CDWordArray, DWORD>; PointerValue = &value; }
Output(CObArray& value) { pGet = &Output::GetCArray<CObArray, CObject*>; PointerValue = &value; }
Output(CPtrArray& value) { pGet = &Output::GetCArray<CPtrArray, void*>; PointerValue = &value; }
Output(CStringArray& value) { pGet = &Output::GetCArray<CStringArray, CString>; PointerValue = &value; }
Output(CUIntArray& value) { pGet = &Output::GetCArray<CUIntArray, UINT>; PointerValue = &value; }
Output(CWordArray& value) { pGet = &Output::GetCArray<CWordArray, WORD>; PointerValue = &value; }
template<class T, class A> Output(CList<T,A>& value) { pGet = &Output::GetCList<CList<T,A>,T>; PointerValue = &value; }
template<class B, class T> Output(CTypedPtrList<B,T>& value) { pGet = &Output::GetCList<CTypedPtrList<B,T>,T>; PointerValue = &value; }
Output(CPtrList& value) { pGet = &Output::GetCList<CPtrList, void*>; PointerValue = &value; }
Output(CObList& value) { pGet = &Output::GetCList<CObList, CObject*>; PointerValue = &value; }
Output(CStringList& value) { pGet = &Output::GetCList<CStringList, CString>; PointerValue = &value; }
template<class K, class AK, class V, class AV> Output(CMap<K,AK,V,AV>& value) { pGet = &Output::GetCMap<CMap<K,AK,V,AV>,K,V>; PointerValue = &value; }
template<class B, class K, class V> Output(CTypedPtrMap<B,K,V>& value) { pGet = &Output::GetCMap<CTypedPtrMap<B,K,V>,K,V>; PointerValue = &value; }
Output(CMapWordToPtr& value) { pGet = &Output::GetCMap<CMapWordToPtr, WORD, void*>; PointerValue = &value; }
Output(CMapPtrToWord& value) { pGet = &Output::GetCMap<CMapPtrToWord, void*, WORD>; PointerValue = &value; }
Output(CMapPtrToPtr& value) { pGet = &Output::GetCMap<CMapPtrToPtr, void*, void*>; PointerValue = &value; }
Output(CMapWordToOb& value) { pGet = &Output::GetCMap<CMapWordToOb, WORD, CObject*>; PointerValue = &value; }
Output(CMapStringToPtr& value) { pGet = &Output::GetCMap<CMapStringToPtr, CString, void*>; PointerValue = &value; }
Output(CMapStringToOb& value) { pGet = &Output::GetCMap<CMapStringToOb, CString, CObject*>; PointerValue = &value; }
Output(CMapStringToString& value) { pGet = &Output::GetCMap<CMapStringToString, CString, CString>; PointerValue = &value; }
#endif
void Get(lua_State* L, int idx) const { (this->*pGet)(L, idx); }
private:
size_t GetSize(size_t s1) const { size_t s2=*pSize; *pSize=s1; return s1 < s2 ? s1 : s2; }
void GetNil(lua_State* /*L*/, int /*idx*/) const {}
void GetRegistry(lua_State* L, int idx) const
{
const Registry* reg = (const Registry*)PointerValue;
reg->get().Push(L);
lua_pushvalue(L, idx);
lua_rawset(L, LUA_REGISTRYINDEX);
}
template<class T> void GetValue(lua_State* L, int idx) const { *(T*)PointerValue = (T)luaL_checknumber(L, idx); }
template<class T> void GetSizedValue(lua_State* L, int idx) const;
template<class T> void GetArray(lua_State* L, int idx) const;
template<class T, size_t L2> void Get2DArray(lua_State* L, int idx) const;
template<class T> void GetPair(lua_State* L, int idx) const;
template<class T> void GetContainer(lua_State* L, int idx) const;
template<class T> void GetMultiMap(lua_State* L, int idx) const;
template<class T> void GetMap(lua_State* L, int idx) const;
template<class T> void GetSet(lua_State* L, int idx) const;
template<class T> void GetQueue(lua_State* L, int idx) const;
template<class T, class V> void GetValArray(lua_State* L, int idx) const;
template<class T> void GetBitSet(lua_State* L, int idx) const;
template<class C, class T> void GetCArray(lua_State* L, int idx) const;
template<class C, class T> void GetCList(lua_State* L, int idx) const;
template<class T, class K, class V> void GetCMap(lua_State* L, int idx) const;
void (Output::*pGet)(lua_State* L, int idx) const;
void* PointerValue;
size_t* pSize;
};
template<> inline void Input::PushNumber<bool>(lua_State* L) const
{
lua_pushboolean(L, *(bool*)PointerValue);
}
template<> inline void Input::PushValue<char>(lua_State* L) const
{
lua_pushstring(L, (const char*)PointerValue);
}
template<> inline void Input::PushSizedValue<char>(lua_State* L) const
{
lua_pushlstring(L, (const char*)PointerValue, Size);
}
template<> inline void Input::PushValue<lua_State>(lua_State* L) const
{
lua_pushthread((lua_State*)PointerValue);
lua_xmove((lua_State*)PointerValue, L, 1);
}
template<> inline void Input::PushValue<void>(lua_State* L) const
{
lua_pushlightuserdata(L, (void*)PointerValue);
}
template<> inline void Input::PushSizedValue<void>(lua_State* L) const
{
memcpy(lua_newuserdata(L, Size), PointerValue, Size);
}
template<class T> inline void Input::PushArray(lua_State* L) const
{
const T* arr = (const T*)PointerValue;
lua_createtable(L, (int)Size, 0);
for(size_t i=0;i<Size;i++)
{
Input input(arr[i]);
input.Push(L);
lua_rawseti(L, -2, (int)i+1);
}
}
template<class T, size_t L2> inline void Input::Push2DArray(lua_State* L) const
{
const T (*arr)[L2] = (const T(*)[L2])PointerValue;
lua_createtable(L, (int)Size, 0);
for(size_t i=0;i<Size;i++)
{
Input input(L2, arr[i]);
input.Push(L);
lua_rawseti(L, -2, (int)i+1);
}
}
template<> inline void Output::GetValue<bool>(lua_State* L, int idx) const
{
*(bool*)PointerValue = lua_toboolean(L, idx) != 0;
}
template<> inline void Output::GetValue<char>(lua_State* L, int idx) const
{
size_t len;
const char* str = luaL_checklstring(L, idx, &len);
if(len != 1)
luaL_error(L, "String length must be 1 for char type but is %d bytes", len);
*(char*)PointerValue = *str;
}
template<> inline void Output::GetValue<wchar_t>(lua_State* L, int idx) const
{
size_t len;
const wchar_t* str = WideString::Get(L, idx, len);
if(len != 1)
luaL_error(L, "String length must be 1 for wchar_t type but is %d characters", len);
*(wchar_t*)PointerValue = *str;
}
template<> inline void Output::GetValue<const char*>(lua_State* L, int idx) const
{
*(const char**)PointerValue = luaL_checklstring(L, idx, NULL);
}
template<> inline void Output::GetSizedValue<char>(lua_State* L, int idx) const
{
*(const char**)PointerValue = luaL_checklstring(L, idx, pSize);
}
template<> inline void Output::GetValue<lua_CFunction>(lua_State* L, int idx) const
{
luaL_checktype(L, idx, LUA_TFUNCTION);
*(lua_CFunction*)PointerValue = lua_tocfunction(L, idx);
}
template<> inline void Output::GetValue<lua_State*>(lua_State* L, int idx) const
{
luaL_checktype(L, idx, LUA_TTHREAD);
*(lua_State**)PointerValue = lua_tothread(L, idx);
}
template<> inline void Output::GetValue<void*>(lua_State* L, int idx) const
{
*(void**)PointerValue = lua_touserdata(L, idx);
}
template<> inline void Output::GetSizedValue<void>(lua_State* L, int idx) const
{
luaL_checktype(L, idx, LUA_TUSERDATA);
*(void**)PointerValue = (void*)lua_topointer(L, idx);
*pSize = lua_objlen(L, idx);
}
template<> inline void Output::GetArray<char>(lua_State* L, int idx) const
{
size_t len = GetSize(lua_objlen(L, idx)+1);
memcpy(PointerValue, luaL_checkstring(L, idx), len);
}
template<class T> inline void Output::GetArray(lua_State* L, int idx) const
{
T* arr = (T*)PointerValue;
luaL_checktype(L, idx, LUA_TTABLE);
size_t len = GetSize(lua_objlen(L, idx));
int top = lua_gettop(L);
for(size_t i=0;i<len;i++)
{
lua_rawgeti(L, idx, (int)i+1);
Output output(arr[i]);
output.Get(L,top+1);
lua_settop(L, top);
}
}
template<class T, size_t L2> inline void Output::Get2DArray(lua_State* L, int idx) const
{
T (*arr)[L2] = (T(*)[L2])PointerValue;
luaL_checktype(L, idx, LUA_TTABLE);
size_t len = GetSize(lua_objlen(L, idx));
int top = lua_gettop(L);
for(size_t i=0;i<len;i++)
{
lua_rawgeti(L, idx, (int)i+1);
size_t len2 = L2;
Output output(len2, arr[i]);
output.Get(L,top+1);
lua_settop(L, top);
}
}
template<> inline void Input::PushValue<wchar_t>(lua_State* L) const
{
WideString::Push(L, (const wchar_t*)PointerValue);
}
template<> inline void Input::PushSizedValue<wchar_t>(lua_State* L) const
{
WideString::Push(L, (const wchar_t*)PointerValue, Size);
}
template<> inline void Output::GetArray<wchar_t>(lua_State* L, int idx) const
{
size_t size;
const wchar_t* str = WideString::Get(L, idx, size);
memcpy(PointerValue, str, GetSize(size+1)*sizeof(wchar_t));
}
template<> inline void Output::GetValue<const wchar_t*>(lua_State* L, int idx) const
{
*(const wchar_t**)PointerValue = WideString::Get(L, idx);
}
template<> inline void Output::GetSizedValue<wchar_t>(lua_State* L, int idx) const
{
*(const wchar_t**)PointerValue = WideString::Get(L, idx, *pSize);
}
#if LCBC_USE_CSL
// This class is a hack to access the container member in adapters!
template<class T> class myqueue : public T
{
public:
myqueue(const T& src) : T(src) {}
typename T::container_type& get_container() { return this->c; }
};
template<class T> inline void Input::PushPair(lua_State* L) const
{
const T* p = (const T*)PointerValue;
lua_createtable(L, 2, 0);
Input first(p->first);
first.Push(L);
lua_rawseti(L, -2, 1);
Input second(p->second);
second.Push(L);
lua_rawseti(L, -2, 2);
}
template<class T> inline void Input::PushMap(lua_State* L) const
{
const T* m = (const T*)PointerValue;
typename T::const_iterator it;
lua_createtable(L, 0, (int)m->size());
for (it=m->begin() ; it != m->end(); it++)
{
Input key(it->first);
key.Push(L);
Input value(it->second);
value.Push(L);
lua_settable(L, -3);
}
}
template<class T> inline void Input::PushContainer(lua_State* L, const T* v) const
{
lua_createtable(L, (int)v->size(), 0);
typename T::const_iterator it;
int i=0;
for (it=v->begin(); it != v->end(); it++,i++)
{
lua_pushinteger(L, i+1);
Input input(*it);
input.Push(L);
lua_settable(L, -3);
}
}
template<class T> inline void Input::PushSet(lua_State* L) const
{
const T* s = (const T*)PointerValue;
typename T::const_iterator it;
lua_createtable(L, 0, (int)s->size());
for (it=s->begin() ; it != s->end(); it++)
{
Input key(*it);
key.Push(L);
lua_pushinteger(L, s->count(*it));
lua_settable(L, -3);
}
}
template<> inline void Input::PushValue<string>(lua_State* L) const
{
string* str = (string*)PointerValue;
lua_pushlstring(L, str->data(), str->size());
}
inline Input::Input(const string& value)
{
pPush = &Input::PushValue<string>;
PointerValue = &value;
}
template<class T> inline void Input::PushQueue(lua_State* L) const
{
const T* q = (const T*)PointerValue;
myqueue<T> copy(*q);
typename T::container_type& c = copy.get_container();
PushContainer(L, &c);
}
template<class T> inline void Input::PushValArray(lua_State* L) const
{
const T* v = (const T*)PointerValue;
size_t size = v->size();
lua_createtable(L, (int)size, 0);
for(size_t i=0;i<size;i++)
{
Input input((*v)[i]);
input.Push(L);
lua_rawseti(L, -2, (int)i+1);
}
}
template<class T> inline void Output::GetPair(lua_State* L, int idx) const
{
T* p = (T*)PointerValue;
luaL_checktype(L, idx, LUA_TTABLE);
int top = lua_gettop(L);
lua_rawgeti(L, idx, 1);
lua_rawgeti(L, idx, 2);
Output o1(p->first);
o1.Get(L, top+1);
Output o2(p->second);
o2.Get(L, top+2);
lua_settop(L, top);
}
template<> inline void Output::GetValue<string>(lua_State* L, int idx) const
{
size_t size;
const char* str = luaL_checklstring(L, idx, &size);
((string*)PointerValue)->assign(str, size);
}
template<class T> inline void Output::GetContainer(lua_State* L, int idx) const
{
T* v = (T*)PointerValue;
luaL_checktype(L, idx, LUA_TTABLE);
size_t len = lua_objlen(L, idx);
int top = lua_gettop(L);
for(size_t i=0;i<len;i++)
{
lua_rawgeti(L, idx, (int)i+1);
typename T::value_type value;
Output output(value);
output.Get(L, top+1);
v->push_back(value);
lua_settop(L, top);
}
}
template<class T> inline void Output::GetMultiMap(lua_State* L, int idx) const
{
T* v = (T*)PointerValue;
luaL_checktype(L, idx, LUA_TTABLE);
size_t len = lua_objlen(L, idx);
int top = lua_gettop(L);
for(size_t i=0;i<len;i++)
{
lua_rawgeti(L, idx, (int)i+1);
pair<typename T::key_type, typename T::mapped_type> value;
Output output(value);
output.Get(L, top+1);
v->insert(value);
lua_settop(L, top);
}
}
template<class T> inline void Output::GetSet(lua_State* L, int idx) const
{
T* s = (T*)PointerValue;
luaL_checktype(L, idx, LUA_TTABLE);
int top = lua_gettop(L);
lua_pushnil(L);
while (lua_next(L, idx) != 0)
{
lua_pushvalue(L, top+1);
typename T::value_type key;
Output outputKey(key);
outputKey.Get(L, top+3);
int val = luaL_checkint(L, top+2);
lua_settop(L, top+1);
for(int i=0;i<val;i++)
s->insert(key);
}
lua_settop(L, top);
}
template<class T> inline void Output::GetMap(lua_State* L, int idx) const
{
T* m = (T*)PointerValue;
luaL_checktype(L, idx, LUA_TTABLE);
int top = lua_gettop(L);
lua_pushnil(L);
while (lua_next(L, idx) != 0)
{
lua_pushvalue(L, top+1);
typename T::key_type key;
Output outputKey(key);
outputKey.Get(L, top+3);
typename T::mapped_type value;
Output output(value);
output.Get(L, top+2);
lua_settop(L, top+1);
(*m)[key] = value;
}
lua_settop(L, top);
}
template<class T> inline void Output::GetQueue(lua_State* L, int idx) const
{
T* q = (T*)PointerValue;
luaL_checktype(L, idx, LUA_TTABLE);
size_t len = lua_objlen(L, idx);
int top = lua_gettop(L);
for(size_t i=0;i<len;i++)
{
lua_rawgeti(L, idx, (int)i+1);
typename T::value_type value;
Output output(value);
output.Get(L, top+1);
q->push(value);
lua_settop(L, top);
}
}
template<class T, class V> inline void Output::GetValArray(lua_State* L, int idx) const
{
T* v = (T*)PointerValue;
luaL_checktype(L, idx, LUA_TTABLE);
size_t len = lua_objlen(L, idx);
v->resize(len);
int top = lua_gettop(L);
for(size_t i=0;i<len;i++)
{
lua_rawgeti(L, idx, (int)i+1);
V value;
Output output(value);
output.Get(L, top+1);
(*v)[i] = value;
lua_settop(L, top);
}
}
template<class T> inline void Output::GetBitSet(lua_State* L, int idx) const
{
T* v = (T*)PointerValue;
luaL_checktype(L, idx, LUA_TTABLE);
size_t len = lua_objlen(L, idx);
int top = lua_gettop(L);
for(size_t i=0;i<len;i++)
{
lua_rawgeti(L, idx, (int)i+1);
bool value;
Output output(value);
output.Get(L, top+1);
(*v)[i] = value;
lua_settop(L, top);
}
}
#if LCBC_USE_WIDESTRING
template<> inline void Input::PushValue<wstring>(lua_State* L) const
{
wstring* str = (wstring*)PointerValue;
WideString::Push(L, str->data(), str->size());
}
inline Input::Input(const wstring& value)
{
pPush = &Input::PushValue<wstring>;
PointerValue = &value;
}
template<> inline void Output::GetValue<wstring>(lua_State* L, int idx) const
{
size_t size;
const wchar_t* str = WideString::Get(L, idx, size);
((wstring*)PointerValue)->assign(str, size);
}
#endif
#endif
#if LCBC_USE_MFC
template<class T> inline void Input::PushCArray(lua_State* L) const
{
const T* v = (const T*)PointerValue;
lua_createtable(L, (int)v->GetSize(), 0);
for(int i=0;i<v->GetSize();i++)
{
Input input(v->GetAt(i));
input.Push(L);
lua_rawseti(L, -2, i+1);
}
}
template<class T> inline void Input::PushCList(lua_State* L) const
{
const T* v = (const T*)PointerValue;
lua_createtable(L, (int)v->GetCount(), 0);
POSITION pos = v->GetHeadPosition();
for(int i=0;pos;i++)
{
Input input(v->GetNext(pos));
input.Push(L);
lua_rawseti(L, -2, i+1);
}
}
template<class T, class K, class V> inline void Input::PushCMap(lua_State* L) const
{
const T* v = (const T*)PointerValue;
lua_createtable(L, 0, (int)v->GetCount());
POSITION pos = v->GetStartPosition();
while(pos)
{
K key;
V value;
v->GetNextAssoc(pos, key, value);
Input inkey(key);
inkey.Push(L);
Input invalue(value);
invalue.Push(L);
lua_rawset(L, -3);
}
}
template<class C, class T> inline void Output::GetCArray(lua_State* L, int idx) const
{
C* v = (C*)PointerValue;
luaL_checktype(L, idx, LUA_TTABLE);
int len = (int)lua_objlen(L, idx);
int top = lua_gettop(L);
v->SetSize(len);
for(int i=0;i<len;i++)
{
lua_rawgeti(L, idx, i+1);
T value;
Output output(value);
output.Get(L, top+1);
v->SetAt(i, value);
lua_settop(L, top);
}
}
template<class C, class T> inline void Output::GetCList(lua_State* L, int idx) const
{
C* v = (C*)PointerValue;
luaL_checktype(L, idx, LUA_TTABLE);
int len = (int)lua_objlen(L, idx);
int top = lua_gettop(L);
for(int i=0;i<len;i++)
{
lua_rawgeti(L, idx, i+1);
T value;
Output output(value);
output.Get(L, top+1);
v->AddTail(value);
lua_settop(L, top);
}
}
template<class T, class K, class V> inline void Output::GetCMap(lua_State* L, int idx) const
{
T* v = (T*)PointerValue;
luaL_checktype(L, idx, LUA_TTABLE);
int top = lua_gettop(L);
lua_pushnil(L);
while (lua_next(L, idx) != 0)
{
lua_pushvalue(L, top+1);
K key;
Output outputKey(key);
outputKey.Get(L, top+3);
V value;
Output output(value);
output.Get(L, top+2);
lua_settop(L, top+1);
v->SetAt(key, value);
}
lua_settop(L, top);
}
template<> inline void Input::PushValue<CStringA>(lua_State* L) const
{
const CStringA* str = (const CStringA*)PointerValue;
lua_pushlstring(L, *str, str->GetLength());
}
inline Input::Input(const CStringA& value)
{
pPush = &Input::PushValue<CStringA>;
PointerValue = &value;
}
template<> inline void Input::PushValue<CPoint>(lua_State* L) const
{
const CPoint* p = (const CPoint*)PointerValue;
lua_createtable(L, 0, 2);
lua_pushinteger(L, p->x);
lua_setfield(L, -2, "x");
lua_pushinteger(L, p->y);
lua_setfield(L, -2, "y");
}
inline Input::Input(const CPoint& value)
{
pPush = &Input::PushValue<CPoint>;
PointerValue = &value;
}
template<> inline void Input::PushValue<CRect>(lua_State* L) const
{