X-Git-Url: https://git.efficios.com/?a=blobdiff_plain;f=src%2Fcpp-common%2Fbt2%2Fvalue.hpp;h=14ae5a2b3b7f325dde5be3c6582fde908506fc3f;hb=7eb1d0da9928b3bbda8c0dcc3ff1123f94a82d11;hp=fe5a434d4ed540b1245dfba023a055c4966458f1;hpb=b3f060ed119961106d34da4db9071653a37f5c53;p=babeltrace.git diff --git a/src/cpp-common/bt2/value.hpp b/src/cpp-common/bt2/value.hpp index fe5a434d..14ae5a2b 100644 --- a/src/cpp-common/bt2/value.hpp +++ b/src/cpp-common/bt2/value.hpp @@ -15,13 +15,13 @@ #include "common/assert.h" #include "common/common.h" -#include "cpp-common/optional.hpp" -#include "cpp-common/string_view.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" @@ -95,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; @@ -113,13 +113,13 @@ private: using typename BorrowedObject::_ThisBorrowedObject; protected: - using typename BorrowedObject::_LibObjPtr; using _ThisCommonValue = CommonValue; public: + using typename BorrowedObject::LibObjPtr; using Shared = SharedValue, LibObjT>; - explicit CommonValue(const _LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr} + explicit CommonValue(const LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr} { } @@ -129,7 +129,7 @@ public: } template - _ThisCommonValue& operator=(const CommonValue val) noexcept + _ThisCommonValue operator=(const CommonValue val) noexcept { _ThisBorrowedObject::operator=(val); return *this; @@ -242,7 +242,7 @@ public: } template - nonstd::optional> operator[](KeyT&& key) const noexcept + OptionalBorrowedObject> operator[](KeyT&& key) const noexcept { return this->asMap()[std::forward(key)]; } @@ -259,10 +259,8 @@ public: this->asMap().insert(std::forward(key), std::forward(val)); } - CommonArrayValue insertEmptyArray(const char *key) const; - CommonArrayValue insertEmptyArray(const std::string& key) const; - CommonMapValue insertEmptyMap(const char *key) const; - CommonMapValue insertEmptyMap(const std::string& key) const; + CommonArrayValue insertEmptyArray(bt2c::CStringView key) const; + CommonMapValue insertEmptyMap(bt2c::CStringView key) const; Shared shared() const noexcept { @@ -336,9 +334,10 @@ CommonValueRawValueProxy::operator=(const char * const rawVal) template CommonValueRawValueProxy& -CommonValueRawValueProxy::operator=(const std::string& rawVal) +CommonValueRawValueProxy::operator=(const bt2c::CStringView rawVal) { - return *this = rawVal.data(); + _mObj.asString().value(rawVal); + return *this; } template @@ -366,7 +365,7 @@ CommonValueRawValueProxy::operator double() const noexcept } template -CommonValueRawValueProxy::operator const char *() const noexcept +CommonValueRawValueProxy::operator bt2c::CStringView() const noexcept { return _mObj.asString().value(); } @@ -410,7 +409,7 @@ public: } template - CommonNullValue& operator=(const CommonNullValue val) noexcept + CommonNullValue operator=(const CommonNullValue val) noexcept { _ThisCommonValue::operator=(val); return *this; @@ -454,14 +453,14 @@ template class CommonBoolValue final : public CommonValue { private: - using typename CommonValue::_LibObjPtr; using typename CommonValue::_ThisCommonValue; public: + using typename CommonValue::LibObjPtr; using Shared = SharedValue, LibObjT>; using Value = bool; - explicit CommonBoolValue(const _LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr} + explicit CommonBoolValue(const LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr} { BT_ASSERT_DBG(this->isBool()); } @@ -480,7 +479,7 @@ public: } template - CommonBoolValue& operator=(const CommonBoolValue val) noexcept + CommonBoolValue operator=(const CommonBoolValue val) noexcept { _ThisCommonValue::operator=(val); return *this; @@ -541,14 +540,14 @@ template class CommonUnsignedIntegerValue final : public CommonValue { private: - using typename CommonValue::_LibObjPtr; using typename CommonValue::_ThisCommonValue; public: + using typename CommonValue::LibObjPtr; using Shared = SharedValue, LibObjT>; using Value = std::uint64_t; - explicit CommonUnsignedIntegerValue(const _LibObjPtr libObjPtr) noexcept : + explicit CommonUnsignedIntegerValue(const LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr} { BT_ASSERT_DBG(this->isUnsignedInteger()); @@ -569,7 +568,7 @@ public: } template - CommonUnsignedIntegerValue& + CommonUnsignedIntegerValue operator=(const CommonUnsignedIntegerValue val) noexcept { _ThisCommonValue::operator=(val); @@ -632,14 +631,14 @@ template class CommonSignedIntegerValue final : public CommonValue { private: - using typename CommonValue::_LibObjPtr; using typename CommonValue::_ThisCommonValue; public: + using typename CommonValue::LibObjPtr; using Shared = SharedValue, LibObjT>; using Value = std::int64_t; - explicit CommonSignedIntegerValue(const _LibObjPtr libObjPtr) noexcept : + explicit CommonSignedIntegerValue(const LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr} { BT_ASSERT_DBG(this->isSignedInteger()); @@ -723,14 +722,14 @@ template class CommonRealValue final : public CommonValue { private: - using typename CommonValue::_LibObjPtr; using typename CommonValue::_ThisCommonValue; public: + using typename CommonValue::LibObjPtr; using Shared = SharedValue, LibObjT>; using Value = double; - explicit CommonRealValue(const _LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr} + explicit CommonRealValue(const LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr} { BT_ASSERT_DBG(this->isReal()); } @@ -749,7 +748,7 @@ public: } template - CommonRealValue& operator=(const CommonRealValue val) noexcept + CommonRealValue operator=(const CommonRealValue val) noexcept { _ThisCommonValue::operator=(val); return *this; @@ -810,19 +809,19 @@ template class CommonStringValue final : public CommonValue { private: - using typename CommonValue::_LibObjPtr; using typename CommonValue::_ThisCommonValue; public: + using typename CommonValue::LibObjPtr; using Shared = SharedValue, LibObjT>; - using Value = const char *; + using Value = bt2c::CStringView; - explicit CommonStringValue(const _LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr} + 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); @@ -830,18 +829,13 @@ public: return CommonStringValue::Shared::createWithoutRef(libObjPtr); } - static Shared create(const std::string& rawVal) - { - return CommonStringValue::create(rawVal.data()); - } - template CommonStringValue(const CommonStringValue val) noexcept : _ThisCommonValue {val} { } template - CommonStringValue& operator=(const CommonStringValue val) noexcept + CommonStringValue operator=(const CommonStringValue val) noexcept { _ThisCommonValue::operator=(val); return *this; @@ -852,9 +846,9 @@ public: return CommonStringValue {*this}; } - RawStringValueProxy operator*() const noexcept + RawValueProxy operator*() const noexcept { - return RawStringValueProxy {*this}; + return RawValueProxy {*this}; } void value(const Value val) const @@ -862,19 +856,14 @@ public: static_assert(!std::is_const::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()); } @@ -936,14 +925,14 @@ template class CommonArrayValue final : public CommonValue { private: - using typename CommonValue::_LibObjPtr; using typename CommonValue::_ThisCommonValue; public: + using typename CommonValue::LibObjPtr; using Shared = SharedValue, LibObjT>; using Iterator = BorrowedObjectIterator>; - explicit CommonArrayValue(const _LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr} + explicit CommonArrayValue(const LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr} { BT_ASSERT_DBG(this->isArray()); } @@ -962,7 +951,7 @@ public: } template - CommonArrayValue& operator=(const CommonArrayValue val) noexcept + CommonArrayValue operator=(const CommonArrayValue val) noexcept { _ThisCommonValue::operator=(val); return *this; @@ -1055,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()); } @@ -1093,7 +1082,7 @@ public: this->append(rawVal); } - void operator+=(const std::string& rawVal) const + void operator+=(const bt2c::CStringView rawVal) const { this->append(rawVal); } @@ -1139,7 +1128,7 @@ struct TypeDescr : public ArrayValueTypeDescr * First argument is the entry's key, second is its value. */ template -using CommonMapValueForEachUserFunc = std::function; +using CommonMapValueForEachUserFunc = std::function; /* * Template of a function to be passed to bt_value_map_foreach_entry() @@ -1149,7 +1138,7 @@ using CommonMapValueForEachUserFunc = std::function` (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 @@ -1240,13 +1229,13 @@ template class CommonMapValue final : public CommonValue { private: - using typename CommonValue::_LibObjPtr; using typename CommonValue::_ThisCommonValue; public: + using typename CommonValue::LibObjPtr; using Shared = SharedValue, LibObjT>; - explicit CommonMapValue(const _LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr} + explicit CommonMapValue(const LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr} { BT_ASSERT_DBG(this->isMap()); } @@ -1265,7 +1254,7 @@ public: } template - CommonMapValue& operator=(const CommonMapValue val) noexcept + CommonMapValue operator=(const CommonMapValue val) noexcept { _ThisCommonValue::operator=(val); return *this; @@ -1286,34 +1275,18 @@ public: return this->length() == 0; } - nonstd::optional> operator[](const char * const key) const noexcept - { - const auto libObjPtr = - internal::CommonMapValueSpec::entryByKey(this->libObjPtr(), key); - - if (!libObjPtr) { - return nonstd::nullopt; - } - - return CommonValue {libObjPtr}; - } - - nonstd::optional> operator[](const std::string& key) const noexcept + OptionalBorrowedObject> + operator[](const bt2c::CStringView key) const noexcept { - return (*this)[key.data()]; + return internal::CommonMapValueSpec::entryByKey(this->libObjPtr(), key); } - bool hasEntry(const char * const key) const noexcept + bool hasEntry(const bt2c::CStringView key) const noexcept { return static_cast(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::value, "Not available with `bt2::ConstMapValue`."); @@ -1322,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::value, "Not available with `bt2::ConstMapValue`."); @@ -1337,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::value, "Not available with `bt2::ConstMapValue`."); @@ -1351,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::value, "Not available with `bt2::ConstMapValue`."); @@ -1367,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::value, "Not available with `bt2::ConstMapValue`."); @@ -1381,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::value, "Not available with `bt2::ConstMapValue`."); @@ -1395,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 insertEmptyArray(const char *key) const; - CommonArrayValue insertEmptyArray(const std::string& key) const; - CommonMapValue insertEmptyMap(const char *key) const; - CommonMapValue insertEmptyMap(const std::string& key) const; + CommonArrayValue insertEmptyArray(bt2c::CStringView key) const; + CommonMapValue insertEmptyMap(bt2c::CStringView key) const; void forEach(const internal::CommonMapValueForEachUserFunc>& func) const { @@ -1470,25 +1405,13 @@ MapValue CommonValue::appendEmptyMap() const } template -ArrayValue CommonValue::insertEmptyArray(const char * const key) const +ArrayValue CommonValue::insertEmptyArray(const bt2c::CStringView key) const { return this->asMap().insertEmptyArray(key); } template -ArrayValue CommonValue::insertEmptyArray(const std::string& key) const -{ - return this->asMap().insertEmptyArray(key); -} - -template -MapValue CommonValue::insertEmptyMap(const char * const key) const -{ - return this->asMap().insertEmptyMap(key); -} - -template -MapValue CommonValue::insertEmptyMap(const std::string& key) const +MapValue CommonValue::insertEmptyMap(const bt2c::CStringView key) const { return this->asMap().insertEmptyMap(key); } @@ -1497,7 +1420,7 @@ template CommonNullValue CommonValue::asNull() const noexcept { BT_ASSERT_DBG(this->isNull()); - return CommonNullValue {this->libObjPtr()}; + return CommonNullValue {}; } template @@ -1574,7 +1497,7 @@ MapValue CommonArrayValue::appendEmptyMap() const } template -ArrayValue CommonMapValue::insertEmptyArray(const char * const key) const +ArrayValue CommonMapValue::insertEmptyArray(const bt2c::CStringView key) const { static_assert(!std::is_const::value, "Not available with `bt2::ConstMapValue`."); @@ -1586,13 +1509,7 @@ ArrayValue CommonMapValue::insertEmptyArray(const char * const key) con } template -ArrayValue CommonMapValue::insertEmptyArray(const std::string& key) const -{ - return this->insertEmptyArray(key.data()); -} - -template -MapValue CommonMapValue::insertEmptyMap(const char * const key) const +MapValue CommonMapValue::insertEmptyMap(const bt2c::CStringView key) const { static_assert(!std::is_const::value, "Not available with `bt2::ConstMapValue`."); @@ -1603,12 +1520,6 @@ MapValue CommonMapValue::insertEmptyMap(const char * const key) const return MapValue {libEntryPtr}; } -template -MapValue CommonMapValue::insertEmptyMap(const std::string& key) const -{ - return this->insertEmptyMap(key.data()); -} - inline BoolValue::Shared createValue(const bool rawVal) { return BoolValue::create(rawVal); @@ -1634,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); }