cpp-common: add `bt2s::optional`, alias of `nonstd::optional`
[babeltrace.git] / src / cpp-common / bt2 / field.hpp
index aa160289b4f686dcece5ba000dd78590429e4688..69bf4ab6d916cc5f8378a397881b67accbe73bd9 100644 (file)
 #include <babeltrace2/babeltrace.h>
 
 #include "common/assert.h"
-#include "cpp-common/optional.hpp"
-#include "cpp-common/string_view.hpp"
+#include "cpp-common/bt2s/optional.hpp"
 
 #include "borrowed-object.hpp"
 #include "field-class.hpp"
 #include "internal/utils.hpp"
+#include "raw-value-proxy.hpp"
 
 namespace bt2 {
 
@@ -114,7 +114,7 @@ public:
     }
 
     template <typename OtherLibObjT>
-    _ThisCommonField& operator=(const CommonField<OtherLibObjT> val) noexcept
+    _ThisCommonField operator=(const CommonField<OtherLibObjT> val) noexcept
     {
         _ThisBorrowedObject::operator=(val);
         return *this;
@@ -271,7 +271,7 @@ public:
     }
 
     template <typename OtherLibObjT>
-    CommonBoolField<LibObjT>& operator=(const CommonBoolField<OtherLibObjT> val) noexcept
+    CommonBoolField<LibObjT> operator=(const CommonBoolField<OtherLibObjT> val) noexcept
     {
         _ThisCommonField::operator=(val);
         return *this;
@@ -282,23 +282,22 @@ public:
         return CommonBoolField<const bt_field> {*this};
     }
 
