Newer
Older
Andrey Lihatskiy
committed
/**
* @file llsd.cpp
* @brief LLSD flexible data system
*
* $LicenseInfo:firstyear=2005&license=viewerlgpl$
* Copyright (C) 2010, Linden Research, Inc.
Andrey Lihatskiy
committed
*
* 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.
Andrey Lihatskiy
committed
*
* 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.
Andrey Lihatskiy
committed
*
* 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
Andrey Lihatskiy
committed
*
* Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
// Must turn on conditional declarations in header file so definitions end up
// with proper linkage.
#define LLSD_DEBUG_INFO
#include "linden_common.h"
Aaron Brashears
committed
#include "llerror.h"
#include "llsdserialize.h"
#include "stringize.h"
#include <limits>
// Defend against a caller forcibly passing a negative number into an unsigned
// size_t index param
inline
bool was_negative(size_t i)
{
return (i > std::numeric_limits<int>::max());
}
#define NEGATIVE_EXIT(i) if (was_negative(i)) return
#define NEGATIVE_RETURN(i, result) NEGATIVE_EXIT(i) (result)
Steven Bennetts
committed
#ifndef LL_RELEASE_FOR_DOWNLOAD
#define NAME_UNNAMED_NAMESPACE
#endif
#ifdef NAME_UNNAMED_NAMESPACE
Andrey Lihatskiy
committed
namespace LLSDUnnamedNamespace
Steven Bennetts
committed
#else
Andrey Lihatskiy
committed
namespace
Steven Bennetts
committed
#endif
Bryan O'Sullivan
committed
{
Andrey Lihatskiy
committed
class ImplMap;
class ImplArray;
Steven Bennetts
committed
#ifdef NAME_UNNAMED_NAMESPACE
using namespace LLSDUnnamedNamespace;
#endif
namespace llsd
{
// statics
Andrey Lihatskiy
committed
S32 sLLSDAllocationCount = 0;
S32 sLLSDNetObjects = 0;
} // namespace llsd
Andrey Lihatskiy
committed
#define ALLOC_LLSD_OBJECT { llsd::sLLSDNetObjects++; llsd::sLLSDAllocationCount++; }
#define FREE_LLSD_OBJECT { llsd::sLLSDNetObjects--; }
Andrey Lihatskiy
committed
/**< This class is the abstract base class of the implementation of LLSD
It provides the reference counting implementation, and the default
implementation of most methods for most data types. It also serves
as a working implementation of the Undefined type.
*/
Andrey Lihatskiy
committed
Impl();
enum StaticAllocationMarker { STATIC_USAGE_COUNT = 0xFFFFFFFF };
Impl(StaticAllocationMarker);
///< This constructor is used for static objects and causes the
// suppresses adjusting the debugging counters when they are
// finally initialized.
virtual ~Impl();
bool shared() const { return (mUseCount > 1) && (mUseCount != STATIC_USAGE_COUNT); }
U32 mUseCount;
const LLSD::map_t& map() const { static const LLSD::map_t empty; return empty; }
const std::vector<LLSD>& array() const { static const std::vector<LLSD> empty; return empty; }
Andrey Lihatskiy
committed
static void reset(Impl*& var, Impl* impl);
///< safely set var to refer to the new impl (possibly shared)
static void move(Impl*& var, Impl*& impl);
///< safely move impl from one object to another
Andrey Lihatskiy
committed
static Impl& safe( Impl*);
static const Impl& safe(const Impl*);
///< since a NULL Impl* is used for undefined, this ensures there is
// always an object you call virtual member functions on
virtual ImplMap& makeMap(Impl*& var);
virtual ImplArray& makeArray(Impl*& var);
///< sure var is a modifiable, non-shared map or array
virtual LLSD::Type type() const { return LLSD::TypeUndefined; }
static void assignUndefined(LLSD::Impl*& var);
static void assign(LLSD::Impl*& var, const LLSD::Impl* other);
virtual void assign(Impl*& var, LLSD::Boolean);
virtual void assign(Impl*& var, LLSD::Integer);
virtual void assign(Impl*& var, LLSD::Real);
virtual void assign(Impl*& var, LLSD::String);
virtual void assign(Impl*& var, LLSD::UUID);
virtual void assign(Impl*& var, LLSD::Date);
virtual void assign(Impl*& var, LLSD::URI);
virtual void assign(Impl*& var, LLSD::Binary);
Andrey Lihatskiy
committed
///< If the receiver is the right type and unshared, these are simple
// data assignments, othewise the default implementation handless
// constructing the proper Impl subclass
virtual Boolean asBoolean() const { return false; }
virtual Integer asInteger() const { return 0; }
virtual Real asReal() const { return 0.0; }
virtual String asString() const { return std::string(); }
virtual UUID asUUID() const { return LLUUID(); }
virtual Date asDate() const { return LLDate(); }
virtual URI asURI() const { return LLURI(); }
virtual const Binary& asBinary() const { static const std::vector<U8> empty; return empty; }
virtual const map_t& asMap() const { return map(); };
virtual const array_t& asArray() const { return array(); };
Andrey Lihatskiy
committed
virtual const String& asStringRef() const { static const std::string empty; return empty; }
virtual bool has(const std::string_view) const { return false; }
virtual LLSD get(const std::string_view) const { return LLSD(); }
Andrey Lihatskiy
committed
virtual LLSD getKeys() const { return LLSD::emptyArray(); }
virtual void erase(const String&) { }
virtual const LLSD& ref(const std::string_view) const{ return undef(); }
Andrey Lihatskiy
committed
virtual size_t size() const { return 0; }
virtual LLSD get(size_t) const { return LLSD(); }
virtual void erase(size_t) { }
virtual const LLSD& ref(size_t) const { return undef(); }
virtual LLSD::map_const_iterator beginMap() const { return endMap(); }
virtual LLSD::map_const_iterator endMap() const { return map().end(); }
Andrey Lihatskiy
committed
virtual LLSD::array_const_iterator beginArray() const { return endArray(); }
virtual LLSD::array_const_iterator endArray() const { return array().end(); }
Andrey Lihatskiy
committed
virtual void dumpStats() const;
virtual void calcStats(S32 type_counts[], S32 share_counts[]) const;
// Container subclasses contain LLSD objects, rather than directly
// containing Impl objects. This helper forwards through LLSD.
void calcStats(const LLSD& llsd, S32 type_counts[], S32 share_counts[]) const
{
safe(llsd.impl).calcStats(type_counts, share_counts);
}
static const Impl& getImpl(const LLSD& llsd) { return safe(llsd.impl); }
static Impl& getImpl(LLSD& llsd) { return safe(llsd.impl); }
static const LLSD& undef();
static U32 sAllocationCount;
static U32 sOutstandingCount;
Steven Bennetts
committed
#ifdef NAME_UNNAMED_NAMESPACE
Andrey Lihatskiy
committed
namespace LLSDUnnamedNamespace
Steven Bennetts
committed
#else
Andrey Lihatskiy
committed
namespace
Steven Bennetts
committed
#endif
Bryan O'Sullivan
committed
{
template<LLSD::Type T, class Data>
Andrey Lihatskiy
committed
class ImplBase : public LLSD::Impl
///< This class handles most of the work for a subclass of Impl
// for a given simple data type. Subclasses of this provide the
// conversion functions and a constructor.
{
protected:
Data mValue;
typedef ImplBase Base;
public:
ImplBase(Data value) : mValue(std::move(value)) {}
LLSD::Type type() const override { return T; }
Andrey Lihatskiy
committed
using LLSD::Impl::assign; // Unhiding base class virtuals...
Andrey Lihatskiy
committed
if (shared())
{
Impl::assign(var, std::move(value));
Andrey Lihatskiy
committed
}
else
{
mValue = std::move(value);
Andrey Lihatskiy
committed
}
}
};
class ImplBoolean final
Andrey Lihatskiy
committed
: public ImplBase<LLSD::TypeBoolean, LLSD::Boolean>
{
public:
ImplBoolean(LLSD::Boolean v) : Base(v) { }
virtual LLSD::Boolean asBoolean() const { return mValue; }
virtual LLSD::Integer asInteger() const { return mValue ? 1 : 0; }
virtual LLSD::Real asReal() const { return mValue ? 1 : 0; }
virtual LLSD::String asString() const;
};
LLSD::String ImplBoolean::asString() const
// *NOTE: The reason that false is not converted to "false" is
// because that would break roundtripping,
// e.g. LLSD(false).asString().asBoolean(). There are many
// reasons for wanting LLSD("false").asBoolean() == true, such
// as "everything else seems to work that way".
{ return mValue ? "true" : ""; }
class ImplInteger final
Andrey Lihatskiy
committed
: public ImplBase<LLSD::TypeInteger, LLSD::Integer>
{
public:
ImplInteger(LLSD::Integer v) : Base(v) { }
virtual LLSD::Boolean asBoolean() const { return mValue != 0; }
virtual LLSD::Integer asInteger() const { return mValue; }
virtual LLSD::Real asReal() const { return mValue; }
virtual LLSD::String asString() const;
};
LLSD::String ImplInteger::asString() const
{ return llformat("%d", mValue); }
class ImplReal final
Andrey Lihatskiy
committed
: public ImplBase<LLSD::TypeReal, LLSD::Real>
{
public:
ImplReal(LLSD::Real v) : Base(v) { }
virtual LLSD::Boolean asBoolean() const;
virtual LLSD::Integer asInteger() const;
virtual LLSD::Real asReal() const { return mValue; }
virtual LLSD::String asString() const;
};
LLSD::Boolean ImplReal::asBoolean() const
{ return !std::isnan(mValue) && mValue != 0.0; }
Andrey Lihatskiy
committed
LLSD::Integer ImplReal::asInteger() const
{ return !std::isnan(mValue) ? (LLSD::Integer)mValue : 0; }
Andrey Lihatskiy
committed
LLSD::String ImplReal::asString() const
{ return llformat("%lg", mValue); }
class ImplString final
: public ImplBase<LLSD::TypeString, LLSD::String>
Andrey Lihatskiy
committed
{
public:
ImplString(LLSD::String v) : Base(std::move(v)) { }
Andrey Lihatskiy
committed
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
virtual LLSD::Boolean asBoolean() const { return !mValue.empty(); }
virtual LLSD::Integer asInteger() const;
virtual LLSD::Real asReal() const;
virtual LLSD::String asString() const { return mValue; }
virtual LLSD::UUID asUUID() const { return LLUUID(mValue); }
virtual LLSD::Date asDate() const { return LLDate(mValue); }
virtual LLSD::URI asURI() const { return LLURI(mValue); }
virtual size_t size() const { return mValue.size(); }
virtual const LLSD::String& asStringRef() const { return mValue; }
};
LLSD::Integer ImplString::asInteger() const
{
// This must treat "1.23" not as an error, but as a number, which is
// then truncated down to an integer. Hence, this code doesn't call
// std::istringstream::operator>>(int&), which would not consume the
// ".23" portion.
return (int)asReal();
}
LLSD::Real ImplString::asReal() const
{
F64 v = 0.0;
std::istringstream i_stream(mValue);
i_stream >> v;
// we would probably like to ignore all trailing whitespace as
// well, but for now, simply eat the next character, and make
// sure we reached the end of the string.
// *NOTE: gcc 2.95 does not generate an eof() event on the
// stream operation above, so we manually get here to force it
// across platforms.
int c = i_stream.get();
return ((EOF ==c) ? v : 0.0);
}
class ImplUUID final
: public ImplBase<LLSD::TypeUUID, LLSD::UUID>
Andrey Lihatskiy
committed
{
public:
ImplUUID(LLSD::UUID v) : Base(std::move(v)) { }
Andrey Lihatskiy
committed
virtual LLSD::String asString() const{ return mValue.asString(); }
virtual LLSD::UUID asUUID() const { return mValue; }
};
class ImplDate final
: public ImplBase<LLSD::TypeDate, LLSD::Date>
Andrey Lihatskiy
committed
{
public:
ImplDate(LLSD::Date v) : Base(std::move(v)) { }
Andrey Lihatskiy
committed
virtual LLSD::Integer asInteger() const
{
return (LLSD::Integer)(mValue.secondsSinceEpoch());
}
virtual LLSD::Real asReal() const
{
return mValue.secondsSinceEpoch();
}
virtual LLSD::String asString() const{ return mValue.asString(); }
virtual LLSD::Date asDate() const { return mValue; }
};
class ImplURI final
: public ImplBase<LLSD::TypeURI, LLSD::URI>
Andrey Lihatskiy
committed
{
public:
ImplURI(LLSD::URI v) : Base(std::move(v)) { }
Andrey Lihatskiy
committed
virtual LLSD::String asString() const{ return mValue.asString(); }
virtual LLSD::URI asURI() const { return mValue; }
};
class ImplBinary final
: public ImplBase<LLSD::TypeBinary, LLSD::Binary>
Andrey Lihatskiy
committed
{
public:
ImplBinary(LLSD::Binary v) : Base(std::move(v)) { }
Andrey Lihatskiy
committed
virtual const LLSD::Binary& asBinary() const{ return mValue; }
};
class ImplMap final : public LLSD::Impl
Andrey Lihatskiy
committed
{
private:
typedef LLSD::map_t DataMap;
Andrey Lihatskiy
committed
DataMap mData;
protected:
ImplMap(DataMap data) : mData(std::move(data)) { }
Andrey Lihatskiy
committed
public:
ImplMap() = default;
Andrey Lihatskiy
committed
LLSD::Type type() const override { return LLSD::TypeMap; }
LLSD::Boolean asBoolean() const override { return !mData.empty(); }
LLSD::map_t& asMap() { return mData; };
const LLSD::map_t& asMap() const override { return mData; };
bool has(const std::string_view) const override;
Andrey Lihatskiy
committed
using LLSD::Impl::get; // Unhiding get(size_t)
using LLSD::Impl::erase; // Unhiding erase(size_t)
using LLSD::Impl::ref; // Unhiding ref(size_t)
LLSD get(const std::string_view) const override;
LLSD getKeys() const override;
void insert(const std::string_view k, const LLSD& v);
void erase(const LLSD::String&) override;
LLSD& ref(const std::string_view);
const LLSD& ref(const std::string_view) const override;
size_t size() const override { return mData.size(); }
Andrey Lihatskiy
committed
LLSD::map_iterator beginMap() { return mData.begin(); }
LLSD::map_iterator endMap() { return mData.end(); }
LLSD::map_const_iterator beginMap() const override { return mData.begin(); }
LLSD::map_const_iterator endMap() const override { return mData.end(); }
void dumpStats() const override;
void calcStats(S32 type_counts[], S32 share_counts[]) const override;
Andrey Lihatskiy
committed
};
ImplMap& ImplMap::makeMap(LLSD::Impl*& var)
{
Andrey Lihatskiy
committed
if (shared())
{
ImplMap* i = new ImplMap(mData);
Impl::assign(var, i);
return *i;
}
else
{
return *this;
}
}
bool ImplMap::has(const std::string_view k) const
Andrey Lihatskiy
committed
{
Andrey Lihatskiy
committed
DataMap::const_iterator i = mData.find(k);
return i != mData.end();
}
LLSD ImplMap::get(const std::string_view k) const
Andrey Lihatskiy
committed
{
Andrey Lihatskiy
committed
DataMap::const_iterator i = mData.find(k);
return (i != mData.end()) ? i->second : LLSD();
}
Andrey Lihatskiy
committed
LLSD ImplMap::getKeys() const
{
Andrey Lihatskiy
committed
LLSD keys = LLSD::emptyArray();
DataMap::const_iterator iter = mData.begin();
while (iter != mData.end())
{
keys.append((*iter).first);
iter++;
}
return keys;
}
void ImplMap::insert(const std::string_view k, const LLSD& v)
Andrey Lihatskiy
committed
{
mData.emplace(k, v);
Andrey Lihatskiy
committed
}
void ImplMap::erase(const LLSD::String& k)
{
Andrey Lihatskiy
committed
mData.erase(k);
}
LLSD& ImplMap::ref(const std::string_view k)
Andrey Lihatskiy
committed
{
DataMap::iterator i = mData.find(k);
if (i == mData.end())
{
return mData.emplace(k, LLSD()).first->second;
}
return i->second;
Andrey Lihatskiy
committed
}
const LLSD& ImplMap::ref(const std::string_view k) const
Andrey Lihatskiy
committed
{
DataMap::const_iterator i = mData.find(k);
if (i == mData.end())
Andrey Lihatskiy
committed
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
{
return undef();
}
return i->second;
}
void ImplMap::dumpStats() const
{
std::cout << "Map size: " << mData.size() << std::endl;
std::cout << "LLSD Net Objects: " << llsd::sLLSDNetObjects << std::endl;
std::cout << "LLSD allocations: " << llsd::sLLSDAllocationCount << std::endl;
std::cout << "LLSD::Impl Net Objects: " << sOutstandingCount << std::endl;
std::cout << "LLSD::Impl allocations: " << sAllocationCount << std::endl;
Impl::dumpStats();
}
void ImplMap::calcStats(S32 type_counts[], S32 share_counts[]) const
{
LLSD::map_const_iterator iter = beginMap();
while (iter != endMap())
{
//std::cout << " " << (*iter).first << ": " << (*iter).second << std::endl;
Impl::calcStats((*iter).second, type_counts, share_counts);
Andrey Lihatskiy
committed
}
// Add in the values for this map
Impl::calcStats(type_counts, share_counts);
}
class ImplArray final : public LLSD::Impl
Andrey Lihatskiy
committed
{
private:
typedef std::vector<LLSD> DataVector;
DataVector mData;
protected:
ImplArray(DataVector data) : mData(std::move(data)) { }
Andrey Lihatskiy
committed
public:
LLSD::Type type() const override { return LLSD::TypeArray; }
LLSD::Boolean asBoolean() const override { return !mData.empty(); }
DataVector& asArray() { return mData; };
const DataVector& asArray() const override { return mData; };
Andrey Lihatskiy
committed
using LLSD::Impl::get; // Unhiding get(LLSD::String)
using LLSD::Impl::erase; // Unhiding erase(LLSD::String)
using LLSD::Impl::ref; // Unhiding ref(LLSD::String)
virtual size_t size() const override;
virtual LLSD get(size_t) const override;
Andrey Lihatskiy
committed
void set(size_t, const LLSD&);
void insert(size_t, const LLSD&);
LLSD& append(const LLSD&);
virtual void erase(size_t) override;
Andrey Lihatskiy
committed
LLSD& ref(size_t);
virtual const LLSD& ref(size_t) const override;
Andrey Lihatskiy
committed
LLSD::array_iterator beginArray() { return mData.begin(); }
LLSD::array_iterator endArray() { return mData.end(); }
LLSD::reverse_array_iterator rbeginArray() { return mData.rbegin(); }
LLSD::reverse_array_iterator rendArray() { return mData.rend(); }
virtual LLSD::array_const_iterator beginArray() const { return mData.begin(); }
virtual LLSD::array_const_iterator endArray() const { return mData.end(); }
void calcStats(S32 type_counts[], S32 share_counts[]) const override;
Andrey Lihatskiy
committed
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
};
ImplArray& ImplArray::makeArray(Impl*& var)
{
if (shared())
{
ImplArray* i = new ImplArray(mData);
Impl::assign(var, i);
return *i;
}
else
{
return *this;
}
}
size_t ImplArray::size() const { return mData.size(); }
LLSD ImplArray::get(size_t i) const
{
NEGATIVE_RETURN(i, LLSD());
DataVector::size_type index = i;
return (index < mData.size()) ? mData[index] : LLSD();
}
void ImplArray::set(size_t i, const LLSD& v)
{
NEGATIVE_EXIT(i);
DataVector::size_type index = i;
if (index >= mData.size())
{
mData.resize(index + 1);
}
mData[index] = v;
}
void ImplArray::insert(size_t i, const LLSD& v)
{
NEGATIVE_EXIT(i);
DataVector::size_type index = i;
if (index >= mData.size()) // tbd - sanity check limit for index ?
{
mData.resize(index + 1);
}
mData.insert(mData.begin() + index, v);
}
LLSD& ImplArray::append(const LLSD& v)
{
mData.push_back(v);
return mData.back();
}
void ImplArray::erase(size_t i)
{
NEGATIVE_EXIT(i);
DataVector::size_type index = i;
if (index < mData.size())
{
mData.erase(mData.begin() + index);
}
}
LLSD& ImplArray::ref(size_t i)
{
DataVector::size_type index = was_negative(i)? 0 : i;
if (index >= mData.size())
{
mData.resize(index + 1);
}
return mData[index];
}
const LLSD& ImplArray::ref(size_t i) const
{
NEGATIVE_RETURN(i, undef());
DataVector::size_type index = i;
if (index >= mData.size())
{
return undef();
}
return mData[index];
}
void ImplArray::calcStats(S32 type_counts[], S32 share_counts[]) const
{
LLSD::array_const_iterator iter = beginArray();
while (iter != endArray())
{ // Add values for all items held in the array
Impl::calcStats((*iter), type_counts, share_counts);
Andrey Lihatskiy
committed
}
// Add in the values for this array
Impl::calcStats(type_counts, share_counts);
}
Andrey Lihatskiy
committed
: mUseCount(0)
Andrey Lihatskiy
committed
++sAllocationCount;
++sOutstandingCount;
Andrey Lihatskiy
committed
: mUseCount(0)
Andrey Lihatskiy
committed
--sOutstandingCount;
}
void LLSD::Impl::reset(Impl*& var, Impl* impl)
{
Andrey Lihatskiy
committed
{
if (impl && impl->mUseCount != STATIC_USAGE_COUNT)
{
++impl->mUseCount;
}
if (var && var->mUseCount != STATIC_USAGE_COUNT && --var->mUseCount == 0)
{
delete var;
}
var = impl;
Andrey Lihatskiy
committed
}
}
void LLSD::Impl::move(Impl*& var, Impl*& impl)
{
if (var == impl) return; // Bail out var is impl
if (var && var->mUseCount != STATIC_USAGE_COUNT && --var->mUseCount == 0)
Andrey Lihatskiy
committed
{
delete var; // destroy var if usage falls to 0 and not static
Andrey Lihatskiy
committed
}
var = impl; // Steal impl to var without incrementing use since this is a move
impl = nullptr; // null out old-impl pointer
}
LLSD::Impl& LLSD::Impl::safe(Impl* impl)
{
Andrey Lihatskiy
committed
static Impl theUndefined(STATIC_USAGE_COUNT);
return impl ? *impl : theUndefined;
}
const LLSD::Impl& LLSD::Impl::safe(const Impl* impl)
{
Andrey Lihatskiy
committed
static Impl theUndefined(STATIC_USAGE_COUNT);
return impl ? *impl : theUndefined;
}
ImplMap& LLSD::Impl::makeMap(Impl*& var)
{
Andrey Lihatskiy
committed
ImplMap* im = new ImplMap;
reset(var, im);
return *im;
}
ImplArray& LLSD::Impl::makeArray(Impl*& var)
{
Andrey Lihatskiy
committed
ImplArray* ia = new ImplArray;
reset(var, ia);
return *ia;
}
void LLSD::Impl::assign(Impl*& var, const Impl* other)
{
Andrey Lihatskiy
committed
reset(var, const_cast<Impl*>(other));
}
void LLSD::Impl::assignUndefined(Impl*& var)
{
Andrey Lihatskiy
committed
reset(var, 0);
}
void LLSD::Impl::assign(Impl*& var, LLSD::Boolean v)
{
Andrey Lihatskiy
committed
reset(var, new ImplBoolean(v));
}
void LLSD::Impl::assign(Impl*& var, LLSD::Integer v)
{
Andrey Lihatskiy
committed
reset(var, new ImplInteger(v));
}
void LLSD::Impl::assign(Impl*& var, LLSD::Real v)
{
Andrey Lihatskiy
committed
reset(var, new ImplReal(v));
void LLSD::Impl::assign(Impl*& var, LLSD::String v)
reset(var, new ImplString(std::move(v)));
void LLSD::Impl::assign(Impl*& var, LLSD::UUID v)
reset(var, new ImplUUID(std::move(v)));
void LLSD::Impl::assign(Impl*& var, LLSD::Date v)
reset(var, new ImplDate(std::move(v)));
void LLSD::Impl::assign(Impl*& var, LLSD::URI v)
reset(var, new ImplURI(std::move(v)));
void LLSD::Impl::assign(Impl*& var, LLSD::Binary v)
reset(var, new ImplBinary(std::move(v)));
Andrey Lihatskiy
committed
static const LLSD immutableUndefined;
return immutableUndefined;
void LLSD::Impl::dumpStats() const
{
Andrey Lihatskiy
committed
S32 type_counts[LLSD::TypeLLSDNumTypes + 1];
memset(&type_counts, 0, sizeof(type_counts));
S32 share_counts[LLSD::TypeLLSDNumTypes + 1];
memset(&share_counts, 0, sizeof(share_counts));
// Add info from all the values this object has
calcStats(type_counts, share_counts);
S32 type_index = LLSD::TypeLLSDTypeBegin;
while (type_index != LLSD::TypeLLSDTypeEnd)
{
std::cout << LLSD::typeString((LLSD::Type)type_index) << " type "
<< type_counts[type_index] << " objects, "
<< share_counts[type_index] << " shared"
<< std::endl;
type_index++;
}
}
void LLSD::Impl::calcStats(S32 type_counts[], S32 share_counts[]) const
Andrey Lihatskiy
committed
S32 tp = S32(type());
if (0 <= tp && tp < LLSD::TypeLLSDNumTypes)
{
type_counts[tp]++;
if (shared())
{
share_counts[tp]++;
}
}
}
U32 LLSD::Impl::sAllocationCount = 0;
U32 LLSD::Impl::sOutstandingCount = 0;
Steven Bennetts
committed
#ifdef NAME_UNNAMED_NAMESPACE
Andrey Lihatskiy
committed
namespace LLSDUnnamedNamespace
Steven Bennetts
committed
#else
Andrey Lihatskiy
committed
namespace
Steven Bennetts
committed
#endif
Bryan O'Sullivan
committed
{
Andrey Lihatskiy
committed
inline LLSD::Impl& safe(LLSD::Impl* impl)
{ return LLSD::Impl::safe(impl); }
inline ImplMap& makeMap(LLSD::Impl*& var)
{ return safe(var).makeMap(var); }
inline ImplArray& makeArray(LLSD::Impl*& var)
{ return safe(var).makeArray(var); }
LLSD::LLSD() : impl(nullptr) { ALLOC_LLSD_OBJECT; }
LLSD::~LLSD() { FREE_LLSD_OBJECT; Impl::reset(impl, nullptr); }
LLSD::LLSD(const LLSD& other) : impl(nullptr) { ALLOC_LLSD_OBJECT; assign(other); }
Andrey Lihatskiy
committed
void LLSD::assign(const LLSD& other) { Impl::assign(impl, other.impl); }
LLSD::LLSD(LLSD&& other) noexcept : impl(nullptr) { ALLOC_LLSD_OBJECT; Impl::move(impl, other.impl); }
void LLSD::assign(LLSD&& other) { Impl::move(impl, other.impl); }
LLSD& LLSD::operator=(LLSD&& other) { Impl::move(impl, other.impl); return *this; }
Andrey Lihatskiy
committed
void LLSD::clear() { Impl::assignUndefined(impl); }
Andrey Lihatskiy
committed
LLSD::Type LLSD::type() const { return safe(impl).type(); }
// Scalar Constructors
Andrey Lihatskiy
committed
LLSD::LLSD(Boolean v) : impl(0) { ALLOC_LLSD_OBJECT; assign(v); }
LLSD::LLSD(Integer v) : impl(0) { ALLOC_LLSD_OBJECT; assign(v); }
LLSD::LLSD(Real v) : impl(0) { ALLOC_LLSD_OBJECT; assign(v); }
LLSD::LLSD(const UUID& v) : impl(0) { ALLOC_LLSD_OBJECT; assign(v); }
LLSD::LLSD(const String& v) : impl(0) { ALLOC_LLSD_OBJECT; assign(v); }
LLSD::LLSD(const Date& v) : impl(0) { ALLOC_LLSD_OBJECT; assign(v); }
LLSD::LLSD(const URI& v) : impl(0) { ALLOC_LLSD_OBJECT; assign(v); }
LLSD::LLSD(const Binary& v) : impl(0) { ALLOC_LLSD_OBJECT; assign(v); }
LLSD::LLSD(UUID&& v) : impl(0) { ALLOC_LLSD_OBJECT; assign(std::move(v)); }
LLSD::LLSD(String&& v) : impl(0) { ALLOC_LLSD_OBJECT; assign(std::move(v)); }
LLSD::LLSD(Date&& v) : impl(0) { ALLOC_LLSD_OBJECT; assign(std::move(v)); }
LLSD::LLSD(URI&& v) : impl(0) { ALLOC_LLSD_OBJECT; assign(std::move(v)); }
LLSD::LLSD(Binary&& v) : impl(0) { ALLOC_LLSD_OBJECT; assign(std::move(v)); }
Andrey Lihatskiy
committed
void LLSD::assign(Boolean v) { safe(impl).assign(impl, v); }
void LLSD::assign(Integer v) { safe(impl).assign(impl, v); }
void LLSD::assign(Real v) { safe(impl).assign(impl, v); }
void LLSD::assign(const String& v) { safe(impl).assign(impl, v); }
void LLSD::assign(const UUID& v) { safe(impl).assign(impl, v); }
void LLSD::assign(const Date& v) { safe(impl).assign(impl, v); }
void LLSD::assign(const URI& v) { safe(impl).assign(impl, v); }
void LLSD::assign(const Binary& v) { safe(impl).assign(impl, v); }
void LLSD::assign(String&& v) { safe(impl).assign(impl, std::move(v)); }
void LLSD::assign(UUID&& v) { safe(impl).assign(impl, std::move(v)); }
void LLSD::assign(Date&& v) { safe(impl).assign(impl, std::move(v)); }
void LLSD::assign(URI&& v) { safe(impl).assign(impl, std::move(v)); }
void LLSD::assign(Binary&& v) { safe(impl).assign(impl, std::move(v)); }
Andrey Lihatskiy
committed
LLSD::Boolean LLSD::asBoolean() const { return safe(impl).asBoolean(); }
LLSD::Integer LLSD::asInteger() const { return safe(impl).asInteger(); }
LLSD::Real LLSD::asReal() const { return safe(impl).asReal(); }
LLSD::String LLSD::asString() const { return safe(impl).asString(); }
LLSD::UUID LLSD::asUUID() const { return safe(impl).asUUID(); }
LLSD::Date LLSD::asDate() const { return safe(impl).asDate(); }
LLSD::URI LLSD::asURI() const { return safe(impl).asURI(); }
const LLSD::Binary& LLSD::asBinary() const { return safe(impl).asBinary(); }
Don Kjer
committed
LLSD::map_t& LLSD::asMap() { return makeMap(impl).asMap(); };
const LLSD::map_t& LLSD::asMap() const { return safe(impl).asMap(); };
LLSD::array_t& LLSD::asArray() { return makeArray(impl).asArray(); };
const LLSD::array_t& LLSD::asArray() const { return safe(impl).asArray(); };
Don Kjer
committed
const LLSD::String& LLSD::asStringRef() const { return safe(impl).asStringRef(); }
Andrey Lihatskiy
committed
LLSD::LLSD(const char* v) : impl(0) { ALLOC_LLSD_OBJECT; assign(v); }
Andrey Lihatskiy
committed
if(v) assign(std::string(v));
else assign(std::string());
Andrey Lihatskiy
committed
LLSD v;
makeMap(v.impl);
return v;
bool LLSD::has(const std::string_view k) const { return safe(impl).has(k); }
LLSD LLSD::get(const std::string_view k) const { return safe(impl).get(k); }
Andrey Lihatskiy
committed
LLSD LLSD::getKeys() const { return safe(impl).getKeys(); }
void LLSD::insert(const std::string_view k, const LLSD& v) { makeMap(impl).insert(k, v); }
LLSD& LLSD::with(const std::string_view k, const LLSD& v)
Andrey Lihatskiy
committed
{
makeMap(impl).insert(k, v);
return *this;
}
void LLSD::erase(const String& k) { makeMap(impl).erase(k); }
LLSD& LLSD::operator[](const std::string_view k)
Andrey Lihatskiy
committed
{
Andrey Lihatskiy
committed
return makeMap(impl).ref(k);
David Parks
committed
}
const LLSD& LLSD::operator[](const std::string_view k) const
Andrey Lihatskiy
committed
{
Andrey Lihatskiy
committed
return safe(impl).ref(k);
David Parks
committed
}
Andrey Lihatskiy
committed
LLSD v;
makeArray(v.impl);
return v;
Andrey Lihatskiy
committed
size_t LLSD::size() const { return safe(impl).size(); }
LLSD LLSD::get(Integer i) const { return safe(impl).get(i); }
void LLSD::set(Integer i, const LLSD& v){ makeArray(impl).set(i, v); }
void LLSD::insert(Integer i, const LLSD& v) { makeArray(impl).insert(i, v); }
LLSD& LLSD::with(Integer i, const LLSD& v)
Andrey Lihatskiy
committed
{
makeArray(impl).insert(i, v);
return *this;
}
LLSD& LLSD::append(const LLSD& v) { return makeArray(impl).append(v); }
void LLSD::erase(Integer i) { makeArray(impl).erase(i); }
LLSD& LLSD::operator[](size_t i)
Andrey Lihatskiy
committed
{
Andrey Lihatskiy
committed
return makeArray(impl).ref(i);
David Parks
committed
}
const LLSD& LLSD::operator[](size_t i) const
Andrey Lihatskiy
committed
{
David Parks
committed
return safe(impl).ref(i);
}
static const char *llsd_dump(const LLSD &llsd, bool useXMLFormat)
{