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/.waf-tools/compiler-features.py b/.waf-tools/compiler-features.py
new file mode 100644
index 0000000..4bc9594
--- /dev/null
+++ b/.waf-tools/compiler-features.py
@@ -0,0 +1,47 @@
+# -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
+
+from waflib.Configure import conf
+
+FRIEND_TYPENAME = '''
+class A;
+
+template<typename T>
+class B
+{
+  friend T;
+};
+
+B<A> g_b;
+'''
+
+FRIEND_WRAPPER = '''
+class A;
+
+template<typename T>
+struct TypeWrapper
+{
+  typedef T Type;
+};
+
+template<typename T>
+class B
+{
+  friend class TypeWrapper<T>::Type;
+};
+
+B<A> g_b;
+'''
+
+@conf
+def check_friend_typename(self):
+    if self.check_cxx(msg='Checking for friend typename-specifier',
+                      fragment=FRIEND_TYPENAME,
+                      features='cxx', mandatory=False):
+        self.define('HAVE_CXX_FRIEND_TYPENAME', 1)
+    elif self.check_cxx(msg='Checking for friend typename using wrapper',
+                      fragment=FRIEND_WRAPPER,
+                      features='cxx', mandatory=True):
+        self.define('HAVE_CXX_FRIEND_TYPENAME_WRAPPER', 1)
+
+def configure(conf):
+    conf.check_friend_typename()
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
diff --git a/tests/unit-tests/util/signal.cpp b/tests/unit-tests/util/signal.cpp
new file mode 100644
index 0000000..0de06a2
--- /dev/null
+++ b/tests/unit-tests/util/signal.cpp
@@ -0,0 +1,345 @@
+/* -*- 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.
+ *
+ * This code is actually copied from NFD project (NDN Forwarding Daemon).
+ * We acknowledge the permission of the authors of NFD.
+ */
+
+#include "util/signal.hpp"
+
+#include "boost-test.hpp"
+
+namespace ndn {
+namespace util {
+namespace signal {
+namespace tests {
+
+BOOST_AUTO_TEST_SUITE(UtilSignal)
+
+class SignalOwner0
+{
+public:
+  Signal<SignalOwner0> sig;
+
+public:
+  DECLARE_SIGNAL_EMIT(sig)
+
+  bool
+  isSigEmpty()
+  {
+    return sig.isEmpty();
+  }
+};
+
+BOOST_AUTO_TEST_CASE(ZeroSlot)
+{
+  SignalOwner0 so;
+  BOOST_CHECK_NO_THROW(so.emitSignal(sig));
+}
+
+BOOST_AUTO_TEST_CASE(TwoListeners)
+{
+  SignalOwner0 so;
+
+  int hit1 = 0, hit2 = 0;
+  so.sig.connect([&hit1] { ++hit1; });
+  so.sig.connect([&hit2] { ++hit2; });
+
+  so.emitSignal(sig);
+
+  BOOST_CHECK_EQUAL(hit1, 1);
+  BOOST_CHECK_EQUAL(hit2, 1);
+}
+
+BOOST_AUTO_TEST_CASE(TwoArguments)
+{
+  Signal<std::remove_pointer<decltype(this)>::type, int, int> sig;
+
+  int hit = 0;
+  sig.connect([&hit] (int a, int b) {
+    ++hit;
+    BOOST_CHECK_EQUAL(a, 21);
+    BOOST_CHECK_EQUAL(b, 22);
+  });
+  sig(21, 22);
+
+  BOOST_CHECK_EQUAL(hit, 1);
+}
+
+class RefObject
+{
+public:
+  RefObject()
+  {
+  }
+
+  RefObject(const RefObject& other)
+  {
+    ++s_copyCount;
+  }
+
+public:
+  static int s_copyCount;
+};
+int RefObject::s_copyCount = 0;
+
+// Signal passes arguments by reference,
+// but it also allows a handler that accept arguments by value
+BOOST_AUTO_TEST_CASE(HandlerByVal)
+{
+  RefObject refObject;
+  RefObject::s_copyCount = 0;
+
+  Signal<std::remove_pointer<decltype(this)>::type, RefObject> sig;
+  sig.connect([] (RefObject ro) {});
+  sig(refObject);
+
+  BOOST_CHECK_EQUAL(RefObject::s_copyCount, 1);
+}
+
+// Signal passes arguments by reference, and no copying
+// is necessary when handler accepts arguments by reference
+BOOST_AUTO_TEST_CASE(HandlerByRef)
+{
+  RefObject refObject;
+  RefObject::s_copyCount = 0;
+
+  Signal<std::remove_pointer<decltype(this)>::type, RefObject> sig;
+  sig.connect([] (const RefObject& ro) {});
+  sig(refObject);
+
+  BOOST_CHECK_EQUAL(RefObject::s_copyCount, 0);
+}
+
+BOOST_AUTO_TEST_CASE(ManualDisconnect)
+{
+  SignalOwner0 so;
+
+  int hit = 0;
+  Connection c1 = so.sig.connect([&hit] { ++hit; });
+
+  so.emitSignal(sig);
+  BOOST_CHECK_EQUAL(hit, 1); // handler called
+
+  Connection c2 = c1; // make a copy
+  c2.disconnect();
+  so.emitSignal(sig);
+  BOOST_CHECK_EQUAL(hit, 1); // handler not called
+
+  BOOST_CHECK_NO_THROW(c2.disconnect());
+  BOOST_CHECK_NO_THROW(c1.disconnect());
+}
+
+BOOST_AUTO_TEST_CASE(ManualDisconnectDestructed)
+{
+  unique_ptr<SignalOwner0> so(new SignalOwner0());
+
+  int hit = 0;
+  Connection connection = so->sig.connect([&hit] { ++hit; });
+
+  so->emitSignal(sig);
+  BOOST_CHECK_EQUAL(hit, 1); // handler called
+
+  so.reset(); // destruct EventEmitter
+  BOOST_CHECK_NO_THROW(connection.disconnect());
+}
+
+BOOST_AUTO_TEST_CASE(AutoDisconnect)
+{
+  SignalOwner0 so;
+
+  int hit = 0;
+  {
+    ScopedConnection sc = so.sig.connect([&hit] { ++hit; });
+
+    so.emitSignal(sig);
+    BOOST_CHECK_EQUAL(hit, 1); // handler called
+
+    // sc goes out of scope, disconnecting
+  }
+
+  so.emitSignal(sig);
+  BOOST_CHECK_EQUAL(hit, 1); // handler not called
+}
+
+BOOST_AUTO_TEST_CASE(AutoDisconnectAssign)
+{
+  SignalOwner0 so;
+
+  int hit1 = 0, hit2 = 0;
+  ScopedConnection sc = so.sig.connect([&hit1] { ++hit1; });
+
+  so.emitSignal(sig);
+  BOOST_CHECK_EQUAL(hit1, 1); // handler1 called
+
+  sc = so.sig.connect([&hit2] { ++hit2; }); // handler1 is disconnected
+
+  so.emitSignal(sig);
+  BOOST_CHECK_EQUAL(hit1, 1); // handler1 not called
+  BOOST_CHECK_EQUAL(hit2, 1); // handler2 called
+}
+
+BOOST_AUTO_TEST_CASE(AutoDisconnectAssignSame)
+{
+  SignalOwner0 so;
+
+  int hit = 0;
+  Connection c1 = so.sig.connect([&hit] { ++hit; });
+
+  ScopedConnection sc(c1);
+  so.emitSignal(sig);
+  BOOST_CHECK_EQUAL(hit, 1); // handler called
+
+  sc = c1; // assign same connection
+  so.emitSignal(sig);
+  BOOST_CHECK_EQUAL(hit, 2); // handler called
+
+  Connection c2 = c1;
+  sc = c2; // assign a copy of same connection
+  so.emitSignal(sig);
+  BOOST_CHECK_EQUAL(hit, 3); // handler called
+}
+
+BOOST_AUTO_TEST_CASE(AutoDisconnectRelease)
+{
+  SignalOwner0 so;
+
+  int hit = 0;
+  {
+    ScopedConnection sc = so.sig.connect([&hit] { ++hit; });
+
+    so.emitSignal(sig);
+    BOOST_CHECK_EQUAL(hit, 1); // handler called
+
+    sc.release();
+    // sc goes out of scope, but not disconnecting
+  }
+
+  so.emitSignal(sig);
+  BOOST_CHECK_EQUAL(hit, 2); // handler called
+}
+
+BOOST_AUTO_TEST_CASE(AutoDisconnectMove)
+{
+  SignalOwner0 so;
+  unique_ptr<ScopedConnection> sc2;
+
+  int hit = 0;
+  {
+    ScopedConnection sc = so.sig.connect([&hit] { ++hit; });
+
+    so.emitSignal(sig);
+    BOOST_CHECK_EQUAL(hit, 1); // handler called
+
+    sc2.reset(new ScopedConnection(std::move(sc)));
+
+    // sc goes out of scope, but not disconnecting
+  }
+
+  so.emitSignal(sig);
+  BOOST_CHECK_EQUAL(hit, 2); // handler called
+}
+
+BOOST_AUTO_TEST_CASE(ConnectInHandler)
+{
+  SignalOwner0 so;
+
+  int hit1 = 0, hit2 = 0; bool hasHandler2 = false;
+  so.sig.connect([&] {
+    ++hit1;
+    if (!hasHandler2) {
+      so.sig.connect([&] { ++hit2; });
+      hasHandler2 = true;
+    }
+  });
+
+  so.emitSignal(sig);
+  BOOST_CHECK_EQUAL(hit1, 1); // handler1 called
+  BOOST_CHECK_EQUAL(hit2, 0); // handler2 not called
+
+  // new subscription takes effect
+  so.emitSignal(sig);
+  BOOST_CHECK_EQUAL(hit1, 2); // handler1 called
+  BOOST_CHECK_EQUAL(hit2, 1); // handler2 called
+}
+
+BOOST_AUTO_TEST_CASE(DisconnectSelfInHandler)
+{
+  SignalOwner0 so;
+
+  int hit = 0;
+  Connection connection;
+  connection = so.sig.connect([&] {
+    ++hit;
+    connection.disconnect();
+    BOOST_CHECK_EQUAL(so.isSigEmpty(), false); // disconnecting hasn't taken effect
+  });
+
+  so.emitSignal(sig);
+  BOOST_CHECK_EQUAL(hit, 1); // handler called
+
+  // disconnecting takes effect
+  BOOST_CHECK_EQUAL(so.isSigEmpty(), true);
+  so.emitSignal(sig);
+  BOOST_CHECK_EQUAL(hit, 1); // handler not called
+}
+
+BOOST_AUTO_TEST_CASE(ThrowInHandler)
+{
+  SignalOwner0 so;
+
+  struct HandlerError
+  {
+  };
+
+  int hit = 0;
+  so.sig.connect([&] {
+    ++hit;
+    throw HandlerError();
+  });
+
+  BOOST_CHECK_THROW(so.emitSignal(sig), HandlerError);
+  BOOST_CHECK_EQUAL(hit, 1); // handler called
+
+  BOOST_CHECK_THROW(so.emitSignal(sig), HandlerError);
+  BOOST_CHECK_EQUAL(hit, 2); // handler called
+}
+
+BOOST_AUTO_TEST_CASE(DestructInHandler)
+{
+  unique_ptr<SignalOwner0> so(new SignalOwner0());
+
+  int hit = 0;
+  so->sig.connect([&] {
+    ++hit;
+    so.reset();
+  });
+
+  BOOST_CHECK_NO_THROW(so->emitSignal(sig));
+  BOOST_CHECK_EQUAL(hit, 1); // handler called
+  BOOST_CHECK(so == nullptr);
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace tests
+} // namespace signal
+} // namespace util
+} // namespace ndn
diff --git a/wscript b/wscript
index bf4c368..f7f3cf7 100644
--- a/wscript
+++ b/wscript
@@ -13,7 +13,7 @@
     opt.load(['compiler_cxx', 'gnu_dirs', 'c_osx'])
     opt.load(['default-compiler-flags', 'coverage', 'osx-security', 'pch',
               'boost', 'openssl', 'cryptopp', 'sqlite3',
-              'doxygen', 'sphinx_build', 'type_traits'],
+              'doxygen', 'sphinx_build', 'type_traits', 'compiler-features'],
              tooldir=['.waf-tools'])
 
     opt = opt.add_option_group('Library Options')
@@ -40,7 +40,7 @@
     conf.load(['compiler_cxx', 'gnu_dirs', 'c_osx',
                'default-compiler-flags', 'osx-security', 'pch',
                'boost', 'openssl', 'cryptopp', 'sqlite3',
-               'doxygen', 'sphinx_build', 'type_traits'])
+               'doxygen', 'sphinx_build', 'type_traits', 'compiler-features'])
 
     conf.env['WITH_TESTS'] = conf.options.with_tests
     conf.env['WITH_TOOLS'] = conf.options.with_tools