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
92template <typename G, typename S, typename T>
93auto setter_coalesce_wrap(G&& getter, S&& setter, T&& value)
94{
95 auto tmp = getter();
96 if (tmp == nullptr)
97 {
98 setter(tmp = value);
99 }
100 return tmp;
101}
102
106#define PROP_ASSIGN_LAMBDA_WRAP(name, op) \
107 template <typename G, typename S, typename T> \
108 auto setter_##name##_wrap(G&& getter, S&& setter, T value) \
109 { \
110 auto tmp = getter(); \
111 tmp = tmp op value; \
112 setter(tmp); \
113 return tmp; \
114 }
115
116PROP_ASSIGN_LAMBDA_WRAP(add, + )
117PROP_ASSIGN_LAMBDA_WRAP(sub, - )
118PROP_ASSIGN_LAMBDA_WRAP(mul, * )
119PROP_ASSIGN_LAMBDA_WRAP(div, / )
120PROP_ASSIGN_LAMBDA_WRAP(mod, % )
121PROP_ASSIGN_LAMBDA_WRAP(shl, << )
122PROP_ASSIGN_LAMBDA_WRAP(shr, >> )
123PROP_ASSIGN_LAMBDA_WRAP(and, & )
124PROP_ASSIGN_LAMBDA_WRAP(or, | )
125PROP_ASSIGN_LAMBDA_WRAP(exor, ^ )
126
130#define SETTER_LVAL_LAMBDA(capture, prop) \
131 [obj = capture](const auto& v){ obj->set_##prop(v); }
135#define GETTER_LVAL_LAMBDA(capture, prop) \
136 [obj = capture]{ return obj->get_##prop(); }
140#if __GNUC__ > 5 && __GNUC__ < 7 // Workaround for GCC bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66735
141#define SETTER_LAMBDA(capture, prop) \
142 SETTER_LVAL_LAMBDA(capture, prop)
143#else
144#define SETTER_LAMBDA(capture, prop) \
145 [&obj = capture](const auto& v){ obj->set_##prop(v); }
146#endif
150#if __GNUC__ > 5 && __GNUC__ < 7 // Workaround for GCC bug https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66735
151#define GETTER_LAMBDA(capture, prop) \
152 GETTER_LVAL_LAMBDA(capture, prop)
153#else
154#define GETTER_LAMBDA(capture, prop) \
155 [&obj = capture]{ return obj->get_##prop(); }
156#endif
160#define SETTER_REF_LAMBDA(capture, prop) \
161 [&obj = capture](const auto& v){ obj.set_##prop(v); }
165#define GETTER_REF_LAMBDA(capture, prop) \
166 [&obj = capture]{ return obj.get_##prop(); }
169#define SETTER_THIS_LAMBDA(prop) \
170 [this](const auto& v){ this->set_##prop(v); }
173#define GETTER_THIS_LAMBDA(prop) \
174 [this]{ return this->get_##prop(); }
178#define STATIC_SETTER_LAMBDA(T, prop) \
179 [](const auto& v){ T::set_##prop(v); }
183#define STATIC_GETTER_LAMBDA(T, prop) \
184 []{ return T::get_##prop(); }
188#define GETTER_SETTER_LAMBDA_ARGS(capture, prop) \
189 GETTER_LAMBDA(capture, prop), SETTER_LAMBDA(capture, prop)
192#define GETTER_SETTER_THIS_LAMBDA_ARGS(prop) \
193 GETTER_THIS_LAMBDA(prop), SETTER_THIS_LAMBDA(prop)
197#define GETTER_SETTER_REF_LAMBDA_ARGS(capture, prop) \
198 GETTER_REF_LAMBDA(capture, prop), SETTER_REF_LAMBDA(capture, prop)
202#define GETTER_SETTER_LVAL_LAMBDA_ARGS(capture, prop) \
203 GETTER_LVAL_LAMBDA(capture, prop), SETTER_LVAL_LAMBDA(capture, prop)
207#define STATIC_GETTER_SETTER_LAMBDA_ARGS(T, prop) \
208 STATIC_GETTER_LAMBDA(T, prop), STATIC_SETTER_LAMBDA(T, prop)
215#define SETTER_LAMBDA_OP_WRAP(T, obj, prop, op, val) \
216 setter_##op##_wrap(GETTER_SETTER_LAMBDA_ARGS(obj, prop), val)
217
220#define INDEXED_SETTER_LAMBDA(capture) \
221 [&obj = capture](auto&& index, auto&& value){ obj->idx_set(index, value); }
224#define INDEXED_GETTER_LAMBDA(capture) \
225 [&obj = capture](auto&& index){ return obj->idx_get(index); }
229#define INDEXED_GETTER_SETTER_ARGS(capture, arg) \
230 INDEXED_GETTER_LAMBDA(capture), INDEXED_SETTER_LAMBDA(capture), [&](){ return arg; }
234#define INDEXED_ASSIGN_LAMBDA_WRAP(name, op) \
235 template <typename G, typename S, typename I, typename T> \
236 void indexed_##name##_wrap(G&& getter, S&& setter, I&& indexer, T&& value) \
237 { \
238 const auto index = indexer(); \
239 auto tmp = getter(index); \
240 tmp op value; \
241 setter(index, tmp); \
242 }
243
244INDEXED_ASSIGN_LAMBDA_WRAP(add, +=)
245INDEXED_ASSIGN_LAMBDA_WRAP(sub, -=)
246INDEXED_ASSIGN_LAMBDA_WRAP(mul, *=)
247INDEXED_ASSIGN_LAMBDA_WRAP(div, /=)
248INDEXED_ASSIGN_LAMBDA_WRAP(mod, %=)
249INDEXED_ASSIGN_LAMBDA_WRAP(shl, <<=)
250INDEXED_ASSIGN_LAMBDA_WRAP(shr, >>=)
251INDEXED_ASSIGN_LAMBDA_WRAP(and, &=)
252INDEXED_ASSIGN_LAMBDA_WRAP(or , |=)
253INDEXED_ASSIGN_LAMBDA_WRAP(exor, ^=)
254}
255
262template <typename T, typename T2>
263inline T setter_wrap(void(*pSetter)(T2), T value)
264{
265 pSetter(value);
266 return value;
267}
268
278template <typename T, typename T2, typename Host, typename HostSet>
279inline typename std::enable_if<std::is_base_of<HostSet, Host>::value, T>::type
280 setter_wrap(Host* const host, void (HostSet::*pSetter)(T2), T value)
281{
282 (host->*pSetter)(value);
283 return value;
284}
285
292template <typename T>
293inline T setter_increment_wrap(T(*pGetter)(), void(*pSetter)(T))
294{
295 T tmp = pGetter();
296 pSetter(++tmp);
297 return tmp;
298}
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)(), void (HostSet::*pSetter)(T))
314{
315 T tmp = (host->*pGetter)();
316 (host->*pSetter)(++tmp);
317 return tmp;
318}
319
330template <typename T, typename Host, typename HostGet, typename HostSet>
331inline typename std::enable_if<std::is_base_of<HostGet, Host>::value && std::is_base_of<HostSet, Host>::value, T>::type
332setter_increment_wrap(Host* const host, T(HostGet::*pGetter)() const, void (HostSet::*pSetter)(T))
333{
334 T tmp = (host->*pGetter)();
335 (host->*pSetter)(++tmp);
336 return tmp;
337}
338
339// post-increment wrap for static and non-static properties
346template <typename T>
347inline T setter_post_increment_wrap(T(*pGetter)(), void (*pSetter)( T))
348{
349 T tmp = pGetter(), rv = tmp;
350 ++tmp;
351 pSetter(tmp);
352 return rv;
353}
354
365template <typename T, typename Host, typename HostGet, typename HostSet>
366inline typename std::enable_if<std::is_base_of<HostGet, Host>::value && std::is_base_of<HostSet, Host>::value, T>::type
367setter_post_increment_wrap(Host* const host, T(HostGet::*pGetter)(), void (HostSet::*pSetter)(T))
368{
369 T tmp = (host->*pGetter)(), rv = tmp;
370 ++tmp;
371 (host->*pSetter)(tmp);
372 return rv;
373}
374
385template <typename T, typename Host, typename HostConstGet, typename HostSet>
386inline typename std::enable_if<std::is_base_of<HostConstGet, Host>::value && std::is_base_of<HostSet, Host>::value, T>::type
387setter_post_increment_wrap(Host* const host, T (HostConstGet::*pGetter)() const, void (HostSet::*pSetter)(T))
388{
389 T tmp = (host->*pGetter)(), rv = tmp;
390 ++tmp;
391 (host->*pSetter)(tmp);
392 return rv;
393}
394
395// decrement wrap for static and non-static properties
402template <typename T>
403inline T setter_decrement_wrap(T(*pGetter)(), void (*pSetter)( T))
404{
405 T tmp = pGetter();
406 pSetter(--tmp);
407 return tmp;
408}
419template <typename T, typename Host, typename HostGet, typename HostSet>
420inline typename std::enable_if<std::is_base_of<HostGet, Host>::value && std::is_base_of<HostSet, Host>::value, T>::type
421setter_decrement_wrap(Host* const host, T(HostGet::*pGetter)(), void (HostSet::*pSetter)(T))
422{
423 T tmp = (host->*pGetter)();
424 (host->*pSetter)(--tmp);
425 return tmp;
426}
437template <typename T, typename Host, typename HostConstGet, typename HostSet>
438inline typename std::enable_if<std::is_base_of<HostConstGet, Host>::value && std::is_base_of<HostSet, Host>::value, T>::type
439setter_decrement_wrap(Host* const host, T(HostConstGet::*pGetter)() const, void (HostSet::*pSetter)(T))
440{
441 T tmp = (host->*pGetter)();
442 (host->*pSetter)(--tmp);
443 return tmp;
444}
445
446// post decrement wrap for static and non-static properties
453template <typename T>
454inline T setter_post_decrement_wrap(T(*pGetter)(), void (*pSetter)( T))
455{
456 T tmp = pGetter(), rv = tmp;
457 --tmp;
458 pSetter(tmp);
459 return rv;
460}
471template <typename T, typename Host, typename HostGet, typename HostSet>
472inline typename std::enable_if<std::is_base_of<HostGet, Host>::value && std::is_base_of<HostSet, Host>::value, T>::type
473setter_post_decrement_wrap(Host* const host, T(HostGet::*pGetter)(), void (HostSet::*pSetter)(T))
474{
475 T tmp = (host->*pGetter)(), rv = tmp;
476 --tmp;
477 (host->*pSetter)(tmp);
478 return rv;
479}
490template <typename T, typename Host, typename HostConstGet, typename HostSet>
491inline typename std::enable_if<std::is_base_of<HostConstGet, Host>::value && std::is_base_of<HostSet, Host>::value, T>::type
492setter_post_decrement_wrap(Host* const host, T(HostConstGet::*pGetter)() const, void (HostSet::*pSetter)(T))
493{
494 T tmp = (host->*pGetter)(), rv = tmp;
495 --tmp;
496 (host->*pSetter)(tmp);
497 return rv;
498}
499
500// assignment operations wrap for static and non-static properties
504#define PROP_ASSIGN_WRAP(name, op) \
505 template <typename PropT, typename PropValT> \
506 inline PropT setter_##name##_wrap(PropT (*pGetter)(), void (*pSetter)(PropT), PropValT value) \
507 { \
508 PropT tmp = pGetter(); \
509 tmp = tmp op value; \
510 pSetter(tmp); \
511 return tmp; \
512 } \
513 template <typename PropT, typename PropValT> \
514 inline PropT setter_##name##_wrap(PropT (*pGetter)(), void (*pSetter)(const PropT&), PropValT value) \
515 { \
516 PropT tmp = pGetter(); \
517 tmp = tmp op value; \
518 pSetter(tmp); \
519 return tmp; \
520 } \
521 template <typename PropT, typename HostT, typename HostGetT, typename HostSetT, typename PropValT> \
522 inline typename std::enable_if<std::is_base_of<HostGetT, HostT>::value && std::is_base_of<HostSetT, HostT>::value, PropT>::type \
523 setter_##name##_wrap(HostT* const host, PropT (HostGetT::*pGetter)(), void (HostSetT::*pSetter)(PropT), PropValT value) \
524 { \
525 PropT tmp = (host->*pGetter)(); \
526 tmp = tmp op value; \
527 (host->*pSetter)(tmp); \
528 return tmp; \
529 } \
530 template <typename PropT, typename HostT, typename HostGetT, typename HostSetT, typename PropValT> \
531 inline typename std::enable_if<std::is_base_of<HostGetT, HostT>::value && std::is_base_of<HostSetT, HostT>::value, PropT>::type \
532 setter_##name##_wrap(HostT* const host, PropT (HostGetT::*pGetter)(), void (HostSetT::*pSetter)(const PropT &), PropValT value) \
533 { \
534 PropT tmp = (host->*pGetter)(); \
535 tmp = tmp op value; \
536 (host->*pSetter)(tmp); \
537 return tmp; \
538 } \
539 template <typename PropT, typename HostT, typename HostGetT, typename HostSetT, typename PropValT> \
540 inline typename std::enable_if<std::is_base_of<HostGetT, HostT>::value && std::is_base_of<HostSetT, HostT>::value, PropT>::type \
541 setter_##name##_wrap(HostT* const host, PropT (HostGetT::*pGetter)() const, void (HostSetT::*pSetter)(PropT), PropValT value) \
542 { \
543 PropT tmp = (host->*pGetter)(); \
544 tmp = tmp op value; \
545 (host->*pSetter)(tmp); \
546 return tmp; \
547 } \
548 template <typename PropT, typename HostT, typename HostGetT, typename HostSetT, typename PropValT> \
549 inline typename std::enable_if<std::is_base_of<HostGetT, HostT>::value && std::is_base_of<HostSetT, HostT>::value, PropT>::type \
550 setter_##name##_wrap(HostT* const host, PropT (HostGetT::*pGetter)() const, void (HostSetT::*pSetter)(const PropT &), PropValT value) \
551 { \
552 PropT tmp = (host->*pGetter)(); \
553 tmp = tmp op value; \
554 (host->*pSetter)(tmp); \
555 return tmp; \
556 }
557
558PROP_ASSIGN_WRAP(add, + )
559PROP_ASSIGN_WRAP(sub, - )
560PROP_ASSIGN_WRAP(mul, * )
561PROP_ASSIGN_WRAP(div, / )
562PROP_ASSIGN_WRAP(mod, % )
563PROP_ASSIGN_WRAP(shl, << )
564PROP_ASSIGN_WRAP(shr, >> )
565PROP_ASSIGN_WRAP(and, & )
566PROP_ASSIGN_WRAP(or, | )
567PROP_ASSIGN_WRAP(exor, ^ )
568
575#define SETTER_OP_WRAP(T, obj, prop, op, val) \
576 setter_##op##_wrap(obj, &T::get_##prop, &T::set_##prop, val)
577
578} // namespace System
auto setter_coalesce_wrap(G &&getter, S &&setter, T &&value)
Wrapper function to wrap calling of coalsesce assignment.
Definition: setters_wrap.h:93
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