X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=src%2Fcpp-common%2Fbt2%2Fvalue.hpp;h=0dfe9dc614d42b8598b80365089700a3958d2ddf;hb=c7e1be4b771715840b4bc3d635f72eeaf1d2e984;hp=feedde27ba985688411ceef69d8c0c2af5c29bfb;hpb=3a34361189fd69c82934ae4b95713b272ccc5e55;p=babeltrace.git diff --git a/src/cpp-common/bt2/value.hpp b/src/cpp-common/bt2/value.hpp index feedde27..0dfe9dc6 100644 --- a/src/cpp-common/bt2/value.hpp +++ b/src/cpp-common/bt2/value.hpp @@ -7,23 +7,24 @@ #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 "cpp-common/optional.hpp" +#include "cpp-common/string_view.hpp" + +#include "common-iter.hpp" +#include "exc.hpp" #include "internal/borrowed-obj.hpp" #include "internal/shared-obj.hpp" #include "internal/utils.hpp" -#include "cpp-common/optional.hpp" -#include "cpp-common/string_view.hpp" -#include "lib-error.hpp" namespace bt2 { - namespace internal { struct ValueRefFuncs final @@ -42,7 +43,7 @@ struct ValueRefFuncs final template using SharedValue = internal::SharedObj; -} // namespace internal +} /* namespace internal */ template class CommonNullValue; @@ -101,13 +102,13 @@ class CommonStream; template class CommonValue : public internal::BorrowedObj { - // Allow append() to call `val._libObjPtr()` + /* Allow append() to call `val.libObjPtr()` */ friend class CommonArrayValue; - // Allow insert() to call `val._libObjPtr()` + /* Allow insert() to call `val.libObjPtr()` */ friend class CommonMapValue; - // Allow userAttributes() to call `val._libObjPtr()` + /* Allow userAttributes() to call `val.libObjPtr()` */ friend class CommonClockClass; friend class CommonFieldClass; friend class CommonTraceClass; @@ -115,7 +116,7 @@ class CommonValue : public internal::BorrowedObj friend class CommonEventClass; friend class CommonStream; - // Allow operator==() to call `other._libObjPtr()` + /* Allow operator==() to call `other.libObjPtr()` */ friend class CommonValue; friend class CommonValue; @@ -134,12 +135,12 @@ public: } template - CommonValue(const CommonValue& val) noexcept : _ThisBorrowedObj {val} + CommonValue(const CommonValue val) noexcept : _ThisBorrowedObj {val} { } template - _ThisCommonValue& operator=(const CommonValue& val) noexcept + _ThisCommonValue& operator=(const CommonValue val) noexcept { _ThisBorrowedObj::operator=(val); return *this; @@ -147,7 +148,7 @@ public: ValueType type() const noexcept { - return static_cast(bt_value_get_type(this->_libObjPtr())); + return static_cast(bt_value_get_type(this->libObjPtr())); } bool isNull() const noexcept @@ -196,20 +197,26 @@ 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())); + 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); } Shared shared() const noexcept { - return Shared {*this}; + return Shared::createWithRef(*this); + } + + template + ValueT as() const noexcept + { + return ValueT {this->libObjPtr()}; } CommonNullValue asNull() const noexcept; @@ -224,13 +231,33 @@ public: protected: bool _libTypeIs(const bt_value_type type) const noexcept { - return bt_value_type_is(bt_value_get_type(this->_libObjPtr()), type); + return bt_value_type_is(bt_value_get_type(this->libObjPtr()), type); } }; using Value = CommonValue; using ConstValue = CommonValue; +namespace internal { + +struct ValueTypeDescr +{ + using Const = ConstValue; + using NonConst = Value; +}; + +template <> +struct TypeDescr : public ValueTypeDescr +{ +}; + +template <> +struct TypeDescr : public ValueTypeDescr +{ +}; + +} /* namespace internal */ + template class CommonNullValue final : public CommonValue { @@ -245,12 +272,12 @@ public: } 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; @@ -258,13 +285,33 @@ public: Shared shared() const noexcept { - return Shared {*this}; + return Shared::createWithRef(*this); } }; using NullValue = CommonNullValue; using ConstNullValue = CommonNullValue; +namespace internal { + +struct NullValueTypeDescr +{ + using Const = ConstNullValue; + using NonConst = NullValue; +}; + +template <> +struct TypeDescr : public NullValueTypeDescr +{ +}; + +template <> +struct TypeDescr : public NullValueTypeDescr +{ +}; + +} /* namespace internal */ + template class CommonBoolValue final : public CommonValue { @@ -282,7 +329,7 @@ public: } template - CommonBoolValue(const CommonBoolValue& val) noexcept : _ThisCommonValue {val} + CommonBoolValue(const CommonBoolValue val) noexcept : _ThisCommonValue {val} { } @@ -291,11 +338,11 @@ 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; @@ -305,13 +352,13 @@ public: { static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); - bt_value_bool_set(this->_libObjPtr(), static_cast(rawVal)); + bt_value_bool_set(this->libObjPtr(), static_cast(rawVal)); return *this; } Value value() const noexcept { - return static_cast(bt_value_bool_get(this->_libObjPtr())); + return static_cast(bt_value_bool_get(this->libObjPtr())); } operator Value() const noexcept @@ -321,13 +368,33 @@ public: Shared shared() const noexcept { - return Shared {*this}; + return Shared::createWithRef(*this); } }; using BoolValue = CommonBoolValue; using ConstBoolValue = CommonBoolValue; +namespace internal { + +struct BoolValueTypeDescr +{ + using Const = ConstBoolValue; + using NonConst = BoolValue; +}; + +template <> +struct TypeDescr : public BoolValueTypeDescr +{ +}; + +template <> +struct TypeDescr : public BoolValueTypeDescr +{ +}; + +} /* namespace internal */ + template class CommonUnsignedIntegerValue final : public CommonValue { @@ -350,18 +417,18 @@ 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 + operator=(const CommonUnsignedIntegerValue val) noexcept { static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); @@ -371,13 +438,13 @@ public: CommonUnsignedIntegerValue& operator=(const Value rawVal) noexcept { - bt_value_integer_unsigned_set(this->_libObjPtr(), rawVal); + bt_value_integer_unsigned_set(this->libObjPtr(), rawVal); return *this; } Value value() const noexcept { - return bt_value_integer_unsigned_get(this->_libObjPtr()); + return bt_value_integer_unsigned_get(this->libObjPtr()); } operator Value() const noexcept @@ -387,13 +454,33 @@ public: Shared shared() const noexcept { - return Shared {*this}; + return Shared::createWithRef(*this); } }; using UnsignedIntegerValue = CommonUnsignedIntegerValue; using ConstUnsignedIntegerValue = CommonUnsignedIntegerValue; +namespace internal { + +struct UnsignedIntegerValueTypeDescr +{ + using Const = ConstUnsignedIntegerValue; + using NonConst = UnsignedIntegerValue; +}; + +template <> +struct TypeDescr : public UnsignedIntegerValueTypeDescr +{ +}; + +template <> +struct TypeDescr : public UnsignedIntegerValueTypeDescr +{ +}; + +} /* namespace internal */ + template class CommonSignedIntegerValue final : public CommonValue { @@ -416,18 +503,18 @@ 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 + operator=(const CommonSignedIntegerValue val) noexcept { _ThisCommonValue::operator=(val); return *this; @@ -437,13 +524,13 @@ public: { static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); - bt_value_integer_signed_set(this->_libObjPtr(), rawVal); + bt_value_integer_signed_set(this->libObjPtr(), rawVal); return *this; } Value value() const noexcept { - return bt_value_integer_signed_get(this->_libObjPtr()); + return bt_value_integer_signed_get(this->libObjPtr()); } operator Value() const noexcept @@ -453,13 +540,33 @@ public: Shared shared() const noexcept { - return Shared {*this}; + return Shared::createWithRef(*this); } }; using SignedIntegerValue = CommonSignedIntegerValue; using ConstSignedIntegerValue = CommonSignedIntegerValue; +namespace internal { + +struct SignedIntegerValueTypeDescr +{ + using Const = ConstSignedIntegerValue; + using NonConst = SignedIntegerValue; +}; + +template <> +struct TypeDescr : public SignedIntegerValueTypeDescr +{ +}; + +template <> +struct TypeDescr : public SignedIntegerValueTypeDescr +{ +}; + +} /* namespace internal */ + template class CommonRealValue final : public CommonValue { @@ -481,16 +588,16 @@ 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; @@ -500,13 +607,13 @@ public: { static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); - bt_value_real_set(this->_libObjPtr(), rawVal); + bt_value_real_set(this->libObjPtr(), rawVal); return *this; } Value value() const noexcept { - return bt_value_real_get(this->_libObjPtr()); + return bt_value_real_get(this->libObjPtr()); } operator Value() const noexcept @@ -516,13 +623,33 @@ public: Shared shared() const noexcept { - return Shared {*this}; + return Shared::createWithRef(*this); } }; using RealValue = CommonRealValue; using ConstRealValue = CommonRealValue; +namespace internal { + +struct RealValueTypeDescr +{ + using Const = ConstRealValue; + using NonConst = RealValue; +}; + +template <> +struct TypeDescr : public RealValueTypeDescr +{ +}; + +template <> +struct TypeDescr : public RealValueTypeDescr +{ +}; + +} /* namespace internal */ + template class CommonStringValue final : public CommonValue { @@ -543,21 +670,21 @@ 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; @@ -567,10 +694,10 @@ public: { static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); - const auto status = bt_value_string_set(this->_libObjPtr(), rawVal); + const auto status = bt_value_string_set(this->libObjPtr(), rawVal); if (status == BT_VALUE_STRING_SET_STATUS_MEMORY_ERROR) { - throw LibMemoryError {}; + throw MemoryError {}; } return *this; @@ -583,12 +710,12 @@ public: bpstd::string_view value() const noexcept { - return bt_value_string_get(this->_libObjPtr()); + return bt_value_string_get(this->libObjPtr()); } Shared shared() const noexcept { - return Shared {*this}; + return Shared::createWithRef(*this); } }; @@ -597,10 +724,26 @@ using ConstStringValue = CommonStringValue; namespace internal { +struct StringValueTypeDescr +{ + using Const = ConstStringValue; + using NonConst = StringValue; +}; + +template <> +struct TypeDescr : public StringValueTypeDescr +{ +}; + +template <> +struct TypeDescr : public StringValueTypeDescr +{ +}; + template struct CommonArrayValueSpec; -// Functions specific to mutable array values +/* Functions specific to mutable array values */ template <> struct CommonArrayValueSpec final { @@ -610,7 +753,7 @@ struct CommonArrayValueSpec final } }; -// Functions specific to constant array values +/* Functions specific to constant array values */ template <> struct CommonArrayValueSpec final { @@ -621,83 +764,7 @@ struct CommonArrayValueSpec final } }; -} // namespace internal - -template -class CommonArrayValueIterator -{ - friend CommonArrayValue; - - using difference_type = std::ptrdiff_t; - using value_type = CommonValue; - using pointer = value_type *; - using reference = value_type&; - using iterator_category = std::input_iterator_tag; - -private: - explicit CommonArrayValueIterator(const CommonArrayValue& arrayVal, - const uint64_t idx) : - _mArrayVal {arrayVal}, - _mIdx {idx} - { - this->_updateCurrentValue(); - } - -public: - CommonArrayValueIterator(const CommonArrayValueIterator&) = default; - CommonArrayValueIterator(CommonArrayValueIterator&&) = default; - CommonArrayValueIterator& operator=(const CommonArrayValueIterator&) = default; - CommonArrayValueIterator& operator=(CommonArrayValueIterator&&) = default; - - CommonArrayValueIterator& operator++() noexcept - { - ++_mIdx; - this->_updateCurrentValue(); - return *this; - } - - CommonArrayValueIterator operator++(int) noexcept - { - const auto tmp = *this; - - ++(*this); - return tmp; - } - - bool operator==(const CommonArrayValueIterator& other) const noexcept - { - return _mIdx == other._mIdx; - } - - bool operator!=(const CommonArrayValueIterator& other) const noexcept - { - return !(*this == other); - } - - reference operator*() noexcept - { - return *_mCurrVal; - } - - pointer operator->() noexcept - { - return &(*_mCurrVal); - } - -private: - void _updateCurrentValue() noexcept - { - if (_mIdx < _mArrayVal.length()) { - _mCurrVal = _mArrayVal[_mIdx]; - } else { - _mCurrVal = nonstd::nullopt; - } - } - - nonstd::optional> _mCurrVal; - CommonArrayValue _mArrayVal; - uint64_t _mIdx; -}; +} /* namespace internal */ template class CommonArrayValue final : public CommonValue @@ -708,7 +775,7 @@ private: public: using Shared = internal::SharedValue, LibObjT>; - using Iterator = CommonArrayValueIterator; + using Iterator = CommonIterator, CommonValue>; explicit CommonArrayValue(const _LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr} { @@ -720,16 +787,16 @@ 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; @@ -737,7 +804,13 @@ public: std::uint64_t length() const noexcept { - return bt_value_array_get_length(this->_libObjPtr()); + return bt_value_array_get_length(this->libObjPtr()); + } + + /* Required by the `CommonIterator` template class */ + std::uint64_t size() const noexcept + { + return this->length(); } Iterator begin() const noexcept @@ -758,20 +831,20 @@ public: ConstValue operator[](const std::uint64_t index) const noexcept { return ConstValue {internal::CommonArrayValueSpec::elementByIndex( - this->_libObjPtr(), index)}; + this->libObjPtr(), index)}; } CommonValue operator[](const std::uint64_t index) noexcept { return CommonValue { - internal::CommonArrayValueSpec::elementByIndex(this->_libObjPtr(), index)}; + internal::CommonArrayValueSpec::elementByIndex(this->libObjPtr(), index)}; } - void append(const Value& val) + void append(const Value val) { static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); - const auto status = bt_value_array_append_element(this->_libObjPtr(), val._libObjPtr()); + const auto status = bt_value_array_append_element(this->libObjPtr(), val.libObjPtr()); this->_handleAppendLibStatus(status); } @@ -781,7 +854,7 @@ public: static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); const auto status = - bt_value_array_append_bool_element(this->_libObjPtr(), static_cast(rawVal)); + bt_value_array_append_bool_element(this->libObjPtr(), static_cast(rawVal)); this->_handleAppendLibStatus(status); } @@ -791,7 +864,7 @@ public: static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); const auto status = - bt_value_array_append_unsigned_integer_element(this->_libObjPtr(), rawVal); + bt_value_array_append_unsigned_integer_element(this->libObjPtr(), rawVal); this->_handleAppendLibStatus(status); } @@ -800,8 +873,7 @@ public: { static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); - const auto status = - bt_value_array_append_signed_integer_element(this->_libObjPtr(), rawVal); + const auto status = bt_value_array_append_signed_integer_element(this->libObjPtr(), rawVal); this->_handleAppendLibStatus(status); } @@ -810,7 +882,7 @@ public: { static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); - const auto status = bt_value_array_append_real_element(this->_libObjPtr(), rawVal); + const auto status = bt_value_array_append_real_element(this->libObjPtr(), rawVal); this->_handleAppendLibStatus(status); } @@ -819,7 +891,7 @@ public: { static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); - const auto status = bt_value_array_append_string_element(this->_libObjPtr(), rawVal); + const auto status = bt_value_array_append_string_element(this->libObjPtr(), rawVal); this->_handleAppendLibStatus(status); } @@ -832,7 +904,7 @@ public: CommonArrayValue appendEmptyArray(); CommonMapValue appendEmptyMap(); - void operator+=(const Value& val) + void operator+=(const Value val) { this->append(val); } @@ -869,14 +941,14 @@ public: 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 {}; } } }; @@ -886,6 +958,22 @@ using ConstArrayValue = CommonArrayValue; namespace internal { +struct ArrayValueTypeDescr +{ + using Const = ConstArrayValue; + using NonConst = ArrayValue; +}; + +template <> +struct TypeDescr : public ArrayValueTypeDescr +{ +}; + +template <> +struct TypeDescr : public ArrayValueTypeDescr +{ +}; + /* * Type of a user function passed to `CommonMapValue::forEach()`. * @@ -923,7 +1011,7 @@ LibStatusT mapValueForEachLibFunc(const char * const key, LibObjT * const libObj template struct CommonMapValueSpec; -// Functions specific to mutable map values +/* Functions specific to mutable map values */ template <> struct CommonMapValueSpec final { @@ -947,14 +1035,14 @@ 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(); } } }; -// Functions specific to constant map values +/* Functions specific to constant map values */ template <> struct CommonMapValueSpec final { @@ -980,14 +1068,14 @@ 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(); } } }; -} // namespace internal +} /* namespace internal */ template class CommonMapValue final : public CommonValue @@ -1009,16 +1097,16 @@ 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; @@ -1026,7 +1114,7 @@ public: std::uint64_t size() const noexcept { - return bt_value_map_get_size(this->_libObjPtr()); + return bt_value_map_get_size(this->libObjPtr()); } bool isEmpty() const noexcept @@ -1037,7 +1125,7 @@ public: nonstd::optional operator[](const char * const key) const noexcept { const auto libObjPtr = - internal::CommonMapValueSpec::entryByKey(this->_libObjPtr(), key); + internal::CommonMapValueSpec::entryByKey(this->libObjPtr(), key); if (!libObjPtr) { return nonstd::nullopt; @@ -1054,7 +1142,7 @@ public: nonstd::optional> operator[](const char * const key) noexcept { const auto libObjPtr = - internal::CommonMapValueSpec::entryByKey(this->_libObjPtr(), key); + internal::CommonMapValueSpec::entryByKey(this->libObjPtr(), key); if (!libObjPtr) { return nonstd::nullopt; @@ -1070,7 +1158,7 @@ public: bool hasEntry(const char * const key) const noexcept { - return static_cast(bt_value_map_has_entry(this->_libObjPtr(), key)); + return static_cast(bt_value_map_has_entry(this->libObjPtr(), key)); } bool hasEntry(const std::string& key) const noexcept @@ -1078,16 +1166,16 @@ public: return this->hasEntry(key.data()); } - void insert(const char * const key, const Value& val) + void insert(const char * const key, const Value val) { static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); - const auto status = bt_value_map_insert_entry(this->_libObjPtr(), key, val._libObjPtr()); + 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) { this->insert(key.data(), val); } @@ -1097,7 +1185,7 @@ public: static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); const auto status = - bt_value_map_insert_bool_entry(this->_libObjPtr(), key, static_cast(rawVal)); + bt_value_map_insert_bool_entry(this->libObjPtr(), key, static_cast(rawVal)); this->_handleInsertLibStatus(status); } @@ -1112,7 +1200,7 @@ public: static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); const auto status = - bt_value_map_insert_unsigned_integer_entry(this->_libObjPtr(), key, rawVal); + bt_value_map_insert_unsigned_integer_entry(this->libObjPtr(), key, rawVal); this->_handleInsertLibStatus(status); } @@ -1127,7 +1215,7 @@ public: static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); const auto status = - bt_value_map_insert_signed_integer_entry(this->_libObjPtr(), key, rawVal); + bt_value_map_insert_signed_integer_entry(this->libObjPtr(), key, rawVal); this->_handleInsertLibStatus(status); } @@ -1141,7 +1229,7 @@ public: { static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); - const auto status = bt_value_map_insert_real_entry(this->_libObjPtr(), key, rawVal); + const auto status = bt_value_map_insert_real_entry(this->libObjPtr(), key, rawVal); this->_handleInsertLibStatus(status); } @@ -1155,7 +1243,7 @@ public: { static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); - const auto status = bt_value_map_insert_string_entry(this->_libObjPtr(), key, rawVal); + const auto status = bt_value_map_insert_string_entry(this->libObjPtr(), key, rawVal); this->_handleInsertLibStatus(status); } @@ -1182,24 +1270,24 @@ public: void forEach(const internal::CommonMapValueForEachUserFunc& func) const { - internal::CommonMapValueSpec::forEach(this->_libObjPtr(), func); + internal::CommonMapValueSpec::forEach(this->libObjPtr(), func); } void forEach(const internal::CommonMapValueForEachUserFunc>& func) { - internal::CommonMapValueSpec::forEach(this->_libObjPtr(), func); + 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 {}; } } }; @@ -1207,60 +1295,80 @@ private: using MapValue = CommonMapValue; using ConstMapValue = CommonMapValue; +namespace internal { + +struct MapValueTypeDescr +{ + using Const = ConstMapValue; + using NonConst = MapValue; +}; + +template <> +struct TypeDescr : public MapValueTypeDescr +{ +}; + +template <> +struct TypeDescr : public MapValueTypeDescr +{ +}; + +} /* namespace internal */ + template CommonNullValue CommonValue::asNull() const noexcept { BT_ASSERT_DBG(this->isNull()); - return CommonNullValue {this->_libObjPtr()}; + return CommonNullValue {this->libObjPtr()}; } template CommonBoolValue CommonValue::asBool() const noexcept { BT_ASSERT_DBG(this->isBool()); - return CommonBoolValue {this->_libObjPtr()}; + return CommonBoolValue {this->libObjPtr()}; } template CommonSignedIntegerValue CommonValue::asSignedInteger() const noexcept { BT_ASSERT_DBG(this->isSignedInteger()); - return CommonSignedIntegerValue {this->_libObjPtr()}; + return CommonSignedIntegerValue {this->libObjPtr()}; } template CommonUnsignedIntegerValue CommonValue::asUnsignedInteger() const noexcept { BT_ASSERT_DBG(this->isUnsignedInteger()); - return CommonUnsignedIntegerValue {this->_libObjPtr()}; + return CommonUnsignedIntegerValue {this->libObjPtr()}; } template CommonRealValue CommonValue::asReal() const noexcept { BT_ASSERT_DBG(this->isReal()); - return CommonRealValue {this->_libObjPtr()}; + return CommonRealValue {this->libObjPtr()}; } template CommonStringValue CommonValue::asString() const noexcept { BT_ASSERT_DBG(this->isString()); - return CommonStringValue {this->_libObjPtr()}; + return CommonStringValue {this->libObjPtr()}; } template CommonArrayValue CommonValue::asArray() const noexcept { BT_ASSERT_DBG(this->isArray()); - return CommonArrayValue {this->_libObjPtr()}; + return CommonArrayValue {this->libObjPtr()}; } template CommonMapValue CommonValue::asMap() const noexcept { BT_ASSERT_DBG(this->isMap()); - return CommonMapValue {this->_libObjPtr()}; + return CommonMapValue {this->libObjPtr()}; } template @@ -1269,7 +1377,7 @@ ArrayValue CommonArrayValue::appendEmptyArray() static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); bt_value *libElemPtr; - const auto status = bt_value_array_append_empty_array_element(this->_libObjPtr(), &libElemPtr); + const auto status = bt_value_array_append_empty_array_element(this->libObjPtr(), &libElemPtr); this->_handleAppendLibStatus(status); return ArrayValue {libElemPtr}; @@ -1281,7 +1389,7 @@ MapValue CommonArrayValue::appendEmptyMap() static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); bt_value *libElemPtr; - const auto status = bt_value_array_append_empty_map_element(this->_libObjPtr(), &libElemPtr); + const auto status = bt_value_array_append_empty_map_element(this->libObjPtr(), &libElemPtr); this->_handleAppendLibStatus(status); return MapValue {libElemPtr}; @@ -1293,8 +1401,7 @@ ArrayValue CommonMapValue::insertEmptyArray(const char * const key) static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); bt_value *libEntryPtr; - const auto status = - bt_value_map_insert_empty_array_entry(this->_libObjPtr(), key, &libEntryPtr); + const auto status = bt_value_map_insert_empty_array_entry(this->libObjPtr(), key, &libEntryPtr); this->_handleInsertLibStatus(status); return ArrayValue {libEntryPtr}; @@ -1312,7 +1419,7 @@ MapValue CommonMapValue::insertEmptyMap(const char * const key) static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); bt_value *libEntryPtr; - const auto status = bt_value_map_insert_empty_map_entry(this->_libObjPtr(), key, &libEntryPtr); + const auto status = bt_value_map_insert_empty_map_entry(this->libObjPtr(), key, &libEntryPtr); this->_handleInsertLibStatus(status); return MapValue {libEntryPtr}; @@ -1354,6 +1461,6 @@ inline StringValue::Shared createValue(const std::string& rawVal) return StringValue::create(rawVal); } -} // namespace bt2 +} /* namespace bt2 */ -#endif // BABELTRACE_CPP_COMMON_BT2_VALUE_HPP +#endif /* BABELTRACE_CPP_COMMON_BT2_VALUE_HPP */