summaryrefslogtreecommitdiff
path: root/indra/llcommon/llsd.cpp
blob: 77fe545c3f04ea704b96b10f1da9ad790762659a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
/**
 * @file llsd.cpp
 * @brief LLSD flexible data system
 *
 * $LicenseInfo:firstyear=2005&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2010, Linden Research, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */

// Must turn on conditional declarations in header file so definitions end up
// with proper linkage.
#define LLSD_DEBUG_INFO
#include "linden_common.h"
#include "llsd.h"

#include "llbase64.h"
#include "llerror.h"
#include "../llmath/llmath.h"
#include "llformat.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)

#ifndef LL_RELEASE_FOR_DOWNLOAD
#define NAME_UNNAMED_NAMESPACE
#endif

#ifdef NAME_UNNAMED_NAMESPACE
namespace LLSDUnnamedNamespace
#else
namespace
#endif
{
    class ImplMap;
    class ImplArray;
}

#ifdef NAME_UNNAMED_NAMESPACE
using namespace LLSDUnnamedNamespace;
#endif

namespace llsd
{

// statics
S32 sLLSDAllocationCount = 0;
S32 sLLSDNetObjects = 0;

} // namespace llsd

#define ALLOC_LLSD_OBJECT           { llsd::sLLSDNetObjects++;  llsd::sLLSDAllocationCount++;   }
#define FREE_LLSD_OBJECT            { llsd::sLLSDNetObjects--;                                  }

class LLSD::Impl
    /**< 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.

    */
{
protected:
    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;

public:
    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

    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, const char*);
    virtual void assign(Impl*& var, const LLSD::String&);
    virtual void assign(Impl*& var, const LLSD::UUID&);
    virtual void assign(Impl*& var, const LLSD::Date&);
    virtual void assign(Impl*& var, const LLSD::URI&);
    virtual void assign(Impl*& var, const LLSD::Binary&);
    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&&);
        ///< 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 String& asStringRef() const { static const std::string empty; return empty; }

    virtual String asXMLRPCValue() const { return "<nil/>"; }

    virtual bool has(std::string_view) const      { return false; }
    virtual LLSD get(std::string_view) const      { return LLSD(); }
    virtual LLSD getKeys() const                { return LLSD::emptyArray(); }
    virtual void erase(const String&)           { }
    virtual const LLSD& ref(std::string_view) const{ return undef(); }

    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 { static const std::map<String, LLSD> empty; return empty.end(); }
    virtual LLSD::array_const_iterator beginArray() const { return endArray(); }
    virtual LLSD::array_const_iterator endArray() const { static const std::vector<LLSD> empty; return empty.end(); }

    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;
};

