Fix documentation build under maverick (doxygen 1.7.1)
[senf.git] / senf / Utils / String.ct
1 // $Id$
2 //
3 // Copyright (C) 2008
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 String non-inline template implementation  */
25
26 //#include "String.ih"
27
28 // Custom includes
29 #include <boost/range.hpp>
30 #include <sstream>
31 #include <boost/lexical_cast.hpp>
32 #include <boost/shared_ptr.hpp>
33
34 #define prefix_
35 //-/////////////////////////////////////////////////////////////////////////////////////////////////
36
37 template <class ForwardReadableRange>
38 prefix_ std::string senf::stringJoin(ForwardReadableRange const & range, std::string sep)
39 {
40     typename boost::range_const_iterator<ForwardReadableRange>::type i (boost::begin(range));
41     typename boost::range_const_iterator<ForwardReadableRange>::type const i_end (boost::end(range));
42     std::stringstream ss;
43
44     if (i != i_end) {
45         for (;;) {
46             ss << *i;
47             if ( ++i != i_end ) ss << sep;
48             else                break;
49         }
50     }
51
52     return ss.str();
53 }
54
55 // Copied from boost/lexical_cast.hpp
56 namespace senf {
57 namespace detail {
58     template<typename Target>
59     class lexical_stream
60     {
61     private:
62         typedef char char_type;
63
64     public:
65         lexical_stream()
66         {
67             stream.unsetf(std::ios::skipws);
68             if (std::numeric_limits<Target>::is_specialized)
69                 stream.precision(std::numeric_limits<Target>::digits10 + 1);
70         }
71         template <class Source>
72         bool operator<<(const Source &input)
73         {
74             if (std::numeric_limits<Source>::is_specialized)
75                 stream.precision(std::numeric_limits<Source>::digits10 + 1);
76             return !(stream << input).fail();
77         }
78         template<typename InputStreamable>
79         bool operator>>(InputStreamable &output)
80         {
81             return !boost::is_pointer<InputStreamable>::value &&
82                    stream >> output &&
83                    stream.get() == std::char_traits<char_type>::eof();
84         }
85         bool operator>>(std::string &output)
86         {
87             output = stream.str();
88             return true;
89         }
90         bool operator>>(std::wstring &output)
91         {
92             output = stream.str();
93             return true;
94         }
95     private:
96         std::basic_stringstream<char_type> stream;
97     };
98
99     template <class Target>
100     class lexical_caster
101     {
102     public:
103         lexical_caster() : interpreter_ (new senf::detail::lexical_stream<Target>()) {}
104         template <class Source>
105         Target operator()(Source const & arg) const
106             {
107                 Target result;
108                 if (!((*interpreter_) << arg && (*interpreter_) >> result))
109                     boost::throw_exception(boost::bad_lexical_cast(typeid(Source), typeid(Target)));
110                 return result;
111             }
112
113         template <class Mod>
114         lexical_caster const & operator[](Mod mod) const
115             {
116                 (*interpreter_) << mod;
117                 return *this;
118             }
119
120     private:
121         boost::shared_ptr< senf::detail::lexical_stream<Target> > interpreter_;
122     };
123 }}
124
125 template <class Target, class Source>
126 prefix_ Target senf::lexical_cast(Source const & arg)
127 {
128     senf::detail::lexical_stream<Target> interpreter;
129     Target result;
130
131     if (!(interpreter << arg && interpreter >> result))
132         boost::throw_exception(boost::bad_lexical_cast(typeid(Source), typeid(Target)));
133     return result;
134 }
135
136 template <class Target>
137 prefix_ senf::detail::lexical_caster<Target> senf::lexical_cast()
138 {
139     return detail::lexical_caster<Target>();
140 }
141
142 //-/////////////////////////////////////////////////////////////////////////////////////////////////
143 #undef prefix_
144
145 \f
146 // Local Variables:
147 // mode: c++
148 // fill-column: 100
149 // comment-column: 40
150 // c-file-style: "senf"
151 // indent-tabs-mode: nil
152 // ispell-local-dictionary: "american"
153 // compile-command: "scons -u test"
154 // End: