util: Signal

Signal is an enhanced version of EventEmitter:

* only the owner can emit a signal (aka trigger an event)
* signal connection (aka event subscription) can be disconnected

EventEmitter is deprecated in favor of Signal.

refs #2279

Change-Id: I74ea5fef2e1e9b34776aa04f01170600b171152e
diff --git a/src/util/event-emitter.hpp b/src/util/event-emitter.hpp
index 261dabc..bcb703b 100644
--- a/src/util/event-emitter.hpp
+++ b/src/util/event-emitter.hpp
@@ -39,6 +39,8 @@
  *    onEventName(args);
  *  To clear event subscriptions:
  *    onEventName.clear();
+ *
+ *  \deprecated use Signal instead
  */
 
 template<typename ...TArgs>
diff --git a/src/util/signal-connection.cpp b/src/util/signal-connection.cpp
new file mode 100644
index 0000000..8660e4d
--- /dev/null
+++ b/src/util/signal-connection.cpp
@@ -0,0 +1,64 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2014 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#include "signal-connection.hpp"
+
+namespace ndn {
+namespace util {
+namespace signal {
+
+BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Connection>));
+
+Connection::Connection()
+{
+}
+
+Connection::Connection(weak_ptr<function<void()>> disconnect)
+  : m_disconnect(disconnect)
+{
+}
+
+void
+Connection::disconnect()
+{
+  shared_ptr<function<void()>> f = m_disconnect.lock();
+  if (f != nullptr) {
+    (*f)();
+  }
+}
+
+bool
+Connection::operator==(const Connection& other) const
+{
+  shared_ptr<function<void()>> f1 = m_disconnect.lock();
+  shared_ptr<function<void()>> f2 = other.m_disconnect.lock();
+  return f1 == f2;
+}
+
+bool
+Connection::operator!=(const Connection& other) const
+{
+  return !(this->operator==(other));
+}
+
+} // namespace signal
+} // namespace util
+} // namespace ndn
diff --git a/src/util/signal-connection.hpp b/src/util/signal-connection.hpp
new file mode 100644
index 0000000..1276182
--- /dev/null
+++ b/src/util/signal-connection.hpp
@@ -0,0 +1,83 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2014 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#ifndef NDN_UTIL_SIGNAL_CONNECTION_HPP
+#define NDN_UTIL_SIGNAL_CONNECTION_HPP
+
+#include "../common.hpp"
+
+namespace ndn {
+namespace util {
+namespace signal {
+
+/** \brief represents a connection to a signal
+ *  \note This type is copyable. Any copy can be used to disconnect.
+ */
+class Connection
+{
+public:
+  Connection();
+
+  /** \brief disconnects from the signal
+   *  \note If the connection is already disconnected, or if the Signal has been destructed,
+   *        this operation has no effect.
+   *  \warning During signal emission, attempting to disconnect a connection other than
+   *           the executing handler's own connection results in undefined behavior.
+   */
+  void
+  disconnect();
+
+  /** \brief compare for equality
+   *
+   *  Two connections are equal if they both refer to the same connection that isn't disconnected,
+   *  or they are both disconnected.
+   */
+  bool
+  operator==(const Connection& other) const;
+
+  bool
+  operator!=(const Connection& other) const;
+
+private:
+  /** \param disconnect weak_ptr to a function that disconnects the handler
+   */
+  explicit
+  Connection(weak_ptr<function<void()>> disconnect);
+
+  template<typename Owner, typename ...TArgs>
+  friend class Signal;
+
+private:
+  /** \note The only shared_ptr to the disconnect function is stored in Signal<..>::Slot,
+   *        and will be destructed if the handler is disconnected (through another Connection
+   *        instance) or the Signal is destructed.
+   *        Connection needs a weak_ptr instead of a shared_ptr to the disconnect function,
+   *        because the disconnect function is bound with an iterator to the Slot,
+   *        which is invalidated when the Slot is erased.
+   */
+  weak_ptr<function<void()>> m_disconnect;
+};
+
+} // namespace signal
+} // namespace util
+} // namespace ndn
+
+#endif // NDN_UTIL_SIGNAL_CONNECTION_HPP
diff --git a/src/util/signal-emit.hpp b/src/util/signal-emit.hpp
new file mode 100644
index 0000000..729d92a
--- /dev/null
+++ b/src/util/signal-emit.hpp
@@ -0,0 +1,81 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2014 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+/** \file
+ *
+ *  This header provides macros that allows a signal to be emitted
+ *  from a derived class of its owner.
+ *
+ *  In 'protected' section of owner class declaration,
+ *    DECLARE_SIGNAL_EMIT(signalName)
+ *  From a derived class of owner,
+ *    this->emitSignal(signalName, arg1, arg2);
+ */
+
+#ifndef NDN_UTIL_SIGNAL_EMIT_HPP
+#define NDN_UTIL_SIGNAL_EMIT_HPP
+
+namespace ndn {
+namespace util {
+namespace signal {
+
+/** \brief (implementation detail) a filler for extra argument
+ */
+class DummyExtraArg
+{
+};
+
+} // namespace signal
+} // namespace util
+} // namespace ndn
+
+/** \brief (implementation detail) declares a 'emit_signalName' method
+ *  \note This macro should be used in 'protected' section so that it's accessible
+ *        by derived classes.
+ *  \note emit_signalName method is implementation detail.
+ *        Derived classes should use 'emit' macro.
+ *  \note The name 'emit_signalName' is an intentional violation of code-style rule 2.5.
+ *  \note The DummyExtraArg is necessary so that 'emit' macro can be used for
+ *        both signals with zero arguments and signals with non-zero arguments.
+ *  \note The method is declared as a template, so that the macro doesn't need argument types.
+ *        But only argument types that are compatible with Signal declaration will work.
+ */
+#define DECLARE_SIGNAL_EMIT(signalName) \
+  template<typename ...TArgs> \
+  void emit_##signalName(const TArgs&... args, const ::ndn::util::signal::DummyExtraArg&) \
+  { \
+    signalName(args...); \
+  }
+
+/** \brief (implementation detail) invokes emit_signalName method
+ *  \note C99 requires at least one argument to be passed in __VA_ARGS__,
+ *        thus a DummyExtraArg is expected at the end of __VA_ARGS__,
+ *        which will be accepted but ignored by emit_signalName method.
+ */
+#define NDN_CXX_SIGNAL_EMIT(signalName, ...) \
+  emit_##signalName(__VA_ARGS__)
+
+/** \brief (implementation detail)
+ */
+#define emitSignal(...) \
+  NDN_CXX_SIGNAL_EMIT(__VA_ARGS__, ::ndn::util::signal::DummyExtraArg())
+
+#endif // NDN_UTIL_SIGNAL_EMIT_HPP
diff --git a/src/util/signal-scoped-connection.cpp b/src/util/signal-scoped-connection.cpp
new file mode 100644
index 0000000..ae90c5f
--- /dev/null
+++ b/src/util/signal-scoped-connection.cpp
@@ -0,0 +1,72 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2014 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#include "signal-scoped-connection.hpp"
+
+namespace ndn {
+namespace util {
+namespace signal {
+
+ScopedConnection::ScopedConnection()
+{
+}
+
+ScopedConnection::ScopedConnection(const Connection& connection)
+  : m_connection(connection)
+{
+}
+
+ScopedConnection::ScopedConnection(ScopedConnection&& other)
+  : m_connection(other.m_connection)
+{
+  other.release();
+}
+
+ScopedConnection&
+ScopedConnection::operator=(const Connection& connection)
+{
+  if (m_connection != connection) {
+    m_connection.disconnect();
+    m_connection = connection;
+  }
+  return *this;
+}
+
+ScopedConnection::~ScopedConnection()
+{
+  m_connection.disconnect();
+}
+
+void
+ScopedConnection::disconnect()
+{
+  m_connection.disconnect();
+}
+
+void
+ScopedConnection::release()
+{
+  m_connection = {};
+}
+
+} // namespace signal
+} // namespace util
+} // namespace ndn
diff --git a/src/util/signal-scoped-connection.hpp b/src/util/signal-scoped-connection.hpp
new file mode 100644
index 0000000..9104791
--- /dev/null
+++ b/src/util/signal-scoped-connection.hpp
@@ -0,0 +1,78 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2014 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#ifndef NDN_UTIL_SIGNAL_SCOPED_CONNECTION_HPP
+#define NDN_UTIL_SIGNAL_SCOPED_CONNECTION_HPP
+
+#include "signal-connection.hpp"
+
+namespace ndn {
+namespace util {
+namespace signal {
+
+/** \brief disconnects a Connection automatically upon destruction
+ */
+class ScopedConnection : noncopyable
+{
+public:
+  ScopedConnection();
+
+  /** \brief implicit constructor from Connection
+   *  \param connection the Connection to be disconnected upon destruction
+   */
+  ScopedConnection(const Connection& connection);
+
+  /** \brief move constructor
+   */
+  ScopedConnection(ScopedConnection&& other);
+
+  /** \brief assigns a connection
+   *
+   *  If a different connection has been assigned to this instance previously,
+   *  that connection will be disconnected immediately.
+   */
+  ScopedConnection&
+  operator=(const Connection& connection);
+
+  /** \brief disconnects the connection
+   */
+  ~ScopedConnection();
+
+  /** \brief disconnects the connection manually
+   */
+  void
+  disconnect();
+
+  /** \brief releases the connection so that it won't be disconnected
+   *         when this ScopedConnection is destructed
+   */
+  void
+  release();
+
+private:
+  Connection m_connection;
+};
+
+} // namespace signal
+} // namespace util
+} // namespace ndn
+
+#endif // NDN_UTIL_SIGNAL_SCOPED_CONNECTION_HPP
diff --git a/src/util/signal-signal.hpp b/src/util/signal-signal.hpp
new file mode 100644
index 0000000..6fd6c70
--- /dev/null
+++ b/src/util/signal-signal.hpp
@@ -0,0 +1,218 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2014 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#ifndef NDN_UTIL_SIGNAL_SIGNAL_HPP
+#define NDN_UTIL_SIGNAL_SIGNAL_HPP
+
+#include "signal-connection.hpp"
+#include <list>
+
+namespace ndn {
+namespace util {
+namespace signal {
+
+/** \brief provides a lightweight signal / event system
+ *
+ *  To declare a signal:
+ *    public:
+ *      Signal<Owner, T1, T2> signalName;
+ *  To connect to a signal:
+ *    owner->signalName.connect(f);
+ *    Multiple functions can connect to the same signal.
+ *  To emit a signal from owner:
+ *    this->signalName(arg1, arg2);
+ *
+ *  \tparam Owner the signal owner class; only this class can emit the signal
+ *  \tparam TArgs types of signal arguments
+ *  \sa signal-emit.hpp allows owner's derived classes to emit signals
+ */
+template<typename Owner, typename ...TArgs>
+class Signal : noncopyable
+{
+public: // API for anyone
+  /** \brief represents a function that can connect to the signal
+   */
+  typedef function<void(const TArgs&...)> Handler;
+
+  Signal();
+
+  /** \brief connects a handler to the signal
+   *  \note If invoked from a handler, the new handler won't receive the current emitted signal.
+   */
+  Connection
+  connect(const Handler& handler);
+
+private: // API for owner
+  /** \retval true if there is no connection
+   */
+  bool
+  isEmpty() const;
+
+  /** \brief emits a signal
+   *  \param args arguments passed to all handlers
+   *  \warning Emitting the signal from a handler is undefined behavior.
+   *  \note If a handler throws, the exception will be propagated to the caller
+   *        who emits this signal, and some handlers may not be executed.
+   */
+  void
+  operator()(const TArgs&...args);
+
+  // make Owner a friend of Signal<Owner, ...> so that API for owner can be called
+#if NDN_CXX_HAVE_CXX_FRIEND_TYPENAME
+  friend Owner;
+#elif NDN_CXX_HAVE_CXX_FRIEND_TYPENAME_WRAPPER
+  template<typename T>
+  struct TypeWrapper
+  {
+    typedef T Type;
+  }; // http://stackoverflow.com/a/5608542/3729203
+  friend class TypeWrapper<Owner>::Type;
+#else
+#  error "cannot declare Owner as friend"
+#endif
+
+private: // internal implementation
+  typedef Signal<Owner, TArgs...> Self;
+  struct Slot;
+
+  /** \brief stores slots
+   *  \note std::list is used because iterators must not be invalidated
+   *        when other slots are added or removed
+   */
+  typedef std::list<Slot> SlotList;
+
+  /** \brief stores a handler function, and a function to disconnect this handler
+   */
+  struct Slot
+  {
+    /** \brief the handler function who will receive emitted signals
+     */
+    Handler handler;
+
+    /** \brief the disconnect function which will disconnect this handler
+     *
+     *  This is .disconnect method bound with the iterator to this slot.
+     *
+     *  This is the only shared_ptr to this function object.
+     *  Connection class has a weak_ptr which references the same function object.
+     *  When the slot is removed or the signal is destructed, this function object would be
+     *  destructed, and the related Connections cannot disconnect this slot again.
+     */
+    shared_ptr<function<void()>> disconnect;
+  };
+
+  /** \brief stores slots
+   */
+  SlotList m_slots;
+
+  /** \brief is a signal handler executing?
+   */
+  bool m_isExecuting;
+
+  /** \brief iterator to current executing slot
+   *  \note This field is meaningful when isExecuting==true
+   */
+  typename SlotList::iterator m_currentSlot;
+
+  /** \brief disconnects the handler in a slot
+   */
+  void
+  disconnect(typename SlotList::iterator it);
+};
+
+template<typename Owner, typename ...TArgs>
+Signal<Owner, TArgs...>::Signal()
+  : m_isExecuting(false)
+{
+}
+
+template<typename Owner, typename ...TArgs>
+inline Connection
+Signal<Owner, TArgs...>::connect(const Handler& handler)
+{
+  typename SlotList::iterator it = m_slots.insert(m_slots.end(), {handler, nullptr});
+  it->disconnect = make_shared<function<void()>>(bind(&Self::disconnect, this, it));
+
+  return signal::Connection(weak_ptr<function<void()>>(it->disconnect));
+}
+
+template<typename Owner, typename ...TArgs>
+inline void
+Signal<Owner, TArgs...>::disconnect(typename SlotList::iterator it)
+{
+  // it could be const_iterator, but gcc 4.6 doesn't support std::list::erase(const_iterator)
+
+  if (m_isExecuting) {
+    // during signal emission, only the currently executing handler can be disconnected
+    BOOST_ASSERT_MSG(it == m_currentSlot,
+                     "cannot disconnect another handler from a handler");
+    m_currentSlot = m_slots.end(); // prevent disconnect twice
+  }
+  m_slots.erase(it);
+}
+
+template<typename Owner, typename ...TArgs>
+inline bool
+Signal<Owner, TArgs...>::isEmpty() const
+{
+  return !m_isExecuting && m_slots.empty();
+}
+
+template<typename Owner, typename ...TArgs>
+inline void
+Signal<Owner, TArgs...>::operator()(const TArgs&... args)
+{
+  BOOST_ASSERT_MSG(!m_isExecuting, "cannot emit signal from a handler");
+  if (m_slots.empty()) {
+    return;
+  }
+  m_isExecuting = true;
+
+  typename SlotList::iterator it = m_slots.begin();
+  typename SlotList::iterator last = m_slots.end();
+  --last;
+
+  try {
+    bool isLast = false;
+    while (!isLast) {
+      m_currentSlot = it;
+      isLast = it == last;
+      ++it;
+
+      m_currentSlot->handler(args...);
+    }
+  }
+  catch (...) {
+    m_isExecuting = false;
+    throw;
+  }
+  m_isExecuting = false;
+}
+
+} // namespace signal
+
+// expose as ndn::util::Signal
+using signal::Signal;
+
+} // namespace util
+} // namespace ndn
+
+#endif // NDN_UTIL_SIGNAL_SIGNAL_HPP
diff --git a/src/util/signal.hpp b/src/util/signal.hpp
new file mode 100644
index 0000000..cd36b5d
--- /dev/null
+++ b/src/util/signal.hpp
@@ -0,0 +1,30 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2014 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#ifndef NDN_UTIL_SIGNAL_HPP
+#define NDN_UTIL_SIGNAL_HPP
+
+#include "signal-signal.hpp"
+#include "signal-emit.hpp"
+#include "signal-connection.hpp"
+#include "signal-scoped-connection.hpp"
+
+#endif // NDN_UTIL_SIGNAL_HPP