binder.hpp
Go to the documentation of this file.
1
2#pragma once
3
5
6struct binderTest;
7
8namespace by {
9
10 class bindTacticable;
11 class bindTag;
12
53 class _nout binder: public typeProvidable, public tbindable<instance> {
54 BY(ME(binder, instance), INIT_META(me))
55
56 friend class weakTactic;
57 friend class strTactic;
58 friend class bindTag; // for _get()
59 friend struct ::binderTest; // for UT
60
61 public:
62 binder(const type& type, bindTacticable& tactic);
63 binder(const me& rhs);
64 virtual ~binder();
65
66 public:
73 const instance* operator->() const BY_CONST_FUNC(operator->())
74 instance& operator*();
75 const instance& operator*() const BY_CONST_FUNC(operator*())
76 me& operator=(const me& rhs);
77
78 public:
79 // binder:
80 id getItsId() const;
81 // tbindable:
82 nbool isBind() const override;
83 void rel() override;
84 using tbindable::canBind;
85 nbool canBind(const type& it) const override;
86 using tbindable::bind;
87 nbool bind(const instance& it) override;
88
89 instance* get();
90 const instance* get() const BY_CONST_FUNC(get())
91
92 template <typename E> E* get() { return get()->template cast<E>(); }
93 template <typename E> const E* get() const BY_CONST_FUNC(get<E>())
94
95 // typeProvidable:
96 const type& getType() const override;
97
98 using typeProvidable::cast;
99 void* cast(const type& to) override;
100
101 protected:
102 nbool _assign(const binder& rhs);
103 nbool _onSame(const typeProvidable& rhs) const override;
104 bindTag* _getBindTag() const;
105
106 protected:
107 id _itsId; // id for binded one
108 const type* _type;
109 bindTacticable* _tactic;
110 };
111
112 // extension for OR macro:
113 template <typename F> tmedium<instance> operator|(binder& t, F&& f) {
114 f(t);
115 // this returns null-reference but take it easy.
116 // it'll never be used.
117 return t.get();
118 }
119
120 template <typename F> tmedium<instance> operator|(const binder& t, F&& f) {
121 f(t);
122 // this returns null-reference but take it easy.
123 // it'll never be used.
124 return t.get();
125 }
126
127 // extension for typeTrait:
128 template <> struct typeTrait<binder> {
129 typedef binder Org;
130 typedef binder& Ref;
131 typedef binder* Ptr;
132
133 static nbool isNul(const binder& it) { return !it.isBind(); }
134
135 static constexpr nbool is_ptr = false;
136 static constexpr nbool is_ref = false;
137 static constexpr nbool is_like_ptr = true;
138 };
139
140 template <> struct typeTrait<binder*> {
141 typedef binder Org;
142 typedef binder& Ref;
143 typedef binder* Ptr;
144
145 static binder* ret() { return nullptr; }
146
147 static nbool isNul(const binder* it) { return !it || !it->isBind(); }
148
149 static constexpr nbool is_ptr = true;
150 static constexpr nbool is_ref = false;
151 static constexpr nbool is_like_ptr = is_ptr;
152 };
153
154 template <> struct typeTrait<binder&> {
155 typedef binder Org;
156 typedef binder& Ref;
157 typedef binder* Ptr;
158
159 static nbool isNul(const binder& it) { return it.isBind(); }
160
161 static constexpr nbool is_ptr = false;
162 static constexpr nbool is_ref = true;
163 static constexpr nbool is_like_ptr = true;
164 };
165
166 // extension for TO macro:
167 template <typename T, typename F> auto operator->*(binder* t, F&& f) {
168 return t ? f(**t) : typeTrait<std::decay_t<decltype(f(**t))>>::ret();
169 }
170
171 template <typename T, typename F> auto operator->*(const binder* t, F&& f) {
172 return t ? f(**t) : typeTrait<std::decay_t<decltype(f(**t))>>::ret();
173 }
174
175 template <typename T, typename F> auto operator->*(binder& t, F&& f) {
176 return t ? f(*t) : typeTrait<std::decay_t<decltype(f(*t))>>::ret();
177 }
178} // namespace by
Interface for binding tactics in memory management.
Definition: bindTacticable.hpp:15
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
instance * operator->()
Dereference operator to access bound instance.
Base class for all managed instances in memlite system.
Definition: instance.hpp:20
Strong reference binding tactic.
Definition: strTactic.hpp:12
Template interface for bindable objects.
Definition: tbindable.hpp:12
Medium class used exclusively in the OR macro for safe reference handling.
Definition: tmedium.hpp:51
Base class for runtime type information in byeol language.
Definition: type.hpp:12
Interface for objects that can provide their type information.
Definition: typeProvidable.hpp:12
Weak reference binding tactic.
Definition: weakTactic.hpp:13
Type trait utilities for template metaprogramming.
Definition: typeTrait.hpp:13