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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
/**
* @file stdtypes.h
* @brief Basic type declarations for cross platform compatibility.
*
* $LicenseInfo:firstyear=2000&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$
*/
#ifndef LL_STDTYPES_H
#define LL_STDTYPES_H
#include <cassert>
#include <cfloat>
#include <climits>
#include <cstddef>
#include <limits>
#include <type_traits>
typedef signed char S8;
typedef unsigned char U8;
typedef signed short S16;
typedef unsigned short U16;
typedef signed int S32;
typedef unsigned int U32;
// to express an index that might go negative
// (ssize_t is provided by SOME compilers, don't collide)
typedef typename std::make_signed<std::size_t>::type llssize;
#if LL_WINDOWS
// https://docs.microsoft.com/en-us/cpp/build/reference/zc-wchar-t-wchar-t-is-native-type
// https://docs.microsoft.com/en-us/cpp/cpp/fundamental-types-cpp
// Windows wchar_t is 16-bit, whichever way /Zc:wchar_t is set. In effect,
// Windows wchar_t is always a typedef, either for unsigned short or __wchar_t.
// (__wchar_t, available either way, is Microsoft's native 2-byte wchar_t type.)
// The version of clang available with VS 2019 also defines wchar_t as __wchar_t
// which is also 16 bits.
// In any case, llwchar should be a UTF-32 type.
typedef U32 llwchar;
#else
typedef wchar_t llwchar;
// What we'd actually want is a simple module-scope 'if constexpr' to test
// std::is_same<wchar_t, llwchar>::value and use that to define, or not
// define, string conversion specializations. Since we don't have that, we'll
// have to rely on #if instead. Sorry, Dr. Stroustrup.
#define LLWCHAR_IS_WCHAR_T 1
#endif
#if LL_WINDOWS
typedef signed __int64 S64;
// probably should be 'hyper' or similiar
#define S64L(a) (a)
typedef unsigned __int64 U64;
#define U64L(a) (a)
#else
typedef long long int S64;
typedef long long unsigned int U64;
#if LL_DARWIN || LL_LINUX || LL_FREEBSD
#define S64L(a) (a##LL)
#define U64L(a) (a##ULL)
#endif
#endif
typedef float F32;
typedef double F64;
typedef S32 BOOL;
typedef U8 KEY;
typedef U32 MASK;
typedef U32 TPACKETID;
// Use #define instead of consts to avoid conversion headaches
#define S8_MAX (SCHAR_MAX)
#define U8_MAX (UCHAR_MAX)
#define S16_MAX (SHRT_MAX)
#define U16_MAX (USHRT_MAX)
#define S32_MAX (INT_MAX)
#define U32_MAX (UINT_MAX)
#define F32_MAX (FLT_MAX)
#define F64_MAX (DBL_MAX)
#define S8_MIN (SCHAR_MIN)
#define U8_MIN (0)
#define S16_MIN (SHRT_MIN)
#define U16_MIN (0)
#define S32_MIN (INT_MIN)
#define U32_MIN (0)
#define F32_MIN (FLT_MIN)
#define F64_MIN (DBL_MIN)
#ifndef TRUE
#define TRUE (1)
#endif
#ifndef FALSE
#define FALSE (0)
#endif
#if LL_FREEBSD
#undef NULL
#endif
#ifndef NULL
#define NULL (0)
#endif
typedef U8 LLPCode;
#define LL_ARRAY_SIZE( _kArray ) ( sizeof( (_kArray) ) / sizeof( _kArray[0] ) )
#if LL_LINUX && __GNUC__ <= 2
typedef int intptr_t;
#endif
/*****************************************************************************
* Narrowing
*****************************************************************************/
/**
* narrow() is used to cast a wider type to a narrower type with validation.
*
* In many cases we take the size() of a container and try to pass it to an
* S32 or a U32 parameter. We used to be able to assume that the size of
* anything we could fit into memory could be expressed as a 32-bit int. With
* 64-bit viewers, though, size_t as returned by size() and length() and so
* forth is 64 bits, and the compiler is unhappy about stuffing such values
* into 32-bit types.
*
* It works to force the compiler to truncate, e.g. static_cast<S32>(len) or
* S32(len) or (S32)len, but we can do better.
*
* For:
* @code
* std::vector<Object> container;
* void somefunc(S32 size);
* @endcode
* call:
* @code
* somefunc(narrow(container.size()));
* @endcode
*
* narrow() truncates but, in RelWithDebInfo builds, it validates (using
* assert()) that the passed value can validly be expressed by the destination
* type.
*/
// narrow_holder is a struct that accepts the passed value as its original
// type and provides templated conversion functions to other types. Once we're
// building with compilers that support Class Template Argument Deduction, we
// can rename this class template 'narrow' and eliminate the narrow() factory
// function below.
template <typename FROM>
class narrow_holder
{
private:
FROM mValue;
public:
narrow_holder(FROM value): mValue(value) {}
/*---------------------- Narrowing unsigned to signed ----------------------*/
template <typename TO,
typename std::enable_if<std::is_unsigned<FROM>::value &&
std::is_signed<TO>::value,
bool>::type = true>
inline
operator TO() const
{
// The reason we skip the
// assert(value >= std::numeric_limits<TO>::lowest());
// like the overload below is that to perform the above comparison,
// the compiler promotes the signed lowest() to the unsigned FROM
// type, making it hugely positive -- so a reasonable 'value' will
// always fail the assert().
assert(mValue <= std::numeric_limits<TO>::max());
return static_cast<TO>(mValue);
}
/*----------------------- Narrowing all other cases ------------------------*/
template <typename TO,
typename std::enable_if<! (std::is_unsigned<FROM>::value &&
std::is_signed<TO>::value),
bool>::type = true>
inline
operator TO() const
{
// two different assert()s so we can tell which condition failed
assert(mValue <= std::numeric_limits<TO>::max());
// Funny, with floating point types min() is "positive epsilon" rather
// than "largest negative" -- that's lowest().
assert(mValue >= std::numeric_limits<TO>::lowest());
// Do we really expect to use this with floating point types?
// If so, does it matter if a very small value truncates to zero?
//assert(fabs(mValue) >= std::numeric_limits<TO>::min());
return static_cast<TO>(mValue);
}
};
/// narrow() factory function returns a narrow_holder<FROM>(), which can be
/// implicitly converted to the target type.
template <typename FROM>
inline
narrow_holder<FROM> narrow(FROM value)
{
return { value };
}
#endif
|