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 {
18 BY_INIT_META(me)
19
20 public:
21 tweak();
22 tweak(const type& subtype);
23 tweak(const T& it);
24 tweak(const T* it);
25 tweak(const tmedium<T>& it);
26 tweak(const me& rhs);
27 explicit tweak(const binder& rhs);
28
29 public:
30 T* operator->();
31 T& operator*();
32 const T* operator->() const BY_CONST_FUNC(operator->())
33 const T& operator*() const BY_CONST_FUNC(operator*())
34 me& operator=(const binder& rhs);
35 me& operator=(const me& rhs);
36
37 public:
38 using super::get;
39 T* get();
40 const T* get() const BY_CONST_FUNC(get())
41
42 using super::bind;
51 nbool bind(const T& new1);
52 };
53
54 // extension for OR macro:
55 template <typename T, typename F> tmedium<T> operator|(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> tmedium<T> operator|(const 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.get();
67 }
68
69 template <typename T, typename F> tweak<T> operator|(tweak<T>&& t, F&& f) {
70 f(t);
71 // this returns null-reference but take it easy.
72 // it'll never be used.
73 return t;
74 }
75
76 // extension for typeTrait:
77 template <typename T> struct typeTrait<tweak<T>> {
78 typedef tweak<T> Org;
79 typedef tweak<T>& Ref;
80 typedef tweak<T>* Ptr;
81
82 static tweak<T> ret() { return tweak<T>{}; } // return default value
83
84 static nbool isNul(const tweak<T>& it) { return !it.isBind(); }
85
86 static constexpr nbool is_ptr = false;
87 static constexpr nbool is_ref = false;
88 static constexpr nbool is_like_ptr = true;
89 };
90
91 template <typename T> struct typeTrait<tweak<T>*> {
92 typedef tweak<T> Org;
93 typedef tweak<T>& Ref;
94 typedef tweak<T>* Ptr;
95
96 static tweak<T>* ret() { return nullptr; }
97
98 static nbool isNul(const tweak<T>* it) { return !it || !it->isBind(); }
99
100 static constexpr nbool is_ptr = true;
101 static constexpr nbool is_ref = false;
102 static constexpr nbool is_like_ptr = is_ptr;
103 };
104
105 template <typename T> struct typeTrait<tweak<T>&> {
106 typedef tweak<T> Org;
107 typedef tweak<T>& Ref;
108 typedef tweak<T>* Ptr;
109
110 static nbool isNul(const tweak<T>& it) { return !it.isBind(); }
111
112 static tweak<T>& ret() {
113 static tweak<T> dummy;
114 return dummy;
115 }
116
117 static constexpr nbool is_ptr = false;
118 static constexpr nbool is_ref = true;
119 static constexpr nbool is_like_ptr = true;
120 };
121
122 // extension for TO macro:
123 template <typename T, typename F>
124 auto operator->*(tweak<T>& t, F&& f) -> decltype(typeTrait<decltype(f(*t))>::ret()) {
125 return t ? f(*t) : typeTrait<decltype(f(*t))>::ret();
126 }
127
128 template <typename T, typename F>
129 auto operator->*(const tweak<T>& t, F&& f) -> decltype(typeTrait<decltype(f(*t))>::ret()) {
130 return t ? f(*t) : typeTrait<decltype(f(*t))>::ret();
131 }
132
133 template <typename T, typename F>
134 auto operator->*(tweak<T>&& t, F&& f) -> decltype(typeTrait<decltype(f(*t))>::ret()) {
135 return t ? f(*t) : typeTrait<decltype(f(*t))>::ret();
136 }
137
138} // 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