diff options
author | Jacek Antonelli | 2008-08-15 23:44:46 -0500 |
---|---|---|
committer | Jacek Antonelli | 2008-08-15 23:44:46 -0500 |
commit | 38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4 (patch) | |
tree | adca584755d22ca041a2dbfc35d4eca01f70b32c /linden/indra/llcommon/llsd.h | |
parent | README.txt (diff) | |
download | meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.zip meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.gz meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.bz2 meta-impy-38d6d37f2d982fa959e9e8a4a3f7e1ccfad7b5d4.tar.xz |
Second Life viewer sources 1.13.2.12
Diffstat (limited to 'linden/indra/llcommon/llsd.h')
-rw-r--r-- | linden/indra/llcommon/llsd.h | 385 |
1 files changed, 385 insertions, 0 deletions
diff --git a/linden/indra/llcommon/llsd.h b/linden/indra/llcommon/llsd.h new file mode 100644 index 0000000..625b480 --- /dev/null +++ b/linden/indra/llcommon/llsd.h | |||
@@ -0,0 +1,385 @@ | |||
1 | /** | ||
2 | * @file llsd.h | ||
3 | * @brief LLSD flexible data system. | ||
4 | * | ||
5 | * Copyright (c) 2005-2007, Linden Research, Inc. | ||
6 | * | ||
7 | * The source code in this file ("Source Code") is provided by Linden Lab | ||
8 | * to you under the terms of the GNU General Public License, version 2.0 | ||
9 | * ("GPL"), unless you have obtained a separate licensing agreement | ||
10 | * ("Other License"), formally executed by you and Linden Lab. Terms of | ||
11 | * the GPL can be found in doc/GPL-license.txt in this distribution, or | ||
12 | * online at http://secondlife.com/developers/opensource/gplv2 | ||
13 | * | ||
14 | * There are special exceptions to the terms and conditions of the GPL as | ||
15 | * it is applied to this Source Code. View the full text of the exception | ||
16 | * in the file doc/FLOSS-exception.txt in this software distribution, or | ||
17 | * online at http://secondlife.com/developers/opensource/flossexception | ||
18 | * | ||
19 | * By copying, modifying or distributing this software, you acknowledge | ||
20 | * that you have read and understood your obligations described above, | ||
21 | * and agree to abide by those obligations. | ||
22 | * | ||
23 | * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO | ||
24 | * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY, | ||
25 | * COMPLETENESS OR PERFORMANCE. | ||
26 | */ | ||
27 | |||
28 | #ifndef LL_LLSD_NEW_H | ||
29 | #define LL_LLSD_NEW_H | ||
30 | |||
31 | #include <map> | ||
32 | #include <string> | ||
33 | #include <vector> | ||
34 | |||
35 | #include "stdtypes.h" | ||
36 | |||
37 | #include "lldate.h" | ||
38 | #include "lluri.h" | ||
39 | #include "../llmath/lluuid.h" | ||
40 | |||
41 | /** | ||
42 | LLSD provides a flexible data system similar to the data facilities of | ||
43 | dynamic languages like Perl and Python. It is created to support exchange | ||
44 | of structured data between loosly coupled systems. (Here, "loosly coupled" | ||
45 | means not compiled together into the same module.) | ||
46 | |||
47 | Data in such exchanges must be highly tollerant of changes on either side | ||
48 | such as: | ||
49 | - recompilation | ||
50 | - implementation in a different langauge | ||
51 | - addition of extra parameters | ||
52 | - execution of older versions (with fewer parameters) | ||
53 | |||
54 | To this aim, the C++ API of LLSD strives to be very easy to use, and to | ||
55 | default to "the right thing" whereever possible. It is extremely tollerant | ||
56 | of errors and unexpected situations. | ||
57 | |||
58 | The fundimental class is LLSD. LLSD is a value holding object. It holds | ||
59 | one value that is either undefined, one of the scalar types, or a map or an | ||
60 | array. LLSD objects have value semantics (copying them copies the value, | ||
61 | though it can be considered efficient, due to shareing.), and mutable. | ||
62 | |||
63 | Undefined is the singular value given to LLSD objects that are not | ||
64 | initialized with any data. It is also used as the return value for | ||
65 | operations that return an LLSD, | ||
66 | |||
67 | The sclar data types are: | ||
68 | - Boolean - true or false | ||
69 | - Integer - a 32 bit signed integer | ||
70 | - Real - a 64 IEEE 754 floating point value | ||
71 | - UUID - a 128 unique value | ||
72 | - String - a sequence of zero or more Unicode chracters | ||
73 | - Date - an absolute point in time, UTC, | ||
74 | with resolution to the second | ||
75 | - URI - a String that is a URI | ||
76 | - Binary - a sequence of zero or more octets (unsigned bytes) | ||
77 | |||
78 | A map is a dictionary mapping String keys to LLSD values. The keys are | ||
79 | unique within a map, and have only one value (though that value could be | ||
80 | an LLSD array). | ||
81 | |||
82 | An array is a sequence of zero or more LLSD values. | ||
83 | |||
84 | @nosubgrouping | ||
85 | */ | ||
86 | |||
87 | class LLSD | ||
88 | { | ||
89 | public: | ||
90 | LLSD(); ///< initially Undefined | ||
91 | ~LLSD(); ///< this class may NOT be subclassed | ||
92 | |||
93 | /** @name Copyable and Assignable */ | ||
94 | //@{ | ||
95 | LLSD(const LLSD&); | ||
96 | void assign(const LLSD& other); | ||
97 | LLSD& operator=(const LLSD& other) { assign(other); return *this; } | ||
98 | |||
99 | //@} | ||
100 | |||
101 | void clear(); ///< resets to Undefined | ||
102 | |||
103 | |||
104 | /** @name Scalar Types | ||
105 | The scalar types, and how they map onto C++ | ||
106 | */ | ||
107 | //@{ | ||
108 | typedef bool Boolean; | ||
109 | typedef S32 Integer; | ||
110 | typedef F64 Real; | ||
111 | typedef std::string String; | ||
112 | typedef LLUUID UUID; | ||
113 | typedef LLDate Date; | ||
114 | typedef LLURI URI; | ||
115 | typedef std::vector<U8> Binary; | ||
116 | //@} | ||
117 | |||
118 | /** @name Scalar Constructors */ | ||
119 | //@{ | ||
120 | LLSD(Boolean); | ||
121 | LLSD(Integer); | ||
122 | LLSD(Real); | ||
123 | LLSD(const String&); | ||
124 | LLSD(const UUID&); | ||
125 | LLSD(const Date&); | ||
126 | LLSD(const URI&); | ||
127 | LLSD(const Binary&); | ||
128 | //@} | ||
129 | |||
130 | /** @name Convenience Constructors */ | ||
131 | //@{ | ||
132 | LLSD(F32); // F32 -> Real | ||
133 | //@} | ||
134 | |||
135 | /** @name Scalar Assignment */ | ||
136 | //@{ | ||
137 | void assign(Boolean); | ||
138 | void assign(Integer); | ||
139 | void assign(Real); | ||
140 | void assign(const String&); | ||
141 | void assign(const UUID&); | ||
142 | void assign(const Date&); | ||
143 | void assign(const URI&); | ||
144 | void assign(const Binary&); | ||
145 | |||
146 | LLSD& operator=(Boolean v) { assign(v); return *this; } | ||
147 | LLSD& operator=(Integer v) { assign(v); return *this; } | ||
148 | LLSD& operator=(Real v) { assign(v); return *this; } | ||
149 | LLSD& operator=(const String& v) { assign(v); return *this; } | ||
150 | LLSD& operator=(const UUID& v) { assign(v); return *this; } | ||
151 | LLSD& operator=(const Date& v) { assign(v); return *this; } | ||
152 | LLSD& operator=(const URI& v) { assign(v); return *this; } | ||
153 | LLSD& operator=(const Binary& v) { assign(v); return *this; } | ||
154 | //@} | ||
155 | |||
156 | /** | ||
157 | @name Scalar Accessors | ||
158 | @brief Fetch a scalar value, converting if needed and possible | ||
159 | |||
160 | Conversion among the basic types, Boolean, Integer, Real and String, is | ||
161 | fully defined. Each type can be converted to another with a reasonable | ||
162 | interpretation. These conversions can be used as a convenience even | ||
163 | when you know the data is in one format, but you want it in another. Of | ||
164 | course, many of these conversions lose information. | ||
165 | |||
166 | Note: These conversions are not the same as Perl's. In particular, when | ||
167 | converting a String to a Boolean, only the empty string converts to | ||
168 | false. Converting the String "0" to Boolean results in true. | ||
169 | |||
170 | Conversion to and from UUID, Date, and URI is only defined to and from | ||
171 | String. Conversion is defined to be information preserving for valid | ||
172 | values of those types. These conversions can be used when one needs to | ||
173 | convert data to or from another system that cannot handle these types | ||
174 | natively, but can handle strings. | ||
175 | |||
176 | Conversion to and from Binary isn't defined. | ||
177 | |||
178 | Conversion of the Undefined value to any scalar type results in a | ||
179 | reasonable null or zero value for the type. | ||
180 | */ | ||
181 | //@{ | ||
182 | Boolean asBoolean() const; | ||
183 | Integer asInteger() const; | ||
184 | Real asReal() const; | ||
185 | String asString() const; | ||
186 | UUID asUUID() const; | ||
187 | Date asDate() const; | ||
188 | URI asURI() const; | ||
189 | Binary asBinary() const; | ||
190 | |||
191 | operator Boolean() const { return asBoolean(); } | ||
192 | operator Integer() const { return asInteger(); } | ||
193 | operator Real() const { return asReal(); } | ||
194 | operator String() const { return asString(); } | ||
195 | operator UUID() const { return asUUID(); } | ||
196 | operator Date() const { return asDate(); } | ||
197 | operator URI() const { return asURI(); } | ||
198 | operator Binary() const { return asBinary(); } | ||
199 | |||
200 | // This is needed because most platforms do not automatically | ||
201 | // convert the boolean negation as a bool in an if statement. | ||
202 | bool operator!() const {return !asBoolean();} | ||
203 | //@} | ||
204 | |||
205 | /** @name Character Pointer Helpers | ||
206 | These are helper routines to make working with char* the same as easy as | ||
207 | working with strings. | ||
208 | */ | ||
209 | //@{ | ||
210 | LLSD(const char*); | ||
211 | void assign(const char*); | ||
212 | LLSD& operator=(const char* v) { assign(v); return *this; } | ||
213 | //@} | ||
214 | |||
215 | /** @name Map Values */ | ||
216 | //@{ | ||
217 | static LLSD emptyMap(); | ||
218 | |||
219 | bool has(const String&) const; | ||
220 | LLSD get(const String&) const; | ||
221 | void insert(const String&, const LLSD&); | ||
222 | void erase(const String&); | ||
223 | |||
224 | LLSD& operator[](const String&); | ||
225 | LLSD& operator[](const char* c) { return (*this)[String(c)]; } | ||
226 | const LLSD& operator[](const String&) const; | ||
227 | const LLSD& operator[](const char* c) const { return (*this)[String(c)]; } | ||
228 | //@} | ||
229 | |||
230 | /** @name Array Values */ | ||
231 | //@{ | ||
232 | static LLSD emptyArray(); | ||
233 | |||
234 | LLSD get(Integer) const; | ||
235 | void set(Integer, const LLSD&); | ||
236 | void insert(Integer, const LLSD&); | ||
237 | void append(const LLSD&); | ||
238 | void erase(Integer); | ||
239 | |||
240 | const LLSD& operator[](Integer) const; | ||
241 | LLSD& operator[](Integer); | ||
242 | //@} | ||
243 | |||
244 | /** @name Iterators */ | ||
245 | //@{ | ||
246 | int size() const; | ||
247 | |||
248 | typedef std::map<String, LLSD>::iterator map_iterator; | ||
249 | typedef std::map<String, LLSD>::const_iterator map_const_iterator; | ||
250 | |||
251 | map_iterator beginMap(); | ||
252 | map_iterator endMap(); | ||
253 | map_const_iterator beginMap() const; | ||
254 | map_const_iterator endMap() const; | ||
255 | |||
256 | typedef std::vector<LLSD>::iterator array_iterator; | ||
257 | typedef std::vector<LLSD>::const_iterator array_const_iterator; | ||
258 | |||
259 | array_iterator beginArray(); | ||
260 | array_iterator endArray(); | ||
261 | array_const_iterator beginArray() const; | ||
262 | array_const_iterator endArray() const; | ||
263 | //@} | ||
264 | |||
265 | /** @name Type Testing */ | ||
266 | //@{ | ||
267 | enum Type { | ||
268 | TypeUndefined, | ||
269 | TypeBoolean, | ||
270 | TypeInteger, | ||
271 | TypeReal, | ||
272 | TypeString, | ||
273 | TypeUUID, | ||
274 | TypeDate, | ||
275 | TypeURI, | ||
276 | TypeBinary, | ||
277 | TypeMap, | ||
278 | TypeArray | ||
279 | }; | ||
280 | |||
281 | Type type() const; | ||
282 | |||
283 | bool isUndefined() const { return type() == TypeUndefined; } | ||
284 | bool isDefined() const { return type() != TypeUndefined; } | ||
285 | bool isBoolean() const { return type() == TypeBoolean; } | ||
286 | bool isInteger() const { return type() == TypeInteger; } | ||
287 | bool isReal() const { return type() == TypeReal; } | ||
288 | bool isString() const { return type() == TypeString; } | ||
289 | bool isUUID() const { return type() == TypeUUID; } | ||
290 | bool isDate() const { return type() == TypeDate; } | ||
291 | bool isURI() const { return type() == TypeURI; } | ||
292 | bool isBinary() const { return type() == TypeBinary; } | ||
293 | bool isMap() const { return type() == TypeMap; } | ||
294 | bool isArray() const { return type() == TypeArray; } | ||
295 | //@} | ||
296 | |||
297 | /** @name Automatic Cast Protection | ||
298 | These are not implemented on purpose. Without them, C++ can perform | ||
299 | some conversions that are clearly not what the programmer intended. | ||
300 | |||
301 | If you get a linker error about these being missing, you have made | ||
302 | mistake in your code. DO NOT IMPLEMENT THESE FUNCTIONS as a fix. | ||
303 | |||
304 | All of thse problems stem from trying to support char* in LLSD or in | ||
305 | std::string. There are too many automatic casts that will lead to | ||
306 | using an arbitrary pointer or scalar type to std::string. | ||
307 | */ | ||
308 | //@{ | ||
309 | LLSD(const void*); ///< construct from aribrary pointers | ||
310 | void assign(const void*); ///< assign from arbitrary pointers | ||
311 | LLSD& operator=(const void*); ///< assign from arbitrary pointers | ||
312 | |||
313 | bool has(Integer) const; ///< has only works for Maps | ||
314 | //@} | ||
315 | |||
316 | /** @name Implementation */ | ||
317 | //@{ | ||
318 | public: | ||
319 | class Impl; | ||
320 | private: | ||
321 | Impl* impl; | ||
322 | //@} | ||
323 | |||
324 | /** @name Unit Testing Interface */ | ||
325 | //@{ | ||
326 | public: | ||
327 | static U32 allocationCount(); ///< how many Impls have been made | ||
328 | static U32 outstandingCount(); ///< how many Impls are still alive | ||
329 | //@} | ||
330 | }; | ||
331 | |||
332 | struct llsd_select_bool : public std::unary_function<LLSD, LLSD::Boolean> | ||
333 | { | ||
334 | LLSD::Boolean operator()(const LLSD& sd) const | ||
335 | { | ||
336 | return sd.asBoolean(); | ||
337 | } | ||
338 | }; | ||
339 | struct llsd_select_integer : public std::unary_function<LLSD, LLSD::Integer> | ||
340 | { | ||
341 | LLSD::Integer operator()(const LLSD& sd) const | ||
342 | { | ||
343 | return sd.asInteger(); | ||
344 | } | ||
345 | }; | ||
346 | struct llsd_select_real : public std::unary_function<LLSD, LLSD::Real> | ||
347 | { | ||
348 | LLSD::Real operator()(const LLSD& sd) const | ||
349 | { | ||
350 | return sd.asReal(); | ||
351 | } | ||
352 | }; | ||
353 | struct llsd_select_float : public std::unary_function<LLSD, F32> | ||
354 | { | ||
355 | F32 operator()(const LLSD& sd) const | ||
356 | { | ||
357 | return (F32)sd.asReal(); | ||
358 | } | ||
359 | }; | ||
360 | struct llsd_select_uuid : public std::unary_function<LLSD, LLSD::UUID> | ||
361 | { | ||
362 | LLSD::UUID operator()(const LLSD& sd) const | ||
363 | { | ||
364 | return sd.asUUID(); | ||
365 | } | ||
366 | }; | ||
367 | struct llsd_select_string : public std::unary_function<LLSD, LLSD::String> | ||
368 | { | ||
369 | LLSD::String operator()(const LLSD& sd) const | ||
370 | { | ||
371 | return sd.asString(); | ||
372 | } | ||
373 | }; | ||
374 | |||
375 | |||
376 | /** QUESTIONS & TO DOS | ||
377 | - Would Binary be more convenient as usigned char* buffer semantics? | ||
378 | - Should Binary be convertable to/from String, and if so how? | ||
379 | - as UTF8 encoded strings (making not like UUID<->String) | ||
380 | - as Base64 or Base96 encoded (making like UUID<->String) | ||
381 | - Conversions to std::string and LLUUID do not result in easy assignment | ||
382 | to std::string, LLString or LLUUID due to non-unique conversion paths | ||
383 | */ | ||
384 | |||
385 | #endif // LL_LLSD_NEW_H | ||