Molybden API
Loading...
Searching...
No Matches
delegate.hpp
1// Copyright (c) 2000-2023 TeamDev Ltd. All rights reserved.
2// TeamDev PROPRIETARY and CONFIDENTIAL.
3// Use is subject to license terms.
4
5#ifndef MOLYBDEN_DELEGATE_HPP
6#define MOLYBDEN_DELEGATE_HPP
7
8#include <functional>
9#include <memory>
10#include <mutex>
11
12namespace molybden {
13
29template <typename Args, typename Action>
30class Delegate {
31 public:
32 using ArgsType = Args;
33 using ActionType = Action;
34
38 using Signature = void(const Args& args, Action action);
39
56 Delegate& operator=(std::function<Signature> callback);
57
61 void reset();
62
63 private:
64 std::mutex callback_mutex_;
65 std::function<Signature> callback_;
66
67 void operator()(const Args& args, Action action);
68
69 friend class ObservableOwner;
70};
71
72template <typename Args, typename Action>
74 std::function<Signature> callback) {
75 std::lock_guard<std::mutex> guard(callback_mutex_);
76 callback_ = callback;
77 return *this;
78}
79
80template <typename Args, typename Action>
81void Delegate<Args, Action>::operator()(const Args& args, Action action) {
82 std::lock_guard<std::mutex> guard(callback_mutex_);
83 if (callback_) {
84 callback_(args, std::move(action));
85 }
86}
87
88template <typename Args, typename Action>
90 std::lock_guard<std::mutex> guard(callback_mutex_);
91 callback_ = nullptr;
92}
93
94} // namespace molybden
95
96#endif // MOLYBDEN_DELEGATE_HPP
The base API that implements classes which represent delegate's action.
Definition delegate_action.hpp:18
Delegates allow you to make decisions that affect the application behavior.
Definition delegate.hpp:30
void reset()
Resets the callback registered for this delegate.
Definition delegate.hpp:89
Delegate & operator=(std::function< Signature > callback)
Registers a callback to be invoked by this delegate.
Definition delegate.hpp:73
void(const Args &args, Action action) Signature
The callback's signature.
Definition delegate.hpp:38