switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Utils / Console / STLSupport.ct
1 // $Id$
2 //
3 // Copyright (C) 2009
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief VectorSupport non-inline template implementation  */
30
31 //#include "VectorSupport.ih"
32
33 // Custom includes
34 #include <boost/format.hpp>
35
36 #define prefix_
37 //-/////////////////////////////////////////////////////////////////////////////////////////////////
38
39 #ifndef DOXYGEN
40
41 //-/////////////////////////////////////////////////////////////////////////////////////////////////
42 // senf::console::detail::CollectionArgumentTraitsBase<Collection>
43
44 template <class Collection>
45 prefix_ std::string
46 senf::console::detail::CollectionArgumentTraitsBase<Collection>::description()
47 {
48     return senf::prettyBaseName(typeid(Collection)) + "<"
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     return senf::prettyBaseName(typeid(Collection)) + "<"
124         + ArgumentTraits<typename Collection::key_type>::description() + ","
125         + ArgumentTraits<typename Collection::mapped_type>::description() + ">";
126 }
127
128 template <class Collection>
129 prefix_ std::string
130 senf::console::detail::MapArgumentTraits<Collection>::str(Collection const & value)
131 {
132     std::stringstream ss;
133     senf::console::format(value, ss);
134     return ss.str();
135 }
136
137 //-/////////////////////////////////////////////////////////////////////////////////////////////////
138 // senf::console::detail::MapReturnValueTraits<Collection>
139
140 template <class Collection>
141 prefix_ void
142 senf::console::detail::MapReturnValueTraits<Collection>::format(Collection const & value,
143                                                                 std::ostream & os)
144 {
145     os << "(";
146     typename type::const_iterator i (value.begin());
147     typename type::const_iterator const i_end (value.end());
148     if (i != i_end)
149         for (;;) {
150             os << senf::console::str(i->first)
151                << "="
152                << senf::console::str(i->second);
153             if (++i == i_end)
154                 break;
155             else
156                 os << " ";
157         }
158     os << ")";
159 }
160
161 //-/////////////////////////////////////////////////////////////////////////////////////////////////
162 // senf::console::ArgumentTraits< std::pair<T1,T2> >
163
164 template <class T1, class T2>
165 prefix_ void senf::console::ArgumentTraits< std::pair<T1,T2> >::
166 parse(ParseCommandInfo::TokensRange const & tokens, type & out)
167 {
168     CheckedArgumentIteratorWrapper arg (tokens);
169     senf::console::parse( *(arg++), out.first );
170     senf::console::parse( *(arg++), out.second );
171 }
172
173 template <class T1, class T2>
174 prefix_ std::string senf::console::ArgumentTraits< std::pair<T1,T2> >::description()
175 {
176     return (boost::format("pair<%s,%s>")
177             % ArgumentTraits<T1>::description()
178             % ArgumentTraits<T2>::description()).str();
179 }
180
181 template <class T1, class T2>
182 prefix_ std::string senf::console::ArgumentTraits< std::pair<T1,T2> >::str(type const & value)
183 {
184     std::stringstream ss;
185     senf::console::format(value, ss);
186     return ss.str();
187 }
188
189 //-/////////////////////////////////////////////////////////////////////////////////////////////////
190 // senf::console::ReturnValueTraits< std::pair<T1,T2> >
191
192 template <class T1, class T2>
193 prefix_ void senf::console::ReturnValueTraits< std::pair<T1,T2> >::format(type const & value,
194                                                                           std::ostream & os)
195 {
196     os << "(" << senf::console::str(value.first)
197        << " " << senf::console::str(value.second) << ")";
198 }
199
200 #endif
201
202 //-/////////////////////////////////////////////////////////////////////////////////////////////////
203 #undef prefix_
204
205 \f
206 // Local Variables:
207 // mode: c++
208 // fill-column: 100
209 // comment-column: 40
210 // c-file-style: "senf"
211 // indent-tabs-mode: nil
212 // ispell-local-dictionary: "american"
213 // compile-command: "scons -u test"
214 // End: