@@ -553,6 +553,61 @@ LLSD shallow(LLSD value, LLSD filter=LLSD()) { return llsd_shallow(value, filter
553553
554554} // namespace llsd
555555
556+ /* ****************************************************************************
557+ * toArray(), toMap()
558+ *****************************************************************************/
559+ namespace llsd
560+ {
561+
562+ // For some T convertible to LLSD, given std::vector<T> myVec,
563+ // toArray(myVec) returns an LLSD array whose entries correspond to the
564+ // items in myVec.
565+ // For some U convertible to LLSD, given function U xform(const T&),
566+ // toArray(myVec, xform) returns an LLSD array whose every entry is
567+ // xform(item) of the corresponding item in myVec.
568+ // toArray() actually works with any container<C> usable with range
569+ // 'for', not just std::vector.
570+ // (Once we get C++20 we can use std::identity instead of this default lambda.)
571+ template <typename C, typename FUNC>
572+ LLSD toArray (const C& container, FUNC&& func = [](const auto & arg) { return arg; })
573+ {
574+ LLSD array;
575+ for (const auto & item : container)
576+ {
577+ array.append (std::forward<FUNC>(func)(item));
578+ }
579+ return array;
580+ }
581+
582+ // For some T convertible to LLSD, given std::map<std::string, T> myMap,
583+ // toMap(myMap) returns an LLSD map whose entries correspond to the
584+ // (key, value) pairs in myMap.
585+ // For some U convertible to LLSD, given function
586+ // std::pair<std::string, U> xform(const std::pair<std::string, T>&),
587+ // toMap(myMap, xform) returns an LLSD map whose every entry is
588+ // xform(pair) of the corresponding (key, value) pair in myMap.
589+ // toMap() actually works with any container usable with range 'for', not
590+ // just std::map. It need not even be an associative container, as long as
591+ // you pass an xform function that returns std::pair<std::string, U>.
592+ // (Once we get C++20 we can use std::identity instead of this default lambda.)
593+ template <typename C, typename FUNC>
594+ LLSD toMap (const C& container, FUNC&& func = [](const auto & arg) { return arg; })
595+ {
596+ LLSD map;
597+ for (const auto & pair : container)
598+ {
599+ const auto & [key, value] = std::forward<FUNC>(func)(pair);
600+ map[key] = value;
601+ }
602+ return map;
603+ }
604+
605+ } // namespace llsd
606+
607+ /* ****************************************************************************
608+ * boost::hash<LLSD>
609+ *****************************************************************************/
610+
556611// Specialization for generating a hash value from an LLSD block.
557612namespace boost
558613{
0 commit comments