Loading...
Searching...
No Matches
tweak.hpp
Go to the documentation of this file.
1
2#pragma once
3
6
7namespace by {
8
9 class origin;
10
16 template <typename T, typename TACTIC = weakTactic> class tweak: public binder {
17 BY(ME(tweak, binder), INIT_META(me))
18
19 public:
20 tweak();
21 tweak(const type& subtype);
22 tweak(const T& it);
23 tweak(const T* it);
24 tweak(const tmedium<T>& it);
25 tweak(const me& rhs);
26 explicit tweak(const binder& rhs);
27
28 public:
29 T* operator->();
30 T& operator*();
31 const T* operator->() const BY_CONST_FUNC(operator->())
32 const T& operator*() const BY_CONST_FUNC(operator*())
33 me& operator=(const binder& rhs);
34 me& operator=(const me& rhs);
35
36 public:
37 using super::get;
38 T* get();
39 const T* get() const BY_CONST_FUNC(get())
40
41 using super::bind;
50 nbool bind(const T& new1);
51 };
52
53 // extension for OR macro:
54 template <typename T, typename F> tmedium<T> operator|(tweak<T>& t, F&& f) {
55 f(t);
56 // this returns null-reference but take it easy.
57 // it'll never be used.
58 return t.get();
59 }
60
61 template <typename T, typename F> tmedium<T> operator|(const tweak<T>& t, F&& f) {
62 f(t);
63 // this returns null-reference but take it easy.
64 // it'll never be used.
65 return t.get();
66 }
67
68 template <typename T, typename F> tweak<T> operator|(tweak<T>&& t, F&& f) {
69 f(t);
70 // this returns null-reference but take it easy.
71 // it'll never be used.
72 return t;
73 }
74
75 // extension for typeTrait:
76 template <typename T> struct typeTrait<tweak<T>> {
77 typedef tweak<T> Org;
78 typedef tweak<T>& Ref;
79 typedef tweak<T>* Ptr;
80
81 static tweak<T> ret() { return tweak<T>{}; } // return default value
82
83 static nbool isNul(const tweak<T>& it) { return !it.isBind(); }
84
85 static constexpr nbool is_ptr = false;
86 static constexpr nbool is_ref = false;
87 static constexpr nbool is_like_ptr = true;
88 };
89
90 template <typename T> struct typeTrait<tweak<T>*> {
91 typedef tweak<T> Org;
92 typedef tweak<T>& Ref;
93 typedef tweak<T>* Ptr;
94
95 static tweak<T>* ret() { return nullptr; }
96
97 static nbool isNul(const tweak<T>* it) { return !it || !it->isBind(); }
98
99 static constexpr nbool is_ptr = true;
100 static constexpr nbool is_ref = false;
101 static constexpr nbool is_like_ptr = is_ptr;
102 };
103
104 template <typename T> struct typeTrait<tweak<T>&> {
105 typedef tweak<T> Org;
106 typedef tweak<T>& Ref;
107 typedef tweak<T>* Ptr;
108
109 static nbool isNul(const tweak<T>& it) { return !it.isBind(); }
110
111 static tweak<T>& ret() {
112 static tweak<T> dummy;
113 return dummy;
114 }
115
116 static constexpr nbool is_ptr = false;
117 static constexpr nbool is_ref = true;
118 static constexpr nbool is_like_ptr = true;
119 };
120
121 // extension for TO macro:
122 template <typename T, typename F>
123 auto operator->*(tweak<T>& t, F&& f) -> decltype(typeTrait<decltype(f(*t))>::ret()) {
124 return t ? f(*t) : typeTrait<decltype(f(*t))>::ret();
125 }
126
127 template <typename T, typename F>
128 auto operator->*(const tweak<T>& t, F&& f) -> decltype(typeTrait<decltype(f(*t))>::ret()) {
129 return t ? f(*t) : typeTrait<decltype(f(*t))>::ret();
130 }
131
132 template <typename T, typename F>
133 auto operator->*(tweak<T>&& t, F&& f) -> decltype(typeTrait<decltype(f(*t))>::ret()) {
134 return t ? f(*t) : typeTrait<decltype(f(*t))>::ret();
135 }
136
137} // namespace by
Generalized binding class for managing instance lifecycles.
Definition binder.hpp:108
Weak reference smart pointer with type-safe access.
Definition tweak.hpp:16
const T *get() const BY_CONST_FUNC(get()) using super nbool bind(const T &new1)
Binds a weak reference to the specified instance.
Core class for runtime type information in byeol language.
Definition type.hpp:100
Rich logging support with polymorphic type conversion.
Definition richLog.hpp:34