X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=src%2Fcpp-common%2Fbt2%2Fvalue.hpp;h=81c033673f8b70d72c056e7dc5c0c4ed2094d54e;hb=e74015680521597497c3218160a9e80081932896;hp=e9b61b130eae29154e419fc3d0f9d4daae8e9e49;hpb=4927bae7b89dff01618509f362c9400ee1993e94;p=babeltrace.git diff --git a/src/cpp-common/bt2/value.hpp b/src/cpp-common/bt2/value.hpp index e9b61b13..81c03367 100644 --- a/src/cpp-common/bt2/value.hpp +++ b/src/cpp-common/bt2/value.hpp @@ -7,43 +7,45 @@ #ifndef BABELTRACE_CPP_COMMON_BT2_VALUE_HPP #define BABELTRACE_CPP_COMMON_BT2_VALUE_HPP -#include #include #include -#include +#include + #include #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/bt2c/c-string-view.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 "optional-borrowed-object.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 -using SharedValue = internal::SharedObj; - } /* namespace internal */ +template +using SharedValue = SharedObject; + template class CommonNullValue; @@ -80,71 +82,65 @@ enum class ValueType MAP = BT_VALUE_TYPE_MAP, }; -template -class CommonClockClass; - -template -class CommonFieldClass; - -template -class CommonTraceClass; - -template -class CommonStreamClass; +template +class CommonValueRawValueProxy final +{ +public: + explicit CommonValueRawValueProxy(const ValueObjT obj) : _mObj {obj} + { + } -template -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; + operator bt2c::CStringView() const noexcept; -template -class CommonStream; +private: + ValueObjT _mObj; +}; template -class CommonValue : public internal::BorrowedObj +class CommonValue : public BorrowedObject { - /* Allow append() to call `val.libObjPtr()` */ - friend class CommonArrayValue; - - /* Allow insert() to call `val.libObjPtr()` */ - friend class CommonMapValue; - - /* Allow userAttributes() to call `val.libObjPtr()` */ - friend class CommonClockClass; - friend class CommonFieldClass; - friend class CommonTraceClass; - friend class CommonStreamClass; - friend class CommonEventClass; - friend class CommonStream; - - /* Allow operator==() to call `other.libObjPtr()` */ - friend class CommonValue; - friend class CommonValue; - private: - using typename internal::BorrowedObj::_ThisBorrowedObj; + using typename BorrowedObject::_ThisBorrowedObject; protected: - using typename internal::BorrowedObj::_LibObjPtr; using _ThisCommonValue = CommonValue; public: - using Shared = internal::SharedValue, LibObjT>; + using typename BorrowedObject::LibObjPtr; + using Shared = SharedValue, LibObjT>; - explicit CommonValue(const _LibObjPtr libObjPtr) noexcept : _ThisBorrowedObj {libObjPtr} + explicit CommonValue(const LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr} { } template - CommonValue(const CommonValue& val) noexcept : _ThisBorrowedObj {val} + CommonValue(const CommonValue val) noexcept : _ThisBorrowedObject {val} { } template - _ThisCommonValue& operator=(const CommonValue& val) noexcept + _ThisCommonValue operator=(const CommonValue val) noexcept { - _ThisBorrowedObj::operator=(val); + _ThisBorrowedObject::operator=(val); return *this; } + CommonValue asConst() const noexcept + { + return CommonValue {*this}; + } + ValueType type() const noexcept { return static_cast(bt_value_get_type(this->libObjPtr())); @@ -196,20 +192,88 @@ public: } template - bool operator==(const CommonValue& other) const noexcept + bool operator==(const CommonValue other) const noexcept { return static_cast(bt_value_is_equal(this->libObjPtr(), other.libObjPtr())); } template - bool operator!=(const CommonValue& other) const noexcept + bool operator!=(const CommonValue other) const noexcept { return !(*this == other); } + CommonValueRawValueProxy operator*() const noexcept + { + return CommonValueRawValueProxy {*this}; + } + + std::uint64_t arrayLength() const noexcept + { + return this->asArray().length(); + } + + bool arrayIsEmpty() const noexcept + { + return this->asArray().isEmpty(); + } + + CommonValue operator[](const std::uint64_t index) const noexcept + { + return this->asArray()[index]; + } + + template + void append(T&& elem) const + { + this->asArray().append(std::forward(elem)); + } + + CommonArrayValue appendEmptyArray() const; + CommonMapValue appendEmptyMap() const; + + std::uint64_t mapLength() const noexcept + { + return this->asMap().length(); + } + + bool mapIsEmpty() const noexcept + { + return this->asMap().isEmpty(); + } + + template + OptionalBorrowedObject> operator[](KeyT&& key) const noexcept + { + return this->asMap()[std::forward(key)]; + } + + template + bool hasEntry(KeyT&& key) const noexcept + { + return this->asMap().hasEntry(std::forward(key)); + } + + template + void insert(KeyT&& key, ValT&& val) const + { + 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; + Shared shared() const noexcept { - return Shared {*this}; + return Shared::createWithRef(*this); + } + + template + ValueT as() const noexcept + { + return ValueT {this->libObjPtr()}; } CommonNullValue asNull() const noexcept; @@ -231,6 +295,90 @@ protected: using Value = CommonValue; using ConstValue = CommonValue; +template +CommonValueRawValueProxy& +CommonValueRawValueProxy::operator=(const bool rawVal) noexcept +{ + _mObj.asBool().value(rawVal); + return *this; +} + +template +CommonValueRawValueProxy& +CommonValueRawValueProxy::operator=(const std::int64_t rawVal) noexcept +{ + _mObj.asSignedInteger().value(rawVal); + return *this; +} + +template +CommonValueRawValueProxy& +CommonValueRawValueProxy::operator=(const std::uint64_t rawVal) noexcept +{ + _mObj.asUnsignedInteger().value(rawVal); + return *this; +} + +template +CommonValueRawValueProxy& +CommonValueRawValueProxy::operator=(const double rawVal) noexcept +{ + _mObj.asReal().value(rawVal); + return *this; +} + +template +CommonValueRawValueProxy& +CommonValueRawValueProxy::operator=(const char * const rawVal) +{ + _mObj.asString().value(rawVal); + return *this; +} + +template +CommonValueRawValueProxy& +CommonValueRawValueProxy::operator=(const std::string& rawVal) +{ + _mObj.asString().value(rawVal); + return *this; +} + +template +CommonValueRawValueProxy::operator bool() const noexcept +{ + return _mObj.asBool().value(); +} + +template +CommonValueRawValueProxy::operator std::int64_t() const noexcept +{ + return _mObj.asSignedInteger().value(); +} + +template +CommonValueRawValueProxy::operator std::uint64_t() const noexcept +{ + return _mObj.asUnsignedInteger().value(); +} + +template +CommonValueRawValueProxy::operator double() const noexcept +{ + return _mObj.asReal().value(); +} + +template +CommonValueRawValueProxy::operator const char *() const noexcept +{ + return _mObj.asString().value(); +} + +template +CommonValueRawValueProxy::operator bt2c::CStringView() const noexcept +{ + return _mObj.asString().value(); +} + namespace internal { struct ValueTypeDescr @@ -258,27 +406,32 @@ private: using typename CommonValue::_ThisCommonValue; public: - using Shared = internal::SharedValue, LibObjT>; + using Shared = SharedValue, LibObjT>; CommonNullValue() noexcept : _ThisCommonValue {bt_value_null} { } template - CommonNullValue(const CommonNullValue& val) noexcept : _ThisCommonValue {val} + CommonNullValue(const CommonNullValue val) noexcept : _ThisCommonValue {val} { } template - CommonNullValue& operator=(const CommonNullValue& val) noexcept + CommonNullValue operator=(const CommonNullValue val) noexcept { _ThisCommonValue::operator=(val); return *this; } + CommonNullValue asConst() const noexcept + { + return CommonNullValue {*this}; + } + Shared shared() const noexcept { - return Shared {*this}; + return Shared::createWithRef(*this); } }; @@ -309,20 +462,20 @@ template class CommonBoolValue final : public CommonValue { private: - using typename CommonValue::_LibObjPtr; using typename CommonValue::_ThisCommonValue; public: - using Shared = internal::SharedValue, LibObjT>; + 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()); } template - CommonBoolValue(const CommonBoolValue& val) noexcept : _ThisCommonValue {val} + CommonBoolValue(const CommonBoolValue val) noexcept : _ThisCommonValue {val} { } @@ -331,22 +484,24 @@ public: const auto libObjPtr = bt_value_bool_create_init(static_cast(rawVal)); internal::validateCreatedObjPtr(libObjPtr); - return Shared {CommonBoolValue {libObjPtr}}; + return CommonBoolValue::Shared::createWithoutRef(libObjPtr); } template - CommonBoolValue& operator=(const CommonBoolValue& val) noexcept + CommonBoolValue operator=(const CommonBoolValue val) noexcept { _ThisCommonValue::operator=(val); return *this; } - CommonBoolValue& operator=(const Value rawVal) noexcept + CommonBoolValue asConst() const noexcept { - static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); + return CommonBoolValue {*this}; + } - bt_value_bool_set(this->libObjPtr(), static_cast(rawVal)); - return *this; + RawValueProxy operator*() const noexcept + { + return RawValueProxy {*this}; } Value value() const noexcept @@ -354,14 +509,16 @@ public: return static_cast(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::value, "Not available with `bt2::ConstBoolValue`."); + + bt_value_bool_set(this->libObjPtr(), static_cast(val)); } Shared shared() const noexcept { - return Shared {*this}; + return Shared::createWithRef(*this); } }; @@ -392,14 +549,14 @@ template class CommonUnsignedIntegerValue final : public CommonValue { private: - using typename CommonValue::_LibObjPtr; using typename CommonValue::_ThisCommonValue; public: - using Shared = internal::SharedValue, LibObjT>; + 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()); @@ -410,44 +567,49 @@ public: const auto libObjPtr = bt_value_integer_unsigned_create_init(rawVal); internal::validateCreatedObjPtr(libObjPtr); - return Shared {CommonUnsignedIntegerValue {libObjPtr}}; + return CommonUnsignedIntegerValue::Shared::createWithoutRef(libObjPtr); } template - CommonUnsignedIntegerValue(const CommonUnsignedIntegerValue& val) noexcept : + CommonUnsignedIntegerValue(const CommonUnsignedIntegerValue val) noexcept : _ThisCommonValue {val} { } template - CommonUnsignedIntegerValue& - operator=(const CommonUnsignedIntegerValue& val) noexcept + CommonUnsignedIntegerValue + operator=(const CommonUnsignedIntegerValue val) noexcept { - static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); - _ThisCommonValue::operator=(val); return *this; } - CommonUnsignedIntegerValue& operator=(const Value rawVal) noexcept + CommonUnsignedIntegerValue asConst() const noexcept { - bt_value_integer_unsigned_set(this->libObjPtr(), rawVal); - return *this; + return CommonUnsignedIntegerValue {*this}; } - Value value() const noexcept + RawValueProxy operator*() const noexcept { - return bt_value_integer_unsigned_get(this->libObjPtr()); + return RawValueProxy {*this}; + } + + void value(const Value val) const noexcept + { + static_assert(!std::is_const::value, + "Not available with `bt2::ConstUnsignedIntegerValue`."); + + bt_value_integer_unsigned_set(this->libObjPtr(), val); } - operator Value() const noexcept + Value value() const noexcept { - return this->value(); + return bt_value_integer_unsigned_get(this->libObjPtr()); } Shared shared() const noexcept { - return Shared {*this}; + return Shared::createWithRef(*this); } }; @@ -478,14 +640,14 @@ template class CommonSignedIntegerValue final : public CommonValue { private: - using typename CommonValue::_LibObjPtr; using typename CommonValue::_ThisCommonValue; public: - using Shared = internal::SharedValue, LibObjT>; + 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()); @@ -496,44 +658,49 @@ public: const auto libObjPtr = bt_value_integer_signed_create_init(rawVal); internal::validateCreatedObjPtr(libObjPtr); - return Shared {CommonSignedIntegerValue {libObjPtr}}; + return CommonSignedIntegerValue::Shared::createWithoutRef(libObjPtr); } template - CommonSignedIntegerValue(const CommonSignedIntegerValue& val) noexcept : + CommonSignedIntegerValue(const CommonSignedIntegerValue val) noexcept : _ThisCommonValue {val} { } template - CommonSignedIntegerValue& - operator=(const CommonSignedIntegerValue& val) noexcept + CommonSignedIntegerValue + operator=(const CommonSignedIntegerValue val) noexcept { _ThisCommonValue::operator=(val); return *this; } - CommonSignedIntegerValue& operator=(const Value rawVal) noexcept + CommonSignedIntegerValue asConst() const noexcept { - static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); + return CommonSignedIntegerValue {*this}; + } - bt_value_integer_signed_set(this->libObjPtr(), rawVal); - return *this; + RawValueProxy operator*() const noexcept + { + return RawValueProxy {*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::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 { - return Shared {*this}; + return Shared::createWithRef(*this); } }; @@ -564,14 +731,14 @@ template class CommonRealValue final : public CommonValue { private: - using typename CommonValue::_LibObjPtr; using typename CommonValue::_ThisCommonValue; public: - using Shared = internal::SharedValue, LibObjT>; + 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()); } @@ -581,42 +748,46 @@ public: const auto libObjPtr = bt_value_real_create_init(rawVal); internal::validateCreatedObjPtr(libObjPtr); - return Shared {CommonRealValue {libObjPtr}}; + return CommonRealValue::Shared::createWithoutRef(libObjPtr); } template - CommonRealValue(const CommonRealValue& val) noexcept : _ThisCommonValue {val} + CommonRealValue(const CommonRealValue val) noexcept : _ThisCommonValue {val} { } template - CommonRealValue& operator=(const CommonRealValue& val) noexcept + CommonRealValue operator=(const CommonRealValue val) noexcept { _ThisCommonValue::operator=(val); return *this; } - CommonRealValue& operator=(const Value rawVal) noexcept + CommonRealValue asConst() const noexcept { - static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); + return CommonRealValue {*this}; + } - bt_value_real_set(this->libObjPtr(), rawVal); - return *this; + RawValueProxy operator*() const noexcept + { + return RawValueProxy {*this}; } - Value value() const noexcept + void value(const Value val) const noexcept { - return bt_value_real_get(this->libObjPtr()); + static_assert(!std::is_const::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 { - return Shared {*this}; + return Shared::createWithRef(*this); } }; @@ -647,13 +818,14 @@ template class CommonStringValue final : public CommonValue { private: - using typename CommonValue::_LibObjPtr; using typename CommonValue::_ThisCommonValue; public: - using Shared = internal::SharedValue, LibObjT>; + using typename CommonValue::LibObjPtr; + using Shared = SharedValue, LibObjT>; + 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()); } @@ -663,52 +835,66 @@ public: const auto libObjPtr = bt_value_string_create_init(rawVal); internal::validateCreatedObjPtr(libObjPtr); - return Shared {CommonStringValue {libObjPtr}}; + return CommonStringValue::Shared::createWithoutRef(libObjPtr); } static Shared create(const std::string& rawVal) { - return CommonStringValue::create(rawVal.data()); + return CommonStringValue::create(rawVal.data()); } template - CommonStringValue(const CommonStringValue& val) noexcept : _ThisCommonValue {val} + 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; } - CommonStringValue& operator=(const char * const rawVal) + CommonStringValue asConst() const noexcept { - static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); + return CommonStringValue {*this}; + } + + RawStringValueProxy operator*() const noexcept + { + return RawStringValueProxy {*this}; + } + + void value(const Value val) const + { + static_assert(!std::is_const::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; + void value(const char * const val) const + { + this->value(bt2c::CStringView {val}); } - CommonStringValue& operator=(const std::string& rawVal) noexcept + void value(const std::string& val) const { - return *this = rawVal.data(); + this->value(bt2c::CStringView {val.data()}); } - bpstd::string_view value() const noexcept + Value value() const noexcept { return bt_value_string_get(this->libObjPtr()); } Shared shared() const noexcept { - return Shared {*this}; + return Shared::createWithRef(*this); } }; @@ -763,14 +949,14 @@ template class CommonArrayValue final : public CommonValue { private: - using typename CommonValue::_LibObjPtr; using typename CommonValue::_ThisCommonValue; public: - using Shared = internal::SharedValue, LibObjT>; - using Iterator = CommonIterator, CommonValue>; + 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()); } @@ -780,30 +966,29 @@ public: const auto libObjPtr = bt_value_array_create(); internal::validateCreatedObjPtr(libObjPtr); - return Shared {CommonArrayValue {libObjPtr}}; + return CommonArrayValue::Shared::createWithoutRef(libObjPtr); } template - CommonArrayValue(const CommonArrayValue& val) noexcept : _ThisCommonValue {val} + CommonArrayValue(const CommonArrayValue val) noexcept : _ThisCommonValue {val} { } template - CommonArrayValue& operator=(const CommonArrayValue& val) noexcept + CommonArrayValue operator=(const CommonArrayValue val) noexcept { _ThisCommonValue::operator=(val); return *this; } - std::uint64_t length() const noexcept + CommonArrayValue asConst() const noexcept { - return bt_value_array_get_length(this->libObjPtr()); + return CommonArrayValue {*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 @@ -821,30 +1006,24 @@ public: return this->length() == 0; } - ConstValue operator[](const std::uint64_t index) const noexcept - { - return ConstValue {internal::CommonArrayValueSpec::elementByIndex( - this->libObjPtr(), index)}; - } - - CommonValue operator[](const std::uint64_t index) noexcept + CommonValue operator[](const std::uint64_t index) const noexcept { return CommonValue { internal::CommonArrayValueSpec::elementByIndex(this->libObjPtr(), index)}; } - void append(const Value& val) + void append(const Value val) const { - static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); + static_assert(!std::is_const::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::value, "`LibObjT` must NOT be `const`."); + static_assert(!std::is_const::value, "Not available with `bt2::ConstArrayValue`."); const auto status = bt_value_array_append_bool_element(this->libObjPtr(), static_cast(rawVal)); @@ -852,9 +1031,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::value, "`LibObjT` must NOT be `const`."); + static_assert(!std::is_const::value, "Not available with `bt2::ConstArrayValue`."); const auto status = bt_value_array_append_unsigned_integer_element(this->libObjPtr(), rawVal); @@ -862,86 +1041,86 @@ public: this->_handleAppendLibStatus(status); } - void append(const std::int64_t rawVal) + void append(const std::int64_t rawVal) const { - static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); + static_assert(!std::is_const::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::value, "`LibObjT` must NOT be `const`."); + static_assert(!std::is_const::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::value, "`LibObjT` must NOT be `const`."); + static_assert(!std::is_const::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 appendEmptyArray(); - CommonMapValue appendEmptyMap(); + CommonArrayValue appendEmptyArray() const; + CommonMapValue 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); } Shared shared() const noexcept { - return Shared {*this}; + return Shared::createWithRef(*this); } 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 {}; } } }; @@ -973,7 +1152,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() @@ -983,7 +1162,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 @@ -1028,7 +1207,7 @@ struct CommonMapValueSpec 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(); } @@ -1061,7 +1240,7 @@ struct CommonMapValueSpec 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(); } @@ -1074,13 +1253,13 @@ template class CommonMapValue final : public CommonValue { private: - using typename CommonValue::_LibObjPtr; using typename CommonValue::_ThisCommonValue; public: - using Shared = internal::SharedValue, LibObjT>; + 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()); } @@ -1090,61 +1269,42 @@ public: const auto libObjPtr = bt_value_map_create(); internal::validateCreatedObjPtr(libObjPtr); - return Shared {CommonMapValue {libObjPtr}}; + return CommonMapValue::Shared::createWithoutRef(libObjPtr); } template - CommonMapValue(const CommonMapValue& val) noexcept : _ThisCommonValue {val} + CommonMapValue(const CommonMapValue val) noexcept : _ThisCommonValue {val} { } template - CommonMapValue& operator=(const CommonMapValue& val) noexcept + CommonMapValue operator=(const CommonMapValue 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 asConst() const noexcept { - return this->size() == 0; + return CommonMapValue {*this}; } - nonstd::optional operator[](const char * const key) const noexcept + std::uint64_t length() const noexcept { - const auto libObjPtr = - internal::CommonMapValueSpec::entryByKey(this->libObjPtr(), key); - - if (!libObjPtr) { - return nonstd::nullopt; - } - - return ConstValue {libObjPtr}; + return bt_value_map_get_size(this->libObjPtr()); } - nonstd::optional operator[](const std::string& key) const noexcept + bool isEmpty() const noexcept { - return (*this)[key.data()]; + return this->length() == 0; } - nonstd::optional> operator[](const char * const key) noexcept + OptionalBorrowedObject> operator[](const char * const key) const noexcept { - const auto libObjPtr = - internal::CommonMapValueSpec::entryByKey(this->libObjPtr(), key); - - if (!libObjPtr) { - return nonstd::nullopt; - } - - return CommonValue {libObjPtr}; + return internal::CommonMapValueSpec::entryByKey(this->libObjPtr(), key); } - nonstd::optional> operator[](const std::string& key) noexcept + OptionalBorrowedObject> operator[](const std::string& key) const noexcept { return (*this)[key.data()]; } @@ -1159,23 +1319,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::value, "`LibObjT` must NOT be `const`."); + static_assert(!std::is_const::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::value, "`LibObjT` must NOT be `const`."); + static_assert(!std::is_const::value, "Not available with `bt2::ConstMapValue`."); const auto status = bt_value_map_insert_bool_entry(this->libObjPtr(), key, static_cast(rawVal)); @@ -1183,14 +1343,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::value, "`LibObjT` must NOT be `const`."); + static_assert(!std::is_const::value, "Not available with `bt2::ConstMapValue`."); const auto status = bt_value_map_insert_unsigned_integer_entry(this->libObjPtr(), key, rawVal); @@ -1198,14 +1358,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::value, "`LibObjT` must NOT be `const`."); + static_assert(!std::is_const::value, "Not available with `bt2::ConstMapValue`."); const auto status = bt_value_map_insert_signed_integer_entry(this->libObjPtr(), key, rawVal); @@ -1213,74 +1373,69 @@ 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::value, "`LibObjT` must NOT be `const`."); + static_assert(!std::is_const::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::value, "`LibObjT` must NOT be `const`."); + static_assert(!std::is_const::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 insertEmptyArray(const char *key); - CommonArrayValue insertEmptyArray(const std::string& key); - CommonMapValue insertEmptyMap(const char *key); - CommonMapValue insertEmptyMap(const std::string& key); - - void forEach(const internal::CommonMapValueForEachUserFunc& func) const - { - internal::CommonMapValueSpec::forEach(this->libObjPtr(), func); - } + 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; - void forEach(const internal::CommonMapValueForEachUserFunc>& func) + void forEach(const internal::CommonMapValueForEachUserFunc>& func) const { internal::CommonMapValueSpec::forEach(this->libObjPtr(), func); } Shared shared() const noexcept { - return Shared {*this}; + return Shared::createWithRef(*this); } 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 {}; } } }; @@ -1308,6 +1463,42 @@ struct TypeDescr : public MapValueTypeDescr } /* namespace internal */ +template +ArrayValue CommonValue::appendEmptyArray() const +{ + return this->asArray().appendEmptyArray(); +} + +template +MapValue CommonValue::appendEmptyMap() const +{ + return this->asArray().appendEmptyMap(); +} + +template +ArrayValue CommonValue::insertEmptyArray(const char * const 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 +{ + return this->asMap().insertEmptyMap(key); +} + template CommonNullValue CommonValue::asNull() const noexcept { @@ -1365,9 +1556,9 @@ CommonMapValue CommonValue::asMap() const noexcept } template -ArrayValue CommonArrayValue::appendEmptyArray() +ArrayValue CommonArrayValue::appendEmptyArray() const { - static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); + static_assert(!std::is_const::value, "Not available with `bt2::ConstArrayValue`."); bt_value *libElemPtr; const auto status = bt_value_array_append_empty_array_element(this->libObjPtr(), &libElemPtr); @@ -1377,9 +1568,9 @@ ArrayValue CommonArrayValue::appendEmptyArray() } template -MapValue CommonArrayValue::appendEmptyMap() +MapValue CommonArrayValue::appendEmptyMap() const { - static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); + static_assert(!std::is_const::value, "Not available with `bt2::ConstArrayValue`."); bt_value *libElemPtr; const auto status = bt_value_array_append_empty_map_element(this->libObjPtr(), &libElemPtr); @@ -1389,9 +1580,9 @@ MapValue CommonArrayValue::appendEmptyMap() } template -ArrayValue CommonMapValue::insertEmptyArray(const char * const key) +ArrayValue CommonMapValue::insertEmptyArray(const char * const key) const { - static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); + static_assert(!std::is_const::value, "Not available with `bt2::ConstMapValue`."); bt_value *libEntryPtr; const auto status = bt_value_map_insert_empty_array_entry(this->libObjPtr(), key, &libEntryPtr); @@ -1401,15 +1592,15 @@ ArrayValue CommonMapValue::insertEmptyArray(const char * const key) } template -ArrayValue CommonMapValue::insertEmptyArray(const std::string& key) +ArrayValue CommonMapValue::insertEmptyArray(const std::string& key) const { return this->insertEmptyArray(key.data()); } template -MapValue CommonMapValue::insertEmptyMap(const char * const key) +MapValue CommonMapValue::insertEmptyMap(const char * const key) const { - static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); + static_assert(!std::is_const::value, "Not available with `bt2::ConstMapValue`."); bt_value *libEntryPtr; const auto status = bt_value_map_insert_empty_map_entry(this->libObjPtr(), key, &libEntryPtr); @@ -1419,7 +1610,7 @@ MapValue CommonMapValue::insertEmptyMap(const char * const key) } template -MapValue CommonMapValue::insertEmptyMap(const std::string& key) +MapValue CommonMapValue::insertEmptyMap(const std::string& key) const { return this->insertEmptyMap(key.data()); }