event.h
Go to the documentation of this file.
1 /*
2  * Software License Agreement (Apache License)
3  *
4  * Copyright (c) 2018 Plus One Robotics
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #ifndef EVENT_HPP
20 #define EVENT_HPP
21 
22 #include <functional>
23 #include <vector>
24 
25 struct EventArgs
26 {
27  static const EventArgs EMPTY;
28 };
29 
30 template <class TOwningType, class TEventArgs>
32 {
33 public:
34  typedef std::function<void(TOwningType&, const TEventArgs&)> EventFunction;
35 
36  EventHandler() = default;
37 
38  friend TOwningType;
39 
40  template <typename TSubscribingType>
41  void bind_member_func(TSubscribingType* caller,
42  void (TSubscribingType::*member_func)(TOwningType&, const TEventArgs&))
43  {
44  callbacks_.push_back(std::bind(member_func, caller, std::placeholders::_1, std::placeholders::_2));
45  }
46 
47  template <typename TSubscribingType>
48  void unbind_member_func(TSubscribingType* caller,
49  void (TSubscribingType::*member_func)(TOwningType&, const TEventArgs&))
50  {
51  std::function<void(TOwningType&, const TEventArgs&)> callback =
52  std::bind(member_func, caller, std::placeholders::_1, std::placeholders::_2);
53  for (auto iter = callbacks_.begin(); iter != callbacks_.end(); iter++)
54  {
55  if (callback.target_type() == iter->target_type() &&
56  iter->template target<std::function<void(TOwningType&, const TEventArgs&)>>() ==
57  callback.template target<std::function<void(TOwningType&, const TEventArgs&)>>())
58  {
59  callbacks_.erase(iter);
60  callbacks_.shrink_to_fit();
61  break;
62  }
63  }
64  }
65 
66 private:
67  std::vector<std::function<void(TOwningType&, const TEventArgs&)>> callbacks_;
68 
69  void invoke(TOwningType& sender, TEventArgs a)
70  {
71  const std::vector<EventFunction> callbacks(callbacks_);
72  for (auto it = callbacks.begin(); it != callbacks.end(); ++it)
73  {
74  (*it)(sender, a);
75  }
76  }
77 };
78 
79 #endif /* EVENT_HPP */
void invoke(TOwningType &sender, TEventArgs a)
Definition: event.h:69
friend TOwningType
Definition: event.h:38
void bind_member_func(TSubscribingType *caller, void(TSubscribingType::*member_func)(TOwningType &, const TEventArgs &))
Definition: event.h:41
void unbind_member_func(TSubscribingType *caller, void(TSubscribingType::*member_func)(TOwningType &, const TEventArgs &))
Definition: event.h:48
std::function< void(TOwningType &, const TEventArgs &)> EventFunction
Definition: event.h:34
std::vector< std::function< void(TOwningType &, const TEventArgs &)> > callbacks_
Definition: event.h:67
static const EventArgs EMPTY
Definition: event.h:27


packml_sm
Author(s): Shaun Edwards
autogenerated on Fri Jul 12 2019 03:30:55