-    CommonBoolField<LibObjT> operator=(const Value val) const noexcept
+    RawValueProxy<CommonBoolField> operator*() const noexcept
+    {
+        return RawValueProxy<CommonBoolField> {*this};
+    }
+
+    void value(const Value val) const noexcept
     {
         static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstBoolField`.");
 
         bt_field_bool_set_value(this->libObjPtr(), static_cast<bt_bool>(val));
-        return *this;
     }
 
     Value value() const noexcept
     {
         return static_cast<Value>(bt_field_bool_get_value(this->libObjPtr()));
     }
-
-    operator Value() const noexcept
-    {
-        return this->value();
-    }
 };
 
 using BoolField = CommonBoolField<bt_field>;
@@ -346,7 +345,7 @@ public:
     }
 
     template <typename OtherLibObjT>
-    CommonBitArrayField<LibObjT>& operator=(const CommonBitArrayField<OtherLibObjT> val) noexcept
+    CommonBitArrayField<LibObjT> operator=(const CommonBitArrayField<OtherLibObjT> val) noexcept
     {
         _ThisCommonField::operator=(val);
         return *this;
@@ -368,13 +367,12 @@ public:
         return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
     }
 
-    CommonBitArrayField<LibObjT> operator=(const std::uint64_t bits) const noexcept
+    void valueAsInteger(const std::uint64_t bits) const noexcept
     {
         static_assert(!std::is_const<LibObjT>::value,
                       "Not available with `bt2::ConstBitArrayField`.");
 
         bt_field_bit_array_set_value_as_integer(this->libObjPtr(), bits);
-        return *this;
     }
 
     std::uint64_t valueAsInteger() const noexcept
@@ -439,7 +437,7 @@ public:
     }
 
     template <typename OtherLibObjT>
-    _ThisCommonUnsignedIntegerField&
+    _ThisCommonUnsignedIntegerField
     operator=(const CommonUnsignedIntegerField<OtherLibObjT> val) noexcept
     {
         _ThisCommonField::operator=(val);
@@ -456,24 +454,23 @@ public:
         return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
     }
 
-    CommonUnsignedIntegerField<LibObjT> operator=(const Value val) const noexcept
+    RawValueProxy<CommonUnsignedIntegerField> operator*() const noexcept
+    {
+        return RawValueProxy<CommonUnsignedIntegerField> {*this};
+    }
+
+    void value(const Value val) const noexcept
     {
         static_assert(!std::is_const<LibObjT>::value,
                       "Not available with `bt2::ConstUnsignedIntegerField`.");
 
         bt_field_integer_unsigned_set_value(this->libObjPtr(), val);
-        return *this;
     }
 
     Value value() const noexcept
     {
         return bt_field_integer_unsigned_get_value(this->libObjPtr());
     }
-
-    operator Value() const noexcept
-    {
-        return this->value();
-    }
 };
 
 using UnsignedIntegerField = CommonUnsignedIntegerField<bt_field>;
@@ -526,7 +523,7 @@ public:
     }
 
     template <typename OtherLibObjT>
-    _ThisCommonSignedIntegerField&
+    _ThisCommonSignedIntegerField
     operator=(const CommonSignedIntegerField<OtherLibObjT> val) noexcept
     {
         _ThisCommonField::operator=(val);
@@ -543,24 +540,23 @@ public:
         return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
     }
 
-    CommonSignedIntegerField<LibObjT> operator=(const Value val) const noexcept
+    RawValueProxy<CommonSignedIntegerField> operator*() const noexcept
+    {
+        return RawValueProxy<CommonSignedIntegerField> {*this};
+    }
+
+    void value(const Value val) const noexcept
     {
         static_assert(!std::is_const<LibObjT>::value,
                       "Not available with `bt2::ConstSignedIntegerField`.");
 
         bt_field_integer_signed_set_value(this->libObjPtr(), val);
-        return *this;
     }
 
     Value value() const noexcept
     {
         return bt_field_integer_signed_get_value(this->libObjPtr());
     }
-
-    operator Value() const noexcept
-    {
-        return this->value();
-    }
 };
 
 using SignedIntegerField = CommonSignedIntegerField<bt_field>;
@@ -596,18 +592,12 @@ public:
     {
     }
 
-    EnumerationFieldClassMappingLabels(const EnumerationFieldClassMappingLabels&) noexcept =
-        default;
-
-    EnumerationFieldClassMappingLabels&
-    operator=(const EnumerationFieldClassMappingLabels&) noexcept = default;
-
     std::uint64_t length() const noexcept
     {
         return _mLen;
     }
 
-    bpstd::string_view operator[](const std::uint64_t index) const noexcept
+    const char *operator[](const std::uint64_t index) const noexcept
     {
         return _mLabels[index];
     }
@@ -642,7 +632,7 @@ public:
     }
 
     template <typename OtherLibObjT>
-    CommonUnsignedEnumerationField<LibObjT>&
+    CommonUnsignedEnumerationField<LibObjT>
     operator=(const CommonUnsignedEnumerationField<OtherLibObjT> val) noexcept
     {
         _ThisCommonUnsignedIntegerField::operator=(val);
@@ -654,8 +644,6 @@ public:
         return CommonUnsignedEnumerationField<const bt_field> {*this};
     }
 
-    using CommonUnsignedIntegerField<LibObjT>::operator=;
-
     Class cls() const noexcept
     {
         return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
@@ -723,7 +711,7 @@ public:
     }
 
     template <typename OtherLibObjT>
-    CommonSignedEnumerationField<LibObjT>&
+    CommonSignedEnumerationField<LibObjT>
     operator=(const CommonSignedEnumerationField<OtherLibObjT> val) noexcept
     {
         _ThisCommonSignedIntegerField::operator=(val);
@@ -735,8 +723,6 @@ public:
         return CommonSignedEnumerationField<const bt_field> {*this};
     }
 
-    using CommonSignedIntegerField<LibObjT>::operator=;
-
     Class cls() const noexcept
     {
         return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
@@ -804,7 +790,7 @@ public:
     }
 
     template <typename OtherLibObjT>
-    CommonSinglePrecisionRealField<LibObjT>&
+    CommonSinglePrecisionRealField<LibObjT>
     operator=(const CommonSinglePrecisionRealField<OtherLibObjT> val) noexcept
     {
         _ThisCommonField::operator=(val);
@@ -816,24 +802,23 @@ public:
         return CommonSinglePrecisionRealField<const bt_field> {*this};
     }
 
-    CommonSinglePrecisionRealField<LibObjT> operator=(const Value val) const noexcept
+    RawValueProxy<CommonSinglePrecisionRealField> operator*() const noexcept
+    {
+        return RawValueProxy<CommonSinglePrecisionRealField> {*this};
+    }
+
+    void value(const Value val) const noexcept
     {
         static_assert(!std::is_const<LibObjT>::value,
                       "Not available with `bt2::ConstSinglePrecisionRealField`.");
 
         bt_field_real_single_precision_set_value(this->libObjPtr(), val);
-        return *this;
     }
 
     Value value() const noexcept
     {
         return bt_field_real_single_precision_get_value(this->libObjPtr());
     }
-
-    operator Value() const noexcept
-    {
-        return this->value();
-    }
 };
 
 using SinglePrecisionRealField = CommonSinglePrecisionRealField<bt_field>;
@@ -883,7 +868,7 @@ public:
     }
 
     template <typename OtherLibObjT>
-    CommonDoublePrecisionRealField<LibObjT>&
+    CommonDoublePrecisionRealField<LibObjT>
     operator=(const CommonDoublePrecisionRealField<OtherLibObjT> val) noexcept
     {
         _ThisCommonField::operator=(val);
@@ -895,24 +880,23 @@ public:
         return CommonDoublePrecisionRealField<const bt_field> {*this};
     }
 
-    CommonDoublePrecisionRealField<LibObjT> operator=(const Value val) const noexcept
+    RawValueProxy<CommonDoublePrecisionRealField> operator*() const noexcept
+    {
+        return RawValueProxy<CommonDoublePrecisionRealField> {*this};
+    }
+
+    void value(const Value val) const noexcept
     {
         static_assert(!std::is_const<LibObjT>::value,
                       "Not available with `bt2::ConstDoublePrecisionRealField`.");
 
         bt_field_real_double_precision_set_value(this->libObjPtr(), val);
-        return *this;
     }
 
     Value value() const noexcept
     {
         return bt_field_real_double_precision_get_value(this->libObjPtr());
     }
-
-    operator Value() const noexcept
-    {
-        return this->value();
-    }
 };
 
 using DoublePrecisionRealField = CommonDoublePrecisionRealField<bt_field>;
@@ -946,6 +930,8 @@ private:
     using typename CommonField<LibObjT>::_ThisCommonField;
 
 public:
+    using Value = const char *;
+
     explicit CommonStringField(const _LibObjPtr libObjPtr) noexcept : _ThisCommonField {libObjPtr}
     {
         BT_ASSERT_DBG(this->isString());
@@ -957,7 +943,7 @@ public:
     }
 
     template <typename OtherLibObjT>
-    CommonStringField<LibObjT>& operator=(const CommonStringField<OtherLibObjT> val) noexcept
+    CommonStringField<LibObjT> operator=(const CommonStringField<OtherLibObjT> val) noexcept
     {
         _ThisCommonField::operator=(val);
         return *this;
@@ -968,7 +954,12 @@ public:
         return CommonStringField<const bt_field> {*this};
     }
 
-    CommonStringField<LibObjT> operator=(const char * const val) const
+    RawStringValueProxy<CommonStringField> operator*() const noexcept
+    {
+        return RawStringValueProxy<CommonStringField> {*this};
+    }
+
+    void value(const char * const val) const
     {
         static_assert(!std::is_const<LibObjT>::value,
                       "Not available with `bt2::ConstStringField`.");
@@ -978,13 +969,11 @@ public:
         if (status == BT_FIELD_STRING_SET_VALUE_STATUS_MEMORY_ERROR) {
             throw MemoryError {};
         }
-
-        return *this;
     }
 
-    CommonStringField<LibObjT> operator=(const std::string& val) const
+    void value(const std::string& val) const
     {
-        return *this = val.data();
+        this->value(val.data());
     }
 
     void append(const char * const begin, const std::uint64_t len) const
@@ -1012,7 +1001,7 @@ public:
         bt_field_string_clear(this->libObjPtr());
     }
 
-    bpstd::string_view value() const noexcept
+    const char *value() const noexcept
     {
         return bt_field_string_get_value(this->libObjPtr());
     }
@@ -1101,7 +1090,7 @@ public:
     }
 
     template <typename OtherLibObjT>
-    CommonStructureField<LibObjT>& operator=(const CommonStructureField<OtherLibObjT> val) noexcept
+    CommonStructureField<LibObjT> operator=(const CommonStructureField<OtherLibObjT> val) noexcept
     {
         _ThisCommonField::operator=(val);
         return *this;
@@ -1127,7 +1116,7 @@ public:
         return CommonField<LibObjT> {_Spec::memberFieldByIndex(this->libObjPtr(), index)};
     }
 
-    nonstd::optional<CommonField<LibObjT>> operator[](const char * const name) const noexcept
+    bt2s::optional<CommonField<LibObjT>> operator[](const char * const name) const noexcept
     {
         const auto libObjPtr = _Spec::memberFieldByName(this->libObjPtr(), name);
 
@@ -1135,10 +1124,10 @@ public:
             return CommonField<LibObjT> {libObjPtr};
         }
 
-        return nonstd::nullopt;
+        return bt2s::nullopt;
     }
 
-    nonstd::optional<CommonField<LibObjT>> operator[](const std::string& name) const noexcept
+    bt2s::optional<CommonField<LibObjT>> operator[](const std::string& name) const noexcept
     {
         return (*this)[name.data()];
     }
@@ -1217,7 +1206,7 @@ public:
     }
 
     template <typename OtherLibObjT>
-    _ThisCommonArrayField& operator=(const CommonArrayField<OtherLibObjT> val) noexcept
+    _ThisCommonArrayField operator=(const CommonArrayField<OtherLibObjT> val) noexcept
     {
         _ThisCommonField::operator=(val);
         return *this;
@@ -1288,7 +1277,7 @@ public:
     }
 
     template <typename OtherLibObjT>
-    CommonDynamicArrayField<LibObjT>&
+    CommonDynamicArrayField<LibObjT>
     operator=(const CommonDynamicArrayField<OtherLibObjT> val) noexcept
     {
         _ThisCommonArrayField::operator=(val);
@@ -1386,7 +1375,7 @@ public:
     }
 
     template <typename OtherLibObjT>
-    CommonOptionField<LibObjT>& operator=(const CommonOptionField<OtherLibObjT> val) noexcept
+    CommonOptionField<LibObjT> operator=(const CommonOptionField<OtherLibObjT> val) noexcept
     {
         _ThisCommonField::operator=(val);
         return *this;
@@ -1415,7 +1404,7 @@ public:
         return this->field().has_value();
     }
 
-    nonstd::optional<CommonField<LibObjT>> field() const noexcept
+    bt2s::optional<CommonField<LibObjT>> field() const noexcept
     {
         const auto libObjPtr = _Spec::field(this->libObjPtr());
 
@@ -1423,7 +1412,7 @@ public:
             return CommonField<LibObjT> {libObjPtr};
         }
 
-        return nonstd::nullopt;
+        return bt2s::nullopt;
     }
 };
 
@@ -1495,7 +1484,7 @@ public:
     }
 
     template <typename OtherLibObjT>
-    CommonVariantField<LibObjT>& operator=(const CommonVariantField<OtherLibObjT> val) noexcept
+    CommonVariantField<LibObjT> operator=(const CommonVariantField<OtherLibObjT> val) noexcept
     {
         _ThisCommonField::operator=(val);
         return *this;
This page took 0.028915 seconds and 4 git commands to generate.