tweak.hpp
Go to the documentation of this file.
1
2#pragma once
3
6
7namespace by {
8
9 class origin;
10
15 template <typename T, typename TACTIC = weakTactic> class tweak: public binder {
16 BY_ME(tweak, binder)
17 BY_INIT_META(me)
18 friend class bindTag;
19
20 public:
23 tweak(const type& subtype);
24 tweak(const T& it);
25 tweak(const T* it);
26 tweak(const tmedium<T>& it);
27 tweak(const me& rhs);
28 explicit tweak(const binder& rhs);
29
30 public:
31 T* operator->();
32 T& operator*();
33 const T* operator->() const BY_CONST_FUNC(operator->())
34 const T& operator*() const BY_CONST_FUNC(operator*())
35 me& operator=(const binder& rhs);
36 me& operator=(const me& rhs);
37
38 public:
39 using super::get;
40 T* get();
41 const T* get() const BY_CONST_FUNC(get())
42
43 using super::bind;
44 nbool bind(const T& new1);
45 };
46
47 // extension for OR macro:
48 template <typename T, typename F> tmedium<T> operator|(tweak<T>& t, F&& f) {
49 f(t);
50 // this returns null-reference but take it easy.
51 // it'll never be used.
52 return t.get();
53 }
54
55 template <typename T, typename F> tmedium<T> operator|(const tweak<T>& t, F&& f) {
56 f(t);
57 // this returns null-reference but take it easy.
58 // it'll never be used.
59 return t.get();
60 }
61
62 template <typename T, typename F> tweak<T> operator|(tweak<T>&& t, F&& f) {
63 f(t);
64 // this returns null-reference but take it easy.
65 // it'll never be used.
66 return t;
67 }
68
69 // extension for typeTrait:
70 template <typename T> struct typeTrait<tweak<T>> {
71 typedef tweak<T> Org;
72 typedef tweak<T>& Ref;
73 typedef tweak<T>* Ptr;
74
75 static tweak<T> ret() { return tweak<T>{}; } // return default value
76
77 static nbool isNul(const tweak<T>& it) { return !it.isBind(); }
78
79 static constexpr nbool is_ptr = false;
80 static constexpr nbool is_ref = false;
81 static constexpr nbool is_like_ptr = true;
82 };
83
84 template <typename T> struct typeTrait<tweak<T>*> {
85 typedef tweak<T> Org;
86 typedef tweak<T>& Ref;
87 typedef tweak<T>* Ptr;
88
89 static tweak<T>* ret() { return nullptr; }
90
91 static nbool isNul(const tweak<T>* it) { return !it || !it->isBind(); }
92
93 static constexpr nbool is_ptr = true;
94 static constexpr nbool is_ref = false;
95 static constexpr nbool is_like_ptr = is_ptr;
96 };
97
98 template <typename T> struct typeTrait<tweak<T>&> {
99 typedef tweak<T> Org;
100 typedef tweak<T>& Ref;
101 typedef tweak<T>* Ptr;
102
103 static nbool isNul(const tweak<T>& it) { return !it.isBind(); }
104
105 static tweak<T>& ret() {
106 static tweak<T> dummy;
107 return dummy;
108 }
109
110 static constexpr nbool is_ptr = false;
111 static constexpr nbool is_ref = true;
112 static constexpr nbool is_like_ptr = true;
113 };
114
115 // extension for TO macro:
116 template <typename T, typename F>
117 auto operator->*(tweak<T>& t, F&& f) -> decltype(typeTrait<decltype(f(*t))>::ret()) {
118 return t ? f(*t) : typeTrait<decltype(f(*t))>::ret();
119 }
120
121 template <typename T, typename F>
122 auto operator->*(const tweak<T>& t, F&& f) -> decltype(typeTrait<decltype(f(*t))>::ret()) {
123 return t ? f(*t) : typeTrait<decltype(f(*t))>::ret();
124 }
125
126 template <typename T, typename F>
127 auto operator->*(tweak<T>&& t, F&& f) -> decltype(typeTrait<decltype(f(*t))>::ret()) {
128 return t ? f(*t) : typeTrait<decltype(f(*t))>::ret();
129 }
130
131} // namespace by
Binding tag for instance reference tracking and lifecycle management.
Definition: bindTag.hpp:15
Smart pointer with loose type checking and lifetime management.
Definition: binder.hpp:53
Medium class used exclusively in the OR macro for safe reference handling.
Definition: tmedium.hpp:51
Weak reference smart pointer with type-safe access.
Definition: tweak.hpp:15
tweak()
tweak:
Base class for runtime type information in byeol language.
Definition: type.hpp:12
Type trait utilities for template metaprogramming.
Definition: typeTrait.hpp:13