NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
event-emitter.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #ifndef NDN_UTIL_EVENT_EMITTER_HPP
23 #define NDN_UTIL_EVENT_EMITTER_HPP
24 
25 #include "../common.hpp"
26 #include <vector>
27 
28 namespace ndn {
29 namespace util {
30 
46 template<typename ...TArgs>
47 class EventEmitter : noncopyable
48 {
49 public:
52  typedef function<void(const TArgs&...)> Handler;
53 
56  void
57  operator+=(const Handler& handler);
58 
61  bool
62  isEmpty() const;
63 
66  void
67  clear();
68 
71  void
72  operator()(const TArgs&...args) const;
73 
74 private:
75  std::vector<Handler> m_handlers;
76 };
77 
78 template<typename ...TArgs>
79 inline void
81 {
82  m_handlers.push_back(handler);
83 }
84 
85 template<typename ...TArgs>
86 inline bool
88 {
89  return m_handlers.empty();
90 }
91 
92 template<typename ...TArgs>
93 inline void
95 {
96  return m_handlers.clear();
97 }
98 
99 template<typename ...TArgs>
100 inline void
101 EventEmitter<TArgs...>::operator()(const TArgs&...args) const
102 {
103  for (const Handler& handler : m_handlers) {
104  handler(args...);
105  if (m_handlers.empty()) // .clear has been called
106  return;
107  }
108 }
109 
110 
111 } // namespace util
112 } // namespace ndn
113 
114 #endif // NDN_UTIL_EVENT_EMITTER_HPP
Copyright (c) 2011-2015 Regents of the University of California.
void clear()
clears all subscriptions
function< void(const TArgs &...)> Handler
represents a handler that can subscribe to the event
void operator()(const TArgs &...args) const
triggers the event
void operator+=(const Handler &handler)
subscribes to the event
provides a lightweight event system