4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
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
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.
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.
19 // The Original Code is Fraunhofer FOKUS code.
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.
26 // Stefan Bund <g0dil@berlios.de>
29 \brief String non-inline template implementation */
31 //#include "String.ih"
34 #include <boost/range.hpp>
36 #include <boost/lexical_cast.hpp>
37 #include <boost/shared_ptr.hpp>
40 //-/////////////////////////////////////////////////////////////////////////////////////////////////
42 template <class ForwardReadableRange>
43 prefix_ std::string senf::stringJoin(ForwardReadableRange const & range, std::string sep)
45 typename boost::range_const_iterator<ForwardReadableRange>::type i (boost::begin(range));
46 typename boost::range_const_iterator<ForwardReadableRange>::type const i_end (boost::end(range));
52 if ( ++i != i_end ) ss << sep;
60 // Copied from boost/lexical_cast.hpp
63 template<typename Target>
67 typedef char char_type;
72 stream.unsetf(std::ios::skipws);
73 if (std::numeric_limits<Target>::is_specialized)
74 stream.precision(std::numeric_limits<Target>::digits10 + 1);
76 template <class Source>
77 bool operator<<(const Source &input)
79 if (std::numeric_limits<Source>::is_specialized)
80 stream.precision(std::numeric_limits<Source>::digits10 + 1);
81 return !(stream << input).fail();
83 template<typename InputStreamable>
84 bool operator>>(InputStreamable &output)
86 return !boost::is_pointer<InputStreamable>::value &&
88 stream.get() == std::char_traits<char_type>::eof();
90 bool operator>>(std::string &output)
92 output = stream.str();
95 bool operator>>(std::wstring &output)
97 output = stream.str();
101 std::basic_stringstream<char_type> stream;
104 template <class Target>
108 lexical_caster() : interpreter_ (new senf::detail::lexical_stream<Target>()) {}
109 template <class Source>
110 Target operator()(Source const & arg) const
113 if (!((*interpreter_) << arg && (*interpreter_) >> result))
114 boost::throw_exception(boost::bad_lexical_cast(typeid(Source), typeid(Target)));
119 lexical_caster const & operator[](Mod mod) const
121 (*interpreter_) << mod;
126 boost::shared_ptr< senf::detail::lexical_stream<Target> > interpreter_;
130 template <class Target, class Source>
131 prefix_ Target senf::lexical_cast(Source const & arg)
133 senf::detail::lexical_stream<Target> interpreter;
136 if (!(interpreter << arg && interpreter >> result))
137 boost::throw_exception(boost::bad_lexical_cast(typeid(Source), typeid(Target)));
141 template <class Target>
142 prefix_ senf::detail::lexical_caster<Target> senf::lexical_cast()
144 return detail::lexical_caster<Target>();
147 //-/////////////////////////////////////////////////////////////////////////////////////////////////
154 // comment-column: 40
155 // c-file-style: "senf"
156 // indent-tabs-mode: nil
157 // ispell-local-dictionary: "american"
158 // compile-command: "scons -u test"