#ifdef NAME_UNNAMED_NAMESPACE
namespace LLSDUnnamedNamespace
#else
namespace
#endif
{
    template<LLSD::Type T, class Data, class DataRef = Data, class DataMove = Data>
    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(DataRef value) : mValue(value) { }
        ImplBase(DataMove value) : mValue(std::move(value)) { }

        virtual LLSD::Type type() const { return T; }

        using LLSD::Impl::assign; // Unhiding base class virtuals...
        virtual void assign(LLSD::Impl*& var, DataRef value) {
            if (shared())
            {
                Impl::assign(var, value);
            }
            else
            {
                mValue = value;
            }
        }
        virtual void assign(LLSD::Impl*& var, DataMove value) {
            if (shared())
            {
                Impl::assign(var, std::move(value));
            }
            else
            {
                mValue = std::move(value);
            }
        }
    };


    class ImplBoolean final
        : public ImplBase<LLSD::TypeBoolean, LLSD::Boolean, LLSD::Boolean, 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;

        virtual LLSD::String asXMLRPCValue() const { return mValue ? "<boolean>1</boolean>" : "<boolean>0</boolean>"; }
    };

    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
        : public ImplBase<LLSD::TypeInteger, LLSD::Integer, LLSD::Integer, 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;

        virtual LLSD::String asXMLRPCValue() const { return "<int>" + std::to_string(mValue) + "</int>"; }
    };

    LLSD::String ImplInteger::asString() const
        { return llformat("%d", mValue); }


    class ImplReal final
        : public ImplBase<LLSD::TypeReal, LLSD::Real, LLSD::Real, 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;

        virtual LLSD::String asXMLRPCValue() const { return "<double>" + std::to_string(mValue) + "</double>"; }
    };

    LLSD::Boolean ImplReal::asBoolean() const
        { return !llisnan(mValue)  &&  mValue != 0.0; }

    LLSD::Integer ImplReal::asInteger() const
        { return !llisnan(mValue) ? (LLSD::Integer)mValue : 0; }

    LLSD::String ImplReal::asString() const
        { return llformat("%lg", mValue); }


    class ImplString final
        : public ImplBase<LLSD::TypeString, LLSD::String, const LLSD::String&, LLSD::String&&>
    {
    public:
        ImplString(const LLSD::String& v) : Base(v) { }
        ImplString(LLSD::String&& v) : Base(std::move(v)) {}

        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; }

        virtual LLSD::String asXMLRPCValue() const { return "<string>" + LLStringFn::xml_encode(mValue) + "</string>"; }

        using LLSD::Impl::assign; // Unhiding base class virtuals...
        virtual void assign(LLSD::Impl*& var, const char* value)
        {
            if (shared())
            {
                Impl::assign(var, value);
            }
            else
            {
                mValue = value;
            }
        }
    };

    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, const LLSD::UUID&, LLSD::UUID&&>
    {
    public:
        ImplUUID(const LLSD::UUID& v) : Base(v) { }
        ImplUUID(LLSD::UUID&& v) : Base(std::move(v)) { }

        virtual LLSD::String    asString() const{ return mValue.asString(); }
        virtual LLSD::UUID      asUUID() const  { return mValue; }

        virtual LLSD::String asXMLRPCValue() const { return "<string>" + mValue.asString() + "</string>"; }
    };


    class ImplDate final
        : public ImplBase<LLSD::TypeDate, LLSD::Date, const LLSD::Date&, LLSD::Date&&>
    {
    public:
        ImplDate(const LLSD::Date& v)
            : ImplBase(v)
            { }

        ImplDate(LLSD::Date&& v)
            : ImplBase(std::move(v))
        { }

        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; }

        virtual LLSD::String asXMLRPCValue() const { return "<dateTime.iso8601>" + mValue.toHTTPDateString("%FT%T") + "</dateTime.iso8601>"; }
    };


    class ImplURI final
        : public ImplBase<LLSD::TypeURI, LLSD::URI, const LLSD::URI&, LLSD::URI&&>
    {
    public:
        ImplURI(const LLSD::URI& v) : Base(v) { }
        ImplURI(LLSD::URI&& v) : Base(std::move(v)) { }

        virtual LLSD::String    asString() const{ return mValue.asString(); }
        virtual LLSD::URI       asURI() const   { return mValue; }

        virtual LLSD::String asXMLRPCValue() const { return "<string>" + LLStringFn::xml_encode(mValue.asString()) + "</string>"; }
    };


    class ImplBinary final
        : public ImplBase<LLSD::TypeBinary, LLSD::Binary, const LLSD::Binary&, LLSD::Binary&&>
    {
    public:
        ImplBinary(const LLSD::Binary& v) : Base(v) { }
        ImplBinary(LLSD::Binary&& v) : Base(std::move(v)) { }

        virtual const LLSD::Binary& asBinary() const{ return mValue; }

        virtual LLSD::String asXMLRPCValue() const { return "<base64>" + LLBase64::encode(mValue.data(), mValue.size()) + "</base64>"; }
    };


    class ImplMap final : public LLSD::Impl
    {
    private:
        typedef std::map<LLSD::String, LLSD, std::less<>> DataMap;

        DataMap mData;

    protected:
        ImplMap(const DataMap& data) : mData(data) { }

    public:
        ImplMap() { }

        virtual ImplMap& makeMap(LLSD::Impl*&);

        virtual LLSD::Type type() const { return LLSD::TypeMap; }

        virtual LLSD::Boolean asBoolean() const { return !mData.empty(); }

        virtual LLSD::String asXMLRPCValue() const
        {
            std::ostringstream os;
            os << "<struct>";
            for (const auto& it : mData)
            {
                os << "<member><name>" << LLStringFn::xml_encode(it.first) << "</name>"
                    << it.second.asXMLRPCValue() << "</member>";
            }
            os << "</struct>";
            return os.str();
        }

        virtual bool has(std::string_view) const;

        using LLSD::Impl::get; // Unhiding get(size_t)
        using LLSD::Impl::erase; // Unhiding erase(size_t)
        using LLSD::Impl::ref; // Unhiding ref(size_t)
        virtual LLSD get(std::string_view) const;
        virtual LLSD getKeys() const;
        void insert(std::string_view k, const LLSD& v);
        virtual void erase(const LLSD::String&);
                      LLSD& ref(std::string_view);
        virtual const LLSD& ref(std::string_view) const;

        virtual size_t size() const { return mData.size(); }

        LLSD::map_iterator beginMap() { return mData.begin(); }
        LLSD::map_iterator endMap() { return mData.end(); }
        virtual LLSD::map_const_iterator beginMap() const { return mData.begin(); }
        virtual LLSD::map_const_iterator endMap() const { return mData.end(); }

        virtual void dumpStats() const;
        virtual void calcStats(S32 type_counts[], S32 share_counts[]) const;
    };

    ImplMap& ImplMap::makeMap(LLSD::Impl*& var)
    {
        LL_PROFILE_ZONE_SCOPED_CATEGORY_LLSD;
        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
    {
        LL_PROFILE_ZONE_SCOPED_CATEGORY_LLSD;
        DataMap::const_iterator i = mData.find(k);
        return i != mData.end();
    }

    LLSD ImplMap::get(const std::string_view k) const
    {
        LL_PROFILE_ZONE_SCOPED_CATEGORY_LLSD;
        DataMap::const_iterator i = mData.find(k);
        return (i != mData.end()) ? i->second : LLSD();
    }

    LLSD ImplMap::getKeys() const
    {
        LL_PROFILE_ZONE_SCOPED_CATEGORY_LLSD;
        LLSD keys = LLSD::emptyArray();
        DataMap::const_iterator iter = mData.begin();
        while (iter != mData.end())
        {
            keys.append((*iter).first);
            iter++;
        }
        return keys;
    }

    void ImplMap::insert(std::string_view k, const LLSD& v)
    {
        LL_PROFILE_ZONE_SCOPED_CATEGORY_LLSD;
        mData.emplace(k, v);
    }

    void ImplMap::erase(const LLSD::String& k)
    {
        LL_PROFILE_ZONE_SCOPED_CATEGORY_LLSD;
        mData.erase(k);
    }

    LLSD& ImplMap::ref(std::string_view k)
    {
        DataMap::iterator i = mData.lower_bound(k);
        if (i == mData.end() || mData.key_comp()(k, i->first))
        {
            return mData.emplace_hint(i, std::make_pair(k, LLSD()))->second;
        }

        return i->second;
    }

    const LLSD& ImplMap::ref(std::string_view k) const
    {
        DataMap::const_iterator i = mData.lower_bound(k);
        if (i == mData.end() || mData.key_comp()(k, i->first))
        {
            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);
            ++iter;
        }

        // Add in the values for this map
        Impl::calcStats(type_counts, share_counts);
    }


    class ImplArray : public LLSD::Impl
    {
    private:
        typedef std::vector<LLSD> DataVector;

        DataVector mData;

    protected:
        ImplArray(const DataVector& data) : mData(data) { }

    public:
        ImplArray() { }

        virtual ImplArray& makeArray(Impl*&);

        virtual LLSD::Type type() const { return LLSD::TypeArray; }

        virtual LLSD::Boolean asBoolean() const { return !mData.empty(); }

        virtual LLSD::String asXMLRPCValue() const
        {
            std::ostringstream os;
            os << "<array><data>";
            for (const auto& it : mData)
            {
                os << it.asXMLRPCValue();
            }
            os << "</data></array>";
            return os.str();
        }

        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;
        virtual LLSD get(size_t) const;
                void set(size_t, const LLSD&);
                void insert(size_t, const LLSD&);
                LLSD& append(const LLSD&);
        virtual void erase(size_t);
                      LLSD& ref(size_t);
        virtual const LLSD& ref(size_t) const;

        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(); }

        virtual void calcStats(S32 type_counts[], S32 share_counts[]) const;
    };

    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);
            ++iter;
        }

        // Add in the values for this array
        Impl::calcStats(type_counts, share_counts);
    }
}

