/indra/llcommon/llsd.cpp

https://bitbucket.org/lindenlab/viewer-beta/ · C++ · 982 lines · 706 code · 193 blank · 83 comment · 45 complexity · 70ed38c4930ec45d26fa9af28ba7a347 MD5 · raw file

  1. /**
  2. * @file llsd.cpp
  3. * @brief LLSD flexible data system
  4. *
  5. * $LicenseInfo:firstyear=2005&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. // Must turn on conditional declarations in header file so definitions end up
  27. // with proper linkage.
  28. #define LLSD_DEBUG_INFO
  29. #include "linden_common.h"
  30. #include "llsd.h"
  31. #include "llerror.h"
  32. #include "../llmath/llmath.h"
  33. #include "llformat.h"
  34. #include "llsdserialize.h"
  35. #include "stringize.h"
  36. #ifndef LL_RELEASE_FOR_DOWNLOAD
  37. #define NAME_UNNAMED_NAMESPACE
  38. #endif
  39. #ifdef NAME_UNNAMED_NAMESPACE
  40. namespace LLSDUnnamedNamespace
  41. #else
  42. namespace
  43. #endif
  44. {
  45. class ImplMap;
  46. class ImplArray;
  47. }
  48. #ifdef NAME_UNNAMED_NAMESPACE
  49. using namespace LLSDUnnamedNamespace;
  50. #endif
  51. namespace llsd
  52. {
  53. // statics
  54. S32 sLLSDAllocationCount = 0;
  55. S32 sLLSDNetObjects = 0;
  56. } // namespace llsd
  57. #define ALLOC_LLSD_OBJECT { llsd::sLLSDNetObjects++; llsd::sLLSDAllocationCount++; }
  58. #define FREE_LLSD_OBJECT { llsd::sLLSDNetObjects--; }
  59. class LLSD::Impl
  60. /**< This class is the abstract base class of the implementation of LLSD
  61. It provides the reference counting implementation, and the default
  62. implementation of most methods for most data types. It also serves
  63. as a working implementation of the Undefined type.
  64. */
  65. {
  66. protected:
  67. Impl();
  68. enum StaticAllocationMarker { STATIC_USAGE_COUNT = 0xFFFFFFFF };
  69. Impl(StaticAllocationMarker);
  70. ///< This constructor is used for static objects and causes the
  71. // suppresses adjusting the debugging counters when they are
  72. // finally initialized.
  73. virtual ~Impl();
  74. bool shared() const { return (mUseCount > 1) && (mUseCount != STATIC_USAGE_COUNT); }
  75. U32 mUseCount;
  76. public:
  77. static void reset(Impl*& var, Impl* impl);
  78. ///< safely set var to refer to the new impl (possibly shared)
  79. static Impl& safe( Impl*);
  80. static const Impl& safe(const Impl*);
  81. ///< since a NULL Impl* is used for undefined, this ensures there is
  82. // always an object you call virtual member functions on
  83. virtual ImplMap& makeMap(Impl*& var);
  84. virtual ImplArray& makeArray(Impl*& var);
  85. ///< sure var is a modifiable, non-shared map or array
  86. virtual LLSD::Type type() const { return LLSD::TypeUndefined; }
  87. static void assignUndefined(LLSD::Impl*& var);
  88. static void assign(LLSD::Impl*& var, const LLSD::Impl* other);
  89. virtual void assign(Impl*& var, LLSD::Boolean);
  90. virtual void assign(Impl*& var, LLSD::Integer);
  91. virtual void assign(Impl*& var, LLSD::Real);
  92. virtual void assign(Impl*& var, const LLSD::String&);
  93. virtual void assign(Impl*& var, const LLSD::UUID&);
  94. virtual void assign(Impl*& var, const LLSD::Date&);
  95. virtual void assign(Impl*& var, const LLSD::URI&);
  96. virtual void assign(Impl*& var, const LLSD::Binary&);
  97. ///< If the receiver is the right type and unshared, these are simple
  98. // data assignments, othewise the default implementation handless
  99. // constructing the proper Impl subclass
  100. virtual Boolean asBoolean() const { return false; }
  101. virtual Integer asInteger() const { return 0; }
  102. virtual Real asReal() const { return 0.0; }
  103. virtual String asString() const { return std::string(); }
  104. virtual UUID asUUID() const { return LLUUID(); }
  105. virtual Date asDate() const { return LLDate(); }
  106. virtual URI asURI() const { return LLURI(); }
  107. virtual Binary asBinary() const { return std::vector<U8>(); }
  108. virtual bool has(const String&) const { return false; }
  109. virtual LLSD get(const String&) const { return LLSD(); }
  110. virtual void erase(const String&) { }
  111. virtual const LLSD& ref(const String&) const{ return undef(); }
  112. virtual int size() const { return 0; }
  113. virtual LLSD get(Integer) const { return LLSD(); }
  114. virtual void erase(Integer) { }
  115. virtual const LLSD& ref(Integer) const { return undef(); }
  116. virtual LLSD::map_const_iterator beginMap() const { return endMap(); }
  117. virtual LLSD::map_const_iterator endMap() const { static const std::map<String, LLSD> empty; return empty.end(); }
  118. virtual LLSD::array_const_iterator beginArray() const { return endArray(); }
  119. virtual LLSD::array_const_iterator endArray() const { static const std::vector<LLSD> empty; return empty.end(); }
  120. virtual void dumpStats() const;
  121. virtual void calcStats(S32 type_counts[], S32 share_counts[]) const;
  122. // Container subclasses contain LLSD objects, rather than directly
  123. // containing Impl objects. This helper forwards through LLSD.
  124. void calcStats(const LLSD& llsd, S32 type_counts[], S32 share_counts[]) const
  125. {
  126. safe(llsd.impl).calcStats(type_counts, share_counts);
  127. }
  128. static const Impl& getImpl(const LLSD& llsd) { return safe(llsd.impl); }
  129. static Impl& getImpl(LLSD& llsd) { return safe(llsd.impl); }
  130. static const LLSD& undef();
  131. static U32 sAllocationCount;
  132. static U32 sOutstandingCount;
  133. };
  134. #ifdef NAME_UNNAMED_NAMESPACE
  135. namespace LLSDUnnamedNamespace
  136. #else
  137. namespace
  138. #endif
  139. {
  140. template<LLSD::Type T, class Data, class DataRef = Data>
  141. class ImplBase : public LLSD::Impl
  142. ///< This class handles most of the work for a subclass of Impl
  143. // for a given simple data type. Subclasses of this provide the
  144. // conversion functions and a constructor.
  145. {
  146. protected:
  147. Data mValue;
  148. typedef ImplBase Base;
  149. public:
  150. ImplBase(DataRef value) : mValue(value) { }
  151. virtual LLSD::Type type() const { return T; }
  152. using LLSD::Impl::assign; // Unhiding base class virtuals...
  153. virtual void assign(LLSD::Impl*& var, DataRef value) {
  154. if (shared())
  155. {
  156. Impl::assign(var, value);
  157. }
  158. else
  159. {
  160. mValue = value;
  161. }
  162. }
  163. };
  164. class ImplBoolean
  165. : public ImplBase<LLSD::TypeBoolean, LLSD::Boolean>
  166. {
  167. public:
  168. ImplBoolean(LLSD::Boolean v) : Base(v) { }
  169. virtual LLSD::Boolean asBoolean() const { return mValue; }
  170. virtual LLSD::Integer asInteger() const { return mValue ? 1 : 0; }
  171. virtual LLSD::Real asReal() const { return mValue ? 1 : 0; }
  172. virtual LLSD::String asString() const;
  173. };
  174. LLSD::String ImplBoolean::asString() const
  175. // *NOTE: The reason that false is not converted to "false" is
  176. // because that would break roundtripping,
  177. // e.g. LLSD(false).asString().asBoolean(). There are many
  178. // reasons for wanting LLSD("false").asBoolean() == true, such
  179. // as "everything else seems to work that way".
  180. { return mValue ? "true" : ""; }
  181. class ImplInteger
  182. : public ImplBase<LLSD::TypeInteger, LLSD::Integer>
  183. {
  184. public:
  185. ImplInteger(LLSD::Integer v) : Base(v) { }
  186. virtual LLSD::Boolean asBoolean() const { return mValue != 0; }
  187. virtual LLSD::Integer asInteger() const { return mValue; }
  188. virtual LLSD::Real asReal() const { return mValue; }
  189. virtual LLSD::String asString() const;
  190. };
  191. LLSD::String ImplInteger::asString() const
  192. { return llformat("%d", mValue); }
  193. class ImplReal
  194. : public ImplBase<LLSD::TypeReal, LLSD::Real>
  195. {
  196. public:
  197. ImplReal(LLSD::Real v) : Base(v) { }
  198. virtual LLSD::Boolean asBoolean() const;
  199. virtual LLSD::Integer asInteger() const;
  200. virtual LLSD::Real asReal() const { return mValue; }
  201. virtual LLSD::String asString() const;
  202. };
  203. LLSD::Boolean ImplReal::asBoolean() const
  204. { return !llisnan(mValue) && mValue != 0.0; }
  205. LLSD::Integer ImplReal::asInteger() const
  206. { return !llisnan(mValue) ? (LLSD::Integer)mValue : 0; }
  207. LLSD::String ImplReal::asString() const
  208. { return llformat("%lg", mValue); }
  209. class ImplString
  210. : public ImplBase<LLSD::TypeString, LLSD::String, const LLSD::String&>
  211. {
  212. public:
  213. ImplString(const LLSD::String& v) : Base(v) { }
  214. virtual LLSD::Boolean asBoolean() const { return !mValue.empty(); }
  215. virtual LLSD::Integer asInteger() const;
  216. virtual LLSD::Real asReal() const;
  217. virtual LLSD::String asString() const { return mValue; }
  218. virtual LLSD::UUID asUUID() const { return LLUUID(mValue); }
  219. virtual LLSD::Date asDate() const { return LLDate(mValue); }
  220. virtual LLSD::URI asURI() const { return LLURI(mValue); }
  221. };
  222. LLSD::Integer ImplString::asInteger() const
  223. {
  224. // This must treat "1.23" not as an error, but as a number, which is
  225. // then truncated down to an integer. Hence, this code doesn't call
  226. // std::istringstream::operator>>(int&), which would not consume the
  227. // ".23" portion.
  228. return (int)asReal();
  229. }
  230. LLSD::Real ImplString::asReal() const
  231. {
  232. F64 v = 0.0;
  233. std::istringstream i_stream(mValue);
  234. i_stream >> v;
  235. // we would probably like to ignore all trailing whitespace as
  236. // well, but for now, simply eat the next character, and make
  237. // sure we reached the end of the string.
  238. // *NOTE: gcc 2.95 does not generate an eof() event on the
  239. // stream operation above, so we manually get here to force it
  240. // across platforms.
  241. int c = i_stream.get();
  242. return ((EOF ==c) ? v : 0.0);
  243. }
  244. class ImplUUID
  245. : public ImplBase<LLSD::TypeUUID, LLSD::UUID, const LLSD::UUID&>
  246. {
  247. public:
  248. ImplUUID(const LLSD::UUID& v) : Base(v) { }
  249. virtual LLSD::String asString() const{ return mValue.asString(); }
  250. virtual LLSD::UUID asUUID() const { return mValue; }
  251. };
  252. class ImplDate
  253. : public ImplBase<LLSD::TypeDate, LLSD::Date, const LLSD::Date&>
  254. {
  255. public:
  256. ImplDate(const LLSD::Date& v)
  257. : ImplBase<LLSD::TypeDate, LLSD::Date, const LLSD::Date&>(v)
  258. { }
  259. virtual LLSD::Integer asInteger() const
  260. {
  261. return (LLSD::Integer)(mValue.secondsSinceEpoch());
  262. }
  263. virtual LLSD::Real asReal() const
  264. {
  265. return mValue.secondsSinceEpoch();
  266. }
  267. virtual LLSD::String asString() const{ return mValue.asString(); }
  268. virtual LLSD::Date asDate() const { return mValue; }
  269. };
  270. class ImplURI
  271. : public ImplBase<LLSD::TypeURI, LLSD::URI, const LLSD::URI&>
  272. {
  273. public:
  274. ImplURI(const LLSD::URI& v) : Base(v) { }
  275. virtual LLSD::String asString() const{ return mValue.asString(); }
  276. virtual LLSD::URI asURI() const { return mValue; }
  277. };
  278. class ImplBinary
  279. : public ImplBase<LLSD::TypeBinary, LLSD::Binary, const LLSD::Binary&>
  280. {
  281. public:
  282. ImplBinary(const LLSD::Binary& v) : Base(v) { }
  283. virtual LLSD::Binary asBinary() const{ return mValue; }
  284. };
  285. class ImplMap : public LLSD::Impl
  286. {
  287. private:
  288. typedef std::map<LLSD::String, LLSD> DataMap;
  289. DataMap mData;
  290. protected:
  291. ImplMap(const DataMap& data) : mData(data) { }
  292. public:
  293. ImplMap() { }
  294. virtual ImplMap& makeMap(LLSD::Impl*&);
  295. virtual LLSD::Type type() const { return LLSD::TypeMap; }
  296. virtual LLSD::Boolean asBoolean() const { return !mData.empty(); }
  297. virtual bool has(const LLSD::String&) const;
  298. using LLSD::Impl::get; // Unhiding get(LLSD::Integer)
  299. using LLSD::Impl::erase; // Unhiding erase(LLSD::Integer)
  300. using LLSD::Impl::ref; // Unhiding ref(LLSD::Integer)
  301. virtual LLSD get(const LLSD::String&) const;
  302. void insert(const LLSD::String& k, const LLSD& v);
  303. virtual void erase(const LLSD::String&);
  304. LLSD& ref(const LLSD::String&);
  305. virtual const LLSD& ref(const LLSD::String&) const;
  306. virtual int size() const { return mData.size(); }
  307. LLSD::map_iterator beginMap() { return mData.begin(); }
  308. LLSD::map_iterator endMap() { return mData.end(); }
  309. virtual LLSD::map_const_iterator beginMap() const { return mData.begin(); }
  310. virtual LLSD::map_const_iterator endMap() const { return mData.end(); }
  311. virtual void dumpStats() const;
  312. virtual void calcStats(S32 type_counts[], S32 share_counts[]) const;
  313. };
  314. ImplMap& ImplMap::makeMap(LLSD::Impl*& var)
  315. {
  316. if (shared())
  317. {
  318. ImplMap* i = new ImplMap(mData);
  319. Impl::assign(var, i);
  320. return *i;
  321. }
  322. else
  323. {
  324. return *this;
  325. }
  326. }
  327. bool ImplMap::has(const LLSD::String& k) const
  328. {
  329. DataMap::const_iterator i = mData.find(k);
  330. return i != mData.end();
  331. }
  332. LLSD ImplMap::get(const LLSD::String& k) const
  333. {
  334. DataMap::const_iterator i = mData.find(k);
  335. return (i != mData.end()) ? i->second : LLSD();
  336. }
  337. void ImplMap::insert(const LLSD::String& k, const LLSD& v)
  338. {
  339. mData.insert(DataMap::value_type(k, v));
  340. }
  341. void ImplMap::erase(const LLSD::String& k)
  342. {
  343. mData.erase(k);
  344. }
  345. LLSD& ImplMap::ref(const LLSD::String& k)
  346. {
  347. return mData[k];
  348. }
  349. const LLSD& ImplMap::ref(const LLSD::String& k) const
  350. {
  351. DataMap::const_iterator i = mData.lower_bound(k);
  352. if (i == mData.end() || mData.key_comp()(k, i->first))
  353. {
  354. return undef();
  355. }
  356. return i->second;
  357. }
  358. void ImplMap::dumpStats() const
  359. {
  360. std::cout << "Map size: " << mData.size() << std::endl;
  361. std::cout << "LLSD Net Objects: " << llsd::sLLSDNetObjects << std::endl;
  362. std::cout << "LLSD allocations: " << llsd::sLLSDAllocationCount << std::endl;
  363. std::cout << "LLSD::Impl Net Objects: " << sOutstandingCount << std::endl;
  364. std::cout << "LLSD::Impl allocations: " << sAllocationCount << std::endl;
  365. Impl::dumpStats();
  366. }
  367. void ImplMap::calcStats(S32 type_counts[], S32 share_counts[]) const
  368. {
  369. LLSD::map_const_iterator iter = beginMap();
  370. while (iter != endMap())
  371. {
  372. //std::cout << " " << (*iter).first << ": " << (*iter).second << std::endl;
  373. Impl::calcStats((*iter).second, type_counts, share_counts);
  374. iter++;
  375. }
  376. // Add in the values for this map
  377. Impl::calcStats(type_counts, share_counts);
  378. }
  379. class ImplArray : public LLSD::Impl
  380. {
  381. private:
  382. typedef std::vector<LLSD> DataVector;
  383. DataVector mData;
  384. protected:
  385. ImplArray(const DataVector& data) : mData(data) { }
  386. public:
  387. ImplArray() { }
  388. virtual ImplArray& makeArray(Impl*&);
  389. virtual LLSD::Type type() const { return LLSD::TypeArray; }
  390. virtual LLSD::Boolean asBoolean() const { return !mData.empty(); }
  391. using LLSD::Impl::get; // Unhiding get(LLSD::String)
  392. using LLSD::Impl::erase; // Unhiding erase(LLSD::String)
  393. using LLSD::Impl::ref; // Unhiding ref(LLSD::String)
  394. virtual int size() const;
  395. virtual LLSD get(LLSD::Integer) const;
  396. void set(LLSD::Integer, const LLSD&);
  397. void insert(LLSD::Integer, const LLSD&);
  398. void append(const LLSD&);
  399. virtual void erase(LLSD::Integer);
  400. LLSD& ref(LLSD::Integer);
  401. virtual const LLSD& ref(LLSD::Integer) const;
  402. LLSD::array_iterator beginArray() { return mData.begin(); }
  403. LLSD::array_iterator endArray() { return mData.end(); }
  404. virtual LLSD::array_const_iterator beginArray() const { return mData.begin(); }
  405. virtual LLSD::array_const_iterator endArray() const { return mData.end(); }
  406. virtual void calcStats(S32 type_counts[], S32 share_counts[]) const;
  407. };
  408. ImplArray& ImplArray::makeArray(Impl*& var)
  409. {
  410. if (shared())
  411. {
  412. ImplArray* i = new ImplArray(mData);
  413. Impl::assign(var, i);
  414. return *i;
  415. }
  416. else
  417. {
  418. return *this;
  419. }
  420. }
  421. int ImplArray::size() const { return mData.size(); }
  422. LLSD ImplArray::get(LLSD::Integer i) const
  423. {
  424. if (i < 0) { return LLSD(); }
  425. DataVector::size_type index = i;
  426. return (index < mData.size()) ? mData[index] : LLSD();
  427. }
  428. void ImplArray::set(LLSD::Integer i, const LLSD& v)
  429. {
  430. if (i < 0) { return; }
  431. DataVector::size_type index = i;
  432. if (index >= mData.size())
  433. {
  434. mData.resize(index + 1);
  435. }
  436. mData[index] = v;
  437. }
  438. void ImplArray::insert(LLSD::Integer i, const LLSD& v)
  439. {
  440. if (i < 0)
  441. {
  442. return;
  443. }
  444. DataVector::size_type index = i;
  445. if (index >= mData.size()) // tbd - sanity check limit for index ?
  446. {
  447. mData.resize(index + 1);
  448. }
  449. mData.insert(mData.begin() + index, v);
  450. }
  451. void ImplArray::append(const LLSD& v)
  452. {
  453. mData.push_back(v);
  454. }
  455. void ImplArray::erase(LLSD::Integer i)
  456. {
  457. if (i < 0) { return; }
  458. DataVector::size_type index = i;
  459. if (index < mData.size())
  460. {
  461. mData.erase(mData.begin() + index);
  462. }
  463. }
  464. LLSD& ImplArray::ref(LLSD::Integer i)
  465. {
  466. DataVector::size_type index = i >= 0 ? i : 0;
  467. if (index >= mData.size())
  468. {
  469. mData.resize(i + 1);
  470. }
  471. return mData[index];
  472. }
  473. const LLSD& ImplArray::ref(LLSD::Integer i) const
  474. {
  475. if (i < 0) { return undef(); }
  476. DataVector::size_type index = i;
  477. if (index >= mData.size())
  478. {
  479. return undef();
  480. }
  481. return mData[index];
  482. }
  483. void ImplArray::calcStats(S32 type_counts[], S32 share_counts[]) const
  484. {
  485. LLSD::array_const_iterator iter = beginArray();
  486. while (iter != endArray())
  487. { // Add values for all items held in the array
  488. Impl::calcStats((*iter), type_counts, share_counts);
  489. iter++;
  490. }
  491. // Add in the values for this array
  492. Impl::calcStats(type_counts, share_counts);
  493. }
  494. }
  495. LLSD::Impl::Impl()
  496. : mUseCount(0)
  497. {
  498. ++sAllocationCount;
  499. ++sOutstandingCount;
  500. }
  501. LLSD::Impl::Impl(StaticAllocationMarker)
  502. : mUseCount(0)
  503. {
  504. }
  505. LLSD::Impl::~Impl()
  506. {
  507. --sOutstandingCount;
  508. }
  509. void LLSD::Impl::reset(Impl*& var, Impl* impl)
  510. {
  511. if (impl && impl->mUseCount != STATIC_USAGE_COUNT)
  512. {
  513. ++impl->mUseCount;
  514. }
  515. if (var && var->mUseCount != STATIC_USAGE_COUNT && --var->mUseCount == 0)
  516. {
  517. delete var;
  518. }
  519. var = impl;
  520. }
  521. LLSD::Impl& LLSD::Impl::safe(Impl* impl)
  522. {
  523. static Impl theUndefined(STATIC_USAGE_COUNT);
  524. return impl ? *impl : theUndefined;
  525. }
  526. const LLSD::Impl& LLSD::Impl::safe(const Impl* impl)
  527. {
  528. static Impl theUndefined(STATIC_USAGE_COUNT);
  529. return impl ? *impl : theUndefined;
  530. }
  531. ImplMap& LLSD::Impl::makeMap(Impl*& var)
  532. {
  533. ImplMap* im = new ImplMap;
  534. reset(var, im);
  535. return *im;
  536. }
  537. ImplArray& LLSD::Impl::makeArray(Impl*& var)
  538. {
  539. ImplArray* ia = new ImplArray;
  540. reset(var, ia);
  541. return *ia;
  542. }
  543. void LLSD::Impl::assign(Impl*& var, const Impl* other)
  544. {
  545. reset(var, const_cast<Impl*>(other));
  546. }
  547. void LLSD::Impl::assignUndefined(Impl*& var)
  548. {
  549. reset(var, 0);
  550. }
  551. void LLSD::Impl::assign(Impl*& var, LLSD::Boolean v)
  552. {
  553. reset(var, new ImplBoolean(v));
  554. }
  555. void LLSD::Impl::assign(Impl*& var, LLSD::Integer v)
  556. {
  557. reset(var, new ImplInteger(v));
  558. }
  559. void LLSD::Impl::assign(Impl*& var, LLSD::Real v)
  560. {
  561. reset(var, new ImplReal(v));
  562. }
  563. void LLSD::Impl::assign(Impl*& var, const LLSD::String& v)
  564. {
  565. reset(var, new ImplString(v));
  566. }
  567. void LLSD::Impl::assign(Impl*& var, const LLSD::UUID& v)
  568. {
  569. reset(var, new ImplUUID(v));
  570. }
  571. void LLSD::Impl::assign(Impl*& var, const LLSD::Date& v)
  572. {
  573. reset(var, new ImplDate(v));
  574. }
  575. void LLSD::Impl::assign(Impl*& var, const LLSD::URI& v)
  576. {
  577. reset(var, new ImplURI(v));
  578. }
  579. void LLSD::Impl::assign(Impl*& var, const LLSD::Binary& v)
  580. {
  581. reset(var, new ImplBinary(v));
  582. }
  583. const LLSD& LLSD::Impl::undef()
  584. {
  585. static const LLSD immutableUndefined;
  586. return immutableUndefined;
  587. }
  588. void LLSD::Impl::dumpStats() const
  589. {
  590. S32 type_counts[LLSD::TypeLLSDNumTypes + 1];
  591. memset(&type_counts, 0, sizeof(type_counts));
  592. S32 share_counts[LLSD::TypeLLSDNumTypes + 1];
  593. memset(&share_counts, 0, sizeof(share_counts));
  594. // Add info from all the values this object has
  595. calcStats(type_counts, share_counts);
  596. S32 type_index = LLSD::TypeLLSDTypeBegin;
  597. while (type_index != LLSD::TypeLLSDTypeEnd)
  598. {
  599. std::cout << LLSD::typeString((LLSD::Type)type_index) << " type "
  600. << type_counts[type_index] << " objects, "
  601. << share_counts[type_index] << " shared"
  602. << std::endl;
  603. type_index++;
  604. }
  605. }
  606. void LLSD::Impl::calcStats(S32 type_counts[], S32 share_counts[]) const
  607. {
  608. S32 tp = S32(type());
  609. if (0 <= tp && tp < LLSD::TypeLLSDNumTypes)
  610. {
  611. type_counts[tp]++;
  612. if (shared())
  613. {
  614. share_counts[tp]++;
  615. }
  616. }
  617. }
  618. U32 LLSD::Impl::sAllocationCount = 0;
  619. U32 LLSD::Impl::sOutstandingCount = 0;
  620. #ifdef NAME_UNNAMED_NAMESPACE
  621. namespace LLSDUnnamedNamespace
  622. #else
  623. namespace
  624. #endif
  625. {
  626. inline LLSD::Impl& safe(LLSD::Impl* impl)
  627. { return LLSD::Impl::safe(impl); }
  628. inline const LLSD::Impl& safe(const LLSD::Impl* impl)
  629. { return LLSD::Impl::safe(impl); }
  630. inline ImplMap& makeMap(LLSD::Impl*& var)
  631. { return safe(var).makeMap(var); }
  632. inline ImplArray& makeArray(LLSD::Impl*& var)
  633. { return safe(var).makeArray(var); }
  634. }
  635. LLSD::LLSD() : impl(0) { ALLOC_LLSD_OBJECT; }
  636. LLSD::~LLSD() { FREE_LLSD_OBJECT; Impl::reset(impl, 0); }
  637. LLSD::LLSD(const LLSD& other) : impl(0) { ALLOC_LLSD_OBJECT; assign(other); }
  638. void LLSD::assign(const LLSD& other) { Impl::assign(impl, other.impl); }
  639. void LLSD::clear() { Impl::assignUndefined(impl); }
  640. LLSD::Type LLSD::type() const { return safe(impl).type(); }
  641. // Scalar Constructors
  642. LLSD::LLSD(Boolean v) : impl(0) { ALLOC_LLSD_OBJECT; assign(v); }
  643. LLSD::LLSD(Integer v) : impl(0) { ALLOC_LLSD_OBJECT; assign(v); }
  644. LLSD::LLSD(Real v) : impl(0) { ALLOC_LLSD_OBJECT; assign(v); }
  645. LLSD::LLSD(const UUID& v) : impl(0) { ALLOC_LLSD_OBJECT; assign(v); }
  646. LLSD::LLSD(const String& v) : impl(0) { ALLOC_LLSD_OBJECT; assign(v); }
  647. LLSD::LLSD(const Date& v) : impl(0) { ALLOC_LLSD_OBJECT; assign(v); }
  648. LLSD::LLSD(const URI& v) : impl(0) { ALLOC_LLSD_OBJECT; assign(v); }
  649. LLSD::LLSD(const Binary& v) : impl(0) { ALLOC_LLSD_OBJECT; assign(v); }
  650. // Convenience Constructors
  651. LLSD::LLSD(F32 v) : impl(0) { ALLOC_LLSD_OBJECT; assign((Real)v); }
  652. // Scalar Assignment
  653. void LLSD::assign(Boolean v) { safe(impl).assign(impl, v); }
  654. void LLSD::assign(Integer v) { safe(impl).assign(impl, v); }
  655. void LLSD::assign(Real v) { safe(impl).assign(impl, v); }
  656. void LLSD::assign(const String& v) { safe(impl).assign(impl, v); }
  657. void LLSD::assign(const UUID& v) { safe(impl).assign(impl, v); }
  658. void LLSD::assign(const Date& v) { safe(impl).assign(impl, v); }
  659. void LLSD::assign(const URI& v) { safe(impl).assign(impl, v); }
  660. void LLSD::assign(const Binary& v) { safe(impl).assign(impl, v); }
  661. // Scalar Accessors
  662. LLSD::Boolean LLSD::asBoolean() const { return safe(impl).asBoolean(); }
  663. LLSD::Integer LLSD::asInteger() const { return safe(impl).asInteger(); }
  664. LLSD::Real LLSD::asReal() const { return safe(impl).asReal(); }
  665. LLSD::String LLSD::asString() const { return safe(impl).asString(); }
  666. LLSD::UUID LLSD::asUUID() const { return safe(impl).asUUID(); }
  667. LLSD::Date LLSD::asDate() const { return safe(impl).asDate(); }
  668. LLSD::URI LLSD::asURI() const { return safe(impl).asURI(); }
  669. LLSD::Binary LLSD::asBinary() const { return safe(impl).asBinary(); }
  670. // const char * helpers
  671. LLSD::LLSD(const char* v) : impl(0) { ALLOC_LLSD_OBJECT; assign(v); }
  672. void LLSD::assign(const char* v)
  673. {
  674. if(v) assign(std::string(v));
  675. else assign(std::string());
  676. }
  677. LLSD LLSD::emptyMap()
  678. {
  679. LLSD v;
  680. makeMap(v.impl);
  681. return v;
  682. }
  683. bool LLSD::has(const String& k) const { return safe(impl).has(k); }
  684. LLSD LLSD::get(const String& k) const { return safe(impl).get(k); }
  685. void LLSD::insert(const String& k, const LLSD& v) { makeMap(impl).insert(k, v); }
  686. LLSD& LLSD::with(const String& k, const LLSD& v)
  687. {
  688. makeMap(impl).insert(k, v);
  689. return *this;
  690. }
  691. void LLSD::erase(const String& k) { makeMap(impl).erase(k); }
  692. LLSD& LLSD::operator[](const String& k)
  693. { return makeMap(impl).ref(k); }
  694. const LLSD& LLSD::operator[](const String& k) const
  695. { return safe(impl).ref(k); }
  696. LLSD LLSD::emptyArray()
  697. {
  698. LLSD v;
  699. makeArray(v.impl);
  700. return v;
  701. }
  702. int LLSD::size() const { return safe(impl).size(); }
  703. LLSD LLSD::get(Integer i) const { return safe(impl).get(i); }
  704. void LLSD::set(Integer i, const LLSD& v){ makeArray(impl).set(i, v); }
  705. void LLSD::insert(Integer i, const LLSD& v) { makeArray(impl).insert(i, v); }
  706. LLSD& LLSD::with(Integer i, const LLSD& v)
  707. {
  708. makeArray(impl).insert(i, v);
  709. return *this;
  710. }
  711. void LLSD::append(const LLSD& v) { makeArray(impl).append(v); }
  712. void LLSD::erase(Integer i) { makeArray(impl).erase(i); }
  713. LLSD& LLSD::operator[](Integer i)
  714. { return makeArray(impl).ref(i); }
  715. const LLSD& LLSD::operator[](Integer i) const
  716. { return safe(impl).ref(i); }
  717. static const char *llsd_dump(const LLSD &llsd, bool useXMLFormat)
  718. {
  719. // sStorage is used to hold the string representation of the llsd last
  720. // passed into this function. If this function is never called (the
  721. // normal case when not debugging), nothing is allocated. Otherwise
  722. // sStorage will point to the result of the last call. This will actually
  723. // be one leak, but since this is used only when running under the
  724. // debugger, it should not be an issue.
  725. static char *sStorage = NULL;
  726. delete[] sStorage;
  727. std::string out_string;
  728. {
  729. std::ostringstream out;
  730. if (useXMLFormat)
  731. out << LLSDXMLStreamer(llsd);
  732. else
  733. out << LLSDNotationStreamer(llsd);
  734. out_string = out.str();
  735. }
  736. int len = out_string.length();
  737. sStorage = new char[len + 1];
  738. memcpy(sStorage, out_string.c_str(), len);
  739. sStorage[len] = '\0';
  740. return sStorage;
  741. }
  742. /// Returns XML version of llsd -- only to be called from debugger
  743. const char *LLSD::dumpXML(const LLSD &llsd)
  744. {
  745. return llsd_dump(llsd, true);
  746. }
  747. /// Returns Notation version of llsd -- only to be called from debugger
  748. const char *LLSD::dump(const LLSD &llsd)
  749. {
  750. return llsd_dump(llsd, false);
  751. }
  752. LLSD::map_iterator LLSD::beginMap() { return makeMap(impl).beginMap(); }
  753. LLSD::map_iterator LLSD::endMap() { return makeMap(impl).endMap(); }
  754. LLSD::map_const_iterator LLSD::beginMap() const { return safe(impl).beginMap(); }
  755. LLSD::map_const_iterator LLSD::endMap() const { return safe(impl).endMap(); }
  756. LLSD::array_iterator LLSD::beginArray() { return makeArray(impl).beginArray(); }
  757. LLSD::array_iterator LLSD::endArray() { return makeArray(impl).endArray(); }
  758. LLSD::array_const_iterator LLSD::beginArray() const{ return safe(impl).beginArray(); }
  759. LLSD::array_const_iterator LLSD::endArray() const { return safe(impl).endArray(); }
  760. namespace llsd
  761. {
  762. U32 allocationCount() { return LLSD::Impl::sAllocationCount; }
  763. U32 outstandingCount() { return LLSD::Impl::sOutstandingCount; }
  764. // Diagnostic dump of contents in an LLSD object
  765. void dumpStats(const LLSD& llsd) { LLSD::Impl::getImpl(llsd).dumpStats(); }
  766. } // namespace llsd
  767. // static
  768. std::string LLSD::typeString(Type type)
  769. {
  770. static const char * sTypeNameArray[] = {
  771. "Undefined",
  772. "Boolean",
  773. "Integer",
  774. "Real",
  775. "String",
  776. "UUID",
  777. "Date",
  778. "URI",
  779. "Binary",
  780. "Map",
  781. "Array"
  782. };
  783. if (0 <= type && type < LL_ARRAY_SIZE(sTypeNameArray))
  784. {
  785. return sTypeNameArray[type];
  786. }
  787. return STRINGIZE("** invalid type value " << type);
  788. }