blob: a696000a624fa3b24afc8112a8e02785eb81d6c0 [file] [log] [blame]
Davide Pesavento25d4f1c2020-04-29 23:31:04 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesavento541a8222022-03-01 15:08:42 -05003 * Copyright (c) 2013-2022 Regents of the University of California.
Davide Pesavento25d4f1c2020-04-29 23:31:04 -04004 *
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
Davide Pesavento09904412021-03-24 16:40:53 -040022#ifndef NDN_CXX_TOOLS_NDNSEC_ACCUMULATOR_HPP
23#define NDN_CXX_TOOLS_NDNSEC_ACCUMULATOR_HPP
Davide Pesavento25d4f1c2020-04-29 23:31:04 -040024
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 {
Davide Pesavento541a8222022-03-01 15:08:42 -050072 return {};
Davide Pesavento25d4f1c2020-04-29 23:31:04 -040073 }
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
Davide Pesavento25d4f1c2020-04-29 23:31:04 -0400137private:
138 T* m_store;
139 T m_interval;
140 T m_default;
141};
142
143template <typename T>
144AccumulatorType<T>*
145accumulator()
146{
147 return new AccumulatorType<T>(nullptr);
148}
149
150template <typename T>
151AccumulatorType<T>*
152accumulator(T* store)
153{
154 return new AccumulatorType<T>(store);
155}
156
157} // namespace ndnsec
158} // namespace ndn
159
Davide Pesavento09904412021-03-24 16:40:53 -0400160#endif // NDN_CXX_TOOLS_NDNSEC_ACCUMULATOR_HPP