blob: 17eacd0df8fe5b8855013b5fc72d688bbb8052bb [file] [log] [blame]
Junxiao Shi8d71fdb2014-12-07 21:55:19 -07001/* -*- 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
36namespace ndn {
37namespace util {
38namespace signal {
39
40/** \brief (implementation detail) a filler for extra argument
41 */
42class 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 Shi018e30d2014-12-25 19:42:35 -070054 * Derived classes should use 'emitSignal' macro.
Junxiao Shi8d71fdb2014-12-07 21:55:19 -070055 * \note The name 'emit_signalName' is an intentional violation of code-style rule 2.5.
Junxiao Shi8d71fdb2014-12-07 21:55:19 -070056 * \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 Shi018e30d2014-12-25 19:42:35 -070061 void emit_##signalName(const TArgs&... args) \
Junxiao Shi8d71fdb2014-12-07 21:55:19 -070062 { \
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 Shi018e30d2014-12-25 19:42:35 -070069 * which will be accepted but ignored by Signal::operator() overload.
Junxiao Shi8d71fdb2014-12-07 21:55:19 -070070 */
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