switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Utils / Console / Traits.cti
1 // $Id$
2 //
3 // Copyright (C) 2008
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 Traits inline template implementation */
30
31 #include "Traits.ih"
32
33 // Custom includes
34 #include <sstream>
35 #include <boost/format.hpp>
36 #include <senf/Utils/TypeInfo.hh>
37
38 #define prefix_ inline
39 //-/////////////////////////////////////////////////////////////////////////////////////////////////
40
41 //-/////////////////////////////////////////////////////////////////////////////////////////////////
42 // senf::console::detail::ReturnValueTraits<Type>
43
44 template <class Type>
45 prefix_ void senf::console::ReturnValueTraits<Type>::format(Type const & value,
46                                                             std::ostream & os)
47 {
48     senf_console_format_value(value, os);
49 }
50
51 template <class Type>
52 prefix_ void senf::console::senf_console_format_value(Type const & value, std::ostream & os)
53 {
54     os << value;
55 }
56
57 //-/////////////////////////////////////////////////////////////////////////////////////////////////
58 // senf::console::ArgumentTraits<Type>
59
60 template <class Type>
61 prefix_ void senf::console::ArgumentTraits<Type>::
62 parse(ParseCommandInfo::TokensRange const & tokens, Type & out)
63 {
64     senf_console_parse_argument(tokens,out);
65 }
66
67 template <class Type>
68 prefix_ void senf::console::parse(ParseCommandInfo::TokensRange const & tokens, Type & out)
69 {
70     ArgumentTraits<Type>::parse(tokens, out);
71 }
72
73 template <class Type>
74 prefix_ std::string senf::console::str(Type const & value)
75 {
76     return ArgumentTraits<Type>::str(value);
77 }
78
79 template <class Type>
80 prefix_ void senf::console::format(Type const & value, std::ostream & os)
81 {
82     ReturnValueTraits<Type>::format(value, os);
83 }
84
85 template <class Type>
86 prefix_ std::string senf::console::ArgumentTraits<Type>::description()
87 {
88     return prettyBaseName(typeid(Type));
89 }
90
91 template <class Type>
92 prefix_ std::string senf::console::ArgumentTraits<Type>::str(Type const & value)
93 {
94     std::stringstream ss;
95     senf::console::format(value, ss);
96     std::string rv (ss.str());
97
98     if (rv.empty() || ! boost::algorithm::all(rv, CommandParser::isWordChar)) {
99         for (std::string::size_type i (0); i < rv.size(); ++i)
100             if (rv[i] == '"' || rv[i] == '\\')
101                 rv.insert(i++,"\\");
102             else if (rv[i] < ' ' || rv[i] > 126) {
103                 rv.insert(i+1, (boost::format("x%02x")
104                                 % unsigned(static_cast<unsigned char>(rv[i]))).str().c_str());
105                 rv[i] = '\\';
106                 i += 3;
107             }
108
109         rv.insert(0,"\"");
110         rv.push_back('"');
111         return rv;
112     }
113
114     return ss.str();
115 }
116
117 //-/////////////////////////////////////////////////////////////////////////////////////////////////
118 // senf::console::detail::CharArgumentTraits<CharT>
119
120 template <class CharT>
121 prefix_ void senf::console::detail::CharArgumentTraits<CharT>::
122 parse(ParseCommandInfo::TokensRange const & tokens, CharT & out)
123 {
124     typename base::type v;
125     base::parse(tokens,v);
126     out = v;
127 }
128
129 template <class CharT>
130 prefix_ std::string senf::console::detail::CharArgumentTraits<CharT>::description()
131 {
132     return std::numeric_limits<CharT>::is_signed ? "byte" : "ubyte";
133 }
134
135 //-/////////////////////////////////////////////////////////////////////////////////////////////////
136 #undef prefix_
137
138 \f
139 // Local Variables:
140 // mode: c++
141 // fill-column: 100
142 // comment-column: 40
143 // c-file-style: "senf"
144 // indent-tabs-mode: nil
145 // ispell-local-dictionary: "american"
146 // compile-command: "scons -u test"
147 // End: