From a2379d68713e210187149c05dd20435d7a244503 Mon Sep 17 00:00:00 2001 From: Nat Goodspeed Date: Thu, 30 May 2019 15:35:54 -0400 Subject: SL-11216: Add llsd::drill() function to drill into an LLSD blob. We include both const and non-const overloads. The latter returns LLSD&, so you can assign to the located element. In fact we already implemented the non-const logic in a less public form as storeToLLSDPath() in lleventcoro.cpp. Reimplement the latter to use the new llsd::drill() function. --- indra/llcommon/llsdutil.h | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'indra/llcommon/llsdutil.h') diff --git a/indra/llcommon/llsdutil.h b/indra/llcommon/llsdutil.h index 01ab6bcb8d..e659aa574e 100644 --- a/indra/llcommon/llsdutil.h +++ b/indra/llcommon/llsdutil.h @@ -142,6 +142,31 @@ template LLSD llsd_copy_array(Input iter, Input end) return dest; } +namespace llsd +{ + +/** + * Drill down to locate an element in 'blob' according to 'path', where 'path' + * is one of the following: + * + * - LLSD::String: 'blob' is an LLSD::Map. Find the entry with key 'path'. + * - LLSD::Integer: 'blob' is an LLSD::Array. Find the entry with index 'path'. + * - Any other 'path' type will be interpreted as LLSD::Array, and 'blob' is a + * nested structure. For each element of 'path': + * - If it's an LLSD::Integer, select the entry with that index from an + * LLSD::Array at that level. + * - If it's an LLSD::String, select the entry with that key from an + * LLSD::Map at that level. + * - Anything else is an error. + * + * By implication, if path.isUndefined() or otherwise equivalent to an empty + * LLSD::Array, drill() returns 'blob' as is. + */ +LLSD drill(const LLSD& blob, const LLSD& path); +LLSD& drill( LLSD& blob, const LLSD& path); + +} + /***************************************************************************** * LLSDArray *****************************************************************************/ -- cgit v1.2.3 From 52d15b64456f2afc54d2fcc30e22c1078e2db12c Mon Sep 17 00:00:00 2001 From: Nat Goodspeed Date: Mon, 14 Oct 2019 15:49:37 -0400 Subject: DRTVWR-476: Add llsd::array() and llsd::map() variadic functions. llsd::array(), as one might suspect, takes an arbitrary number of arguments of arbitrary convertible types and returns an LLSD::Array constructed from those elements. This supercedes the older LLSDArray class. llsd::map() takes an even number of arguments paired as (LLSD::String, arbitrary convertible type) and returns an LLSD::Map constructed from those (key, value) pairs. This supercedes the older LLSDMap class. These two functions not only have a simpler API -- arbitrary function arguments rather than an (arg list)(arg list) sequence -- but also specifically return a final LLSD object, rather than needing conversion to LLSD from the LLSDArray or LLSDMap object. Also support LLSD == LLSD and LLSD != LLSD comparisons, using llsd_equals() with default exact-float-equality semantics. --- indra/llcommon/llsdutil.h | 70 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) (limited to 'indra/llcommon/llsdutil.h') diff --git a/indra/llcommon/llsdutil.h b/indra/llcommon/llsdutil.h index e659aa574e..2a8b44ec4e 100644 --- a/indra/llcommon/llsdutil.h +++ b/indra/llcommon/llsdutil.h @@ -129,6 +129,16 @@ LL_COMMON_API std::string llsd_matches(const LLSD& prototype, const LLSD& data, /// equality rather than bitwise equality, pass @a bits as for /// is_approx_equal_fraction(). LL_COMMON_API bool llsd_equals(const LLSD& lhs, const LLSD& rhs, int bits=-1); +/// If you don't care about LLSD::Real equality +inline bool operator==(const LLSD& lhs, const LLSD& rhs) +{ + return llsd_equals(lhs, rhs); +} +inline bool operator!=(const LLSD& lhs, const LLSD& rhs) +{ + // operator!=() should always be the negation of operator==() + return ! (lhs == rhs); +} // Simple function to copy data out of input & output iterators if // there is no need for casting. @@ -236,6 +246,36 @@ private: LLSD _data; }; +namespace llsd +{ + +/** + * Construct an LLSD::Array inline, using modern C++ variadic arguments. + */ + +// recursion tail +inline +void array_(LLSD&) {} + +// recursive call +template +void array_(LLSD& data, T0&& v0, Ts&&... vs) +{ + data.append(std::forward(v0)); + array_(data, std::forward(vs)...); +} + +// public interface +template +LLSD array(Ts&&... vs) +{ + LLSD data; + array_(data, std::forward(vs)...); + return data; +} + +} // namespace llsd + /***************************************************************************** * LLSDMap *****************************************************************************/ @@ -280,6 +320,36 @@ private: LLSD _data; }; +namespace llsd +{ + +/** + * Construct an LLSD::Map inline, using modern C++ variadic arguments. + */ + +// recursion tail +inline +void map_(LLSD&) {} + +// recursive call +template +void map_(LLSD& data, const LLSD::String& k0, T0&& v0, Ts&&... vs) +{ + data[k0] = v0; + map_(data, std::forward(vs)...); +} + +// public interface +template +LLSD map(Ts&&... vs) +{ + LLSD data; + map_(data, std::forward(vs)...); + return data; +} + +} // namespace llsd + /***************************************************************************** * LLSDParam *****************************************************************************/ -- cgit v1.2.3 From 61ec84b1d88a5972cd8cc7ed0f5ad9f0ae92c27c Mon Sep 17 00:00:00 2001 From: Nat Goodspeed Date: Wed, 6 May 2020 16:16:06 -0400 Subject: DRTVWR-476: Add llsd::clone(), llsd::shallow() aliases for new llsd_clone(), llsd_shallow() functions. --- indra/llcommon/llsdutil.h | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'indra/llcommon/llsdutil.h') diff --git a/indra/llcommon/llsdutil.h b/indra/llcommon/llsdutil.h index 2ff9ecdf89..84be95ba54 100644 --- a/indra/llcommon/llsdutil.h +++ b/indra/llcommon/llsdutil.h @@ -547,6 +547,16 @@ LLSD llsd_clone(LLSD value, LLSD filter = LLSD()); // the filter parameter. LLSD llsd_shallow(LLSD value, LLSD filter = LLSD()); +namespace llsd +{ + +// llsd namespace aliases +inline +LLSD clone (LLSD value, LLSD filter=LLSD()) { return llsd_clone (value, filter); } +inline +LLSD shallow(LLSD value, LLSD filter=LLSD()) { return llsd_shallow(value, filter); } + +} // namespace llsd // Specialization for generating a hash value from an LLSD block. template <> -- cgit v1.2.3