LLSD::Impl::Impl()
    : mUseCount(0)
{
    ++sAllocationCount;
    ++sOutstandingCount;
}

LLSD::Impl::Impl(StaticAllocationMarker)
    : mUseCount(0)
{
}

LLSD::Impl::~Impl()
{
    --sOutstandingCount;
}

void LLSD::Impl::reset(Impl*& var, Impl* impl)
{
    if (impl && impl->mUseCount != STATIC_USAGE_COUNT)
    {
        ++impl->mUseCount;
    }
    if (var  &&  var->mUseCount != STATIC_USAGE_COUNT && --var->mUseCount == 0)
    {
        delete var;
    }
    var = impl;
}

void LLSD::Impl::move(Impl*& var, Impl*& impl)
{
    if (var && var->mUseCount != STATIC_USAGE_COUNT && --var->mUseCount == 0)
    {
        delete var; // destroy var if usage falls to 0 and not static
    }
    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)
{
    static Impl theUndefined(STATIC_USAGE_COUNT);
    return impl ? *impl : theUndefined;
}

const LLSD::Impl& LLSD::Impl::safe(const Impl* impl)
{
    static Impl theUndefined(STATIC_USAGE_COUNT);
    return impl ? *impl : theUndefined;
}

ImplMap& LLSD::Impl::makeMap(Impl*& var)
{
    LL_PROFILE_ZONE_SCOPED_CATEGORY_LLSD;
    ImplMap* im = new ImplMap;
    reset(var, im);
    return *im;
}

