/** * @file stringize.h * @author Nat Goodspeed * @date 2008-12-17 * @brief stringize(item) template function and STRINGIZE(expression) macro * * $LicenseInfo:firstyear=2008&license=viewerlgpl$ * Second Life Viewer Source Code * Copyright (C) 2010, Linden Research, Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; * version 2.1 of the License only. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA * $/LicenseInfo$ */ #if ! defined(LL_STRINGIZE_H) #define LL_STRINGIZE_H #include #include "llstring.h" #include /** * stream_to(std::ostream&, items, ...) streams each item in the parameter list * to the passed std::ostream using the insertion operator <<. This can be * used, for instance, to make a simple print() function, e.g.: * * @code * template * void print(Items&&... items) * { * stream_to(std::cout, std::forward(items)...); * } * @endcode */ // recursion tail template void stream_to(std::basic_ostream& out) {} // stream one or more items template void stream_to(std::basic_ostream& out, T&& item, Items&&... items) { out << std::forward(item); stream_to(out, std::forward(items)...); } // why we use function overloads, not function template specializations: // http://www.gotw.ca/publications/mill17.htm /** * gstringize(item, ...) encapsulates an idiom we use constantly, using * operator<<(std::ostringstream&, TYPE) followed by std::ostringstream::str() * or their wstring equivalents to render a string expressing one or more items. */ // two or more args - the case of a single argument is handled separately template auto gstringize(T0&& item0, T1&& item1, Items&&... items) { std::basic_ostringstream out; stream_to(out, std::forward(item0), std::forward(item1), std::forward(items)...); return out.str(); } // generic single argument: stream to out, as above template struct gstringize_impl { auto operator()(typename boost::call_traits::param_type arg) { std::basic_ostringstream out; out << arg; return out.str(); } }; // partially specialize for a single STRING argument - // note that ll_convert_to(T) already handles the trivial case template struct gstringize_impl> { auto operator()(const std::basic_string& arg) { return ll_convert_to>(arg); } }; // partially specialize for a single CHARTYPE* argument - // since it's not a basic_string and we do want to optimize this common case template struct gstringize_impl { auto operator()(const INCHAR* arg) { return ll_convert_to>(arg); } }; // gstringize(single argument) template auto gstringize(T&& item) { // use decay so we don't require separate specializations // for T, const T, T&, const T& ... return gstringize_impl>()(std::forward(item)); } /** * Specialization of gstringize for std::string return types */ template auto stringize(Items&&... items) { return gstringize(std::forward(items)...); } /** * Specialization of gstringize for std::wstring return types */ template auto wstringize(Items&&... items) { return gstringize(std::forward(items)...); } /** * stringize_f(functor) */ template std::basic_string stringize_f(Functor const & f) { std::basic_ostringstream out; f(out); return out.str(); } /** * STRINGIZE(item1 << item2 << item3 ...) effectively expands to the * following: * @code * std::ostringstream out; * out << item1 << item2 << item3 ... ; * return out.str(); * @endcode */ #define STRINGIZE(EXPRESSION) (stringize_f([&](std::ostream& out){ out << EXPRESSION; })) /** * WSTRINGIZE() is the wstring equivalent of STRINGIZE() */ #define WSTRINGIZE(EXPRESSION) (stringize_f([&](std::wostream& out){ out << EXPRESSION; })) /** * destringize(str) * defined for symmetry with stringize * @NOTE - this has distinct behavior from boost::lexical_cast regarding * leading/trailing whitespace and handling of bad_lexical_cast exceptions * @NOTE - no need for dewstringize(), since passing std::wstring will Do The * Right Thing */ template T destringize(std::basic_string const & str) { T val; std::basic_istringstream in(str); in >> val; return val; } /** * destringize_f(str, functor) */ template void destringize_f(std::basic_string const & str, Functor const & f) { std::basic_istringstream in(str); f(in); } /** * DESTRINGIZE(str, item1 >> item2 >> item3 ...) effectively expands to the * following: * @code * std::istringstream in(str); * in >> item1 >> item2 >> item3 ... ; * @endcode */ #define DESTRINGIZE(STR, EXPRESSION) (destringize_f((STR), [&](auto& in){in >> EXPRESSION;})) // legacy name, just use DESTRINGIZE() going forward #define DEWSTRINGIZE(STR, EXPRESSION) DESTRINGIZE(STR, EXPRESSION) #endif /* ! defined(LL_STRINGIZE_H) */