From 328a274a0722349196f8c7161dce7d5adc7e2489 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Fri, 3 Nov 2023 13:26:55 -0400 Subject: [PATCH] cpp-common/bt2: add asConst() methods Add many asConst() methods to get the `bt2::ConstXyz` version of a `bt2::Xyz` wrapper instead of using the constructor directly. Just some sugar. For example: void f(const bt2::Value val) { { const auto cVal = val.asConst(); // Operate on `cVal` } } I only added those methods where it was easy to do so. Signed-off-by: Philippe Proulx Change-Id: Iabd9d6266fe75f33f2008c2320444624b842d9d2 Reviewed-on: https://review.lttng.org/c/babeltrace/+/11232 Reviewed-by: Simon Marchi --- src/cpp-common/bt2/clock-class.hpp | 5 ++ src/cpp-common/bt2/field-class.hpp | 80 ++++++++++++++++++++++++ src/cpp-common/bt2/field.hpp | 75 ++++++++++++++++++++++ src/cpp-common/bt2/integer-range-set.hpp | 5 ++ src/cpp-common/bt2/message.hpp | 45 +++++++++++++ src/cpp-common/bt2/trace-ir.hpp | 35 +++++++++++ src/cpp-common/bt2/value.hpp | 45 +++++++++++++ 7 files changed, 290 insertions(+) diff --git a/src/cpp-common/bt2/clock-class.hpp b/src/cpp-common/bt2/clock-class.hpp index ce306eef..b101ee66 100644 --- a/src/cpp-common/bt2/clock-class.hpp +++ b/src/cpp-common/bt2/clock-class.hpp @@ -121,6 +121,11 @@ public: return *this; } + CommonClockClass asConst() const noexcept + { + return CommonClockClass {*this}; + } + void frequency(const std::uint64_t frequency) const noexcept { static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); diff --git a/src/cpp-common/bt2/field-class.hpp b/src/cpp-common/bt2/field-class.hpp index 3d3c4795..5719c053 100644 --- a/src/cpp-common/bt2/field-class.hpp +++ b/src/cpp-common/bt2/field-class.hpp @@ -192,6 +192,11 @@ public: return *this; } + CommonFieldClass asConst() const noexcept + { + return CommonFieldClass {*this}; + } + FieldClassType type() const noexcept { return static_cast(bt_field_class_get_type(this->libObjPtr())); @@ -476,6 +481,11 @@ public: return *this; } + CommonBitArrayFieldClass asConst() const noexcept + { + return CommonBitArrayFieldClass {*this}; + } + std::uint64_t length() const noexcept { return bt_field_class_bit_array_get_length(this->libObjPtr()); @@ -550,6 +560,11 @@ public: return *this; } + CommonIntegerFieldClass asConst() const noexcept + { + return CommonIntegerFieldClass {*this}; + } + void fieldValueRange(const std::uint64_t n) const noexcept { static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); @@ -785,6 +800,11 @@ public: return *this; } + CommonBaseEnumerationFieldClass asConst() const noexcept + { + return CommonBaseEnumerationFieldClass {*this}; + } + std::uint64_t length() const noexcept { return bt_field_class_enumeration_get_mapping_count(this->libObjPtr()); @@ -1004,6 +1024,11 @@ public: return *this; } + CommonStructureFieldClassMember asConst() const noexcept + { + return CommonStructureFieldClassMember {*this}; + } + bpstd::string_view name() const noexcept { return bt_field_class_structure_member_get_name(this->libObjPtr()); @@ -1128,6 +1153,11 @@ public: return *this; } + CommonStructureFieldClass asConst() const noexcept + { + return CommonStructureFieldClass {*this}; + } + void appendMember(const char * const name, const FieldClass fc) const { static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); @@ -1270,6 +1300,11 @@ public: return *this; } + CommonArrayFieldClass asConst() const noexcept + { + return CommonArrayFieldClass {*this}; + } + _FieldClass elementFieldClass() const noexcept { return _FieldClass { @@ -1335,6 +1370,11 @@ public: return *this; } + CommonStaticArrayFieldClass asConst() const noexcept + { + return CommonStaticArrayFieldClass {*this}; + } + std::uint64_t length() const noexcept { return bt_field_class_array_static_get_length(this->libObjPtr()); @@ -1400,6 +1440,11 @@ public: return *this; } + CommonDynamicArrayWithLengthFieldClass asConst() const noexcept + { + return CommonDynamicArrayWithLengthFieldClass {*this}; + } + ConstFieldPath lengthFieldPath() const noexcept { return ConstFieldPath { @@ -1498,6 +1543,11 @@ public: return *this; } + CommonOptionFieldClass asConst() const noexcept + { + return CommonOptionFieldClass {*this}; + } + _FieldClass fieldClass() const noexcept { return _FieldClass { @@ -1567,6 +1617,11 @@ public: return *this; } + CommonOptionWithSelectorFieldClass asConst() const noexcept + { + return CommonOptionWithSelectorFieldClass {*this}; + } + ConstFieldPath selectorFieldPath() const noexcept { return ConstFieldPath { @@ -1636,6 +1691,11 @@ public: return *this; } + CommonOptionWithBoolSelectorFieldClass asConst() const noexcept + { + return CommonOptionWithBoolSelectorFieldClass {*this}; + } + bool selectorIsReversed() const noexcept { return bt_field_class_option_with_selector_field_bool_selector_is_reversed( @@ -1872,6 +1932,11 @@ public: return *this; } + CommonVariantFieldClassOption asConst() const noexcept + { + return CommonVariantFieldClassOption {*this}; + } + nonstd::optional name() const noexcept { const auto name = bt_field_class_variant_option_get_name(this->libObjPtr()); @@ -2121,6 +2186,11 @@ public: return *this; } + CommonVariantFieldClass asConst() const noexcept + { + return CommonVariantFieldClass {*this}; + } + std::uint64_t length() const noexcept { return bt_field_class_variant_get_option_count(this->libObjPtr()); @@ -2219,6 +2289,11 @@ public: return *this; } + CommonVariantWithoutSelectorFieldClass asConst() const noexcept + { + return CommonVariantWithoutSelectorFieldClass {*this}; + } + void appendOption(const char * const name, const FieldClass fc) const { static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); @@ -2366,6 +2441,11 @@ public: return *this; } + CommonVariantWithSelectorFieldClass asConst() const noexcept + { + return CommonVariantWithSelectorFieldClass {*this}; + } + ConstFieldPath selectorFieldPath() const noexcept { return ConstFieldPath { diff --git a/src/cpp-common/bt2/field.hpp b/src/cpp-common/bt2/field.hpp index d5048b6e..4c270c2d 100644 --- a/src/cpp-common/bt2/field.hpp +++ b/src/cpp-common/bt2/field.hpp @@ -121,6 +121,11 @@ public: return *this; } + CommonField asConst() const noexcept + { + return CommonField {*this}; + } + FieldClassType classType() const noexcept { return static_cast(bt_field_get_class_type(this->libObjPtr())); @@ -273,6 +278,11 @@ public: return *this; } + CommonBoolField asConst() const noexcept + { + return CommonBoolField {*this}; + } + CommonBoolField operator=(const Value val) const noexcept { static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); @@ -344,6 +354,11 @@ public: return *this; } + CommonBitArrayField asConst() const noexcept + { + return CommonBitArrayField {*this}; + } + ConstBitArrayFieldClass cls() const noexcept { return ConstBitArrayFieldClass { @@ -434,6 +449,11 @@ public: return *this; } + CommonUnsignedIntegerField asConst() const noexcept + { + return CommonUnsignedIntegerField {*this}; + } + Class cls() const noexcept { return Class {internal::CommonFieldSpec::cls(this->libObjPtr())}; @@ -517,6 +537,11 @@ public: return *this; } + CommonSignedIntegerField asConst() const noexcept + { + return CommonSignedIntegerField {*this}; + } + Class cls() const noexcept { return Class {internal::CommonFieldSpec::cls(this->libObjPtr())}; @@ -628,6 +653,11 @@ public: return *this; } + CommonUnsignedEnumerationField asConst() const noexcept + { + return CommonUnsignedEnumerationField {*this}; + } + using CommonUnsignedIntegerField::operator=; Class cls() const noexcept @@ -705,6 +735,11 @@ public: return *this; } + CommonSignedEnumerationField asConst() const noexcept + { + return CommonSignedEnumerationField {*this}; + } + using CommonSignedIntegerField::operator=; Class cls() const noexcept @@ -781,6 +816,11 @@ public: return *this; } + CommonSinglePrecisionRealField asConst() const noexcept + { + return CommonSinglePrecisionRealField {*this}; + } + CommonSinglePrecisionRealField operator=(const Value val) const noexcept { static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); @@ -854,6 +894,11 @@ public: return *this; } + CommonDoublePrecisionRealField asConst() const noexcept + { + return CommonDoublePrecisionRealField {*this}; + } + CommonDoublePrecisionRealField operator=(const Value val) const noexcept { static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); @@ -921,6 +966,11 @@ public: return *this; } + CommonStringField asConst() const noexcept + { + return CommonStringField {*this}; + } + CommonStringField operator=(const char * const val) const { static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); @@ -1058,6 +1108,11 @@ public: return *this; } + CommonStructureField asConst() const noexcept + { + return CommonStructureField {*this}; + } + Class cls() const noexcept { return Class {internal::CommonFieldSpec::cls(this->libObjPtr())}; @@ -1170,6 +1225,11 @@ public: return *this; } + CommonArrayField asConst() const noexcept + { + return CommonArrayField {*this}; + } + Class cls() const noexcept { return Class {internal::CommonFieldSpec::cls(this->libObjPtr())}; @@ -1237,6 +1297,11 @@ public: return *this; } + CommonDynamicArrayField asConst() const noexcept + { + return CommonDynamicArrayField {*this}; + } + std::uint64_t length() const noexcept { return _ThisCommonArrayField::length(); @@ -1329,6 +1394,11 @@ public: return *this; } + CommonOptionField asConst() const noexcept + { + return CommonOptionField {*this}; + } + Class cls() const noexcept { return Class {internal::CommonFieldSpec::cls(this->libObjPtr())}; @@ -1433,6 +1503,11 @@ public: return *this; } + CommonVariantField asConst() const noexcept + { + return CommonVariantField {*this}; + } + Class cls() const noexcept { return Class {internal::CommonFieldSpec::cls(this->libObjPtr())}; diff --git a/src/cpp-common/bt2/integer-range-set.hpp b/src/cpp-common/bt2/integer-range-set.hpp index d50fe285..27f5b003 100644 --- a/src/cpp-common/bt2/integer-range-set.hpp +++ b/src/cpp-common/bt2/integer-range-set.hpp @@ -179,6 +179,11 @@ public: return *this; } + _ConstLibObjT asConst() const noexcept + { + return _ConstLibObjT {*this}; + } + template bool operator==(const CommonIntegerRangeSet other) const noexcept { diff --git a/src/cpp-common/bt2/message.hpp b/src/cpp-common/bt2/message.hpp index 5812b06d..2b210518 100644 --- a/src/cpp-common/bt2/message.hpp +++ b/src/cpp-common/bt2/message.hpp @@ -107,6 +107,11 @@ public: return *this; } + CommonMessage asConst() const noexcept + { + return CommonMessage {*this}; + } + MessageType type() const noexcept { return static_cast(bt_message_get_type(this->libObjPtr())); @@ -253,6 +258,11 @@ public: return *this; } + CommonStreamBeginningMessage asConst() const noexcept + { + return CommonStreamBeginningMessage {*this}; + } + _Stream stream() const noexcept { return _Stream { @@ -365,6 +375,11 @@ public: return *this; } + CommonStreamEndMessage asConst() const noexcept + { + return CommonStreamEndMessage {*this}; + } + _Stream stream() const noexcept { return _Stream {internal::CommonStreamEndMessageSpec::stream(this->libObjPtr())}; @@ -476,6 +491,11 @@ public: return *this; } + CommonPacketBeginningMessage asConst() const noexcept + { + return CommonPacketBeginningMessage {*this}; + } + _Packet packet() const noexcept { return _Packet { @@ -583,6 +603,11 @@ public: return *this; } + CommonPacketEndMessage asConst() const noexcept + { + return CommonPacketEndMessage {*this}; + } + _Packet packet() const noexcept { return _Packet {internal::CommonPacketEndMessageSpec::packet(this->libObjPtr())}; @@ -688,6 +713,11 @@ public: return *this; } + CommonEventMessage asConst() const noexcept + { + return CommonEventMessage {*this}; + } + _Event event() const noexcept { return _Event {internal::CommonEventMessageSpec::event(this->libObjPtr())}; @@ -787,6 +817,11 @@ public: return *this; } + CommonDiscardedEventsMessage asConst() const noexcept + { + return CommonDiscardedEventsMessage {*this}; + } + _Stream stream() const noexcept { return _Stream { @@ -915,6 +950,11 @@ public: return *this; } + CommonDiscardedPacketsMessage asConst() const noexcept + { + return CommonDiscardedPacketsMessage {*this}; + } + _Stream stream() const noexcept { return _Stream { @@ -1017,6 +1057,11 @@ public: return *this; } + CommonMessageIteratorInactivityMessage asConst() const noexcept + { + return CommonMessageIteratorInactivityMessage {*this}; + } + ConstClockSnapshot clockSnapshot() const noexcept { const auto libObjPtr = diff --git a/src/cpp-common/bt2/trace-ir.hpp b/src/cpp-common/bt2/trace-ir.hpp index 5ee98960..e0095081 100644 --- a/src/cpp-common/bt2/trace-ir.hpp +++ b/src/cpp-common/bt2/trace-ir.hpp @@ -163,6 +163,11 @@ public: return *this; } + CommonEvent asConst() const noexcept + { + return CommonEvent {*this}; + } + Class cls() const noexcept; _Stream stream() const noexcept; nonstd::optional<_Packet> packet() const noexcept; @@ -305,6 +310,11 @@ public: return *this; } + CommonPacket asConst() const noexcept + { + return CommonPacket {*this}; + } + _Stream stream() const noexcept; nonstd::optional<_StructureField> contextField() const noexcept @@ -459,6 +469,11 @@ public: return *this; } + CommonStream asConst() const noexcept + { + return CommonStream {*this}; + } + Packet::Shared createPacket() const { static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); @@ -675,6 +690,11 @@ public: return *this; } + CommonTrace asConst() const noexcept + { + return CommonTrace {*this}; + } + Class cls() const noexcept; void name(const char * const name) const @@ -993,6 +1013,11 @@ public: return *this; } + CommonEventClass asConst() const noexcept + { + return CommonEventClass {*this}; + } + _StreamClass streamClass() const noexcept; std::uint64_t id() const noexcept @@ -1322,6 +1347,11 @@ public: return *this; } + CommonStreamClass asConst() const noexcept + { + return CommonStreamClass {*this}; + } + Stream::Shared instantiate(const Trace trace) const { static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); @@ -1736,6 +1766,11 @@ public: return *this; } + CommonTraceClass asConst() const noexcept + { + return CommonTraceClass {*this}; + } + Trace::Shared instantiate() const { static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); diff --git a/src/cpp-common/bt2/value.hpp b/src/cpp-common/bt2/value.hpp index c8e3dc79..408710c0 100644 --- a/src/cpp-common/bt2/value.hpp +++ b/src/cpp-common/bt2/value.hpp @@ -110,6 +110,11 @@ public: return *this; } + CommonValue asConst() const noexcept + { + return CommonValue {*this}; + } + ValueType type() const noexcept { return static_cast(bt_value_get_type(this->libObjPtr())); @@ -247,6 +252,11 @@ public: return *this; } + CommonNullValue asConst() const noexcept + { + return CommonNullValue {*this}; + } + Shared shared() const noexcept { return Shared::createWithRef(*this); @@ -312,6 +322,11 @@ public: return *this; } + CommonBoolValue asConst() const noexcept + { + return CommonBoolValue {*this}; + } + CommonBoolValue operator=(const Value rawVal) const noexcept { static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); @@ -400,6 +415,11 @@ public: return *this; } + CommonUnsignedIntegerValue asConst() const noexcept + { + return CommonUnsignedIntegerValue {*this}; + } + CommonUnsignedIntegerValue operator=(const Value rawVal) const noexcept { bt_value_integer_unsigned_set(this->libObjPtr(), rawVal); @@ -484,6 +504,11 @@ public: return *this; } + CommonSignedIntegerValue asConst() const noexcept + { + return CommonSignedIntegerValue {*this}; + } + CommonSignedIntegerValue operator=(const Value rawVal) const noexcept { static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); @@ -567,6 +592,11 @@ public: return *this; } + CommonRealValue asConst() const noexcept + { + return CommonRealValue {*this}; + } + CommonRealValue operator=(const Value rawVal) const noexcept { static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); @@ -654,6 +684,11 @@ public: return *this; } + CommonStringValue asConst() const noexcept + { + return CommonStringValue {*this}; + } + CommonStringValue operator=(const char * const rawVal) const { static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); @@ -766,6 +801,11 @@ public: return *this; } + CommonArrayValue asConst() const noexcept + { + return CommonArrayValue {*this}; + } + std::uint64_t length() const noexcept { return bt_value_array_get_length(this->libObjPtr()); @@ -1064,6 +1104,11 @@ public: return *this; } + CommonMapValue asConst() const noexcept + { + return CommonMapValue {*this}; + } + std::uint64_t length() const noexcept { return bt_value_map_get_size(this->libObjPtr()); -- 2.34.1