ImplArray& LLSD::Impl::makeArray(Impl*& var)
{
    ImplArray* ia = new ImplArray;
    reset(var, ia);
    return *ia;
}


void LLSD::Impl::assign(Impl*& var, const Impl* other)
{
    reset(var, const_cast<Impl*>(other));
}

void LLSD::Impl::assignUndefined(Impl*& var)
{
    reset(var, 0);
}

void LLSD::Impl::assign(Impl*& var, LLSD::Boolean v)
{
    reset(var, new ImplBoolean(v));
}

void LLSD::Impl::assign(Impl*& var, LLSD::Integer v)
{
    reset(var, new ImplInteger(v));
}

void LLSD::Impl::assign(Impl*& var, LLSD::Real v)
{
    reset(var, new ImplReal(v));
}

void LLSD::Impl::assign(Impl*& var, const char* v)
{
    reset(var, new ImplString(v));
}

void LLSD::Impl::assign(Impl*& var, const LLSD::String& v)
{
    reset(var, new ImplString(v));
}

void LLSD::Impl::assign(Impl*& var, const LLSD::UUID& v)
{
    reset(var, new ImplUUID(v));
}

void LLSD::Impl::assign(Impl*& var, const LLSD::Date& v)
{
    reset(var, new ImplDate(v));
}

void LLSD::Impl::assign(Impl*& var, const LLSD::URI& v)
{
    reset(var, new ImplURI(v));
}

void LLSD::Impl::assign(Impl*& var, const LLSD::Binary& v)
{
    reset(var, new ImplBinary(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)));
}


const LLSD& LLSD::Impl::undef()
{
    static const LLSD immutableUndefined;
    return immutableUndefined;
}

void LLSD::Impl::dumpStats() const
{
    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
{
    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;



#ifdef NAME_UNNAMED_NAMESPACE
namespace LLSDUnnamedNamespace
#else
namespace
#endif
{
    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(0)                  { ALLOC_LLSD_OBJECT; }
LLSD::~LLSD()                           { FREE_LLSD_OBJECT; Impl::reset(impl, 0); }

LLSD::LLSD(const LLSD& other) : impl(0) { ALLOC_LLSD_OBJECT;  assign(other); }
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) noexcept { Impl::move(impl, other.impl); return *this; }

void LLSD::clear()                      { Impl::assignUndefined(impl); }

LLSD::Type LLSD::type() const           { return safe(impl).type(); }

// Scalar Constructors
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)); }

// Scalar Assignment
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)); }

// Scalar Accessors
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(); }

const LLSD::String& LLSD::asStringRef() const { return safe(impl).asStringRef(); }

LLSD::String LLSD::asXMLRPCValue() const { return "<value>" + safe(impl).asXMLRPCValue() + "</value>"; }

// const char * helpers
LLSD::LLSD(const char* v) : impl(0)     { ALLOC_LLSD_OBJECT;    assign(v); }
void LLSD::assign(const char* v)
{
    if(v) safe(impl).assign(impl, v);
    else assign(std::string());
}


