PageRenderTime 10ms CodeModel.GetById 2ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llcommon/stringize.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 107 lines | 35 code | 11 blank | 61 comment | 0 complexity | 7cc879a2adfd934ba67a6c481a26e803 MD5 | raw file
  1/**
  2 * @file   stringize.h
  3 * @author Nat Goodspeed
  4 * @date   2008-12-17
  5 * @brief  stringize(item) template function and STRINGIZE(expression) macro
  6 * 
  7 * $LicenseInfo:firstyear=2008&license=viewerlgpl$
  8 * Second Life Viewer Source Code
  9 * Copyright (C) 2010, Linden Research, Inc.
 10 * 
 11 * This library is free software; you can redistribute it and/or
 12 * modify it under the terms of the GNU Lesser General Public
 13 * License as published by the Free Software Foundation;
 14 * version 2.1 of the License only.
 15 * 
 16 * This library is distributed in the hope that it will be useful,
 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 19 * Lesser General Public License for more details.
 20 * 
 21 * You should have received a copy of the GNU Lesser General Public
 22 * License along with this library; if not, write to the Free Software
 23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 24 * 
 25 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 26 * $/LicenseInfo$
 27 */
 28
 29#if ! defined(LL_STRINGIZE_H)
 30#define LL_STRINGIZE_H
 31
 32#include <sstream>
 33#include <boost/lambda/lambda.hpp>
 34
 35/**
 36 * stringize(item) encapsulates an idiom we use constantly, using
 37 * operator<<(std::ostringstream&, TYPE) followed by std::ostringstream::str()
 38 * to render a string expressing some item.
 39 */
 40template <typename T>
 41std::string stringize(const T& item)
 42{
 43    std::ostringstream out;
 44    out << item;
 45    return out.str();
 46}
 47
 48/**
 49 * stringize_f(functor)
 50 */
 51template <typename Functor>
 52std::string stringize_f(Functor const & f)
 53{
 54    std::ostringstream out;
 55    f(out);
 56    return out.str();
 57}
 58
 59/**
 60 * STRINGIZE(item1 << item2 << item3 ...) effectively expands to the
 61 * following:
 62 * @code
 63 * std::ostringstream out;
 64 * out << item1 << item2 << item3 ... ;
 65 * return out.str();
 66 * @endcode
 67 */
 68#define STRINGIZE(EXPRESSION) (stringize_f(boost::lambda::_1 << EXPRESSION))
 69
 70
 71/**
 72 * destringize(str)
 73 * defined for symmetry with stringize
 74 * *NOTE - this has distinct behavior from boost::lexical_cast<T> regarding
 75 * leading/trailing whitespace and handling of bad_lexical_cast exceptions
 76 */
 77template <typename T>
 78T destringize(std::string const & str)
 79{
 80	T val;
 81    std::istringstream in(str);
 82	in >> val;
 83    return val;
 84}
 85
 86/**
 87 * destringize_f(str, functor)
 88 */
 89template <typename Functor>
 90void destringize_f(std::string const & str, Functor const & f)
 91{
 92    std::istringstream in(str);
 93    f(in);
 94}
 95
 96/**
 97 * DESTRINGIZE(str, item1 >> item2 >> item3 ...) effectively expands to the
 98 * following:
 99 * @code
100 * std::istringstream in(str);
101 * in >> item1 >> item2 >> item3 ... ;
102 * @endcode
103 */
104#define DESTRINGIZE(STR, EXPRESSION) (destringize_f((STR), (boost::lambda::_1 >> EXPRESSION)))
105
106
107#endif /* ! defined(LL_STRINGIZE_H) */