Junxiao Shi | 8d71fdb | 2014-12-07 21:55:19 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
| 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. |
| 54 | * Derived classes should use 'emit' macro. |
| 55 | * \note The name 'emit_signalName' is an intentional violation of code-style rule 2.5. |
| 56 | * \note The DummyExtraArg is necessary so that 'emit' macro can be used for |
| 57 | * both signals with zero arguments and signals with non-zero arguments. |
| 58 | * \note The method is declared as a template, so that the macro doesn't need argument types. |
| 59 | * But only argument types that are compatible with Signal declaration will work. |
| 60 | */ |
| 61 | #define DECLARE_SIGNAL_EMIT(signalName) \ |
| 62 | template<typename ...TArgs> \ |
| 63 | void emit_##signalName(const TArgs&... args, const ::ndn::util::signal::DummyExtraArg&) \ |
| 64 | { \ |
| 65 | signalName(args...); \ |
| 66 | } |
| 67 | |
| 68 | /** \brief (implementation detail) invokes emit_signalName method |
| 69 | * \note C99 requires at least one argument to be passed in __VA_ARGS__, |
| 70 | * thus a DummyExtraArg is expected at the end of __VA_ARGS__, |
| 71 | * which will be accepted but ignored by emit_signalName method. |
| 72 | */ |
| 73 | #define NDN_CXX_SIGNAL_EMIT(signalName, ...) \ |
| 74 | emit_##signalName(__VA_ARGS__) |
| 75 | |
| 76 | /** \brief (implementation detail) |
| 77 | */ |
| 78 | #define emitSignal(...) \ |
| 79 | NDN_CXX_SIGNAL_EMIT(__VA_ARGS__, ::ndn::util::signal::DummyExtraArg()) |
| 80 | |
| 81 | #endif // NDN_UTIL_SIGNAL_EMIT_HPP |