cpp-common: add `bt2s::optional`, alias of `nonstd::optional`
[babeltrace.git] / src / cpp-common / bt2 / value.hpp
index a065af6c0c8715dec1c129847c6afde4c9d2ccb2..19919ed205fbfd0c06c85fea3015ce2416b529ae 100644 (file)
@@ -7,43 +7,44 @@
 #ifndef BABELTRACE_CPP_COMMON_BT2_VALUE_HPP
 #define BABELTRACE_CPP_COMMON_BT2_VALUE_HPP
 
-#include <type_traits>
 #include <cstdint>
 #include <functional>
-#include <iterator>
+#include <type_traits>
+
 #include <babeltrace2/babeltrace.h>
 
 #include "common/assert.h"
 #include "common/common.h"
-#include "common-iter.hpp"
-#include "internal/borrowed-obj.hpp"
-#include "internal/shared-obj.hpp"
+#include "cpp-common/bt2s/optional.hpp"
+
+#include "borrowed-object-iterator.hpp"
+#include "borrowed-object.hpp"
+#include "exc.hpp"
 #include "internal/utils.hpp"
-#include "cpp-common/optional.hpp"
-#include "cpp-common/string_view.hpp"
-#include "lib-error.hpp"
+#include "raw-value-proxy.hpp"
+#include "shared-object.hpp"
 
 namespace bt2 {
 namespace internal {
 
 struct ValueRefFuncs final
 {
-    static void get(const bt_value * const libObjPtr)
+    static void get(const bt_value * const libObjPtr) noexcept
     {
         bt_value_get_ref(libObjPtr);
     }
 
-    static void put(const bt_value * const libObjPtr)
+    static void put(const bt_value * const libObjPtr) noexcept
     {
         bt_value_put_ref(libObjPtr);
     }
 };
 
-template <typename ObjT, typename LibObjT>
-using SharedValue = internal::SharedObj<ObjT, LibObjT, internal::ValueRefFuncs>;
-
 } /* namespace internal */
 
+template <typename ObjT, typename LibObjT>
+using SharedValue = SharedObject<ObjT, LibObjT, internal::ValueRefFuncs>;
+
 template <typename LibObjT>
 class CommonNullValue;
 
@@ -80,71 +81,64 @@ enum class ValueType
     MAP = BT_VALUE_TYPE_MAP,
 };
 
-template <typename LibObjT>
-class CommonClockClass;
-
-template <typename LibObjT>
-class CommonFieldClass;
-
-template <typename LibObjT>
-class CommonTraceClass;
-
-template <typename LibObjT>
-class CommonStreamClass;
+template <typename ValueObjT>
+class CommonValueRawValueProxy final
+{
+public:
+    explicit CommonValueRawValueProxy(const ValueObjT obj) : _mObj {obj}
+    {
+    }
 
-template <typename LibObjT>
-class CommonEventClass;
+    CommonValueRawValueProxy& operator=(bool rawVal) noexcept;
+    CommonValueRawValueProxy& operator=(std::int64_t rawVal) noexcept;
+    CommonValueRawValueProxy& operator=(std::uint64_t rawVal) noexcept;
+    CommonValueRawValueProxy& operator=(double rawVal) noexcept;
+    CommonValueRawValueProxy& operator=(const char *rawVal);
+    CommonValueRawValueProxy& operator=(const std::string& rawVal);
+    operator bool() const noexcept;
+    operator std::int64_t() const noexcept;
+    operator std::uint64_t() const noexcept;
+    operator double() const noexcept;
+    operator const char *() const noexcept;
 
-template <typename LibObjT>
-class CommonStream;
+private:
+    ValueObjT _mObj;
+};
 
 template <typename LibObjT>
-class CommonValue : public internal::BorrowedObj<LibObjT>
+class CommonValue : public BorrowedObject<LibObjT>
 {
-    /* Allow append() to call `val.libObjPtr()` */
-    friend class CommonArrayValue<bt_value>;
-
-    /* Allow insert() to call `val.libObjPtr()` */
-    friend class CommonMapValue<bt_value>;
-
-    /* Allow userAttributes() to call `val.libObjPtr()` */
-    friend class CommonClockClass<bt_clock_class>;
-    friend class CommonFieldClass<bt_field_class>;
-    friend class CommonTraceClass<bt_trace_class>;
-    friend class CommonStreamClass<bt_stream_class>;
-    friend class CommonEventClass<bt_event_class>;
-    friend class CommonStream<bt_stream>;
-
-    /* Allow operator==() to call `other.libObjPtr()` */
-    friend class CommonValue<bt_value>;
-    friend class CommonValue<const bt_value>;
-
 private:
-    using typename internal::BorrowedObj<LibObjT>::_ThisBorrowedObj;
+    using typename BorrowedObject<LibObjT>::_ThisBorrowedObject;
 
 protected:
-    using typename internal::BorrowedObj<LibObjT>::_LibObjPtr;
+    using typename BorrowedObject<LibObjT>::_LibObjPtr;
     using _ThisCommonValue = CommonValue<LibObjT>;
 
 public:
-    using Shared = internal::SharedValue<CommonValue<LibObjT>, LibObjT>;
+    using Shared = SharedValue<CommonValue<LibObjT>, LibObjT>;
 
-    explicit CommonValue(const _LibObjPtr libObjPtr) noexcept : _ThisBorrowedObj {libObjPtr}
+    explicit CommonValue(const _LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr}
     {
     }
 
     template <typename OtherLibObjT>
-    CommonValue(const CommonValue<OtherLibObjT>& val) noexcept : _ThisBorrowedObj {val}
+    CommonValue(const CommonValue<OtherLibObjT> val) noexcept : _ThisBorrowedObject {val}
     {
     }
 
     template <typename OtherLibObjT>
-    _ThisCommonValue& operator=(const CommonValue<OtherLibObjT>& val) noexcept
+    _ThisCommonValue operator=(const CommonValue<OtherLibObjT> val) noexcept
     {
-        _ThisBorrowedObj::operator=(val);
+        _ThisBorrowedObject::operator=(val);
         return *this;
     }
 
+    CommonValue<const bt_value> asConst() const noexcept
+    {
+        return CommonValue<const bt_value> {*this};
+    }
+
     ValueType type() const noexcept
     {
         return static_cast<ValueType>(bt_value_get_type(this->libObjPtr()));
@@ -196,17 +190,79 @@ public:
     }
 
     template <typename OtherLibObjT>
-    bool operator==(const CommonValue<OtherLibObjT>& other) const noexcept
+    bool operator==(const CommonValue<OtherLibObjT> other) const noexcept
     {
         return static_cast<bool>(bt_value_is_equal(this->libObjPtr(), other.libObjPtr()));
     }
 
     template <typename OtherLibObjT>
-    bool operator!=(const CommonValue<OtherLibObjT>& other) const noexcept
+    bool operator!=(const CommonValue<OtherLibObjT> other) const noexcept
     {
         return !(*this == other);
     }
 
+    CommonValueRawValueProxy<CommonValue> operator*() const noexcept
+    {
+        return CommonValueRawValueProxy<CommonValue> {*this};
+    }
+
+    std::uint64_t arrayLength() const noexcept
+    {
+        return this->asArray().length();
+    }
+
+    bool arrayIsEmpty() const noexcept
+    {
+        return this->asArray().isEmpty();
+    }
+
+    CommonValue<LibObjT> operator[](const std::uint64_t index) const noexcept
+    {
+        return this->asArray()[index];
+    }
+
+    template <typename T>
+    void append(T&& elem) const
+    {
+        this->asArray().append(std::forward<T>(elem));
+    }
+
+    CommonArrayValue<bt_value> appendEmptyArray() const;
+    CommonMapValue<bt_value> appendEmptyMap() const;
+
+    std::uint64_t mapLength() const noexcept
+    {
+        return this->asMap().length();
+    }
+
+    bool mapIsEmpty() const noexcept
+    {
+        return this->asMap().isEmpty();
+    }
+
+    template <typename KeyT>
+    bt2s::optional<CommonValue<LibObjT>> operator[](KeyT&& key) const noexcept
+    {
+        return this->asMap()[std::forward<KeyT>(key)];
+    }
+
+    template <typename KeyT>
+    bool hasEntry(KeyT&& key) const noexcept
+    {
+        return this->asMap().hasEntry(std::forward<KeyT>(key));
+    }
+
+    template <typename KeyT, typename ValT>
+    void insert(KeyT&& key, ValT&& val) const
+    {
+        this->asMap().insert(std::forward<KeyT>(key), std::forward<ValT>(val));
+    }
+
+    CommonArrayValue<bt_value> insertEmptyArray(const char *key) const;
+    CommonArrayValue<bt_value> insertEmptyArray(const std::string& key) const;
+    CommonMapValue<bt_value> insertEmptyMap(const char *key) const;
+    CommonMapValue<bt_value> insertEmptyMap(const std::string& key) const;
+
     Shared shared() const noexcept
     {
         return Shared::createWithRef(*this);
@@ -237,6 +293,83 @@ protected:
 using Value = CommonValue<bt_value>;
 using ConstValue = CommonValue<const bt_value>;
 
+template <typename ValueObjT>
+CommonValueRawValueProxy<ValueObjT>&
+CommonValueRawValueProxy<ValueObjT>::operator=(const bool rawVal) noexcept
+{
+    _mObj.asBool().value(rawVal);
+    return *this;
+}
+
+template <typename ValueObjT>
+CommonValueRawValueProxy<ValueObjT>&
+CommonValueRawValueProxy<ValueObjT>::operator=(const std::int64_t rawVal) noexcept
+{
+    _mObj.asSignedInteger().value(rawVal);
+    return *this;
+}
+
+template <typename ValueObjT>
+CommonValueRawValueProxy<ValueObjT>&
+CommonValueRawValueProxy<ValueObjT>::operator=(const std::uint64_t rawVal) noexcept
+{
+    _mObj.asUnsignedInteger().value(rawVal);
+    return *this;
+}
+
+template <typename ValueObjT>
+CommonValueRawValueProxy<ValueObjT>&
+CommonValueRawValueProxy<ValueObjT>::operator=(const double rawVal) noexcept
+{
+    _mObj.asReal().value(rawVal);
+    return *this;
+}
+
+template <typename ValueObjT>
+CommonValueRawValueProxy<ValueObjT>&
+CommonValueRawValueProxy<ValueObjT>::operator=(const char * const rawVal)
+{
+    _mObj.asString().value(rawVal);
+    return *this;
+}
+
+template <typename ValueObjT>
+CommonValueRawValueProxy<ValueObjT>&
+CommonValueRawValueProxy<ValueObjT>::operator=(const std::string& rawVal)
+{
+    return *this = rawVal.data();
+}
+
+template <typename ValueObjT>
+CommonValueRawValueProxy<ValueObjT>::operator bool() const noexcept
+{
+    return _mObj.asBool().value();
+}
+
+template <typename ValueObjT>
+CommonValueRawValueProxy<ValueObjT>::operator std::int64_t() const noexcept
+{
+    return _mObj.asSignedInteger().value();
+}
+
+template <typename ValueObjT>
+CommonValueRawValueProxy<ValueObjT>::operator std::uint64_t() const noexcept
+{
+    return _mObj.asUnsignedInteger().value();
+}
+
+template <typename ValueObjT>
+CommonValueRawValueProxy<ValueObjT>::operator double() const noexcept
+{
+    return _mObj.asReal().value();
+}
+
+template <typename ValueObjT>
+CommonValueRawValueProxy<ValueObjT>::operator const char *() const noexcept
+{
+    return _mObj.asString().value();
+}
+
 namespace internal {
 
 struct ValueTypeDescr
@@ -264,24 +397,29 @@ private:
     using typename CommonValue<LibObjT>::_ThisCommonValue;
 
 public:
-    using Shared = internal::SharedValue<CommonNullValue<LibObjT>, LibObjT>;
+    using Shared = SharedValue<CommonNullValue<LibObjT>, LibObjT>;
 
     CommonNullValue() noexcept : _ThisCommonValue {bt_value_null}
     {
     }
 
     template <typename OtherLibObjT>
-    CommonNullValue(const CommonNullValue<OtherLibObjT>& val) noexcept : _ThisCommonValue {val}
+    CommonNullValue(const CommonNullValue<OtherLibObjT> val) noexcept : _ThisCommonValue {val}
     {
     }
 
     template <typename OtherLibObjT>
-    CommonNullValue<LibObjT>& operator=(const CommonNullValue<OtherLibObjT>& val) noexcept
+    CommonNullValue<LibObjT> operator=(const CommonNullValue<OtherLibObjT> val) noexcept
     {
         _ThisCommonValue::operator=(val);
         return *this;
     }
 
+    CommonNullValue<const bt_value> asConst() const noexcept
+    {
+        return CommonNullValue<const bt_value> {*this};
+    }
+
     Shared shared() const noexcept
     {
         return Shared::createWithRef(*this);
@@ -319,7 +457,7 @@ private:
     using typename CommonValue<LibObjT>::_ThisCommonValue;
 
 public:
-    using Shared = internal::SharedValue<CommonBoolValue<LibObjT>, LibObjT>;
+    using Shared = SharedValue<CommonBoolValue<LibObjT>, LibObjT>;
     using Value = bool;
 
     explicit CommonBoolValue(const _LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr}
@@ -328,7 +466,7 @@ public:
     }
 
     template <typename OtherLibObjT>
-    CommonBoolValue(const CommonBoolValue<OtherLibObjT>& val) noexcept : _ThisCommonValue {val}
+    CommonBoolValue(const CommonBoolValue<OtherLibObjT> val) noexcept : _ThisCommonValue {val}
     {
     }
 
@@ -341,18 +479,20 @@ public:
     }
 
     template <typename OtherLibObjT>
-    CommonBoolValue<LibObjT>& operator=(const CommonBoolValue<OtherLibObjT>& val) noexcept
+    CommonBoolValue<LibObjT> operator=(const CommonBoolValue<OtherLibObjT> val) noexcept
     {
         _ThisCommonValue::operator=(val);
         return *this;
     }
 
-    CommonBoolValue<LibObjT>& operator=(const Value rawVal) noexcept
+    CommonBoolValue<const bt_value> asConst() const noexcept
     {
-        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+        return CommonBoolValue<const bt_value> {*this};
+    }
 
-        bt_value_bool_set(this->libObjPtr(), static_cast<bt_bool>(rawVal));
-        return *this;
+    RawValueProxy<CommonBoolValue> operator*() const noexcept
+    {
+        return RawValueProxy<CommonBoolValue> {*this};
     }
 
     Value value() const noexcept
@@ -360,9 +500,11 @@ public:
         return static_cast<Value>(bt_value_bool_get(this->libObjPtr()));
     }
 
-    operator Value() const noexcept
+    void value(const Value val) const noexcept
     {
-        return this->value();
+        static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstBoolValue`.");
+
+        bt_value_bool_set(this->libObjPtr(), static_cast<bt_bool>(val));
     }
 
     Shared shared() const noexcept
@@ -402,7 +544,7 @@ private:
     using typename CommonValue<LibObjT>::_ThisCommonValue;
 
 public:
-    using Shared = internal::SharedValue<CommonUnsignedIntegerValue<LibObjT>, LibObjT>;
+    using Shared = SharedValue<CommonUnsignedIntegerValue<LibObjT>, LibObjT>;
     using Value = std::uint64_t;
 
     explicit CommonUnsignedIntegerValue(const _LibObjPtr libObjPtr) noexcept :
@@ -420,35 +562,40 @@ public:
     }
 
     template <typename OtherLibObjT>
-    CommonUnsignedIntegerValue(const CommonUnsignedIntegerValue<OtherLibObjT>& val) noexcept :
+    CommonUnsignedIntegerValue(const CommonUnsignedIntegerValue<OtherLibObjT> val) noexcept :
         _ThisCommonValue {val}
     {
     }
 
     template <typename OtherLibObjT>
-    CommonUnsignedIntegerValue<LibObjT>&
-    operator=(const CommonUnsignedIntegerValue<OtherLibObjT>& val) noexcept
+    CommonUnsignedIntegerValue<LibObjT>
+    operator=(const CommonUnsignedIntegerValue<OtherLibObjT> val) noexcept
     {
-        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
-
         _ThisCommonValue::operator=(val);
         return *this;
     }
 
-    CommonUnsignedIntegerValue<LibObjT>& operator=(const Value rawVal) noexcept
+    CommonUnsignedIntegerValue<const bt_value> asConst() const noexcept
     {
-        bt_value_integer_unsigned_set(this->libObjPtr(), rawVal);
-        return *this;
+        return CommonUnsignedIntegerValue<const bt_value> {*this};
     }
 
-    Value value() const noexcept
+    RawValueProxy<CommonUnsignedIntegerValue> operator*() const noexcept
     {
-        return bt_value_integer_unsigned_get(this->libObjPtr());
+        return RawValueProxy<CommonUnsignedIntegerValue> {*this};
     }
 
-    operator Value() const noexcept
+    void value(const Value val) const noexcept
     {
-        return this->value();
+        static_assert(!std::is_const<LibObjT>::value,
+                      "Not available with `bt2::ConstUnsignedIntegerValue`.");
+
+        bt_value_integer_unsigned_set(this->libObjPtr(), val);
+    }
+
+    Value value() const noexcept
+    {
+        return bt_value_integer_unsigned_get(this->libObjPtr());
     }
 
     Shared shared() const noexcept
@@ -488,7 +635,7 @@ private:
     using typename CommonValue<LibObjT>::_ThisCommonValue;
 
 public:
-    using Shared = internal::SharedValue<CommonSignedIntegerValue<LibObjT>, LibObjT>;
+    using Shared = SharedValue<CommonSignedIntegerValue<LibObjT>, LibObjT>;
     using Value = std::int64_t;
 
     explicit CommonSignedIntegerValue(const _LibObjPtr libObjPtr) noexcept :
@@ -506,35 +653,40 @@ public:
     }
 
     template <typename OtherLibObjT>
-    CommonSignedIntegerValue(const CommonSignedIntegerValue<OtherLibObjT>& val) noexcept :
+    CommonSignedIntegerValue(const CommonSignedIntegerValue<OtherLibObjT> val) noexcept :
         _ThisCommonValue {val}
     {
     }
 
     template <typename OtherLibObjT>
-    CommonSignedIntegerValue<LibObjT>&
-    operator=(const CommonSignedIntegerValue<OtherLibObjT>& val) noexcept
+    CommonSignedIntegerValue<LibObjT>
+    operator=(const CommonSignedIntegerValue<OtherLibObjT> val) noexcept
     {
         _ThisCommonValue::operator=(val);
         return *this;
     }
 
-    CommonSignedIntegerValue<LibObjT>& operator=(const Value rawVal) noexcept
+    CommonSignedIntegerValue<const bt_value> asConst() const noexcept
     {
-        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+        return CommonSignedIntegerValue<const bt_value> {*this};
+    }
 
-        bt_value_integer_signed_set(this->libObjPtr(), rawVal);
-        return *this;
+    RawValueProxy<CommonSignedIntegerValue> operator*() const noexcept
+    {
+        return RawValueProxy<CommonSignedIntegerValue> {*this};
     }
 
-    Value value() const noexcept
+    void value(const Value val) const noexcept
     {
-        return bt_value_integer_signed_get(this->libObjPtr());
+        static_assert(!std::is_const<LibObjT>::value,
+                      "Not available with `bt2::ConstSignedIntegerValue`.");
+
+        bt_value_integer_signed_set(this->libObjPtr(), val);
     }
 
-    operator Value() const noexcept
+    Value value() const noexcept
     {
-        return this->value();
+        return bt_value_integer_signed_get(this->libObjPtr());
     }
 
     Shared shared() const noexcept
@@ -574,7 +726,7 @@ private:
     using typename CommonValue<LibObjT>::_ThisCommonValue;
 
 public:
-    using Shared = internal::SharedValue<CommonRealValue<LibObjT>, LibObjT>;
+    using Shared = SharedValue<CommonRealValue<LibObjT>, LibObjT>;
     using Value = double;
 
     explicit CommonRealValue(const _LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr}
@@ -591,33 +743,37 @@ public:
     }
 
     template <typename OtherLibObjT>
-    CommonRealValue(const CommonRealValue<OtherLibObjT>& val) noexcept : _ThisCommonValue {val}
+    CommonRealValue(const CommonRealValue<OtherLibObjT> val) noexcept : _ThisCommonValue {val}
     {
     }
 
     template <typename OtherLibObjT>
-    CommonRealValue<LibObjT>& operator=(const CommonRealValue<OtherLibObjT>& val) noexcept
+    CommonRealValue<LibObjT> operator=(const CommonRealValue<OtherLibObjT> val) noexcept
     {
         _ThisCommonValue::operator=(val);
         return *this;
     }
 
-    CommonRealValue<LibObjT>& operator=(const Value rawVal) noexcept
+    CommonRealValue<const bt_value> asConst() const noexcept
     {
-        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+        return CommonRealValue<const bt_value> {*this};
+    }
 
-        bt_value_real_set(this->libObjPtr(), rawVal);
-        return *this;
+    RawValueProxy<CommonRealValue> operator*() const noexcept
+    {
+        return RawValueProxy<CommonRealValue> {*this};
     }
 
-    Value value() const noexcept
+    void value(const Value val) const noexcept
     {
-        return bt_value_real_get(this->libObjPtr());
+        static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstRealValue`.");
+
+        bt_value_real_set(this->libObjPtr(), val);
     }
 
-    operator Value() const noexcept
+    Value value() const noexcept
     {
-        return this->value();
+        return bt_value_real_get(this->libObjPtr());
     }
 
     Shared shared() const noexcept
@@ -657,7 +813,8 @@ private:
     using typename CommonValue<LibObjT>::_ThisCommonValue;
 
 public:
-    using Shared = internal::SharedValue<CommonStringValue<LibObjT>, LibObjT>;
+    using Shared = SharedValue<CommonStringValue<LibObjT>, LibObjT>;
+    using Value = const char *;
 
     explicit CommonStringValue(const _LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr}
     {
@@ -678,36 +835,45 @@ public:
     }
 
     template <typename OtherLibObjT>
-    CommonStringValue(const CommonStringValue<OtherLibObjT>& val) noexcept : _ThisCommonValue {val}
+    CommonStringValue(const CommonStringValue<OtherLibObjT> val) noexcept : _ThisCommonValue {val}
     {
     }
 
     template <typename OtherLibObjT>
-    CommonStringValue<LibObjT>& operator=(const CommonStringValue<OtherLibObjT>& val) noexcept
+    CommonStringValue<LibObjT> operator=(const CommonStringValue<OtherLibObjT> val) noexcept
     {
         _ThisCommonValue::operator=(val);
         return *this;
     }
 
-    CommonStringValue<LibObjT>& operator=(const char * const rawVal)
+    CommonStringValue<const bt_value> asConst() const noexcept
     {
-        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+        return CommonStringValue<const bt_value> {*this};
+    }
+
+    RawStringValueProxy<CommonStringValue> operator*() const noexcept
+    {
+        return RawStringValueProxy<CommonStringValue> {*this};
+    }
+
+    void value(const Value val) const
+    {
+        static_assert(!std::is_const<LibObjT>::value,
+                      "Not available with `bt2::ConstStringValue`.");
 
-        const auto status = bt_value_string_set(this->libObjPtr(), rawVal);
+        const auto status = bt_value_string_set(this->libObjPtr(), val);
 
         if (status == BT_VALUE_STRING_SET_STATUS_MEMORY_ERROR) {
-            throw LibMemoryError {};
+            throw MemoryError {};
         }
-
-        return *this;
     }
 
-    CommonStringValue<LibObjT>& operator=(const std::string& rawVal) noexcept
+    void value(const std::string& val) const
     {
-        return *this = rawVal.data();
+        this->value(val.data());
     }
 
-    bpstd::string_view value() const noexcept
+    const char *value() const noexcept
     {
         return bt_value_string_get(this->libObjPtr());
     }
@@ -773,8 +939,8 @@ private:
     using typename CommonValue<LibObjT>::_ThisCommonValue;
 
 public:
-    using Shared = internal::SharedValue<CommonArrayValue<LibObjT>, LibObjT>;
-    using Iterator = CommonIterator<CommonArrayValue<LibObjT>, CommonValue<LibObjT>>;
+    using Shared = SharedValue<CommonArrayValue<LibObjT>, LibObjT>;
+    using Iterator = BorrowedObjectIterator<CommonArrayValue<LibObjT>>;
 
     explicit CommonArrayValue(const _LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr}
     {
@@ -790,26 +956,25 @@ public:
     }
 
     template <typename OtherLibObjT>
-    CommonArrayValue(const CommonArrayValue<OtherLibObjT>& val) noexcept : _ThisCommonValue {val}
+    CommonArrayValue(const CommonArrayValue<OtherLibObjT> val) noexcept : _ThisCommonValue {val}
     {
     }
 
     template <typename OtherLibObjT>
-    CommonArrayValue<LibObjT>& operator=(const CommonArrayValue<OtherLibObjT>& val) noexcept
+    CommonArrayValue<LibObjT> operator=(const CommonArrayValue<OtherLibObjT> val) noexcept
     {
         _ThisCommonValue::operator=(val);
         return *this;
     }
 
-    std::uint64_t length() const noexcept
+    CommonArrayValue<const bt_value> asConst() const noexcept
     {
-        return bt_value_array_get_length(this->libObjPtr());
+        return CommonArrayValue<const bt_value> {*this};
     }
 
-    /* Required by the `CommonIterator` template class */
-    std::uint64_t size() const noexcept
+    std::uint64_t length() const noexcept
     {
-        return this->length();
+        return bt_value_array_get_length(this->libObjPtr());
     }
 
     Iterator begin() const noexcept
@@ -827,30 +992,24 @@ public:
         return this->length() == 0;
     }
 
-    ConstValue operator[](const std::uint64_t index) const noexcept
-    {
-        return ConstValue {internal::CommonArrayValueSpec<const bt_value>::elementByIndex(
-            this->libObjPtr(), index)};
-    }
-
-    CommonValue<LibObjT> operator[](const std::uint64_t index) noexcept
+    CommonValue<LibObjT> operator[](const std::uint64_t index) const noexcept
     {
         return CommonValue<LibObjT> {
             internal::CommonArrayValueSpec<LibObjT>::elementByIndex(this->libObjPtr(), index)};
     }
 
-    void append(const Value& val)
+    void append(const Value val) const
     {
-        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+        static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`.");
 
         const auto status = bt_value_array_append_element(this->libObjPtr(), val.libObjPtr());
 
         this->_handleAppendLibStatus(status);
     }
 
-    void append(const bool rawVal)
+    void append(const bool rawVal) const
     {
-        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+        static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`.");
 
         const auto status =
             bt_value_array_append_bool_element(this->libObjPtr(), static_cast<bt_bool>(rawVal));
@@ -858,9 +1017,9 @@ public:
         this->_handleAppendLibStatus(status);
     }
 
-    void append(const std::uint64_t rawVal)
+    void append(const std::uint64_t rawVal) const
     {
-        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+        static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`.");
 
         const auto status =
             bt_value_array_append_unsigned_integer_element(this->libObjPtr(), rawVal);
@@ -868,72 +1027,72 @@ public:
         this->_handleAppendLibStatus(status);
     }
 
-    void append(const std::int64_t rawVal)
+    void append(const std::int64_t rawVal) const
     {
-        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+        static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`.");
 
         const auto status = bt_value_array_append_signed_integer_element(this->libObjPtr(), rawVal);
 
         this->_handleAppendLibStatus(status);
     }
 
-    void append(const double rawVal)
+    void append(const double rawVal) const
     {
-        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+        static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`.");
 
         const auto status = bt_value_array_append_real_element(this->libObjPtr(), rawVal);
 
         this->_handleAppendLibStatus(status);
     }
 
-    void append(const char * const rawVal)
+    void append(const char * const rawVal) const
     {
-        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+        static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`.");
 
         const auto status = bt_value_array_append_string_element(this->libObjPtr(), rawVal);
 
         this->_handleAppendLibStatus(status);
     }
 
-    void append(const std::string& rawVal)
+    void append(const std::string& rawVal) const
     {
         this->append(rawVal.data());
     }
 
-    CommonArrayValue<bt_value> appendEmptyArray();
-    CommonMapValue<bt_value> appendEmptyMap();
+    CommonArrayValue<bt_value> appendEmptyArray() const;
+    CommonMapValue<bt_value> appendEmptyMap() const;
 
-    void operator+=(const Value& val)
+    void operator+=(const Value val) const
     {
         this->append(val);
     }
 
-    void operator+=(const bool rawVal)
+    void operator+=(const bool rawVal) const
     {
         this->append(rawVal);
     }
 
-    void operator+=(const std::uint64_t rawVal)
+    void operator+=(const std::uint64_t rawVal) const
     {
         this->append(rawVal);
     }
 
-    void operator+=(const std::int64_t rawVal)
+    void operator+=(const std::int64_t rawVal) const
     {
         this->append(rawVal);
     }
 
-    void operator+=(const double rawVal)
+    void operator+=(const double rawVal) const
     {
         this->append(rawVal);
     }
 
-    void operator+=(const char * const rawVal)
+    void operator+=(const char * const rawVal) const
     {
         this->append(rawVal);
     }
 
-    void operator+=(const std::string& rawVal)
+    void operator+=(const std::string& rawVal) const
     {
         this->append(rawVal);
     }
@@ -947,7 +1106,7 @@ private:
     void _handleAppendLibStatus(const bt_value_array_append_element_status status) const
     {
         if (status == BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_MEMORY_ERROR) {
-            throw LibMemoryError {};
+            throw MemoryError {};
         }
     }
 };
@@ -979,7 +1138,7 @@ struct TypeDescr<ConstArrayValue> : public ArrayValueTypeDescr
  * First argument is the entry's key, second is its value.
  */
 template <typename ObjT>
-using CommonMapValueForEachUserFunc = std::function<void(const bpstd::string_view&, ObjT)>;
+using CommonMapValueForEachUserFunc = std::function<void(const char *, ObjT)>;
 
 /*
  * Template of a function to be passed to bt_value_map_foreach_entry()
@@ -1034,7 +1193,7 @@ struct CommonMapValueSpec<bt_value> final
             return;
         case BT_VALUE_MAP_FOREACH_ENTRY_STATUS_USER_ERROR:
         case BT_VALUE_MAP_FOREACH_ENTRY_STATUS_ERROR:
-            throw LibError {};
+            throw Error {};
         default:
             bt_common_abort();
         }
@@ -1067,7 +1226,7 @@ struct CommonMapValueSpec<const bt_value> final
             return;
         case BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_USER_ERROR:
         case BT_VALUE_MAP_FOREACH_ENTRY_CONST_STATUS_ERROR:
-            throw LibError {};
+            throw Error {};
         default:
             bt_common_abort();
         }
@@ -1084,7 +1243,7 @@ private:
     using typename CommonValue<LibObjT>::_ThisCommonValue;
 
 public:
-    using Shared = internal::SharedValue<CommonMapValue<LibObjT>, LibObjT>;
+    using Shared = SharedValue<CommonMapValue<LibObjT>, LibObjT>;
 
     explicit CommonMapValue(const _LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr}
     {
@@ -1100,57 +1259,45 @@ public:
     }
 
     template <typename OtherLibObjT>
-    CommonMapValue(const CommonMapValue<OtherLibObjT>& val) noexcept : _ThisCommonValue {val}
+    CommonMapValue(const CommonMapValue<OtherLibObjT> val) noexcept : _ThisCommonValue {val}
     {
     }
 
     template <typename OtherLibObjT>
-    CommonMapValue<LibObjT>& operator=(const CommonMapValue<OtherLibObjT>& val) noexcept
+    CommonMapValue<LibObjT> operator=(const CommonMapValue<OtherLibObjT> val) noexcept
     {
         _ThisCommonValue::operator=(val);
         return *this;
     }
 
-    std::uint64_t size() const noexcept
-    {
-        return bt_value_map_get_size(this->libObjPtr());
-    }
-
-    bool isEmpty() const noexcept
+    CommonMapValue<const bt_value> asConst() const noexcept
     {
-        return this->size() == 0;
+        return CommonMapValue<const bt_value> {*this};
     }
 
-    nonstd::optional<ConstValue> operator[](const char * const key) const noexcept
+    std::uint64_t length() const noexcept
     {
-        const auto libObjPtr =
-            internal::CommonMapValueSpec<const bt_value>::entryByKey(this->libObjPtr(), key);
-
-        if (!libObjPtr) {
-            return nonstd::nullopt;
-        }
-
-        return ConstValue {libObjPtr};
+        return bt_value_map_get_size(this->libObjPtr());
     }
 
-    nonstd::optional<ConstValue> operator[](const std::string& key) const noexcept
+    bool isEmpty() const noexcept
     {
-        return (*this)[key.data()];
+        return this->length() == 0;
     }
 
-    nonstd::optional<CommonValue<LibObjT>> operator[](const char * const key) noexcept
+    bt2s::optional<CommonValue<LibObjT>> operator[](const char * const key) const noexcept
     {
         const auto libObjPtr =
             internal::CommonMapValueSpec<LibObjT>::entryByKey(this->libObjPtr(), key);
 
         if (!libObjPtr) {
-            return nonstd::nullopt;
+            return bt2s::nullopt;
         }
 
         return CommonValue<LibObjT> {libObjPtr};
     }
 
-    nonstd::optional<CommonValue<LibObjT>> operator[](const std::string& key) noexcept
+    bt2s::optional<CommonValue<LibObjT>> operator[](const std::string& key) const noexcept
     {
         return (*this)[key.data()];
     }
@@ -1165,23 +1312,23 @@ public:
         return this->hasEntry(key.data());
     }
 
-    void insert(const char * const key, const Value& val)
+    void insert(const char * const key, const Value val) const
     {
-        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+        static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
 
         const auto status = bt_value_map_insert_entry(this->libObjPtr(), key, val.libObjPtr());
 
         this->_handleInsertLibStatus(status);
     }
 
-    void insert(const std::string& key, const Value& val)
+    void insert(const std::string& key, const Value val) const
     {
         this->insert(key.data(), val);
     }
 
-    void insert(const char * const key, const bool rawVal)
+    void insert(const char * const key, const bool rawVal) const
     {
-        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+        static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
 
         const auto status =
             bt_value_map_insert_bool_entry(this->libObjPtr(), key, static_cast<bt_bool>(rawVal));
@@ -1189,14 +1336,14 @@ public:
         this->_handleInsertLibStatus(status);
     }
 
-    void insert(const std::string& key, const bool rawVal)
+    void insert(const std::string& key, const bool rawVal) const
     {
         this->insert(key.data(), rawVal);
     }
 
-    void insert(const char * const key, const std::uint64_t rawVal)
+    void insert(const char * const key, const std::uint64_t rawVal) const
     {
-        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+        static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
 
         const auto status =
             bt_value_map_insert_unsigned_integer_entry(this->libObjPtr(), key, rawVal);
@@ -1204,14 +1351,14 @@ public:
         this->_handleInsertLibStatus(status);
     }
 
-    void insert(const std::string& key, const std::uint64_t rawVal)
+    void insert(const std::string& key, const std::uint64_t rawVal) const
     {
         this->insert(key.data(), rawVal);
     }
 
-    void insert(const char * const key, const std::int64_t rawVal)
+    void insert(const char * const key, const std::int64_t rawVal) const
     {
-        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+        static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
 
         const auto status =
             bt_value_map_insert_signed_integer_entry(this->libObjPtr(), key, rawVal);
@@ -1219,60 +1366,55 @@ public:
         this->_handleInsertLibStatus(status);
     }
 
-    void insert(const std::string& key, const std::int64_t rawVal)
+    void insert(const std::string& key, const std::int64_t rawVal) const
     {
         this->insert(key.data(), rawVal);
     }
 
-    void insert(const char * const key, const double rawVal)
+    void insert(const char * const key, const double rawVal) const
     {
-        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+        static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
 
         const auto status = bt_value_map_insert_real_entry(this->libObjPtr(), key, rawVal);
 
         this->_handleInsertLibStatus(status);
     }
 
-    void insert(const std::string& key, const double rawVal)
+    void insert(const std::string& key, const double rawVal) const
     {
         this->insert(key.data(), rawVal);
     }
 
-    void insert(const char * const key, const char * const rawVal)
+    void insert(const char * const key, const char * const rawVal) const
     {
-        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+        static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
 
         const auto status = bt_value_map_insert_string_entry(this->libObjPtr(), key, rawVal);
 
         this->_handleInsertLibStatus(status);
     }
 
-    void insert(const char * const key, const std::string& rawVal)
+    void insert(const char * const key, const std::string& rawVal) const
     {
         this->insert(key, rawVal.data());
     }
 
-    void insert(const std::string& key, const char * const rawVal)
+    void insert(const std::string& key, const char * const rawVal) const
     {
         this->insert(key.data(), rawVal);
     }
 
-    void insert(const std::string& key, const std::string& rawVal)
+    void insert(const std::string& key, const std::string& rawVal) const
     {
         this->insert(key.data(), rawVal.data());
     }
 
-    CommonArrayValue<bt_value> insertEmptyArray(const char *key);
-    CommonArrayValue<bt_value> insertEmptyArray(const std::string& key);
-    CommonMapValue<bt_value> insertEmptyMap(const char *key);
-    CommonMapValue<bt_value> insertEmptyMap(const std::string& key);
+    CommonArrayValue<bt_value> insertEmptyArray(const char *key) const;
+    CommonArrayValue<bt_value> insertEmptyArray(const std::string& key) const;
+    CommonMapValue<bt_value> insertEmptyMap(const char *key) const;
+    CommonMapValue<bt_value> insertEmptyMap(const std::string& key) const;
 
-    void forEach(const internal::CommonMapValueForEachUserFunc<ConstValue>& func) const
-    {
-        internal::CommonMapValueSpec<const bt_value>::forEach(this->libObjPtr(), func);
-    }
-
-    void forEach(const internal::CommonMapValueForEachUserFunc<CommonValue<LibObjT>>& func)
+    void forEach(const internal::CommonMapValueForEachUserFunc<CommonValue<LibObjT>>& func) const
     {
         internal::CommonMapValueSpec<LibObjT>::forEach(this->libObjPtr(), func);
     }
@@ -1286,7 +1428,7 @@ private:
     void _handleInsertLibStatus(const bt_value_map_insert_entry_status status) const
     {
         if (status == BT_VALUE_MAP_INSERT_ENTRY_STATUS_MEMORY_ERROR) {
-            throw LibMemoryError {};
+            throw MemoryError {};
         }
     }
 };
@@ -1314,6 +1456,42 @@ struct TypeDescr<ConstMapValue> : public MapValueTypeDescr
 
 } /* namespace internal */
 
+template <typename LibObjT>
+ArrayValue CommonValue<LibObjT>::appendEmptyArray() const
+{
+    return this->asArray().appendEmptyArray();
+}
+
+template <typename LibObjT>
+MapValue CommonValue<LibObjT>::appendEmptyMap() const
+{
+    return this->asArray().appendEmptyMap();
+}
+
+template <typename LibObjT>
+ArrayValue CommonValue<LibObjT>::insertEmptyArray(const char * const key) const
+{
+    return this->asMap().insertEmptyArray(key);
+}
+
+template <typename LibObjT>
+ArrayValue CommonValue<LibObjT>::insertEmptyArray(const std::string& key) const
+{
+    return this->asMap().insertEmptyArray(key);
+}
+
+template <typename LibObjT>
+MapValue CommonValue<LibObjT>::insertEmptyMap(const char * const key) const
+{
+    return this->asMap().insertEmptyMap(key);
+}
+
+template <typename LibObjT>
+MapValue CommonValue<LibObjT>::insertEmptyMap(const std::string& key) const
+{
+    return this->asMap().insertEmptyMap(key);
+}
+
 template <typename LibObjT>
 CommonNullValue<LibObjT> CommonValue<LibObjT>::asNull() const noexcept
 {
@@ -1371,9 +1549,9 @@ CommonMapValue<LibObjT> CommonValue<LibObjT>::asMap() const noexcept
 }
 
 template <typename LibObjT>
-ArrayValue CommonArrayValue<LibObjT>::appendEmptyArray()
+ArrayValue CommonArrayValue<LibObjT>::appendEmptyArray() const
 {
-    static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+    static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`.");
 
     bt_value *libElemPtr;
     const auto status = bt_value_array_append_empty_array_element(this->libObjPtr(), &libElemPtr);
@@ -1383,9 +1561,9 @@ ArrayValue CommonArrayValue<LibObjT>::appendEmptyArray()
 }
 
 template <typename LibObjT>
-MapValue CommonArrayValue<LibObjT>::appendEmptyMap()
+MapValue CommonArrayValue<LibObjT>::appendEmptyMap() const
 {
-    static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+    static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`.");
 
     bt_value *libElemPtr;
     const auto status = bt_value_array_append_empty_map_element(this->libObjPtr(), &libElemPtr);
@@ -1395,9 +1573,9 @@ MapValue CommonArrayValue<LibObjT>::appendEmptyMap()
 }
 
 template <typename LibObjT>
-ArrayValue CommonMapValue<LibObjT>::insertEmptyArray(const char * const key)
+ArrayValue CommonMapValue<LibObjT>::insertEmptyArray(const char * const key) const
 {
-    static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+    static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
 
     bt_value *libEntryPtr;
     const auto status = bt_value_map_insert_empty_array_entry(this->libObjPtr(), key, &libEntryPtr);
@@ -1407,15 +1585,15 @@ ArrayValue CommonMapValue<LibObjT>::insertEmptyArray(const char * const key)
 }
 
 template <typename LibObjT>
-ArrayValue CommonMapValue<LibObjT>::insertEmptyArray(const std::string& key)
+ArrayValue CommonMapValue<LibObjT>::insertEmptyArray(const std::string& key) const
 {
     return this->insertEmptyArray(key.data());
 }
 
 template <typename LibObjT>
-MapValue CommonMapValue<LibObjT>::insertEmptyMap(const char * const key)
+MapValue CommonMapValue<LibObjT>::insertEmptyMap(const char * const key) const
 {
-    static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+    static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
 
     bt_value *libEntryPtr;
     const auto status = bt_value_map_insert_empty_map_entry(this->libObjPtr(), key, &libEntryPtr);
@@ -1425,7 +1603,7 @@ MapValue CommonMapValue<LibObjT>::insertEmptyMap(const char * const key)
 }
 
 template <typename LibObjT>
-MapValue CommonMapValue<LibObjT>::insertEmptyMap(const std::string& key)
+MapValue CommonMapValue<LibObjT>::insertEmptyMap(const std::string& key) const
 {
     return this->insertEmptyMap(key.data());
 }
This page took 0.039314 seconds and 4 git commands to generate.