Fix documentation build under maverick (doxygen 1.7.1)
[senf.git] / senf / Utils / Console / Utility.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 Utility non-inline template implementation  */
25
26 //#include "Utility.ih"
27
28 // Custom includes
29 #include <sstream>
30 #include <limits>
31 #include <boost/format.hpp>
32 #include "Traits.hh"
33
34 #define prefix_
35 //-/////////////////////////////////////////////////////////////////////////////////////////////////
36
37 //-/////////////////////////////////////////////////////////////////////////////////////////////////
38 // senf::console::ArgumentTraits< CharAsString<CharT> >
39
40 template <class CharT>
41 prefix_ void senf::console::ArgumentTraits< senf::console::CharAsString<CharT> >::
42 parse(ParseCommandInfo::TokensRange const & tokens, CharAsString<CharT> & out)
43 {
44     std::string v;
45     senf::console::parse(tokens,v);
46     if (v.size() != 1)
47         throw SyntaxErrorException("Invalid size of character constant");
48     out.value = static_cast<CharT>(v[0]);
49 }
50
51 template <class CharT>
52 prefix_ std::string
53 senf::console::ArgumentTraits< senf::console::CharAsString<CharT> >::description()
54 {
55     return std::numeric_limits<CharT>::is_signed ? "char" : "uchar";
56 }
57
58 template <class CharT>
59 prefix_ std::string senf::console::ArgumentTraits< senf::console::CharAsString<CharT> >::
60 str(CharAsString<CharT> value)
61 {
62     return senf::console::str(std::string(1,value.value));
63 }
64
65 template <class CharT>
66 prefix_ void senf::console::ReturnValueTraits< senf::console::CharAsString<CharT> >::
67 format(CharAsString<CharT> value, std::ostream & os)
68 {
69     return senf::console::format(std::string(1,value.value),os);
70 }
71
72 //-/////////////////////////////////////////////////////////////////////////////////////////////////
73 // senf::console::ArgumentTraits< senf::console::ValueRange<T> >
74
75 template <class T>
76 prefix_ void senf::console::ArgumentTraits< senf::console::ValueRange<T> >::
77 parse(ParseCommandInfo::TokensRange const & tokens, type & out)
78 {
79     if (tokens.size() != 1)
80         throw senf::console::SyntaxErrorException("parameter syntax error");
81     std::string v (tokens.begin()[0].value());
82     std::string::size_type i (v.find(':'));
83     try {
84         if (i == std::string::npos)
85             out.low = out.high = boost::lexical_cast<T>(v);
86         else {
87             out.low = boost::lexical_cast<T>(v.substr(0,i));
88             out.high = boost::lexical_cast<T>(v.substr(i+1));
89         }
90     }
91     catch (std::bad_cast & ex) {
92         throw senf::console::SyntaxErrorException("parameter syntax error");
93     }
94 }
95
96 template <class T>
97 prefix_ std::string senf::console::ArgumentTraits< senf::console::ValueRange<T> >::
98 description()
99 {
100     return (boost::format("range<%s>") % ArgumentTraits<T>::description()).str();
101 }
102
103 template <class T>
104 prefix_ std::string senf::console::ArgumentTraits< senf::console::ValueRange<T> >::
105 str(type const & value)
106 {
107     std::stringstream ss;
108     senf::console::format(value, ss);
109     return ss.str();
110 }
111
112 //-/////////////////////////////////////////////////////////////////////////////////////////////////
113 // senf::console::ReturnValueTraits< senf::console::ValueRange<T> >
114
115 template <class T>
116 prefix_ void senf::console::ReturnValueTraits< senf::console::ValueRange<T> >::
117 format(type const & value, std::ostream & os)
118 {
119     os << senf::console::str(value.low);
120     if (value.low != value.high)
121         os << ':' << senf::console::str(value.high);
122 }
123
124 //-/////////////////////////////////////////////////////////////////////////////////////////////////
125 // senf::console::ArgumentTraits< senf::console::FlagCollection<Enum> >
126
127 template <class Enum>
128 prefix_ void senf::console::ArgumentTraits< senf::console::FlagCollection<Enum> >::
129 parse(ParseCommandInfo::TokensRange const & tokens, type & out)
130 {
131     CheckedArgumentIteratorWrapper arg (tokens);
132     out.value = 0;
133     while (arg) {
134         Enum v;
135         senf::console::parse( *(arg++), v);
136         out.value |= v;
137     }
138 }
139
140 template <class Enum>
141 prefix_ std::string
142 senf::console::ArgumentTraits< senf::console::FlagCollection<Enum> >::description()
143 {
144     return ArgumentTraits<Enum>::description();
145 }
146
147 template <class Enum>
148 prefix_ std::string
149 senf::console::ArgumentTraits< senf::console::FlagCollection<Enum> >::str(type const & value)
150 {
151     std::stringstream ss;
152     senf::console::format(value, ss);
153     return ss.str();
154 }
155
156 //-/////////////////////////////////////////////////////////////////////////////////////////////////
157 // senf::console::ReturnValueTraits< senf::console::FlagCollection<Enum> >
158
159 template <class Enum>
160 prefix_ void senf::console::ReturnValueTraits< senf::console::FlagCollection<Enum> >::
161 format(type const & value, std::ostream & os)
162 {
163     unsigned n (0);
164     std::stringstream ss;
165     unsigned long flag (1);
166     for (unsigned bit (0); bit<sizeof(value.value)*CHAR_BIT; ++bit, flag<<=1) {
167         if (value.value & flag) {
168             if (n++) ss << " ";
169             senf::console::format(static_cast<Enum>(flag), ss);
170         }
171     }
172     os << (n != 1 ? "(" + ss.str() + ")" : ss.str());
173 }
174
175 //-/////////////////////////////////////////////////////////////////////////////////////////////////
176 #undef prefix_
177
178 \f
179 // Local Variables:
180 // mode: c++
181 // fill-column: 100
182 // comment-column: 40
183 // c-file-style: "senf"
184 // indent-tabs-mode: nil
185 // ispell-local-dictionary: "american"
186 // compile-command: "scons -u test"
187 // End: