blob: 7c08581ecf8bd8c706e8c4a7bcbb7e00781bdd8f [file] [log] [blame]
Davide Pesavento25d4f1c2020-04-29 23:31:04 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2013-2020 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#ifndef NDN_TOOLS_NDNSEC_ACCUMULATOR_HPP
23#define NDN_TOOLS_NDNSEC_ACCUMULATOR_HPP
24
25#include <boost/program_options/value_semantic.hpp>
26
27namespace ndn {
28namespace ndnsec {
29
30/**
31 * @brief An accumulating option value to handle multiple incrementing options.
32 *
33 * Based on https://gitorious.org/bwy/bwy/source/8753148c324ddfacb1f3cdc315650586bd7b75a4:use/accumulator.hpp
34 * @sa http://benjaminwolsey.de/node/103
35 */
36template<typename T>
37class AccumulatorType : public boost::program_options::value_semantic
38{
39public:
40 explicit
41 AccumulatorType(T* store)
42 : m_store(store)
43 , m_interval(1)
44 , m_default(0)
45 {
46 }
47
48 /// @brief Set the default value for this option.
49 AccumulatorType*
50 setDefaultValue(const T& t)
51 {
52 m_default = t;
53 return this;
54 }
55
56 /**
57 * @brief Set the interval for this option.
58 *
59 * Unlike for program_options::value, this specifies a value
60 * to be applied on each occurrence of the option.
61 */
62 AccumulatorType*
63 setInterval(const T& t)
64 {
65 m_interval = t;
66 return this;
67 }
68
69 std::string
70 name() const final
71 {
72 return std::string();
73 }
74
75 // There are no tokens for an AccumulatorType
76 unsigned
77 min_tokens() const final
78 {
79 return 0;
80 }
81
82 unsigned
83 max_tokens() const final
84 {
85 return 0;
86 }
87
88 // Accumulating from different sources is silly.
89 bool
90 is_composing() const final
91 {
92 return false;
93 }
94
95 // Requiring one or more appearances is unlikely.
96 bool
97 is_required() const final
98 {
99 return false;
100 }
101
102 /**
103 * @brief Parse options
104 *
105 * Every appearance of the option simply increments the value
106 * There should never be any tokens.
107 */
108 void
109 parse(boost::any& value_store, const std::vector<std::string>& new_tokens, bool utf8) const final
110 {
111 if (value_store.empty())
112 value_store = T();
113 boost::any_cast<T&>(value_store) += m_interval;
114 }
115
116 /**
117 * @brief If the option doesn't appear, this is the default value.
118 */
119 bool
120 apply_default(boost::any& value_store) const final
121 {
122 value_store = m_default;
123 return true;
124 }
125
126 /**
127 * @brief Notify the user function with the value of the value store.
128 */
129 void
130 notify(const boost::any& value_store) const final
131 {
132 const T* val = boost::any_cast<T>(&value_store);
133 if (m_store)
134 *m_store = *val;
135 }
136
137#if (BOOST_VERSION >= 105900) && (BOOST_VERSION < 106500)
138 bool
139 adjacent_tokens_only() const final
140 {
141 return false;
142 }
143#endif // (BOOST_VERSION >= 105900) && (BOOST_VERSION < 106500)
144
145private:
146 T* m_store;
147 T m_interval;
148 T m_default;
149};
150
151template <typename T>
152AccumulatorType<T>*
153accumulator()
154{
155 return new AccumulatorType<T>(nullptr);
156}
157
158template <typename T>
159AccumulatorType<T>*
160accumulator(T* store)
161{
162 return new AccumulatorType<T>(store);
163}
164
165} // namespace ndnsec
166} // namespace ndn
167
168#endif // NDN_TOOLS_NDNSEC_ACCUMULATOR_HPP