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
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
15 \brief STLSupport non-inline template implementation */
18 #include <boost/format.hpp>
21 //-/////////////////////////////////////////////////////////////////////////////////////////////////
25 //-/////////////////////////////////////////////////////////////////////////////////////////////////
26 // senf::console::detail::CollectionArgumentTraitsBase<Collection>
28 template <class Collection>
30 senf::console::detail::CollectionArgumentTraitsBase<Collection>::description()
32 return senf::prettyBaseName(typeid(Collection)) + "<"
33 + ArgumentTraits<typename Collection::value_type>::description() + ">";
36 template <class Collection>
38 senf::console::detail::CollectionArgumentTraitsBase<Collection>::str(Collection const & value)
41 senf::console::format(value, ss);
45 //-/////////////////////////////////////////////////////////////////////////////////////////////////
46 // senf::console::detail::CollectionArgumentTraits<Collection,Adder>
48 template <class Collection, class Adder>
49 prefix_ void senf::console::detail::CollectionArgumentTraits<Collection,Adder>::
50 parse(ParseCommandInfo::TokensRange const & tokens, Collection & out)
53 CheckedArgumentIteratorWrapper arg (tokens);
55 typename Collection::value_type v;
56 senf::console::parse( *(arg++), v );
61 //-/////////////////////////////////////////////////////////////////////////////////////////////////
62 // senf::console::detail::CollectionReturnValueTraits<Collection>
64 template <class Collection>
66 senf::console::detail::CollectionReturnValueTraits<Collection>::format(Collection const & value,
70 typename type::const_iterator i (value.begin());
71 typename type::const_iterator const i_end (value.end());
74 os << senf::console::str(*i);
83 //-/////////////////////////////////////////////////////////////////////////////////////////////////
84 // senf::console::detail::MapArgumentTraits<Collection>
86 template <class Collection>
87 prefix_ void senf::console::detail::MapArgumentTraits<Collection>::
88 parse(ParseCommandInfo::TokensRange const & tokens, Collection & out)
91 CheckedArgumentIteratorWrapper arg (tokens);
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));
104 template <class Collection>
105 prefix_ std::string senf::console::detail::MapArgumentTraits<Collection>::description()
107 return senf::prettyBaseName(typeid(Collection)) + "<"
108 + ArgumentTraits<typename Collection::key_type>::description() + ","
109 + ArgumentTraits<typename Collection::mapped_type>::description() + ">";
112 template <class Collection>
114 senf::console::detail::MapArgumentTraits<Collection>::str(Collection const & value)
116 std::stringstream ss;
117 senf::console::format(value, ss);
121 //-/////////////////////////////////////////////////////////////////////////////////////////////////
122 // senf::console::detail::MapReturnValueTraits<Collection>
124 template <class Collection>
126 senf::console::detail::MapReturnValueTraits<Collection>::format(Collection const & value,
130 typename type::const_iterator i (value.begin());
131 typename type::const_iterator const i_end (value.end());
134 os << senf::console::str(i->first)
136 << senf::console::str(i->second);
145 //-/////////////////////////////////////////////////////////////////////////////////////////////////
146 // senf::console::ArgumentTraits< std::pair<T1,T2> >
148 template <class T1, class T2>
149 prefix_ void senf::console::ArgumentTraits< std::pair<T1,T2> >::
150 parse(ParseCommandInfo::TokensRange const & tokens, type & out)
152 CheckedArgumentIteratorWrapper arg (tokens);
153 senf::console::parse( *(arg++), out.first );
154 senf::console::parse( *(arg++), out.second );
157 template <class T1, class T2>
158 prefix_ std::string senf::console::ArgumentTraits< std::pair<T1,T2> >::description()
160 return (boost::format("pair<%s,%s>")
161 % ArgumentTraits<T1>::description()
162 % ArgumentTraits<T2>::description()).str();
165 template <class T1, class T2>
166 prefix_ std::string senf::console::ArgumentTraits< std::pair<T1,T2> >::str(type const & value)
168 std::stringstream ss;
169 senf::console::format(value, ss);
173 //-/////////////////////////////////////////////////////////////////////////////////////////////////
174 // senf::console::ReturnValueTraits< std::pair<T1,T2> >
176 template <class T1, class T2>
177 prefix_ void senf::console::ReturnValueTraits< std::pair<T1,T2> >::format(type const & value,
180 os << "(" << senf::console::str(value.first)
181 << " " << senf::console::str(value.second) << ")";
184 #ifdef SENF_CXX11_ENABLED
186 //-/////////////////////////////////////////////////////////////////////////////////////////////////
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)
192 senf::console::parse( *(arg++), std::get<Index>(out) );
193 TupleHelper<TupleType, Index+1, MaxIndex>::parse(arg, out);
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)
199 ss << senf::console::ArgumentTraits<typename std::tuple_element<Index, TupleType>::type>::description() << ',';
200 TupleHelper<TupleType, Index+1, MaxIndex>::description(ss);
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)
206 os << senf::console::str(std::get<Index>(value)) << ' ';
207 TupleHelper<TupleType, Index+1, MaxIndex>::format(value, os);
210 template <typename TupleType, std::size_t MaxIndex>
211 prefix_ void senf::console::detail::TupleHelper<TupleType, MaxIndex, MaxIndex>::parse(CheckedArgumentIteratorWrapper & arg, TupleType & out)
213 senf::console::parse( *(arg++), std::get<MaxIndex>(out) );
216 template <typename TupleType, std::size_t MaxIndex>
217 prefix_ void senf::console::detail::TupleHelper<TupleType, MaxIndex, MaxIndex>::description(std::stringstream & ss)
219 ss << senf::console::ArgumentTraits<typename std::tuple_element<MaxIndex, TupleType>::type>::description();
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)
225 os << senf::console::str(std::get<MaxIndex>(value));
228 //-/////////////////////////////////////////////////////////////////////////////////////////////////
229 // senf::console::ArgumentTraits< std::tuple<Types...> >
231 template <typename ...Types>
232 prefix_ void senf::console::ArgumentTraits< std::tuple<Types...> >::
233 parse(ParseCommandInfo::TokensRange const & tokens, type & out)
235 CheckedArgumentIteratorWrapper arg (tokens);
236 detail::TupleHelper<type, 0, std::tuple_size<type>::value - 1>::parse(arg, out);
239 template <typename ...Types>
240 prefix_ std::string senf::console::ArgumentTraits< std::tuple<Types...> >::description()
242 std::stringstream ss;
244 detail::TupleHelper<type, 0, std::tuple_size<type>::value - 1>::description(ss);
249 template <typename ...Types>
250 prefix_ std::string senf::console::ArgumentTraits< std::tuple<Types...> >::str(type const & value)
252 std::stringstream ss;
253 senf::console::format(value, ss);
257 //-/////////////////////////////////////////////////////////////////////////////////////////////////
258 // senf::console::ReturnValueTraits< std::tuple<Types...> >
260 template <typename ...Types>
261 prefix_ void senf::console::ReturnValueTraits< std::tuple<Types...> >::format(type const & value, std::ostream & os)
264 detail::TupleHelper<type, 0, std::tuple_size<type>::value - 1>::format(value, os);
271 //-/////////////////////////////////////////////////////////////////////////////////////////////////
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"