summaryrefslogtreecommitdiff
path: root/indra/llcommon/stringize.h
blob: 12df6939108e47b3f0e05c8ccfa40ffae7f8f5c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/**
 * @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 <sstream>
#include <llstring.h>
#include <boost/call_traits.hpp>

/**
 * 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 <typename... Items>
 * void print(Items&&... items)
 * {
 *     stream_to(std::cout, std::forward<Items>(items)...);
 * }
 * @endcode
 */
// recursion tail
template <typename CHARTYPE>
void stream_to(std::basic_ostream<CHARTYPE>& out) {}
// stream one or more items
template <typename CHARTYPE, typename T, typename... Items>
void stream_to(std::basic_ostream<CHARTYPE>& out, T&& item, Items&&... items)
{
    out << std::forward<T>(item);
    stream_to(out, std::forward<Items>(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 <typename CHARTYPE, typename T0, typename T1, typename... Items>
auto gstringize(T0&& item0, T1&& item1, Items&&... items)
{
    std::basic_ostringstream<CHARTYPE> out;
    stream_to(out, std::forward<T0>(item0), std::forward<T1>(item1),
              std::forward<Items>(items)...);
    return out.str();
}

// generic single argument: stream to out, as above
template <typename CHARTYPE, typename T>
struct gstringize_impl
{
    auto operator()(typename boost::call_traits<T>::param_type arg)
    {
        std::basic_ostringstream<CHARTYPE> out;
        out << arg;
        return out.str();
    }
};

// partially specialize for a single STRING argument -
// note that ll_convert<T>(T) already handles the trivial case
template <typename OUTCHAR, typename INCHAR>
struct gstringize_impl<OUTCHAR, std::basic_string<INCHAR>>
{
    auto operator()(const std::basic_string<INCHAR>& arg)
    {
        return ll_convert<std::basic_string<OUTCHAR>>(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 <typename OUTCHAR, typename INCHAR>
struct gstringize_impl<OUTCHAR, INCHAR*>
{
    auto operator()(const INCHAR* arg)
    {
        return ll_convert<std::basic_string<OUTCHAR>>(arg);
    }
};

// gstringize(single argument)
template <typename CHARTYPE, typename T>
auto gstringize(T&& item)
{
    // use decay<T> so we don't require separate specializations
    // for T, const T, T&, const T& ...
    return gstringize_impl<CHARTYPE, std::decay_t<T>>()(std::forward<T>(item));
}

/**
 * Specialization of gstringize for std::string return types
 */
template <typename... Items>
auto stringize(Items&&... items)
{
    return gstringize<char>(std::forward<Items>(items)...);
}

/**
 * Specialization of gstringize for std::wstring return types
 */
template <typename... Items>
auto wstringize(Items&&... items)
{
    return gstringize<wchar_t>(std::forward<Items>(items)...);
}

/**
 * stringize_f(functor)
 */
template <typename CHARTYPE, typename Functor>
std::basic_string<CHARTYPE> stringize_f(Functor const & f)
{
    std::basic_ostringstream<CHARTYPE> 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<char>([&](std::ostream& out){ out << EXPRESSION; }))

/**
 * WSTRINGIZE() is the wstring equivalent of STRINGIZE()
 */
#define WSTRINGIZE(EXPRESSION) (stringize_f<wchar_t>([&](std::wostream& out){ out << EXPRESSION; }))

/**
 * destringize(str)
 * defined for symmetry with stringize
 * @NOTE - this has distinct behavior from boost::lexical_cast<T> 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 <typename T, typename CHARTYPE>
T destringize(std::basic_string<CHARTYPE> const & str)
{
    T val;
    std::basic_istringstream<CHARTYPE> in(str);
    in >> val;
    return val;
}

/**
 * destringize_f(str, functor)
 */
template <typename CHARTYPE, typename Functor>
void destringize_f(std::basic_string<CHARTYPE> const & str, Functor const & f)
{
    std::basic_istringstream<CHARTYPE> 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) */