Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | ba4fbbe | 2017-07-03 01:06:30 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | */ |
| 21 | |
| 22 | /** \file |
| 23 | * |
| 24 | * This header provides macros that allows a signal to be emitted |
| 25 | * from a derived class of its owner. |
| 26 | * |
| 27 | * In 'protected' section of owner class declaration, |
| 28 | * DECLARE_SIGNAL_EMIT(signalName) |
| 29 | * From a derived class of owner, |
| 30 | * this->emitSignal(signalName, arg1, arg2); |
| 31 | */ |
| 32 | |
| 33 | #ifndef NDN_UTIL_SIGNAL_EMIT_HPP |
| 34 | #define NDN_UTIL_SIGNAL_EMIT_HPP |
| 35 | |
| 36 | namespace ndn { |
| 37 | namespace util { |
| 38 | namespace signal { |
| 39 | |
| 40 | /** \brief (implementation detail) a filler for extra argument |
| 41 | */ |
| 42 | class DummyExtraArg |
| 43 | { |
| 44 | }; |
| 45 | |
| 46 | } // namespace signal |
| 47 | } // namespace util |
| 48 | } // namespace ndn |
| 49 | |
| 50 | /** \brief (implementation detail) declares a 'emit_signalName' method |
| 51 | * \note This macro should be used in 'protected' section so that it's accessible |
| 52 | * by derived classes. |
| 53 | * \note emit_signalName method is implementation detail. |
Junxiao Shi | 018e30d | 2014-12-25 19:42:35 -0700 | [diff] [blame] | 54 | * Derived classes should use 'emitSignal' macro. |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 55 | * \note The name 'emit_signalName' is an intentional violation of code-style rule 2.5. |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 56 | * \note The method is declared as a template, so that the macro doesn't need argument types. |
| 57 | * But only argument types that are compatible with Signal declaration will work. |
| 58 | */ |
| 59 | #define DECLARE_SIGNAL_EMIT(signalName) \ |
| 60 | template<typename ...TArgs> \ |
Junxiao Shi | 018e30d | 2014-12-25 19:42:35 -0700 | [diff] [blame] | 61 | void emit_##signalName(const TArgs&... args) \ |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 62 | { \ |
| 63 | signalName(args...); \ |
| 64 | } |
| 65 | |
| 66 | /** \brief (implementation detail) invokes emit_signalName method |
| 67 | * \note C99 requires at least one argument to be passed in __VA_ARGS__, |
| 68 | * thus a DummyExtraArg is expected at the end of __VA_ARGS__, |
Junxiao Shi | 018e30d | 2014-12-25 19:42:35 -0700 | [diff] [blame] | 69 | * which will be accepted but ignored by Signal::operator() overload. |
Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 70 | */ |
| 71 | #define NDN_CXX_SIGNAL_EMIT(signalName, ...) \ |
| 72 | emit_##signalName(__VA_ARGS__) |
| 73 | |
| 74 | /** \brief (implementation detail) |
| 75 | */ |
| 76 | #define emitSignal(...) \ |
| 77 | NDN_CXX_SIGNAL_EMIT(__VA_ARGS__, ::ndn::util::signal::DummyExtraArg()) |
| 78 | |
| 79 | #endif // NDN_UTIL_SIGNAL_EMIT_HPP |