LLSD LLSD::emptyMap()
{
    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); }
LLSD LLSD::getKeys() const              { return safe(impl).getKeys(); }
void LLSD::insert(std::string_view k, const LLSD& v) { makeMap(impl).insert(k, v); }

LLSD& LLSD::with(std::string_view k, const LLSD& v)
                                        {
                                            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)
{
    LL_PROFILE_ZONE_SCOPED_CATEGORY_LLSD;
    return makeMap(impl).ref(k);
}
const LLSD& LLSD::operator[](const std::string_view k) const
{
    LL_PROFILE_ZONE_SCOPED_CATEGORY_LLSD;
    return safe(impl).ref(k);
}

LLSD LLSD::emptyArray()
{
    LLSD v;
    makeArray(v.impl);
    return v;
}

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)
                                        {
                                            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)
{
    LL_PROFILE_ZONE_SCOPED_CATEGORY_LLSD;
    return makeArray(impl).ref(i);
}
const LLSD& LLSD::operator[](size_t i) const
{
    LL_PROFILE_ZONE_SCOPED_CATEGORY_LLSD;
    return safe(impl).ref(i);
}

static const char *llsd_dump(const LLSD &llsd, bool useXMLFormat)
{
    // sStorage is used to hold the string representation of the llsd last
    // passed into this function.  If this function is never called (the
    // normal case when not debugging), nothing is allocated.  Otherwise
    // sStorage will point to the result of the last call.  This will actually
    // be one leak, but since this is used only when running under the
    // debugger, it should not be an issue.
    static char *sStorage = NULL;
    delete[] sStorage;
    std::string out_string;
    {
        std::ostringstream out;
        if (useXMLFormat)
            out << LLSDXMLStreamer(llsd);
        else
            out << LLSDNotationStreamer(llsd);
        out_string = out.str();
    }
    auto len = out_string.length();
    sStorage = new char[len + 1];
    memcpy(sStorage, out_string.c_str(), len);
    sStorage[len] = '\0';
    return sStorage;
}

/// Returns XML version of llsd -- only to be called from debugger
const char *LLSD::dumpXML(const LLSD &llsd)
{
    return llsd_dump(llsd, true);
}

/// Returns Notation version of llsd -- only to be called from debugger
const char *LLSD::dump(const LLSD &llsd)
{
    return llsd_dump(llsd, false);
}

LLSD::map_iterator          LLSD::beginMap()        { return makeMap(impl).beginMap(); }
LLSD::map_iterator          LLSD::endMap()          { return makeMap(impl).endMap(); }
LLSD::map_const_iterator    LLSD::beginMap() const  { return safe(impl).beginMap(); }
LLSD::map_const_iterator    LLSD::endMap() const    { return safe(impl).endMap(); }

LLSD::array_iterator        LLSD::beginArray()      { return makeArray(impl).beginArray(); }
LLSD::array_iterator        LLSD::endArray()        { return makeArray(impl).endArray(); }
LLSD::array_const_iterator  LLSD::beginArray() const{ return safe(impl).beginArray(); }
LLSD::array_const_iterator  LLSD::endArray() const  { return safe(impl).endArray(); }

LLSD::reverse_array_iterator    LLSD::rbeginArray()     { return makeArray(impl).rbeginArray(); }
LLSD::reverse_array_iterator    LLSD::rendArray()       { return makeArray(impl).rendArray(); }

namespace llsd
{

U32 allocationCount()                               { return LLSD::Impl::sAllocationCount; }
U32 outstandingCount()                              { return LLSD::Impl::sOutstandingCount; }

// Diagnostic dump of contents in an LLSD object
void dumpStats(const LLSD& llsd)                    { LLSD::Impl::getImpl(llsd).dumpStats(); }

} // namespace llsd

// static
std::string     LLSD::typeString(Type type)
{
    static const char * sTypeNameArray[] = {
        "Undefined",
        "Boolean",
        "Integer",
        "Real",
        "String",
        "UUID",
        "Date",
        "URI",
        "Binary",
        "Map",
        "Array"
    };

    if (0 <= type && type < LL_ARRAY_SIZE(sTypeNameArray))
    {
        return sTypeNameArray[type];
    }
    return STRINGIZE("** invalid type value " << type);
}