util: move signal-*.* into util/signal/ directory
refs #3940
Change-Id: I36e3420846959f39bb51a3ce7ef7f03e7c2c22aa
diff --git a/src/util/signal/connection.cpp b/src/util/signal/connection.cpp
new file mode 100644
index 0000000..7d46eb1
--- /dev/null
+++ b/src/util/signal/connection.cpp
@@ -0,0 +1,70 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2013-2017 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 "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::isConnected() const
+{
+ return !m_disconnect.expired();
+}
+
+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..6ee8e1c
--- /dev/null
+++ b/src/util/signal/connection.hpp
@@ -0,0 +1,89 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2013-2017 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 check if connected to the signal
+ * \return false if disconnected from the signal
+ */
+ bool
+ isConnected() const;
+
+ /** \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..a28872c
--- /dev/null
+++ b/src/util/signal/emit.hpp
@@ -0,0 +1,79 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2013-2017 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 'emitSignal' macro.
+ * \note The name 'emit_signalName' is an intentional violation of code-style rule 2.5.
+ * \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) \
+ { \
+ 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 Signal::operator() overload.
+ */
+#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..cbbfaba
--- /dev/null
+++ b/src/util/signal/scoped-connection.cpp
@@ -0,0 +1,83 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2013-2017 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 "scoped-connection.hpp"
+
+namespace ndn {
+namespace util {
+namespace signal {
+
+#if NDN_CXX_HAVE_IS_NOTHROW_MOVE_CONSTRUCTIBLE
+static_assert(std::is_nothrow_move_constructible<ScopedConnection>::value,
+ "ScopedConnection must be MoveConstructible with noexcept");
+#endif // NDN_CXX_HAVE_IS_NOTHROW_MOVE_CONSTRUCTIBLE
+
+ScopedConnection::ScopedConnection()
+{
+}
+
+ScopedConnection::ScopedConnection(const Connection& connection)
+ : m_connection(connection)
+{
+}
+
+ScopedConnection::ScopedConnection(ScopedConnection&& other) noexcept
+ : 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() noexcept
+{
+ m_connection.disconnect();
+}
+
+void
+ScopedConnection::disconnect()
+{
+ m_connection.disconnect();
+}
+
+bool
+ScopedConnection::isConnected() const
+{
+ return m_connection.isConnected();
+}
+
+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..5c77c2f
--- /dev/null
+++ b/src/util/signal/scoped-connection.hpp
@@ -0,0 +1,85 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2013-2017 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 "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) noexcept;
+
+ /** \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() noexcept;
+
+ /** \brief disconnects the connection manually
+ */
+ void
+ disconnect();
+
+ /** \brief check if the connection is connected to the signal
+ * \return false when a default-constructed connection is used, the connection is released,
+ * or the connection is disconnected
+ */
+ bool
+ isConnected() const;
+
+ /** \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..9b502ab
--- /dev/null
+++ b/src/util/signal/signal.hpp
@@ -0,0 +1,268 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2013-2017 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 "connection.hpp"
+#include <list>
+
+namespace ndn {
+namespace util {
+namespace signal {
+
+class DummyExtraArg;
+
+/** \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();
+
+ ~Signal();
+
+ /** \brief connects a handler to the signal
+ * \note If invoked from a handler, the new handler won't receive the current emitted signal.
+ * \warning The handler is permitted to disconnect itself, but it must ensure its validity.
+ */
+ Connection
+ connect(const Handler& handler);
+
+ /** \brief connects a single-shot handler to the signal
+ *
+ * After the handler is executed once, it is automatically disconnected.
+ */
+ Connection
+ connectSingleShot(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.
+ * \warning Destructing the Signal object during signal emission 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);
+
+ /** \brief (implementation detail) emits a signal
+ * \note This overload is used by signal-emit.hpp.
+ */
+ void
+ operator()(const TArgs&... args, const DummyExtraArg&);
+
+ // make Owner a friend of Signal<Owner, ...> so that API for owner can be called
+ friend Owner;
+
+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
+ *
+ * In practice this is the Signal::disconnect method bound to an iterator
+ * pointing at 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 erased or the signal is destructed, this function object is
+ * 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>
+Signal<Owner, TArgs...>::~Signal()
+{
+ BOOST_ASSERT(!m_isExecuting);
+}
+
+template<typename Owner, typename ...TArgs>
+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>
+Connection
+Signal<Owner, TArgs...>::connectSingleShot(const Handler& handler)
+{
+ typename SlotList::iterator it = m_slots.insert(m_slots.end(), {nullptr, nullptr});
+ it->disconnect = make_shared<function<void()>>(bind(&Self::disconnect, this, it));
+ signal::Connection conn(weak_ptr<function<void()>>(it->disconnect));
+
+ it->handler = [conn, handler] (const TArgs&... args) mutable {
+ handler(args...);
+ conn.disconnect();
+ };
+
+ return conn;
+}
+
+template<typename Owner, typename ...TArgs>
+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");
+
+ // this serves to indicate that the current slot needs to be erased from the list
+ // after it finishes executing; we cannot do it here because of bug #2333
+ m_currentSlot = m_slots.end();
+
+ // expire all weak_ptrs, to prevent double disconnections
+ it->disconnect.reset();
+ }
+ else {
+ m_slots.erase(it);
+ }
+}
+
+template<typename Owner, typename ...TArgs>
+bool
+Signal<Owner, TArgs...>::isEmpty() const
+{
+ return !m_isExecuting && m_slots.empty();
+}
+
+template<typename Owner, typename ...TArgs>
+void
+Signal<Owner, TArgs...>::operator()(const TArgs&... args)
+{
+ BOOST_ASSERT_MSG(!m_isExecuting, "cannot emit signal from a handler");
+
+ if (m_slots.empty()) {
+ return;
+ }
+
+ auto it = m_slots.begin();
+ auto last = std::prev(m_slots.end());
+ m_isExecuting = true;
+
+ try {
+ bool isLast = false;
+ while (!isLast) {
+ m_currentSlot = it;
+ isLast = it == last;
+
+ m_currentSlot->handler(args...);
+
+ if (m_currentSlot == m_slots.end())
+ it = m_slots.erase(it);
+ else
+ ++it;
+ }
+ }
+ catch (...) {
+ m_isExecuting = false;
+ throw;
+ }
+
+ m_isExecuting = false;
+}
+
+template<typename Owner, typename ...TArgs>
+void
+Signal<Owner, TArgs...>::operator()(const TArgs&... args, const DummyExtraArg&)
+{
+ this->operator()(args...);
+}
+
+} // namespace signal
+
+// expose as ndn::util::Signal
+using signal::Signal;
+
+} // namespace util
+} // namespace ndn
+
+#endif // NDN_UTIL_SIGNAL_SIGNAL_HPP