2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
15 \brief String non-inline template implementation */
17 //#include "String.ih"
20 #include <boost/range.hpp>
22 #include <boost/lexical_cast.hpp>
23 #include <boost/shared_ptr.hpp>
26 //-/////////////////////////////////////////////////////////////////////////////////////////////////
28 template <class ForwardReadableRange>
29 prefix_ std::string senf::stringJoin(ForwardReadableRange const & range, std::string sep)
31 typename boost::range_const_iterator<ForwardReadableRange>::type i (boost::begin(range));
32 typename boost::range_const_iterator<ForwardReadableRange>::type const i_end (boost::end(range));
38 if ( ++i != i_end ) ss << sep;
46 // Copied from boost/lexical_cast.hpp
49 template<typename Target>
53 typedef char char_type;
58 stream.unsetf(std::ios::skipws);
59 if (std::numeric_limits<Target>::is_specialized)
60 stream.precision(std::numeric_limits<Target>::digits10 + 1);
62 template <class Source>
63 bool operator<<(const Source &input)
65 if (std::numeric_limits<Source>::is_specialized)
66 stream.precision(std::numeric_limits<Source>::digits10 + 1);
67 return !(stream << input).fail();
69 template<typename InputStreamable>
70 bool operator>>(InputStreamable &output)
72 return !boost::is_pointer<InputStreamable>::value &&
74 stream.get() == std::char_traits<char_type>::eof();
76 bool operator>>(std::string &output)
78 output = stream.str();
81 bool operator>>(std::wstring &output)
83 output = stream.str();
87 std::basic_stringstream<char_type> stream;
90 template <class Target>
94 lexical_caster() : interpreter_ (new senf::detail::lexical_stream<Target>()) {}
95 template <class Source>
96 Target operator()(Source const & arg) const
99 if (!((*interpreter_) << arg && (*interpreter_) >> result))
100 boost::throw_exception(boost::bad_lexical_cast(typeid(Source), typeid(Target)));
105 lexical_caster const & operator[](Mod mod) const
107 (*interpreter_) << mod;
112 boost::shared_ptr< senf::detail::lexical_stream<Target> > interpreter_;
116 template <class Target, class Source>
117 prefix_ Target senf::lexical_cast(Source const & arg)
119 senf::detail::lexical_stream<Target> interpreter;
122 if (!(interpreter << arg && interpreter >> result))
123 boost::throw_exception(boost::bad_lexical_cast(typeid(Source), typeid(Target)));
127 template <class Target>
128 prefix_ senf::detail::lexical_caster<Target> senf::lexical_cast()
130 return detail::lexical_caster<Target>();
133 //-/////////////////////////////////////////////////////////////////////////////////////////////////
140 // comment-column: 40
141 // c-file-style: "senf"
142 // indent-tabs-mode: nil
143 // ispell-local-dictionary: "american"
144 // compile-command: "scons -u test"