llsd.cpp 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282
  1. /**
  2. * @file llsd.cpp
  3. * @brief LLSD flexible data system
  4. *
  5. * $LicenseInfo:firstyear=2005&license=viewergpl$
  6. *
  7. * Copyright (c) 2005-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #include "linden_common.h"
  33. #include <limits>
  34. #include <sstream>
  35. #include "llsd.h"
  36. #include "llatomic.h"
  37. #include "llbase64.h"
  38. #include "llcommonmath.h" // For llisnan()
  39. #include "llsdserialize.h"
  40. #if LL_DEBUG
  41. # define NAME_UNNAMED_NAMESPACE 1
  42. #endif
  43. // Defend against a caller forcibly passing a negative number into an unsigned
  44. // size_t index param
  45. LL_INLINE static bool was_negative(size_t i)
  46. {
  47. return i > std::numeric_limits<int>::max();
  48. }
  49. #define NEGATIVE_EXIT(i) if (was_negative(i)) return
  50. #define NEGATIVE_RETURN(i, result) if (was_negative(i)) return (result)
  51. #ifdef NAME_UNNAMED_NAMESPACE
  52. namespace LLSDUnnamedNamespace
  53. #else
  54. namespace
  55. #endif
  56. {
  57. class ImplMap;
  58. class ImplArray;
  59. }
  60. #ifdef NAME_UNNAMED_NAMESPACE
  61. using namespace LLSDUnnamedNamespace;
  62. #endif
  63. // This class is the abstract base class of the implementation of LLSD. It
  64. // provides the reference counting implementation, and the default
  65. // implementation of most methods for most data types. It also serves as a
  66. // working implementation of the Undefined type.
  67. class LLSD::Impl
  68. {
  69. protected:
  70. Impl();
  71. enum StaticAllocationMarker { STATIC_USAGE_COUNT = U32_MAX };
  72. // This constructor is used for static objects.
  73. Impl(StaticAllocationMarker);
  74. virtual ~Impl() = default;
  75. LL_INLINE bool shared() const
  76. {
  77. // Must be placed in a temp variable so that we do not risk seeing
  78. // the value changed by another thread during the && below. HB
  79. U32 use_count = mUseCount.get();
  80. return use_count > 1 && use_count != STATIC_USAGE_COUNT;
  81. }
  82. public:
  83. // Safely sets var to refer to the new impl (possibly shared)
  84. static void reset(Impl*& var, Impl* impl);
  85. // Since a NULL Impl* is used for undefined, this ensures there is always
  86. // an object you call virtual member functions on
  87. static Impl& safe(Impl*);
  88. // Do make sure var is a modifiable, non-shared map or array
  89. virtual ImplMap& makeMap(Impl*& var) const;
  90. virtual ImplArray& makeArray(Impl*& var) const;
  91. LL_INLINE virtual LLSD::Type type() const { return LLSD::TypeUndefined; }
  92. static void assignUndefined(Impl*& var);
  93. static void assign(Impl*& var, const LLSD::Impl* other);
  94. // If the receiver is the right type and unshared, these are simple data
  95. // assignments, otherwise the default implementation handles constructing
  96. // the proper Impl subclass
  97. virtual void assign(Impl*& var, LLSD::Boolean) const;
  98. virtual void assign(Impl*& var, LLSD::Integer) const;
  99. virtual void assign(Impl*& var, LLSD::Real) const;
  100. virtual void assign(Impl*& var, const char*) const;
  101. virtual void assign(Impl*& var, const LLSD::String&) const;
  102. virtual void assign(Impl*& var, const LLSD::UUID&) const;
  103. virtual void assign(Impl*& var, const LLSD::Date&) const;
  104. virtual void assign(Impl*& var, const LLSD::URI&) const;
  105. virtual void assign(Impl*& var, const LLSD::Binary&) const;
  106. LL_INLINE virtual Boolean asBoolean() const { return false; }
  107. LL_INLINE virtual Integer asInteger() const { return 0; }
  108. LL_INLINE virtual Real asReal() const { return 0.0; }
  109. LL_INLINE virtual String asString() const { return std::string(); }
  110. LL_INLINE virtual UUID asUUID() const { return LLUUID(); }
  111. LL_INLINE virtual Date asDate() const { return LLDate(); }
  112. LL_INLINE virtual URI asURI() const { return LLURI(); }
  113. LL_INLINE virtual const Binary& asBinary() const
  114. {
  115. static const LLSD::Binary empty;
  116. return empty;
  117. }
  118. LL_INLINE virtual const String& asStringRef() const
  119. {
  120. static const std::string empty;
  121. return empty;
  122. }
  123. LL_INLINE virtual String asXMLRPCValue() const { return "<nil/>"; }
  124. LL_INLINE virtual bool has(const String&) const { return false; }
  125. LL_INLINE virtual LLSD get(const String&) const { return LLSD(); }
  126. LL_INLINE virtual bool has(const char*) const { return false; }
  127. LL_INLINE virtual LLSD get(const char*) const { return LLSD(); }
  128. LL_INLINE virtual LLSD getKeys() const { return LLSD::emptyArray(); }
  129. LL_INLINE virtual void erase(const String&) {}
  130. LL_INLINE virtual const LLSD& ref(const String&) const { return undef(); }
  131. LL_INLINE virtual size_t size() const { return 0; }
  132. LL_INLINE virtual LLSD get(size_t) const { return LLSD(); }
  133. LL_INLINE virtual void erase(size_t) {}
  134. LL_INLINE virtual const LLSD& ref(size_t) const { return undef(); }
  135. LL_INLINE virtual LLSD::map_const_iterator beginMap() const
  136. {
  137. return sEmptyMap.end();
  138. }
  139. LL_INLINE virtual LLSD::map_const_iterator endMap() const
  140. {
  141. return sEmptyMap.end();
  142. }
  143. LL_INLINE virtual LLSD::map_const_iterator find(const String&) const
  144. {
  145. return sEmptyMap.end();
  146. }
  147. LL_INLINE virtual LLSD::map_const_iterator find(const char*) const
  148. {
  149. return sEmptyMap.end();
  150. }
  151. LL_INLINE virtual LLSD::array_const_iterator beginArray() const
  152. {
  153. return endArray();
  154. }
  155. LL_INLINE virtual LLSD::array_const_iterator endArray() const
  156. {
  157. static const std::vector<LLSD> empty;
  158. return empty.end();
  159. }
  160. public:
  161. static const LLSD& undef();
  162. private:
  163. LLAtomicU32 mUseCount;
  164. static std::map<String, LLSD> sEmptyMap;
  165. };
  166. //static
  167. std::map<LLSD::String, LLSD> LLSD::Impl::sEmptyMap;
  168. #ifdef NAME_UNNAMED_NAMESPACE
  169. namespace LLSDUnnamedNamespace
  170. #else
  171. namespace
  172. #endif
  173. {
  174. // This class handles most of the work for a subclass of Impl for a given
  175. // simple data type. Subclasses of this provide the conversion functions
  176. // and a constructor.
  177. template<LLSD::Type T, class Data, class DataRef = Data>
  178. class ImplBase : public LLSD::Impl
  179. {
  180. public:
  181. typedef ImplBase Base;
  182. LL_INLINE ImplBase(DataRef value)
  183. : mValue(value)
  184. {
  185. }
  186. LL_INLINE virtual LLSD::Type type() const { return T; }
  187. using LLSD::Impl::assign; // Unhiding base class virtuals...
  188. LL_INLINE virtual void assign(LLSD::Impl*& var, DataRef value) const
  189. {
  190. if (shared())
  191. {
  192. Impl::assign(var, value);
  193. }
  194. else
  195. {
  196. const_cast<ImplBase*>(this)->mValue = value;
  197. }
  198. }
  199. protected:
  200. Data mValue;
  201. };
  202. class ImplBoolean final : public ImplBase<LLSD::TypeBoolean, LLSD::Boolean>
  203. {
  204. public:
  205. LL_INLINE ImplBoolean(LLSD::Boolean v)
  206. : Base(v)
  207. {
  208. }
  209. LL_INLINE LLSD::Boolean asBoolean() const override { return mValue; }
  210. LL_INLINE LLSD::Integer asInteger() const override { return mValue ? 1 : 0; }
  211. LL_INLINE LLSD::Real asReal() const override { return mValue ? 1 : 0; }
  212. // *NOTE: The reason that false is not converted to "false" is because
  213. // that would break roundtripping, such as, for example;
  214. // LLSD(false).asString().asBoolean()
  215. // There are many reasons for wanting LLSD("false").asBoolean() to be
  216. // true such as "everything else seems to work that way".
  217. LL_INLINE LLSD::String asString() const override
  218. {
  219. return mValue ? "true" : "";
  220. }
  221. LL_INLINE LLSD::String asXMLRPCValue() const override
  222. {
  223. return mValue ? "<boolean>1</boolean>" : "<boolean>0</boolean>";
  224. }
  225. };
  226. class ImplInteger final : public ImplBase<LLSD::TypeInteger, LLSD::Integer>
  227. {
  228. public:
  229. LL_INLINE ImplInteger(LLSD::Integer v)
  230. : Base(v)
  231. {
  232. }
  233. LL_INLINE LLSD::Boolean asBoolean() const override { return mValue != 0; }
  234. LL_INLINE LLSD::Integer asInteger() const override { return mValue; }
  235. LL_INLINE LLSD::Real asReal() const override { return mValue; }
  236. LL_INLINE LLSD::String asString() const override
  237. {
  238. return llformat("%d", mValue);
  239. }
  240. LL_INLINE LLSD::String asXMLRPCValue() const override
  241. {
  242. return llformat("<int>%d</int>", mValue);
  243. }
  244. };
  245. class ImplReal final : public ImplBase<LLSD::TypeReal, LLSD::Real>
  246. {
  247. public:
  248. LL_INLINE ImplReal(LLSD::Real v)
  249. : Base(v)
  250. {
  251. }
  252. LL_INLINE LLSD::Boolean asBoolean() const override
  253. {
  254. return !llisnan(mValue) && mValue != 0.0;
  255. }
  256. LL_INLINE LLSD::Integer asInteger() const override
  257. {
  258. return llisnan(mValue) ? 0 : (LLSD::Integer)mValue;
  259. }
  260. LL_INLINE LLSD::Real asReal() const override { return mValue; }
  261. LL_INLINE LLSD::String asString() const override
  262. {
  263. return llformat("%lg", mValue);
  264. }
  265. LL_INLINE LLSD::String asXMLRPCValue() const override
  266. {
  267. return llformat("<double>%lg</double>", mValue);
  268. }
  269. };
  270. class ImplString final : public ImplBase<LLSD::TypeString, LLSD::String,
  271. const LLSD::String&>
  272. {
  273. public:
  274. LL_INLINE ImplString(const LLSD::String& v)
  275. : Base(v)
  276. {
  277. }
  278. using LLSD::Impl::assign; // Unhiding base class virtuals...
  279. LL_INLINE void assign(LLSD::Impl*& var, const char* value)
  280. {
  281. if (shared())
  282. {
  283. Impl::assign(var, value);
  284. }
  285. else
  286. {
  287. mValue = value;
  288. }
  289. }
  290. LL_INLINE LLSD::Boolean asBoolean() const override { return !mValue.empty(); }
  291. LL_INLINE LLSD::Integer asInteger() const override
  292. {
  293. // This must treat "1.23" not as an error, but as a number, which
  294. // is then truncated down to an integer. Hence, this code does not
  295. // call std::istringstream::operator>>(int&), which would not
  296. // consume the ".23" portion.
  297. return (int)asReal();
  298. }
  299. LL_INLINE LLSD::Real asReal() const override
  300. {
  301. F64 v = 0.0;
  302. std::istringstream i_stream(mValue);
  303. i_stream >> v;
  304. // We would probably like to ignore all trailing whitespace as
  305. // well, but for now, simply eat the next character, and make sure
  306. // we reached the end of the string.
  307. return i_stream.get() == EOF ? v : 0.0;
  308. }
  309. LL_INLINE LLSD::String asString() const override { return mValue; }
  310. LL_INLINE LLSD::UUID asUUID() const override { return LLUUID(mValue); }
  311. LL_INLINE LLSD::Date asDate() const override { return LLDate(mValue); }
  312. LL_INLINE LLSD::URI asURI() const override { return LLURI(mValue); }
  313. LL_INLINE size_t size() const override { return mValue.size(); }
  314. LL_INLINE const LLSD::String& asStringRef() const override
  315. {
  316. return mValue;
  317. }
  318. LL_INLINE LLSD::String asXMLRPCValue() const override
  319. {
  320. return "<string>" + LLStringFn::xml_encode(mValue) + "</string>";
  321. }
  322. };
  323. class ImplUUID final : public ImplBase<LLSD::TypeUUID, LLSD::UUID,
  324. const LLSD::UUID&>
  325. {
  326. public:
  327. LL_INLINE ImplUUID(const LLSD::UUID& v)
  328. : Base(v)
  329. {
  330. }
  331. LL_INLINE LLSD::String asString() const override { return mValue.asString(); }
  332. LL_INLINE LLSD::UUID asUUID() const override { return mValue; }
  333. LL_INLINE LLSD::String asXMLRPCValue() const override
  334. {
  335. return "<string>" + mValue.asString() + "</string>";
  336. }
  337. };
  338. class ImplDate final : public ImplBase<LLSD::TypeDate, LLSD::Date,
  339. const LLSD::Date&>
  340. {
  341. public:
  342. LL_INLINE ImplDate(const LLSD::Date& v)
  343. : ImplBase<LLSD::TypeDate, LLSD::Date, const LLSD::Date&>(v)
  344. {
  345. }
  346. LL_INLINE LLSD::Integer asInteger() const override
  347. {
  348. return (LLSD::Integer)(mValue.secondsSinceEpoch());
  349. }
  350. LL_INLINE LLSD::Real asReal() const override
  351. {
  352. return mValue.secondsSinceEpoch();
  353. }
  354. LL_INLINE LLSD::String asString() const override { return mValue.asString(); }
  355. LL_INLINE LLSD::Date asDate() const override { return mValue; }
  356. LL_INLINE LLSD::String asXMLRPCValue() const override
  357. {
  358. return "<dateTime.iso8601>" + mValue.toHTTPDateString("%FT%T") +
  359. "</dateTime.iso8601>";
  360. }
  361. };
  362. class ImplURI final : public ImplBase<LLSD::TypeURI, LLSD::URI,
  363. const LLSD::URI&>
  364. {
  365. public:
  366. ImplURI(const LLSD::URI& v)
  367. : Base(v)
  368. {
  369. }
  370. LL_INLINE LLSD::String asString() const override { return mValue.asString(); }
  371. LL_INLINE LLSD::URI asURI() const override { return mValue; }
  372. LL_INLINE LLSD::String asXMLRPCValue() const override
  373. {
  374. return "<string>" + LLStringFn::xml_encode(mValue.asString()) +
  375. "</string>";
  376. }
  377. };
  378. class ImplBinary final : public ImplBase<LLSD::TypeBinary, LLSD::Binary,
  379. const LLSD::Binary&>
  380. {
  381. public:
  382. ImplBinary(const LLSD::Binary& v)
  383. : Base(v)
  384. {
  385. }
  386. LL_INLINE const LLSD::Binary& asBinary() const override
  387. {
  388. return mValue;
  389. }
  390. LL_INLINE LLSD::String asXMLRPCValue() const override
  391. {
  392. return "<base64>" +
  393. LLBase64::encode((const char*)mValue.data(),
  394. mValue.size()) + "</base64>";
  395. }
  396. };
  397. class ImplMap final : public LLSD::Impl
  398. {
  399. private:
  400. typedef std::map<LLSD::String, LLSD> DataMap;
  401. protected:
  402. LL_INLINE ImplMap(const DataMap& data)
  403. : mData(data)
  404. {
  405. }
  406. public:
  407. ImplMap() = default;
  408. ImplMap& makeMap(LLSD::Impl*&) const override;
  409. LL_INLINE LLSD::Type type() const override { return LLSD::TypeMap; }
  410. LL_INLINE LLSD::Boolean asBoolean() const override { return !mData.empty(); }
  411. LLSD::String asXMLRPCValue() const override
  412. {
  413. std::ostringstream os;
  414. os << "<struct>";
  415. for (const auto& it : mData)
  416. {
  417. os << "<member><name>" << LLStringFn::xml_encode(it.first)
  418. << "</name>" << it.second.asXMLRPCValue() << "</member>";
  419. }
  420. os << "</struct>";
  421. return os.str();
  422. }
  423. bool has(const LLSD::String&) const override;
  424. bool has(const char*) const override;
  425. using LLSD::Impl::get; // Unhiding LLSD::get()
  426. using LLSD::Impl::erase; // Unhiding LLSD::erase()
  427. using LLSD::Impl::ref; // Unhiding LLSD::ref()
  428. LLSD get(const LLSD::String&) const override;
  429. LLSD get(const char*) const override;
  430. LLSD getKeys() const override;
  431. void insert(const LLSD::String& k, const LLSD& v);
  432. void erase(const LLSD::String&) override;
  433. const LLSD& ref(const LLSD::String&) const override;
  434. LLSD& ref(const LLSD::String&);
  435. LL_INLINE size_t size() const override { return mData.size(); }
  436. LL_INLINE LLSD::map_iterator beginMap() { return mData.begin(); }
  437. LL_INLINE LLSD::map_iterator endMap() { return mData.end(); }
  438. LL_INLINE LLSD::map_const_iterator beginMap() const override
  439. {
  440. return mData.begin();
  441. }
  442. LL_INLINE LLSD::map_const_iterator endMap() const override
  443. {
  444. return mData.end();
  445. }
  446. LL_INLINE LLSD::map_const_iterator find(const LLSD::String& k) const override
  447. {
  448. return mData.find(k);
  449. }
  450. LL_INLINE LLSD::map_const_iterator find(const char* k) const override
  451. {
  452. return mData.find(k);
  453. }
  454. private:
  455. DataMap mData;
  456. };
  457. ImplMap& ImplMap::makeMap(LLSD::Impl*& var) const
  458. {
  459. if (shared())
  460. {
  461. ImplMap* i = new ImplMap(mData);
  462. Impl::assign(var, i);
  463. return *i;
  464. }
  465. // Only we have determined that 'this' is already an ImplMap that is
  466. // not shared do we cast away its const-ness
  467. return const_cast<ImplMap&>(*this);
  468. }
  469. LL_INLINE bool ImplMap::has(const LLSD::String& k) const
  470. {
  471. DataMap::const_iterator i = mData.find(k);
  472. return i != mData.end();
  473. }
  474. LL_INLINE LLSD ImplMap::get(const LLSD::String& k) const
  475. {
  476. DataMap::const_iterator i = mData.find(k);
  477. return i != mData.end() ? i->second : LLSD();
  478. }
  479. LL_INLINE bool ImplMap::has(const char* k) const
  480. {
  481. DataMap::const_iterator i = mData.find(k);
  482. return i != mData.end();
  483. }
  484. LL_INLINE LLSD ImplMap::get(const char* k) const
  485. {
  486. DataMap::const_iterator i = mData.find(k);
  487. return i != mData.end() ? i->second : LLSD();
  488. }
  489. LLSD ImplMap::getKeys() const
  490. {
  491. LLSD keys = LLSD::emptyArray();
  492. for (DataMap::const_iterator iter = mData.begin(), end = mData.end();
  493. iter != end; ++iter)
  494. {
  495. keys.append(iter->first);
  496. }
  497. return keys;
  498. }
  499. LL_INLINE void ImplMap::insert(const LLSD::String& k, const LLSD& v)
  500. {
  501. mData.insert(DataMap::value_type(k, v));
  502. }
  503. LL_INLINE void ImplMap::erase(const LLSD::String& k)
  504. {
  505. mData.erase(k);
  506. }
  507. LL_INLINE LLSD& ImplMap::ref(const LLSD::String& k)
  508. {
  509. return mData[k];
  510. }
  511. LL_INLINE const LLSD& ImplMap::ref(const LLSD::String& k) const
  512. {
  513. DataMap::const_iterator i = mData.find(k);
  514. return i != mData.end() ? i->second : undef();
  515. }
  516. class ImplArray final : public LLSD::Impl
  517. {
  518. private:
  519. typedef std::vector<LLSD> DataVector;
  520. DataVector mData;
  521. protected:
  522. ImplArray(const DataVector& data)
  523. : mData(data)
  524. {
  525. }
  526. public:
  527. ImplArray() = default;
  528. ImplArray& makeArray(Impl*&) const override;
  529. LL_INLINE LLSD::Type type() const override { return LLSD::TypeArray; }
  530. LL_INLINE LLSD::Boolean asBoolean() const override { return !mData.empty(); }
  531. LLSD::String asXMLRPCValue() const override
  532. {
  533. std::ostringstream os;
  534. os << "<array><data>";
  535. for (const auto& it : mData)
  536. {
  537. os << it.asXMLRPCValue();
  538. }
  539. os << "</data></array>";
  540. return os.str();
  541. }
  542. using LLSD::Impl::get; // Unhiding get(LLSD::String)
  543. using LLSD::Impl::erase; // Unhiding erase(LLSD::String)
  544. using LLSD::Impl::ref; // Unhiding ref(LLSD::String)
  545. LL_INLINE size_t size() const override { return mData.size(); }
  546. LLSD get(size_t i) const override;
  547. void set(size_t i, const LLSD& v);
  548. void insert(size_t i, const LLSD& v);
  549. void erase(size_t i) override;
  550. LL_INLINE LLSD& append(const LLSD& v)
  551. {
  552. mData.push_back(v);
  553. return mData.back();
  554. }
  555. LLSD& ref(size_t);
  556. const LLSD& ref(size_t i) const override;
  557. LL_INLINE LLSD::array_iterator beginArray()
  558. {
  559. return mData.begin();
  560. }
  561. LL_INLINE LLSD::array_iterator endArray() { return mData.end(); }
  562. LL_INLINE LLSD::reverse_array_iterator rbeginArray()
  563. {
  564. return mData.rbegin();
  565. }
  566. LL_INLINE LLSD::reverse_array_iterator rendArray() { return mData.rend(); }
  567. LL_INLINE LLSD::array_const_iterator beginArray() const override
  568. {
  569. return mData.begin();
  570. }
  571. LL_INLINE LLSD::array_const_iterator endArray() const override
  572. {
  573. return mData.end();
  574. }
  575. };
  576. ImplArray& ImplArray::makeArray(Impl*& var) const
  577. {
  578. if (shared())
  579. {
  580. ImplArray* i = new ImplArray(mData);
  581. Impl::assign(var, i);
  582. return *i;
  583. }
  584. // Only once we have determined that 'this' is already an ImplArray
  585. // that is not shared do we cast away its const-ness
  586. return const_cast<ImplArray&>(*this);
  587. }
  588. LLSD ImplArray::get(size_t i) const
  589. {
  590. NEGATIVE_RETURN(i, LLSD());
  591. return i < mData.size() ? mData[i] : LLSD();
  592. }
  593. void ImplArray::set(size_t i, const LLSD& v)
  594. {
  595. NEGATIVE_EXIT(i);
  596. if (i >= mData.size())
  597. {
  598. mData.resize(i + 1);
  599. }
  600. mData[i] = v;
  601. }
  602. void ImplArray::insert(size_t i, const LLSD& v)
  603. {
  604. NEGATIVE_EXIT(i);
  605. if (i >= mData.size())
  606. {
  607. mData.resize(i + 1);
  608. }
  609. mData.insert(mData.begin() + i, v);
  610. }
  611. void ImplArray::erase(size_t i)
  612. {
  613. NEGATIVE_EXIT(i);
  614. if (i < mData.size())
  615. {
  616. mData.erase(mData.begin() + i);
  617. }
  618. }
  619. LLSD& ImplArray::ref(size_t i)
  620. {
  621. DataVector::size_type index = was_negative(i) ? 0 : i;
  622. if (index >= mData.size())
  623. {
  624. mData.resize(index + 1);
  625. }
  626. return mData[index];
  627. }
  628. const LLSD& ImplArray::ref(size_t i) const
  629. {
  630. NEGATIVE_RETURN(i, undef());
  631. if (i >= mData.size())
  632. {
  633. return undef();
  634. }
  635. return mData[i];
  636. }
  637. }
  638. LLSD::Impl::Impl()
  639. : mUseCount(0)
  640. {
  641. }
  642. LLSD::Impl::Impl(StaticAllocationMarker)
  643. : mUseCount(0)
  644. {
  645. }
  646. //static
  647. void LLSD::Impl::reset(Impl*& var, Impl* impl)
  648. {
  649. if (impl == var)
  650. {
  651. return;
  652. }
  653. if (impl && impl->mUseCount != STATIC_USAGE_COUNT)
  654. {
  655. ++impl->mUseCount;
  656. }
  657. if (var && var->mUseCount != STATIC_USAGE_COUNT && --var->mUseCount == 0)
  658. {
  659. delete var;
  660. }
  661. var = impl;
  662. }
  663. //static
  664. LLSD::Impl& LLSD::Impl::safe(Impl* impl)
  665. {
  666. static Impl theUndefined(STATIC_USAGE_COUNT);
  667. return impl ? *impl : theUndefined;
  668. }
  669. //virtual
  670. ImplMap& LLSD::Impl::makeMap(Impl*& var) const
  671. {
  672. ImplMap* im = new ImplMap;
  673. reset(var, im);
  674. return *im;
  675. }
  676. //virtual
  677. ImplArray& LLSD::Impl::makeArray(Impl*& var) const
  678. {
  679. ImplArray* ia = new ImplArray;
  680. reset(var, ia);
  681. return *ia;
  682. }
  683. //virtual
  684. void LLSD::Impl::assign(Impl*& var, const Impl* other)
  685. {
  686. reset(var, const_cast<Impl*>(other));
  687. }
  688. //virtual
  689. void LLSD::Impl::assignUndefined(Impl*& var)
  690. {
  691. reset(var, 0);
  692. }
  693. //virtual
  694. void LLSD::Impl::assign(Impl*& var, LLSD::Boolean v) const
  695. {
  696. reset(var, new ImplBoolean(v));
  697. }
  698. //virtual
  699. void LLSD::Impl::assign(Impl*& var, LLSD::Integer v) const
  700. {
  701. reset(var, new ImplInteger(v));
  702. }
  703. //virtual
  704. void LLSD::Impl::assign(Impl*& var, LLSD::Real v) const
  705. {
  706. reset(var, new ImplReal(v));
  707. }
  708. //virtual
  709. void LLSD::Impl::assign(Impl*& var, const char* v) const
  710. {
  711. reset(var, new ImplString(v));
  712. }
  713. //virtual
  714. void LLSD::Impl::assign(Impl*& var, const LLSD::String& v) const
  715. {
  716. reset(var, new ImplString(v));
  717. }
  718. //virtual
  719. void LLSD::Impl::assign(Impl*& var, const LLSD::UUID& v) const
  720. {
  721. reset(var, new ImplUUID(v));
  722. }
  723. //virtual
  724. void LLSD::Impl::assign(Impl*& var, const LLSD::Date& v) const
  725. {
  726. reset(var, new ImplDate(v));
  727. }
  728. //virtual
  729. void LLSD::Impl::assign(Impl*& var, const LLSD::URI& v) const
  730. {
  731. reset(var, new ImplURI(v));
  732. }
  733. //virtual
  734. void LLSD::Impl::assign(Impl*& var, const LLSD::Binary& v) const
  735. {
  736. reset(var, new ImplBinary(v));
  737. }
  738. //static
  739. const LLSD& LLSD::Impl::undef()
  740. {
  741. static const LLSD immutableUndefined;
  742. return immutableUndefined;
  743. }
  744. #ifdef NAME_UNNAMED_NAMESPACE
  745. namespace LLSDUnnamedNamespace
  746. #else
  747. namespace
  748. #endif
  749. {
  750. LL_INLINE const LLSD::Impl& safe(LLSD::Impl* impl)
  751. {
  752. return LLSD::Impl::safe(impl);
  753. }
  754. LL_INLINE ImplMap& makeMap(LLSD::Impl*& var)
  755. {
  756. return safe(var).makeMap(var);
  757. }
  758. LL_INLINE ImplArray& makeArray(LLSD::Impl*& var)
  759. {
  760. return safe(var).makeArray(var);
  761. }
  762. }
  763. LLSD::LLSD()
  764. : impl(NULL)
  765. {
  766. }
  767. LLSD::~LLSD()
  768. {
  769. Impl::reset(impl, NULL);
  770. }
  771. LLSD::LLSD(const LLSD& other)
  772. : impl(NULL)
  773. {
  774. assign(other);
  775. }
  776. void LLSD::assign(const LLSD& other)
  777. {
  778. Impl::assign(impl, other.impl);
  779. }
  780. void LLSD::clear()
  781. {
  782. Impl::assignUndefined(impl);
  783. }
  784. LLSD::Type LLSD::type() const
  785. {
  786. return safe(impl).type();
  787. }
  788. // Scalar Constructors
  789. LLSD::LLSD(Boolean v)
  790. : impl(NULL)
  791. {
  792. assign(v);
  793. }
  794. LLSD::LLSD(Integer v)
  795. : impl(NULL)
  796. {
  797. assign(v);
  798. }
  799. LLSD::LLSD(Real v)
  800. : impl(NULL)
  801. {
  802. assign(v);
  803. }
  804. LLSD::LLSD(const UUID& v)
  805. : impl(NULL)
  806. {
  807. assign(v);
  808. }
  809. LLSD::LLSD(const String& v)
  810. : impl(NULL)
  811. {
  812. assign(v);
  813. }
  814. LLSD::LLSD(const Date& v)
  815. : impl(NULL)
  816. {
  817. assign(v);
  818. }
  819. LLSD::LLSD(const URI& v)
  820. : impl(NULL)
  821. {
  822. assign(v);
  823. }
  824. LLSD::LLSD(const Binary& v)
  825. : impl(NULL)
  826. {
  827. assign(v);
  828. }
  829. // Scalar assignments
  830. void LLSD::assign(Boolean v)
  831. {
  832. safe(impl).assign(impl, v);
  833. }
  834. void LLSD::assign(Integer v)
  835. {
  836. safe(impl).assign(impl, v);
  837. }
  838. void LLSD::assign(Real v)
  839. {
  840. safe(impl).assign(impl, v);
  841. }
  842. void LLSD::assign(const String& v)
  843. {
  844. safe(impl).assign(impl, v);
  845. }
  846. void LLSD::assign(const UUID& v)
  847. {
  848. safe(impl).assign(impl, v);
  849. }
  850. void LLSD::assign(const Date& v)
  851. {
  852. safe(impl).assign(impl, v);
  853. }
  854. void LLSD::assign(const URI& v)
  855. {
  856. safe(impl).assign(impl, v);
  857. }
  858. void LLSD::assign(const Binary& v)
  859. {
  860. safe(impl).assign(impl, v);
  861. }
  862. // Scalar Accessors
  863. LLSD::Boolean LLSD::asBoolean() const
  864. {
  865. return safe(impl).asBoolean();
  866. }
  867. LLSD::Integer LLSD::asInteger() const
  868. {
  869. return safe(impl).asInteger();
  870. }
  871. LLSD::Real LLSD::asReal() const
  872. {
  873. return safe(impl).asReal();
  874. }
  875. LLSD::String LLSD::asString() const
  876. {
  877. return safe(impl).asString();
  878. }
  879. LLSD::UUID LLSD::asUUID() const
  880. {
  881. return safe(impl).asUUID();
  882. }
  883. LLSD::Date LLSD::asDate() const
  884. {
  885. return safe(impl).asDate();
  886. }
  887. LLSD::URI LLSD::asURI() const
  888. {
  889. return safe(impl).asURI();
  890. }
  891. const LLSD::Binary& LLSD::asBinary() const
  892. {
  893. return safe(impl).asBinary();
  894. }
  895. const LLSD::String& LLSD::asStringRef() const
  896. {
  897. return safe(impl).asStringRef();
  898. }
  899. LLSD::String LLSD::asXMLRPCValue() const
  900. {
  901. return "<value>" + safe(impl).asXMLRPCValue() + "</value>";
  902. }
  903. // const char* helpers
  904. LLSD::LLSD(const char* v)
  905. : impl(NULL)
  906. {
  907. assign(v);
  908. }
  909. void LLSD::assign(const char* v)
  910. {
  911. if (v)
  912. {
  913. safe(impl).assign(impl, v);
  914. }
  915. else
  916. {
  917. assign(std::string());
  918. }
  919. }
  920. LLSD LLSD::emptyMap()
  921. {
  922. LLSD v;
  923. makeMap(v.impl);
  924. return v;
  925. }
  926. bool LLSD::has(const String& k) const
  927. {
  928. return safe(impl).has(k);
  929. }
  930. LLSD LLSD::get(const String& k) const
  931. {
  932. return safe(impl).get(k);
  933. }
  934. bool LLSD::has(const char* k) const
  935. {
  936. return safe(impl).has(k);
  937. }
  938. LLSD LLSD::get(const char* k) const
  939. {
  940. return safe(impl).get(k);
  941. }
  942. LLSD LLSD::getKeys() const
  943. {
  944. return safe(impl).getKeys();
  945. }
  946. void LLSD::erase(const String& k)
  947. {
  948. makeMap(impl).erase(k);
  949. }
  950. void LLSD::insert(const String& k, const LLSD& v)
  951. {
  952. makeMap(impl).insert(k, v);
  953. }
  954. LLSD& LLSD::with(const char* k, const LLSD& v)
  955. {
  956. makeMap(impl).insert(k, v);
  957. return *this;
  958. }
  959. LLSD& LLSD::operator[](const String& k)
  960. {
  961. return makeMap(impl).ref(k);
  962. }
  963. const LLSD& LLSD::operator[](const String& k) const
  964. {
  965. return safe(impl).ref(k);
  966. }
  967. LLSD LLSD::emptyArray()
  968. {
  969. LLSD v;
  970. makeArray(v.impl);
  971. return v;
  972. }
  973. size_t LLSD::size() const
  974. {
  975. return safe(impl).size();
  976. }
  977. LLSD LLSD::get(size_t i) const
  978. {
  979. return safe(impl).get(i);
  980. }
  981. void LLSD::set(size_t i, const LLSD& v)
  982. {
  983. makeArray(impl).set(i, v);
  984. }
  985. void LLSD::insert(size_t i, const LLSD& v)
  986. {
  987. makeArray(impl).insert(i, v);
  988. }
  989. void LLSD::erase(size_t i)
  990. {
  991. makeArray(impl).erase(i);
  992. }
  993. LLSD& LLSD::with(size_t i, const LLSD& v)
  994. {
  995. makeArray(impl).insert(Integer(i), v);
  996. return *this;
  997. }
  998. LLSD& LLSD::append(const LLSD& v)
  999. {
  1000. return makeArray(impl).append(v);
  1001. }
  1002. LLSD& LLSD::operator[](size_t i)
  1003. {
  1004. return makeArray(impl).ref(i);
  1005. }
  1006. const LLSD& LLSD::operator[](size_t i) const
  1007. {
  1008. return safe(impl).ref(i);
  1009. }
  1010. LLSD::map_iterator LLSD::beginMap()
  1011. {
  1012. return makeMap(impl).beginMap();
  1013. }
  1014. LLSD::map_iterator LLSD::endMap()
  1015. {
  1016. return makeMap(impl).endMap();
  1017. }
  1018. LLSD::map_const_iterator LLSD::beginMap() const
  1019. {
  1020. return safe(impl).beginMap();
  1021. }
  1022. LLSD::map_const_iterator LLSD::endMap() const
  1023. {
  1024. return safe(impl).endMap();
  1025. }
  1026. LLSD::map_const_iterator LLSD::find(const String& k) const
  1027. {
  1028. return safe(impl).find(k);
  1029. }
  1030. LLSD::map_const_iterator LLSD::find(const char* k) const
  1031. {
  1032. return safe(impl).find(k);
  1033. }
  1034. LLSD::array_iterator LLSD::beginArray()
  1035. {
  1036. return makeArray(impl).beginArray();
  1037. }
  1038. LLSD::array_iterator LLSD::endArray()
  1039. {
  1040. return makeArray(impl).endArray();
  1041. }
  1042. LLSD::array_const_iterator LLSD::beginArray() const
  1043. {
  1044. return safe(impl).beginArray();
  1045. }
  1046. LLSD::array_const_iterator LLSD::endArray() const
  1047. {
  1048. return safe(impl).endArray();
  1049. }
  1050. LLSD::reverse_array_iterator LLSD::rbeginArray()
  1051. {
  1052. return makeArray(impl).rbeginArray();
  1053. }
  1054. LLSD::reverse_array_iterator LLSD::rendArray()
  1055. {
  1056. return makeArray(impl).rendArray();
  1057. }