mn_event.hpp
Go to the documentation of this file.
1 
21 #ifndef __MINILIB_EVENT_H__
22 #define __MINILIB_EVENT_H__
23 
24 #include "mn_config.hpp"
25 
26 #include "mn_function.hpp"
27 
28 #include "mn_autolock.hpp"
29 #include "container/mn_vector.hpp"
30 #include "mn_mutex.hpp"
31 
32 namespace mn {
33 
34  struct base_eventargs {
35  base_eventargs() = default;
36  const char* get_name() { return "base_eventargs"; }
37  };
38 
40  public:
41  exit_eventargs(int exitcode) : m_iExitCode(exitcode) { }
42 
43  int get_exitcode() { return m_iExitCode; }
44  const char* get_name() { return "exit_eventargs"; }
45  private:
47  };
48 
49  template <typename T>
51  public:
52  handler_eventargs(T* handler) : m_pHandler(handler) { }
53 
54  T* get_handler() { return m_pHandler; }
55  const char* get_name() { return "handler_eventargs"; }
56  private:
58  };
59 
60  template<class TSender, typename TArgs = base_eventargs>
61  struct base_event {
63  using args_type = TArgs;
64  using sender_type = TSender;
65  using sender_reference = TSender&;
67 
69  base_event(const self_type& other) : m_ptr2Func(other.m_ptr2Func) { }
70  base_event(const self_type&& other) : m_ptr2Func(mn::move(other.m_ptr2Func)) { }
71 
72  void emit(const sender_type& sender, const args_type args){
73  get_function()(sender, args);
74  }
75 
76  bool operator == (const self_type& pOther){
77  return pOther.m_ptr2Func == m_ptr2Func;
78  }
79 
81  m_ptr2Func = other.m_ptr2Func; return *this;
82  }
83 
85  protected:
87  };
88 
89  template< class TSender, typename TArgs,
90  class TBaseEvent = base_event<TSender, TArgs>,
91  class TContainer = mn::container::vector<TBaseEvent>
92  >
93  class event_system {
94  public:
96 
97  using sender_type = TSender;
98  using args_type = TArgs;
99  using container_type = TContainer;
100 
101  using value_type = TBaseEvent;
102  using reference = TBaseEvent&;
103  using const_reference = const TBaseEvent&;
104  using pointer = TBaseEvent*;
105  using const_pointer = const TBaseEvent*;
106 
107  using size_type = typename container_type::size_type;
108  using allocator_type = typename container_type::allocator_type;
109 
110  event_system(size_type initialSize = 4, const allocator_type& allocator = allocator_type())
111  : m_observers(initialSize, allocator), m_mutex() { }
112 
113  event_system(const self_type& other)
114  : m_observers(other.m_observers),
115  m_mutex(other.m_mutex) { }
116 
117  void emit(const TSender& sender, const args_type args) {
119 
120  for(auto itr = m_observers.begin();
121  itr != m_observers.end();
122  itr++) {
123  (*itr)->emit(sender, args);
124  }
125  }
126 
127  void bind( const_reference funcHandler) {
129 
130  m_observers.push_back(funcHandler);
131  }
132  void remove( const_reference funcHandler) {
134 
135  auto it = m_observers.find(funcHandler);
136  if(it == m_observers.end()) return;
137 
138  m_observers.erase(it);
139  }
140 
141  void operator += ( const_reference funcHandler) {
142  add(funcHandler);
143  }
144  void operator -= ( const_reference funcHandler) {
145  rem(funcHandler);
146  }
147  void operator () ( const TSender& sender, const args_type args) {
148  emit(sender, args);
149  }
150  private:
153  };
154 }
155 
156 #endif // __MINILIB_EVENT_H__
Definition: mn_lock.hpp:122
Definition: mn_mutex.hpp:37
Definition: mn_vector.hpp:117
Definition: mn_event.hpp:93
typename container_type::allocator_type allocator_type
Definition: mn_event.hpp:108
event_system(const self_type &other)
Definition: mn_event.hpp:113
TContainer container_type
Definition: mn_event.hpp:99
const TBaseEvent * const_pointer
Definition: mn_event.hpp:105
void remove(const_reference funcHandler)
Definition: mn_event.hpp:132
void operator-=(const_reference funcHandler)
Definition: mn_event.hpp:144
mutex_t m_mutex
Definition: mn_event.hpp:152
void emit(const TSender &sender, const args_type args)
Definition: mn_event.hpp:117
TBaseEvent & reference
Definition: mn_event.hpp:102
const TBaseEvent & const_reference
Definition: mn_event.hpp:103
void operator+=(const_reference funcHandler)
Definition: mn_event.hpp:141
typename container_type::size_type size_type
Definition: mn_event.hpp:107
TArgs args_type
Definition: mn_event.hpp:98
TSender sender_type
Definition: mn_event.hpp:97
event_system(size_type initialSize=4, const allocator_type &allocator=allocator_type())
Definition: mn_event.hpp:110
void operator()(const TSender &sender, const args_type args)
Definition: mn_event.hpp:147
void bind(const_reference funcHandler)
Definition: mn_event.hpp:127
TBaseEvent value_type
Definition: mn_event.hpp:101
container_type m_observers
Definition: mn_event.hpp:151
TBaseEvent * pointer
Definition: mn_event.hpp:104
Definition: mn_event.hpp:39
int get_exitcode()
Definition: mn_event.hpp:43
int m_iExitCode
Definition: mn_event.hpp:46
const char * get_name()
Definition: mn_event.hpp:44
exit_eventargs(int exitcode)
Definition: mn_event.hpp:41
Definition: mn_event.hpp:50
T * get_handler()
Definition: mn_event.hpp:54
handler_eventargs(T *handler)
Definition: mn_event.hpp:52
const char * get_name()
Definition: mn_event.hpp:55
T * m_pHandler
Definition: mn_event.hpp:57
Basic vector container This file is part of the Mini Thread Library (https://github....
struct mn::memory::detail::ptr_difference T
Definition: mn_atomic_singleton.hpp:38
Definition: mn_allocator_typetraits.hpp:25
int lock(TLOCK &m1, unsigned int timeout)
Definition: mn_autolock.hpp:70
void move(const T *src, const T *last, T *dest)
Definition: mn_algorithm.hpp:100
Definition: mn_function.hpp:34
Definition: mn_event.hpp:61
self_type & operator=(self_type &other)
Definition: mn_event.hpp:80
TSender & sender_reference
Definition: mn_event.hpp:65
base_event(function_type func)
Definition: mn_event.hpp:68
TSender sender_type
Definition: mn_event.hpp:64
bool operator==(const self_type &pOther)
Definition: mn_event.hpp:76
function_type get_function()
Definition: mn_event.hpp:84
base_event(const self_type &&other)
Definition: mn_event.hpp:70
function_type m_ptr2Func
Definition: mn_event.hpp:86
base_event(const self_type &other)
Definition: mn_event.hpp:69
void emit(const sender_type &sender, const args_type args)
Definition: mn_event.hpp:72
TArgs args_type
Definition: mn_event.hpp:63
Definition: mn_event.hpp:34
const char * get_name()
Definition: mn_event.hpp:36
base_eventargs()=default