tmarshaling.hpp
Go to the documentation of this file.
1
2#pragma once
3
4#include "core/ast/tmock.hpp"
6
7namespace by {
8
9 struct marshalErr {};
10 template <typename T> class tbridge;
11 template <typename T, typename defaultElemType> class tarr;
12 class arr;
13
17 template <typename T, nbool isNode = tifSub<typename typeTrait<T>::Org, node>::is>
18 struct tmarshaling: public metaIf {
19 typedef T mgd;
20
21 template <typename E> static str toMgd(E& it) { throw marshalErr(); }
22
23 static T& toNative(const str& it) { throw marshalErr(); }
24
25 static const mgd& onAddParam() { return *new mgd(); }
26
27 static const mgd* onGetRet() { throw marshalErr(); }
28
29 static no canMarshal();
30 };
31
32 template <typename T, typename TACTIC> struct tmarshaling<tweak<T, TACTIC>, true>: public metaIf {
34
35 template <typename E> static str toMgd(E& it) { return it; }
36
37 static tweak<T, TACTIC> toNative(node& it) { return it; }
38
39 static const mgd& onAddParam() { return *new mgd(new T()); }
40
41 static const mgd* onGetRet() { return new mgd(new T()); }
42
43 static yes canMarshal();
44 };
45
46 template <typename T, typename TACTIC> struct tmarshaling<tstr<T, TACTIC>, true>: public metaIf {
48
49 template <typename E> static str toMgd(E& it) { return it; }
50
51 static tstr<T, TACTIC> toNative(node& it) { return it; }
52
53 static const mgd& onAddParam() { return *new mgd(new T()); }
54
55 static const mgd* onGetRet() { return new mgd(new T()); }
56
57 static yes canMarshal();
58 };
59
60 template <> struct tmarshaling<node&, true>: public metaIf {
61 typedef node mgd;
62
63 template <typename E> static str toMgd(E& it) { return it; }
64
65 static node& toNative(node& it) { return it; }
66
67 static const mgd& onAddParam() { return *new mockNode(); }
68
69 static const mgd* onGetRet() { return new mockNode(); }
70
71 static yes canMarshal();
72 };
73
74 template <> struct tmarshaling<node*, true>: public metaIf {
75 typedef node mgd;
76
77 template <typename E> static str toMgd(E* it) { return it; }
78
79 static node* toNative(node& it) { return &it; }
80
81 static const mgd& onAddParam() { return *new mockNode(); }
82
83 static const mgd* onGetRet() { return new mockNode(); }
84
85 static yes canMarshal();
86 };
87
88 template <typename T> struct tmarshaling<T&, true>: public metaIf {
89 typedef T mgd;
90
91 template <typename E> static str toMgd(E& it) { return it; }
92
93 static T& toNative(node& it) { return *it.cast<T>(); }
94
95 static const mgd& onAddParam() { return *new mgd(); }
96
97 static const mgd* onGetRet() { return new mgd(); }
98
99 static yes canMarshal();
100 };
101
102 template <typename T> struct tmarshaling<T*, true>: public metaIf {
103 typedef T mgd;
104
105 template <typename E> static str toMgd(E* it) { return it; }
106
107 static T* toNative(node& it) { return it.cast<T>(); }
108
109 static const mgd& onAddParam() { return *new mgd(); }
110
111 static const mgd* onGetRet() { return new mgd(); }
112
113 static yes canMarshal();
114 };
115
116 template <typename T> struct tmarshaling<T, true>: public metaIf {
117 typedef T mgd;
118
119 template <typename E> static str toMgd(const E& it) { return str((E*) it.clone()); }
120
121 static T& toNative(node& it) { return *it.cast<T>(); }
122
123 static const mgd& onAddParam() { return *new mgd(); }
124
125 static const mgd* onGetRet() { return new mgd(); }
126
127 static yes canMarshal();
128 };
129
133 template <typename tnativeType, typename tmarshalType> struct tnormalMarshaling: public metaIf {
134 typedef tmarshalType mgd;
135 typedef tnativeType native;
136
137 static native toNative(node& it) { return ((mgd&) it).get(); }
138
139 static str toMgd(native it) { return str(new mgd(it)); }
140
141 static const mgd& onAddParam() { return *new mgd(); }
142
143 static const mgd* onGetRet() { return new mgd(); }
144
145 static yes canMarshal();
146 };
147
148 template <> struct tnormalMarshaling<char, nStr>: public metaIf {
149 typedef nStr mgd;
150 typedef char native;
151
152 static native toNative(node& it) {
153 const std::string& cast = ((mgd&) it).get();
154 return cast.length() > 0 ? cast[0] : '\0';
155 }
156
157 static str toMgd(native it) { return str(new mgd(it)); }
158
159 static const mgd& onAddParam() { return *new mgd(); }
160
161 static const mgd* onGetRet() { return new mgd(); }
162
163 static yes canMarshal();
164 };
165
166 template <> struct tnormalMarshaling<const char*, nStr>: public metaIf {
167 typedef nStr mgd;
168 typedef const char* native;
169
170 static native toNative(node& it) { return ((mgd&) it).get().c_str(); }
171
172 static str toMgd(native it) { return str(new mgd(it)); }
173
174 static const mgd& onAddParam() { return *new mgd(); }
175
176 static const mgd* onGetRet() { return new mgd(); }
177
178 static yes canMarshal();
179 };
180
184 template <> struct _nout tnormalMarshaling<void, nVoid>: public metaIf {
185 typedef nVoid mgd;
186 typedef void native;
187
188 static str toMgd() { return str(new nVoid()); }
189
190 static const mgd& onAddParam() { return *new mgd(); }
191
192 static const mgd* onGetRet() { return new mgd(); }
193
194 static yes canMarshal();
195 };
196
197 template <> struct _nout tmarshaling<nint, false>: public tnormalMarshaling<nint, nInt> {};
198
199 template <> struct _nout tmarshaling<nint&, false>: public tnormalMarshaling<nint, nInt> {};
200
201 template <> struct _nout tmarshaling<const nint&, false>: public tnormalMarshaling<nint, nInt> {};
202
203 template <> struct _nout tmarshaling<nbool, false>: public tnormalMarshaling<nbool, nBool> {};
204
205 template <> struct _nout tmarshaling<nflt, false>: public tnormalMarshaling<nflt, nFlt> {};
206
207 template <> struct _nout tmarshaling<nchar, false>: public tnormalMarshaling<nchar, nStr> {};
208
209 template <> struct _nout tmarshaling<nchar*, false>: public tnormalMarshaling<const nchar*, nStr> {};
210
211 template <> struct _nout tmarshaling<const nchar*, false>: public tnormalMarshaling<const nchar*, nStr> {};
212
213 template <> struct _nout tmarshaling<std::string, false>: public tnormalMarshaling<const std::string&, nStr> {};
214
215 template <> struct _nout tmarshaling<std::string&, false>: public tnormalMarshaling<const std::string&, nStr> {};
216
217 template <>
218 struct _nout tmarshaling<const std::string&, false>: public tnormalMarshaling<const std::string&, nStr> {};
219
220 template <>
221 struct _nout tmarshaling<const std::string, false>: public tnormalMarshaling<const std::string&, nStr> {};
222
223 template <> struct _nout tmarshaling<void, false>: public tnormalMarshaling<void, nVoid> {};
224
225 template <typename T> struct tmarshaling<T, false>: public metaIf {
226 typedef class tbridge<T> mgd;
227
228 template <typename E> static str toMgd(const E& it) { return new mgd(new E(it)); }
229
230 static T& toNative(node& it) { return *it.cast<T>(); }
231
232 static const mgd& onAddParam() { return *new mgd(); }
233
234 static const mgd* onGetRet() { return new mgd(); }
235
236 static yes canMarshal();
237 };
238
239 template <typename T> struct tmarshaling<T&, false>: public metaIf {
240 typedef class tbridge<T> mgd;
241
242 template <typename E> static str toMgd(E& it) { return new mgd(&it); }
243
244 static T& toNative(node& it) { return it.cast<tbridge<T>>()->get(); }
245
246 static const mgd& onAddParam() { return *new mgd(); }
247
248 static const mgd* onGetRet() { return new mgd(); }
249
250 static yes canMarshal();
251 };
252
253 template <typename T> struct tmarshaling<T*, false>: public metaIf {
254 typedef tbridge<T> mgd;
255
256 template <typename E> static str toMgd(E* it) { return new mgd(it); }
257
258 static T* toNative(node& it) { return &it.cast<tbridge<T>>()->get(); }
259
260 static const mgd& onAddParam() { return *new mgd(); }
261
262 static const mgd* onGetRet() { return new mgd(); }
263
264 static yes canMarshal();
265 };
266
267 template <typename T, typename E> struct tmarshaling<tarr<T, E>, true>: public metaIf {
268 typedef arr mgd;
269
270 static tarr<T, E> toNative(node& it);
271
272 template <typename E2> static str toMgd(E2* it);
273
274 static const mgd& onAddParam();
275 static const mgd* onGetRet();
276 static yes canMarshal();
277 };
278} // namespace by
Managed array container for byeol language.
Definition: arr.hpp:14
String primitive type in byeol language.
Definition: nStr.hpp:13
Void primitive type in byeol language.
Definition: nVoid.hpp:12
Base class for all AST nodes in byeol language.
Definition: node.hpp:30
Definition: tarr.hpp:8
Bridge template for C++ objects.
Definition: tbridge.hpp:17
Mock object template for testing and verification.
Definition: tmock.hpp:13
Weak reference smart pointer with type-safe access.
Definition: tweak.hpp:15
Definition: tmarshaling.hpp:9
Base interface for SFINAE-based type checking.
Definition: rtti.hpp:15
Template marshaling interface for C++ bridge.
Definition: tmarshaling.hpp:18
Normal marshaling for primitive types.
Definition: tmarshaling.hpp:133