STLSupport.ct
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
6 //
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
11 //
12 
13 
14 /** \file
15  \brief STLSupport non-inline template implementation */
16 
17 // Custom includes
18 #include <boost/format.hpp>
19 
20 #define prefix_
21 //-/////////////////////////////////////////////////////////////////////////////////////////////////
22 
23 #ifndef DOXYGEN
24 
25 //-/////////////////////////////////////////////////////////////////////////////////////////////////
26 // senf::console::detail::CollectionArgumentTraitsBase<Collection>
27 
28 template <class Collection>
29 prefix_ std::string
30 senf::console::detail::CollectionArgumentTraitsBase<Collection>::description()
31 {
32  return senf::prettyBaseName(typeid(Collection)) + "<"
33  + ArgumentTraits<typename Collection::value_type>::description() + ">";
34 }
35 
36 template <class Collection>
37 prefix_ std::string
38 senf::console::detail::CollectionArgumentTraitsBase<Collection>::str(Collection const & value)
39 {
40  std::stringstream ss;
41  senf::console::format(value, ss);
42  return ss.str();
43 }
44 
45 //-/////////////////////////////////////////////////////////////////////////////////////////////////
46 // senf::console::detail::CollectionArgumentTraits<Collection,Adder>
47 
48 template <class Collection, class Adder>
49 prefix_ void senf::console::detail::CollectionArgumentTraits<Collection,Adder>::
50 parse(ParseCommandInfo::TokensRange const & tokens, Collection & out)
51 {
52  out.clear();
53  CheckedArgumentIteratorWrapper arg (tokens);
54  while (arg) {
55  typename Collection::value_type v;
56  senf::console::parse( *(arg++), v );
57  Adder::add(out,v);
58  }
59 }
60 
61 //-/////////////////////////////////////////////////////////////////////////////////////////////////
62 // senf::console::detail::CollectionReturnValueTraits<Collection>
63 
64 template <class Collection>
65 prefix_ void
66 senf::console::detail::CollectionReturnValueTraits<Collection>::format(Collection const & value,
67  std::ostream & os)
68 {
69  os << "(";
70  typename type::const_iterator i (value.begin());
71  typename type::const_iterator const i_end (value.end());
72  if (i != i_end)
73  for (;;) {
74  os << senf::console::str(*i);
75  if (++i == i_end)
76  break;
77  else
78  os << " ";
79  }
80  os << ")";
81 }
82 
83 //-/////////////////////////////////////////////////////////////////////////////////////////////////
84 // senf::console::detail::MapArgumentTraits<Collection>
85 
86 template <class Collection>
87 prefix_ void senf::console::detail::MapArgumentTraits<Collection>::
88 parse(ParseCommandInfo::TokensRange const & tokens, Collection & out)
89 {
90  out.clear();
91  CheckedArgumentIteratorWrapper arg (tokens);
92  while (arg) {
93  typename Collection::key_type key;
94  typename Collection::mapped_type data;
95  senf::console::parse( *(arg++), key );
96  ParseCommandInfo::TokensRange sep (*(arg++));
97  if (sep.size() != 1 || sep[0].type() != Token::OtherPunctuation || sep[0].value() != "=")
98  throw SyntaxErrorException("'=' expected");
99  senf::console::parse( *(arg++), data );
100  out.insert(std::make_pair(key,data));
101  }
102 }
103 
104 template <class Collection>
105 prefix_ std::string senf::console::detail::MapArgumentTraits<Collection>::description()
106 {
107  return senf::prettyBaseName(typeid(Collection)) + "<"
108  + ArgumentTraits<typename Collection::key_type>::description() + ","
109  + ArgumentTraits<typename Collection::mapped_type>::description() + ">";
110 }
111 
112 template <class Collection>
113 prefix_ std::string
114 senf::console::detail::MapArgumentTraits<Collection>::str(Collection const & value)
115 {
116  std::stringstream ss;
117  senf::console::format(value, ss);
118  return ss.str();
119 }
120 
121 //-/////////////////////////////////////////////////////////////////////////////////////////////////
122 // senf::console::detail::MapReturnValueTraits<Collection>
123 
124 template <class Collection>
125 prefix_ void
126 senf::console::detail::MapReturnValueTraits<Collection>::format(Collection const & value,
127  std::ostream & os)
128 {
129  os << "(";
130  typename type::const_iterator i (value.begin());
131  typename type::const_iterator const i_end (value.end());
132  if (i != i_end)
133  for (;;) {
134  os << senf::console::str(i->first)
135  << "="
136  << senf::console::str(i->second);
137  if (++i == i_end)
138  break;
139  else
140  os << " ";
141  }
142  os << ")";
143 }
144 
145 //-/////////////////////////////////////////////////////////////////////////////////////////////////
146 // senf::console::ArgumentTraits< std::pair<T1,T2> >
147 
148 template <class T1, class T2>
149 prefix_ void senf::console::ArgumentTraits< std::pair<T1,T2> >::
150 parse(ParseCommandInfo::TokensRange const & tokens, type & out)
151 {
152  CheckedArgumentIteratorWrapper arg (tokens);
153  senf::console::parse( *(arg++), out.first );
154  senf::console::parse( *(arg++), out.second );
155 }
156 
157 template <class T1, class T2>
158 prefix_ std::string senf::console::ArgumentTraits< std::pair<T1,T2> >::description()
159 {
160  return (boost::format("pair<%s,%s>")
161  % ArgumentTraits<T1>::description()
162  % ArgumentTraits<T2>::description()).str();
163 }
164 
165 template <class T1, class T2>
166 prefix_ std::string senf::console::ArgumentTraits< std::pair<T1,T2> >::str(type const & value)
167 {
168  std::stringstream ss;
169  senf::console::format(value, ss);
170  return ss.str();
171 }
172 
173 //-/////////////////////////////////////////////////////////////////////////////////////////////////
174 // senf::console::ReturnValueTraits< std::pair<T1,T2> >
175 
176 template <class T1, class T2>
177 prefix_ void senf::console::ReturnValueTraits< std::pair<T1,T2> >::format(type const & value,
178  std::ostream & os)
179 {
180  os << "(" << senf::console::str(value.first)
181  << " " << senf::console::str(value.second) << ")";
182 }
183 
184 #ifdef SENF_CXX11_ENABLED
185 
186 //-/////////////////////////////////////////////////////////////////////////////////////////////////
187 // TupleHelper
188 
189 template <typename TupleType, std::size_t Index, std::size_t MaxIndex>
190 prefix_ void senf::console::detail::TupleHelper<TupleType, Index, MaxIndex>::parse(CheckedArgumentIteratorWrapper & arg, TupleType & out)
191 {
192  senf::console::parse( *(arg++), std::get<Index>(out) );
193  TupleHelper<TupleType, Index+1, MaxIndex>::parse(arg, out);
194 }
195 
196 template <typename TupleType, std::size_t Index, std::size_t MaxIndex>
197 prefix_ void senf::console::detail::TupleHelper<TupleType, Index, MaxIndex>::description(std::stringstream & ss)
198 {
199  ss << senf::console::ArgumentTraits<typename std::tuple_element<Index, TupleType>::type>::description() << ',';
200  TupleHelper<TupleType, Index+1, MaxIndex>::description(ss);
201 }
202 
203 template <typename TupleType, std::size_t Index, std::size_t MaxIndex>
204 prefix_ void senf::console::detail::TupleHelper<TupleType, Index, MaxIndex>::format(TupleType const & value, std::ostream & os)
205 {
206  os << senf::console::str(std::get<Index>(value)) << ' ';
207  TupleHelper<TupleType, Index+1, MaxIndex>::format(value, os);
208 }
209 
210 template <typename TupleType, std::size_t MaxIndex>
211 prefix_ void senf::console::detail::TupleHelper<TupleType, MaxIndex, MaxIndex>::parse(CheckedArgumentIteratorWrapper & arg, TupleType & out)
212 {
213  senf::console::parse( *(arg++), std::get<MaxIndex>(out) );
214 }
215 
216 template <typename TupleType, std::size_t MaxIndex>
217 prefix_ void senf::console::detail::TupleHelper<TupleType, MaxIndex, MaxIndex>::description(std::stringstream & ss)
218 {
219  ss << senf::console::ArgumentTraits<typename std::tuple_element<MaxIndex, TupleType>::type>::description();
220 }
221 
222 template <typename TupleType, std::size_t MaxIndex>
223 prefix_ void senf::console::detail::TupleHelper<TupleType, MaxIndex, MaxIndex>::format(TupleType const & value, std::ostream & os)
224 {
225  os << senf::console::str(std::get<MaxIndex>(value));
226 }
227 
228 //-/////////////////////////////////////////////////////////////////////////////////////////////////
229 // senf::console::ArgumentTraits< std::tuple<Types...> >
230 
231 template <typename ...Types>
232 prefix_ void senf::console::ArgumentTraits< std::tuple<Types...> >::
233 parse(ParseCommandInfo::TokensRange const & tokens, type & out)
234 {
235  CheckedArgumentIteratorWrapper arg (tokens);
236  detail::TupleHelper<type, 0, std::tuple_size<type>::value - 1>::parse(arg, out);
237 }
238 
239 template <typename ...Types>
240 prefix_ std::string senf::console::ArgumentTraits< std::tuple<Types...> >::description()
241 {
242  std::stringstream ss;
243  ss << "tuple<";
244  detail::TupleHelper<type, 0, std::tuple_size<type>::value - 1>::description(ss);
245  ss << '>';
246  return ss.str();
247 }
248 
249 template <typename ...Types>
250 prefix_ std::string senf::console::ArgumentTraits< std::tuple<Types...> >::str(type const & value)
251 {
252  std::stringstream ss;
253  senf::console::format(value, ss);
254  return ss.str();
255 }
256 
257 //-/////////////////////////////////////////////////////////////////////////////////////////////////
258 // senf::console::ReturnValueTraits< std::tuple<Types...> >
259 
260 template <typename ...Types>
261 prefix_ void senf::console::ReturnValueTraits< std::tuple<Types...> >::format(type const & value, std::ostream & os)
262 {
263  os << '(';
264  detail::TupleHelper<type, 0, std::tuple_size<type>::value - 1>::format(value, os);
265  os << ')';
266 }
267 
268 #endif
269 #endif
270 
271 //-/////////////////////////////////////////////////////////////////////////////////////////////////
272 #undef prefix_
273 
274 
275 // Local Variables:
276 // mode: c++
277 // fill-column: 100
278 // comment-column: 40
279 // c-file-style: "senf"
280 // indent-tabs-mode: nil
281 // ispell-local-dictionary: "american"
282 // compile-command: "scons -u test"
283 // End: