Fix: cpp-common/bt2: don't pass a lib obj ptr when constructing a `CommonNullValue`
[babeltrace.git] / src / cpp-common / bt2 / value.hpp
index 13d30a98c450bbadf1f8f60d0ec43670e27294c0..14ae5a2b3b7f325dde5be3c6582fde908506fc3f 100644 (file)
 
 #include "common/assert.h"
 #include "common/common.h"
-#include "cpp-common/bt2s/optional.hpp"
+#include "cpp-common/bt2c/c-string-view.hpp"
 
 #include "borrowed-object-iterator.hpp"
 #include "borrowed-object.hpp"
 #include "exc.hpp"
 #include "internal/utils.hpp"
+#include "optional-borrowed-object.hpp"
 #include "raw-value-proxy.hpp"
 #include "shared-object.hpp"
 
@@ -94,12 +95,12 @@ public:
     CommonValueRawValueProxy& operator=(std::uint64_t rawVal) noexcept;
     CommonValueRawValueProxy& operator=(double rawVal) noexcept;
     CommonValueRawValueProxy& operator=(const char *rawVal);
-    CommonValueRawValueProxy& operator=(const std::string& rawVal);
+    CommonValueRawValueProxy& operator=(bt2c::CStringView 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;
+    operator bt2c::CStringView() const noexcept;
 
 private:
     ValueObjT _mObj;
@@ -241,7 +242,7 @@ public:
     }
 
     template <typename KeyT>
-    bt2s::optional<CommonValue<LibObjT>> operator[](KeyT&& key) const noexcept
+    OptionalBorrowedObject<CommonValue<LibObjT>> operator[](KeyT&& key) const noexcept
     {
         return this->asMap()[std::forward<KeyT>(key)];
     }
@@ -258,10 +259,8 @@ public:
         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;
+    CommonArrayValue<bt_value> insertEmptyArray(bt2c::CStringView key) const;
+    CommonMapValue<bt_value> insertEmptyMap(bt2c::CStringView key) const;
 
     Shared shared() const noexcept
     {
@@ -335,9 +334,10 @@ CommonValueRawValueProxy<ValueObjT>::operator=(const char * const rawVal)
 
 template <typename ValueObjT>
 CommonValueRawValueProxy<ValueObjT>&
-CommonValueRawValueProxy<ValueObjT>::operator=(const std::string& rawVal)
+CommonValueRawValueProxy<ValueObjT>::operator=(const bt2c::CStringView rawVal)
 {
-    return *this = rawVal.data();
+    _mObj.asString().value(rawVal);
+    return *this;
 }
 
 template <typename ValueObjT>
@@ -365,7 +365,7 @@ CommonValueRawValueProxy<ValueObjT>::operator double() const noexcept
 }
 
 template <typename ValueObjT>
-CommonValueRawValueProxy<ValueObjT>::operator const char *() const noexcept
+CommonValueRawValueProxy<ValueObjT>::operator bt2c::CStringView() const noexcept
 {
     return _mObj.asString().value();
 }
@@ -814,14 +814,14 @@ private:
 public:
     using typename CommonValue<LibObjT>::LibObjPtr;
     using Shared = SharedValue<CommonStringValue<LibObjT>, LibObjT>;
-    using Value = const char *;
+    using Value = bt2c::CStringView;
 
     explicit CommonStringValue(const LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr}
     {
         BT_ASSERT_DBG(this->isString());
     }
 
-    static Shared create(const char * const rawVal = "")
+    static Shared create(const bt2c::CStringView rawVal = "")
     {
         const auto libObjPtr = bt_value_string_create_init(rawVal);
 
@@ -829,11 +829,6 @@ public:
         return CommonStringValue::Shared::createWithoutRef(libObjPtr);
     }
 
-    static Shared create(const std::string& rawVal)
-    {
-        return CommonStringValue::create(rawVal.data());
-    }
-
     template <typename OtherLibObjT>
     CommonStringValue(const CommonStringValue<OtherLibObjT> val) noexcept : _ThisCommonValue {val}
     {
@@ -851,9 +846,9 @@ public:
         return CommonStringValue<const bt_value> {*this};
     }
 
-    RawStringValueProxy<CommonStringValue> operator*() const noexcept
+    RawValueProxy<CommonStringValue> operator*() const noexcept
     {
-        return RawStringValueProxy<CommonStringValue> {*this};
+        return RawValueProxy<CommonStringValue> {*this};
     }
 
     void value(const Value val) const
@@ -861,19 +856,14 @@ public:
         static_assert(!std::is_const<LibObjT>::value,
                       "Not available with `bt2::ConstStringValue`.");
 
-        const auto status = bt_value_string_set(this->libObjPtr(), val);
+        const auto status = bt_value_string_set(this->libObjPtr(), *val);
 
         if (status == BT_VALUE_STRING_SET_STATUS_MEMORY_ERROR) {
             throw MemoryError {};
         }
     }
 
-    void value(const std::string& val) const
-    {
-        this->value(val.data());
-    }
-
-    const char *value() const noexcept
+    Value value() const noexcept
     {
         return bt_value_string_get(this->libObjPtr());
     }
@@ -1054,7 +1044,7 @@ public:
         this->_handleAppendLibStatus(status);
     }
 
-    void append(const std::string& rawVal) const
+    void append(const bt2c::CStringView rawVal) const
     {
         this->append(rawVal.data());
     }
@@ -1092,7 +1082,7 @@ public:
         this->append(rawVal);
     }
 
-    void operator+=(const std::string& rawVal) const
+    void operator+=(const bt2c::CStringView rawVal) const
     {
         this->append(rawVal);
     }
@@ -1138,7 +1128,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 char *, ObjT)>;
+using CommonMapValueForEachUserFunc = std::function<void(bt2c::CStringView, ObjT)>;
 
 /*
  * Template of a function to be passed to bt_value_map_foreach_entry()
@@ -1148,7 +1138,7 @@ using CommonMapValueForEachUserFunc = std::function<void(const char *, ObjT)>;
  * `CommonMapValueForEachUserFunc<ObjT>` (the user function to call).
  *
  * This function catches any exception which the user function throws
- * and returns the `ErrorStatus` value. If there's no execption, this
+ * and returns the `ErrorStatus` value. If there's no exception, this
  * function returns the `OkStatus` value.
  */
 template <typename ObjT, typename LibObjT, typename LibStatusT, int OkStatus, int ErrorStatus>
@@ -1285,34 +1275,18 @@ public:
         return this->length() == 0;
     }
 
-    bt2s::optional<CommonValue<LibObjT>> operator[](const char * const key) const noexcept
-    {
-        const auto libObjPtr =
-            internal::CommonMapValueSpec<LibObjT>::entryByKey(this->libObjPtr(), key);
-
-        if (!libObjPtr) {
-            return bt2s::nullopt;
-        }
-
-        return CommonValue<LibObjT> {libObjPtr};
-    }
-
-    bt2s::optional<CommonValue<LibObjT>> operator[](const std::string& key) const noexcept
+    OptionalBorrowedObject<CommonValue<LibObjT>>
+    operator[](const bt2c::CStringView key) const noexcept
     {
-        return (*this)[key.data()];
+        return internal::CommonMapValueSpec<LibObjT>::entryByKey(this->libObjPtr(), key);
     }
 
-    bool hasEntry(const char * const key) const noexcept
+    bool hasEntry(const bt2c::CStringView key) const noexcept
     {
         return static_cast<bool>(bt_value_map_has_entry(this->libObjPtr(), key));
     }
 
-    bool hasEntry(const std::string& key) const noexcept
-    {
-        return this->hasEntry(key.data());
-    }
-
-    void insert(const char * const key, const Value val) const
+    void insert(const bt2c::CStringView key, const Value val) const
     {
         static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
 
@@ -1321,12 +1295,7 @@ public:
         this->_handleInsertLibStatus(status);
     }
 
-    void insert(const std::string& key, const Value val) const
-    {
-        this->insert(key.data(), val);
-    }
-
-    void insert(const char * const key, const bool rawVal) const
+    void insert(const bt2c::CStringView key, const bool rawVal) const
     {
         static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
 
@@ -1336,12 +1305,7 @@ public:
         this->_handleInsertLibStatus(status);
     }
 
-    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) const
+    void insert(const bt2c::CStringView key, const std::uint64_t rawVal) const
     {
         static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
 
@@ -1350,13 +1314,7 @@ public:
 
         this->_handleInsertLibStatus(status);
     }
-
-    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) const
+    void insert(const bt2c::CStringView key, const std::int64_t rawVal) const
     {
         static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
 
@@ -1366,12 +1324,7 @@ public:
         this->_handleInsertLibStatus(status);
     }
 
-    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) const
+    void insert(const bt2c::CStringView key, const double rawVal) const
     {
         static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
 
@@ -1380,12 +1333,7 @@ public:
         this->_handleInsertLibStatus(status);
     }
 
-    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) const
+    void insert(const bt2c::CStringView key, const char *rawVal) const
     {
         static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
 
@@ -1394,25 +1342,13 @@ public:
         this->_handleInsertLibStatus(status);
     }
 
-    void insert(const char * const key, const std::string& rawVal) const
+    void insert(const bt2c::CStringView key, const bt2c::CStringView rawVal) const
     {
-        this->insert(key, rawVal.data());
+        return this->insert(key, rawVal.data());
     }
 
-    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) const
-    {
-        this->insert(key.data(), rawVal.data());
-    }
-
-    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;
+    CommonArrayValue<bt_value> insertEmptyArray(bt2c::CStringView key) const;
+    CommonMapValue<bt_value> insertEmptyMap(bt2c::CStringView key) const;
 
     void forEach(const internal::CommonMapValueForEachUserFunc<CommonValue<LibObjT>>& func) const
     {
@@ -1469,25 +1405,13 @@ MapValue CommonValue<LibObjT>::appendEmptyMap() const
 }
 
 template <typename LibObjT>
-ArrayValue CommonValue<LibObjT>::insertEmptyArray(const char * const key) const
+ArrayValue CommonValue<LibObjT>::insertEmptyArray(const bt2c::CStringView 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
+MapValue CommonValue<LibObjT>::insertEmptyMap(const bt2c::CStringView key) const
 {
     return this->asMap().insertEmptyMap(key);
 }
@@ -1496,7 +1420,7 @@ template <typename LibObjT>
 CommonNullValue<LibObjT> CommonValue<LibObjT>::asNull() const noexcept
 {
     BT_ASSERT_DBG(this->isNull());
-    return CommonNullValue<LibObjT> {this->libObjPtr()};
+    return CommonNullValue<LibObjT> {};
 }
 
 template <typename LibObjT>
@@ -1573,7 +1497,7 @@ MapValue CommonArrayValue<LibObjT>::appendEmptyMap() const
 }
 
 template <typename LibObjT>
-ArrayValue CommonMapValue<LibObjT>::insertEmptyArray(const char * const key) const
+ArrayValue CommonMapValue<LibObjT>::insertEmptyArray(const bt2c::CStringView key) const
 {
     static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
 
@@ -1585,13 +1509,7 @@ ArrayValue CommonMapValue<LibObjT>::insertEmptyArray(const char * const key) con
 }
 
 template <typename LibObjT>
-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) const
+MapValue CommonMapValue<LibObjT>::insertEmptyMap(const bt2c::CStringView key) const
 {
     static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
 
@@ -1602,12 +1520,6 @@ MapValue CommonMapValue<LibObjT>::insertEmptyMap(const char * const key) const
     return MapValue {libEntryPtr};
 }
 
-template <typename LibObjT>
-MapValue CommonMapValue<LibObjT>::insertEmptyMap(const std::string& key) const
-{
-    return this->insertEmptyMap(key.data());
-}
-
 inline BoolValue::Shared createValue(const bool rawVal)
 {
     return BoolValue::create(rawVal);
@@ -1633,7 +1545,7 @@ inline StringValue::Shared createValue(const char * const rawVal)
     return StringValue::create(rawVal);
 }
 
-inline StringValue::Shared createValue(const std::string& rawVal)
+inline StringValue::Shared createValue(const bt2c::CStringView rawVal)
 {
     return StringValue::create(rawVal);
 }
This page took 0.03468 seconds and 4 git commands to generate.