CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
setters_wrap.h
1
3#pragma once
4
5#include "system/shared_ptr.h"
6
7namespace System {
8
9namespace WithLambda {
10
17template<typename S, typename T>
18T setter_wrap(S &&setter, T value)
19{
20 setter(value);
21 return value;
22}
23
31template <typename G, typename S>
32auto setter_increment_wrap(G&& getter, S&& setter)
33{
34 auto tmp = getter();
35 setter(++tmp);
36 return tmp;
37}
38
46template <typename G, typename S>
47auto setter_post_increment_wrap(G&& getter, S&& setter)
48{
49 auto tmp = getter();
50 setter(tmp + 1);
51 return tmp;
52}
53
61template <typename G, typename S>
62auto setter_decrement_wrap(G&& getter, S&& setter)
63{
64 auto tmp = getter();
65 setter(--tmp);
66 return tmp;
67}
68
76template <typename G, typename S>
77auto setter_post_decrement_wrap(G&& getter, S&& setter)
78{
79 auto tmp = getter();
80 setter(tmp - 1);
81 return tmp;
82}
83
87#define PROP_ASSIGN_LAMBDA_WRAP(name, op) \
88 template <typename G, typename S, typename T> \
89 auto setter_##name##_wrap(G&& getter, S&& setter, T value) \
90 { \
91 auto tmp = getter(); \
92 tmp = tmp op value; \
93 setter(tmp); \
94 return tmp; \
95 }
96
97PROP_ASSIGN_LAMBDA_WRAP(add, + )
98PROP_ASSIGN_LAMBDA_WRAP(sub, - )
99PROP_ASSIGN_LAMBDA_WRAP(mul, * )
100PROP_ASSIGN_LAMBDA_WRAP(div, / )
101PROP_ASSIGN_LAMBDA_WRAP(mod, % )
102PROP_ASSIGN_LAMBDA_WRAP(shl, << )
103PROP_ASSIGN_LAMBDA_WRAP(shr, >> )
104PROP_ASSIGN_LAMBDA_WRAP(and, & )
105PROP_ASSIGN_LAMBDA_WRAP(or, | )
106PROP_ASSIGN_LAMBDA_WRAP(exor, ^ )
107
111#define SETTER_LVAL_LAMBDA(capture, prop) \
112 [obj = capture](const auto& v){ obj->set_##prop(v); }
116#define GETTER_LVAL_LAMBDA(capture, prop) \
117 [obj = capture]{ return obj->get_##prop(); }
121#if __GNUC__ > 5 && __GNUC__ < 7 // Workaround for GCC bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66735
122#define SETTER_LAMBDA(capture, prop) \
123 SETTER_LVAL_LAMBDA(capture, prop)
124#else
125#define SETTER_LAMBDA(capture, prop) \
126 [&obj = capture](const auto& v){ obj->set_##prop(v); }
127#endif
131#if __GNUC__ > 5 && __GNUC__ < 7 // Workaround for GCC bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66735
132#define GETTER_LAMBDA(capture, prop) \
133 GETTER_LVAL_LAMBDA(capture, prop)
134#else
135#define GETTER_LAMBDA(capture, prop) \
136 [&obj = capture]{ return obj->get_##prop(); }
137#endif
141#define SETTER_REF_LAMBDA(capture, prop) \
142 [&obj = capture](const auto& v){ obj.set_##prop(v); }
146#define GETTER_REF_LAMBDA(capture, prop) \
147 [&obj = capture]{ return obj.get_##prop(); }
150#define SETTER_THIS_LAMBDA(prop) \
151 [this](const auto& v){ this->set_##prop(v); }
154#define GETTER_THIS_LAMBDA(prop) \
155 [this]{ return this->get_##prop(); }
159#define STATIC_SETTER_LAMBDA(T, prop) \
160 [](const auto& v){ T::set_##prop(v); }
164#define STATIC_GETTER_LAMBDA(T, prop) \
165 []{ return T::get_##prop(); }
169#define GETTER_SETTER_LAMBDA_ARGS(capture, prop) \
170 GETTER_LAMBDA(capture, prop), SETTER_LAMBDA(capture, prop)
173#define GETTER_SETTER_THIS_LAMBDA_ARGS(prop) \
174 GETTER_THIS_LAMBDA(prop), SETTER_THIS_LAMBDA(prop)
178#define GETTER_SETTER_REF_LAMBDA_ARGS(capture, prop) \
179 GETTER_REF_LAMBDA(capture, prop), SETTER_REF_LAMBDA(capture, prop)
183#define GETTER_SETTER_LVAL_LAMBDA_ARGS(capture, prop) \
184 GETTER_LVAL_LAMBDA(capture, prop), SETTER_LVAL_LAMBDA(capture, prop)
188#define STATIC_GETTER_SETTER_LAMBDA_ARGS(T, prop) \
189 STATIC_GETTER_LAMBDA(T, prop), STATIC_SETTER_LAMBDA(T, prop)
196#define SETTER_LAMBDA_OP_WRAP(T, obj, prop, op, val) \
197 setter_##op##_wrap(GETTER_SETTER_LAMBDA_ARGS(obj, prop), val)
198
201#define INDEXED_SETTER_LAMBDA(capture) \
202 [&obj = capture](auto&& index, auto&& value){ obj->idx_set(index, value); }
205#define INDEXED_GETTER_LAMBDA(capture) \
206 [&obj = capture](auto&& index){ return obj->idx_get(index); }
210#define INDEXED_GETTER_SETTER_ARGS(capture, arg) \
211 INDEXED_GETTER_LAMBDA(capture), INDEXED_SETTER_LAMBDA(capture), [&](){ return arg; }
215#define INDEXED_ASSIGN_LAMBDA_WRAP(name, op) \
216 template <typename G, typename S, typename I, typename T> \
217 void indexed_##name##_wrap(G&& getter, S&& setter, I&& indexer, T&& value) \
218 { \
219 const auto index = indexer(); \
220 auto tmp = getter(index); \
221 tmp op value; \
222 setter(index, tmp); \
223 }
224
225INDEXED_ASSIGN_LAMBDA_WRAP(add, +=)
226INDEXED_ASSIGN_LAMBDA_WRAP(sub, -=)
227INDEXED_ASSIGN_LAMBDA_WRAP(mul, *=)
228INDEXED_ASSIGN_LAMBDA_WRAP(div, /=)
229INDEXED_ASSIGN_LAMBDA_WRAP(mod, %=)
230INDEXED_ASSIGN_LAMBDA_WRAP(shl, <<=)
231INDEXED_ASSIGN_LAMBDA_WRAP(shr, >>=)
232INDEXED_ASSIGN_LAMBDA_WRAP(and, &=)
233INDEXED_ASSIGN_LAMBDA_WRAP(or , |=)
234INDEXED_ASSIGN_LAMBDA_WRAP(exor, ^=)
235}
236
243template <typename T, typename T2>
244inline T setter_wrap(void(*pSetter)(T2), T value)
245{
246 pSetter(value);
247 return value;
248}
249
259template <typename T, typename T2, typename Host, typename HostSet>
260inline typename std::enable_if<std::is_base_of<HostSet, Host>::value, T>::type
261 setter_wrap(Host* const host, void (HostSet::*pSetter)(T2), T value)
262{
263 (host->*pSetter)(value);
264 return value;
265}
266
273template <typename T>
274inline T setter_increment_wrap(T(*pGetter)(), void(*pSetter)(T))
275{
276 T tmp = pGetter();
277 pSetter(++tmp);
278 return tmp;
279}
280
281
292template <typename T, typename Host, typename HostGet, typename HostSet>
293inline typename std::enable_if<std::is_base_of<HostGet, Host>::value && std::is_base_of<HostSet, Host>::value, T>::type
294setter_increment_wrap(Host* const host, T(HostGet::*pGetter)(), void (HostSet::*pSetter)(T))
295{
296 T tmp = (host->*pGetter)();
297 (host->*pSetter)(++tmp);
298 return tmp;
299}
300
311template <typename T, typename Host, typename HostGet, typename HostSet>
312inline typename std::enable_if<std::is_base_of<HostGet, Host>::value && std::is_base_of<HostSet, Host>::value, T>::type
313setter_increment_wrap(Host* const host, T(HostGet::*pGetter)() const, void (HostSet::*pSetter)(T))
314{
315 T tmp = (host->*pGetter)();
316 (host->*pSetter)(++tmp);
317 return tmp;
318}
319
320// post-increment wrap for static and non-static properties
327template <typename T>
328inline T setter_post_increment_wrap(T(*pGetter)(), void (*pSetter)( T))
329{
330 T tmp = pGetter(), rv = tmp;
331 ++tmp;
332 pSetter(tmp);
333 return rv;
334}
335
346template <typename T, typename Host, typename HostGet, typename HostSet>
347inline typename std::enable_if<std::is_base_of<HostGet, Host>::value && std::is_base_of<HostSet, Host>::value, T>::type
348setter_post_increment_wrap(Host* const host, T(HostGet::*pGetter)(), void (HostSet::*pSetter)(T))
349{
350 T tmp = (host->*pGetter)(), rv = tmp;
351 ++tmp;
352 (host->*pSetter)(tmp);
353 return rv;
354}
355
366template <typename T, typename Host, typename HostConstGet, typename HostSet>
367inline typename std::enable_if<std::is_base_of<HostConstGet, Host>::value && std::is_base_of<HostSet, Host>::value, T>::type
368setter_post_increment_wrap(Host* const host, T (HostConstGet::*pGetter)() const, void (HostSet::*pSetter)(T))
369{
370 T tmp = (host->*pGetter)(), rv = tmp;
371 ++tmp;
372 (host->*pSetter)(tmp);
373 return rv;
374}
375
376// decrement wrap for static and non-static properties
383template <typename T>
384inline T setter_decrement_wrap(T(*pGetter)(), void (*pSetter)( T))
385{
386 T tmp = pGetter();
387 pSetter(--tmp);
388 return tmp;
389}
400template <typename T, typename Host, typename HostGet, typename HostSet>
401inline typename std::enable_if<std::is_base_of<HostGet, Host>::value && std::is_base_of<HostSet, Host>::value, T>::type
402setter_decrement_wrap(Host* const host, T(HostGet::*pGetter)(), void (HostSet::*pSetter)(T))
403{
404 T tmp = (host->*pGetter)();
405 (host->*pSetter)(--tmp);
406 return tmp;
407}
418template <typename T, typename Host, typename HostConstGet, typename HostSet>
419inline typename std::enable_if<std::is_base_of<HostConstGet, Host>::value && std::is_base_of<HostSet, Host>::value, T>::type
420setter_decrement_wrap(Host* const host, T(HostConstGet::*pGetter)() const, void (HostSet::*pSetter)(T))
421{
422 T tmp = (host->*pGetter)();
423 (host->*pSetter)(--tmp);
424 return tmp;
425}
426
427// post decrement wrap for static and non-static properties
434template <typename T>
435inline T setter_post_decrement_wrap(T(*pGetter)(), void (*pSetter)( T))
436{
437 T tmp = pGetter(), rv = tmp;
438 --tmp;
439 pSetter(tmp);
440 return rv;
441}
452template <typename T, typename Host, typename HostGet, typename HostSet>
453inline typename std::enable_if<std::is_base_of<HostGet, Host>::value && std::is_base_of<HostSet, Host>::value, T>::type
454setter_post_decrement_wrap(Host* const host, T(HostGet::*pGetter)(), void (HostSet::*pSetter)(T))
455{
456 T tmp = (host->*pGetter)(), rv = tmp;
457 --tmp;
458 (host->*pSetter)(tmp);
459 return rv;
460}
471template <typename T, typename Host, typename HostConstGet, typename HostSet>
472inline typename std::enable_if<std::is_base_of<HostConstGet, Host>::value && std::is_base_of<HostSet, Host>::value, T>::type
473setter_post_decrement_wrap(Host* const host, T(HostConstGet::*pGetter)() const, void (HostSet::*pSetter)(T))
474{
475 T tmp = (host->*pGetter)(), rv = tmp;
476 --tmp;
477 (host->*pSetter)(tmp);
478 return rv;
479}
480
481// assignment operations wrap for static and non-static properties
485#define PROP_ASSIGN_WRAP(name, op) \
486 template <typename PropT, typename PropValT> \
487 inline PropT setter_##name##_wrap(PropT (*pGetter)(), void (*pSetter)(PropT), PropValT value) \
488 { \
489 PropT tmp = pGetter(); \
490 tmp = tmp op value; \
491 pSetter(tmp); \
492 return tmp; \
493 } \
494 template <typename PropT, typename PropValT> \
495 inline PropT setter_##name##_wrap(PropT (*pGetter)(), void (*pSetter)(const PropT&), PropValT value) \
496 { \
497 PropT tmp = pGetter(); \
498 tmp = tmp op value; \
499 pSetter(tmp); \
500 return tmp; \
501 } \
502 template <typename PropT, typename HostT, typename HostGetT, typename HostSetT, typename PropValT> \
503 inline typename std::enable_if<std::is_base_of<HostGetT, HostT>::value && std::is_base_of<HostSetT, HostT>::value, PropT>::type \
504 setter_##name##_wrap(HostT* const host, PropT (HostGetT::*pGetter)(), void (HostSetT::*pSetter)(PropT), PropValT value) \
505 { \
506 PropT tmp = (host->*pGetter)(); \
507 tmp = tmp op value; \
508 (host->*pSetter)(tmp); \
509 return tmp; \
510 } \
511 template <typename PropT, typename HostT, typename HostGetT, typename HostSetT, typename PropValT> \
512 inline typename std::enable_if<std::is_base_of<HostGetT, HostT>::value && std::is_base_of<HostSetT, HostT>::value, PropT>::type \
513 setter_##name##_wrap(HostT* const host, PropT (HostGetT::*pGetter)(), void (HostSetT::*pSetter)(const PropT &), PropValT value) \
514 { \
515 PropT tmp = (host->*pGetter)(); \
516 tmp = tmp op value; \
517 (host->*pSetter)(tmp); \
518 return tmp; \
519 } \
520 template <typename PropT, typename HostT, typename HostGetT, typename HostSetT, typename PropValT> \
521 inline typename std::enable_if<std::is_base_of<HostGetT, HostT>::value && std::is_base_of<HostSetT, HostT>::value, PropT>::type \
522 setter_##name##_wrap(HostT* const host, PropT (HostGetT::*pGetter)() const, void (HostSetT::*pSetter)(PropT), PropValT value) \
523 { \
524 PropT tmp = (host->*pGetter)(); \
525 tmp = tmp op value; \
526 (host->*pSetter)(tmp); \
527 return tmp; \
528 } \
529 template <typename PropT, typename HostT, typename HostGetT, typename HostSetT, typename PropValT> \
530 inline typename std::enable_if<std::is_base_of<HostGetT, HostT>::value && std::is_base_of<HostSetT, HostT>::value, PropT>::type \
531 setter_##name##_wrap(HostT* const host, PropT (HostGetT::*pGetter)() const, void (HostSetT::*pSetter)(const PropT &), PropValT value) \
532 { \
533 PropT tmp = (host->*pGetter)(); \
534 tmp = tmp op value; \
535 (host->*pSetter)(tmp); \
536 return tmp; \
537 }
538
539PROP_ASSIGN_WRAP(add, + )
540PROP_ASSIGN_WRAP(sub, - )
541PROP_ASSIGN_WRAP(mul, * )
542PROP_ASSIGN_WRAP(div, / )
543PROP_ASSIGN_WRAP(mod, % )
544PROP_ASSIGN_WRAP(shl, << )
545PROP_ASSIGN_WRAP(shr, >> )
546PROP_ASSIGN_WRAP(and, & )
547PROP_ASSIGN_WRAP(or, | )
548PROP_ASSIGN_WRAP(exor, ^ )
549
556#define SETTER_OP_WRAP(T, obj, prop, op, val) \
557 setter_##op##_wrap(obj, &T::get_##prop, &T::set_##prop, val)
558
559} // namespace System
auto setter_decrement_wrap(G &&getter, S &&setter)
Translator translates C#'s decrement expressions targeting class' property that has setter and getter...
Definition: setters_wrap.h:62
T setter_wrap(S &&setter, T value)
Wrapper function to wrap calling setter method and returning set value (normally, set methods do not ...
Definition: setters_wrap.h:18
auto setter_post_decrement_wrap(G &&getter, S &&setter)
Translator translates C#'s post-decrement expressions targeting a class' property that has a setter a...
Definition: setters_wrap.h:77
auto setter_increment_wrap(G &&getter, S &&setter)
Translator translates C#'s increment expressions targeting class' property that has setter and getter...
Definition: setters_wrap.h:32
auto setter_post_increment_wrap(G &&getter, S &&setter)
Translator translates C#'s post-increment expressions targeting class' property that has setter and g...
Definition: setters_wrap.h:47
Definition: db_command.h:9