Utils/Console: Introduce senf::console::str()
[senf.git] / senf / Utils / Console / STLSupport.ct
1 // $Id$
2 //
3 // Copyright (C) 2009 
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief VectorSupport non-inline template implementation  */
25
26 //#include "VectorSupport.ih"
27
28 // Custom includes
29 #include <boost/format.hpp>
30
31 #define prefix_
32 ///////////////////////////////ct.p////////////////////////////////////////
33
34 #ifndef DOXYGEN
35
36 ///////////////////////////////////////////////////////////////////////////
37 // senf::console::detail::CollectionArgumentTraitsBase<Collection>
38
39 template <class Collection>
40 prefix_ std::string
41 senf::console::detail::CollectionArgumentTraitsBase<Collection>::description()
42 {
43     std::string type (prettyName(typeid(Collection)));
44     std::string::size_type e (type.find('<'));
45     if (e == std::string::npos) e = type.size();
46     std::string::size_type b (type.rfind(':', e));
47     if (b == std::string::npos) b = 0; else ++b;
48     return type.substr(b,e-b) + "<" 
49         + ArgumentTraits<typename Collection::value_type>::description() + ">";
50 }
51
52 template <class Collection>
53 prefix_ std::string
54 senf::console::detail::CollectionArgumentTraitsBase<Collection>::str(Collection const & value)
55 {
56     std::stringstream ss;
57     senf::console::format(value, ss);
58     return ss.str();
59 }
60
61 ///////////////////////////////////////////////////////////////////////////
62 // senf::console::detail::CollectionArgumentTraits<Collection,Adder>
63
64 template <class Collection, class Adder>
65 prefix_ void senf::console::detail::CollectionArgumentTraits<Collection,Adder>::
66 parse(ParseCommandInfo::TokensRange const & tokens, Collection & out)
67 {
68     out.clear();
69     CheckedArgumentIteratorWrapper arg (tokens);
70     while (arg) {
71         typename Collection::value_type v;
72         senf::console::parse( *(arg++), v );
73         Adder::add(out,v);
74     }
75 }
76
77 ///////////////////////////////////////////////////////////////////////////
78 // senf::console::detail::CollectionReturnValueTraits<Collection>
79
80 template <class Collection>
81 prefix_ void
82 senf::console::detail::CollectionReturnValueTraits<Collection>::format(Collection const & value,
83                                                                        std::ostream & os)
84 {
85     os << "(";
86     typename type::const_iterator i (value.begin());
87     typename type::const_iterator const i_end (value.end());
88     if (i != i_end)
89         for (;;) {
90             os << senf::console::str(*i);
91             if (++i == i_end) 
92                 break;
93             else
94                 os << " ";
95         }
96     os << ")";
97 }
98
99 ///////////////////////////////////////////////////////////////////////////
100 // senf::console::detail::MapArgumentTraits<Collection>
101
102 template <class Collection>
103 prefix_ void senf::console::detail::MapArgumentTraits<Collection>::
104 parse(ParseCommandInfo::TokensRange const & tokens, Collection & out)
105 {
106     out.clear();
107     CheckedArgumentIteratorWrapper arg (tokens);
108     while (arg) {
109         typename Collection::key_type key;
110         typename Collection::mapped_type data;
111         senf::console::parse( *(arg++), key );
112         ParseCommandInfo::TokensRange sep (*(arg++));
113         if (sep.size() != 1 || sep[0].type() != Token::OtherPunctuation || sep[0].value() != "=")
114             throw SyntaxErrorException("'=' expected");
115         senf::console::parse( *(arg++), data );
116         out.insert(std::make_pair(key,data));
117     }
118 }
119
120 template <class Collection>
121 prefix_ std::string senf::console::detail::MapArgumentTraits<Collection>::description()
122 {
123     std::string type (prettyName(typeid(Collection)));
124     std::string::size_type e (type.find('<'));
125     if (e == std::string::npos) e = type.size();
126     std::string::size_type b (type.rfind(':', e));
127     if (b == std::string::npos) b = 0; else ++b;
128     return type.substr(b,e-b) + "<" 
129         + ArgumentTraits<typename Collection::key_type>::description() + ","
130         + ArgumentTraits<typename Collection::mapped_type>::description() + ">";
131 }
132
133 template <class Collection>
134 prefix_ std::string
135 senf::console::detail::MapArgumentTraits<Collection>::str(Collection const & value)
136 {
137     std::stringstream ss;
138     senf::console::format(value, ss);
139     return ss.str();
140 }
141
142 ///////////////////////////////////////////////////////////////////////////
143 // senf::console::detail::MapReturnValueTraits<Collection>
144
145 template <class Collection>
146 prefix_ void
147 senf::console::detail::MapReturnValueTraits<Collection>::format(Collection const & value,
148                                                                 std::ostream & os)
149 {
150     os << "(";
151     typename type::const_iterator i (value.begin());
152     typename type::const_iterator const i_end (value.end());
153     if (i != i_end)
154         for (;;) {
155             os << senf::console::str(i->first)
156                << "="
157                << senf::console::str(i->second);
158             if (++i == i_end) 
159                 break;
160             else
161                 os << " ";
162         }
163     os << ")";
164 }
165
166 ///////////////////////////////////////////////////////////////////////////
167 // senf::console::ArgumentTraits< std::pair<T1,T2> >
168
169 template <class T1, class T2>
170 prefix_ void senf::console::ArgumentTraits< std::pair<T1,T2> >::
171 parse(ParseCommandInfo::TokensRange const & tokens, type & out)
172 {
173     CheckedArgumentIteratorWrapper arg (tokens);
174     senf::console::parse( *(arg++), out.first );
175     senf::console::parse( *(arg++), out.second );
176 }
177
178 template <class T1, class T2>
179 prefix_ std::string senf::console::ArgumentTraits< std::pair<T1,T2> >::description()
180 {
181     return (boost::format("pair<%s,%s>")
182             % ArgumentTraits<T1>::description()
183             % ArgumentTraits<T2>::description()).str();
184 }
185
186 template <class T1, class T2>
187 prefix_ std::string senf::console::ArgumentTraits< std::pair<T1,T2> >::str(type const & value)
188 {
189     std::stringstream ss;
190     senf::console::format(value, ss);
191     return ss.str();
192 }
193
194 ///////////////////////////////////////////////////////////////////////////
195 // senf::console::ReturnValueTraits< std::pair<T1,T2> >
196
197 template <class T1, class T2>
198 prefix_ void senf::console::ReturnValueTraits< std::pair<T1,T2> >::format(type const & value,
199                                                                           std::ostream & os)
200 {
201     os << "(" << senf::console::str(value.first)
202        << " " << senf::console::str(value.second) << ")";
203 }
204
205 #endif
206
207 ///////////////////////////////ct.e////////////////////////////////////////
208 #undef prefix_
209
210 \f
211 // Local Variables:
212 // mode: c++
213 // fill-column: 100
214 // comment-column: 40
215 // c-file-style: "senf"
216 // indent-tabs-mode: nil
217 // ispell-local-dictionary: "american"
218 // compile-command: "scons -u test"
219 // End: