23 template<
class observer_t>
65 void reset() noexcept;
74 operator bool()
const noexcept;
78 void* m_pObserver =
nullptr;
81 using observer_token = std::unique_ptr<observer_token_data>;
104 virtual void detach(
void* pObserver) noexcept = 0;
118 template<
class observer_t>
121 template<
class base_iterator_t>
122 class base_iterator :
public base_iterator_t
125 base_iterator() noexcept =
default;
132 base_iterator(
const base_iterator_t& other,
subject* pSubject) noexcept;
134 base_iterator(
const base_iterator& other) noexcept;
135 base_iterator(base_iterator&&) noexcept =
default;
136 ~base_iterator() noexcept;
138 base_iterator& operator=(
const base_iterator& other) noexcept;
139 observer_t* operator->() noexcept;
140 observer_t& operator*() noexcept;
146 void init() noexcept;
152 template<
class base_iterator_t>
153 class const_base_iterator :
public base_iterator_t
156 const_base_iterator() noexcept =
default;
157 const_base_iterator(
const const_base_iterator&) noexcept =
default;
158 const_base_iterator(const_base_iterator&&) noexcept =
default;
159 const_base_iterator(
const base_iterator_t& other) noexcept;
161 const observer_t* operator->()
const noexcept;
162 const observer_t& operator*()
const noexcept;
166 using observers_container = std::vector<observer_t*>;
167 using iterator = base_iterator<typename observers_container::iterator>;
168 using const_iterator = const_base_iterator<typename observers_container::const_iterator>;
169 using reverse_iterator = base_iterator<typename observers_container::reverse_iterator>;
170 using const_reverse_iterator = const_base_iterator<typename observers_container::const_reverse_iterator>;
171 using notify_func = std::function<void(observer_t*)>;
185 [[nodiscard]] observer_token
attach(observer_t* pObserver) noexcept;
191 void notify(
const notify_func& notifyFunc)
const noexcept;
209 const_iterator
cbegin()
const;
221 const_iterator
end()
const;
227 const_iterator
cend()
const;
233 reverse_iterator
rbegin();
245 const_reverse_iterator
crbegin()
const;
251 reverse_iterator
rend();
257 const_reverse_iterator
rend()
const;
263 const_reverse_iterator
crend()
const;
276 virtual
void detach(
void* pObserver) noexcept override;
281 void on_iterator_destructed() noexcept;
286 void on_iterator_constructed() noexcept;
289 observers_container m_Observers;
291 size_t m_nIterators = 0;
296 #include <qx/patterns/observer.inl>
virtual void detach(void *pObserver) noexcept=0
Detach observer from subject.
Tokens are used to automatically detach observer when the observer object is destroyed.
void reset() noexcept
Reset observer_token.
Class maintains a list of its dependents, called observers, and notifies them automatically of any st...
const_iterator cbegin() const
Return const iterator to beginning.
iterator begin()
Return iterator to beginning.
const_iterator cend() const
Return const iterator to end.
const_reverse_iterator rend() const
Return reverse iterator to reverse end.
observer_token attach(observer_t *pObserver) noexcept
Attach observer to this subject.
iterator end()
Return iterator to end.
reverse_iterator rbegin()
Return reverse iterator to reverse beginning.
const_iterator end() const
Return iterator to end.
size_t get_num_observers() const noexcept
Get number of observers attached to this subject.
const_reverse_iterator rbegin() const
Return reverse iterator to reverse beginning.
void notify(const notify_func ¬ifyFunc) const noexcept
Notify all observers.
const_iterator begin() const
Return iterator to beginning.
const_reverse_iterator crend() const
Return const reverse iterator to reverse end.
reverse_iterator rend()
Return reverse iterator to reverse end.
const_reverse_iterator crbegin() const
Return const reverse iterator to reverse beginning.