cpp-common/bt2: add asConst() methods
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Fri, 3 Nov 2023 17:26:55 +0000 (13:26 -0400)
committerSimon Marchi <simon.marchi@efficios.com>
Thu, 14 Dec 2023 15:57:04 +0000 (10:57 -0500)
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 <eeppeliteloop@gmail.com>
Change-Id: Iabd9d6266fe75f33f2008c2320444624b842d9d2
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11232
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
src/cpp-common/bt2/clock-class.hpp
src/cpp-common/bt2/field-class.hpp
src/cpp-common/bt2/field.hpp
src/cpp-common/bt2/integer-range-set.hpp
src/cpp-common/bt2/message.hpp
src/cpp-common/bt2/trace-ir.hpp
src/cpp-common/bt2/value.hpp

index ce306eef9340c2be414a01fa85ae15f6b4f8f3eb..b101ee66f4c9e985170ed2e548f37fe9cccddbc3 100644 (file)
@@ -121,6 +121,11 @@ public:
         return *this;
     }
 
+    CommonClockClass<const bt_clock_class> asConst() const noexcept
+    {
+        return CommonClockClass<const bt_clock_class> {*this};
+    }
+
     void frequency(const std::uint64_t frequency) const noexcept
     {
         static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
index 3d3c479554162b757b6094c0caf0e75fab0df207..5719c053d214a01ee9409dd651332e961f386efb 100644 (file)
@@ -192,6 +192,11 @@ public:
         return *this;
     }
 
+    CommonFieldClass<const bt_field_class> asConst() const noexcept
+    {
+        return CommonFieldClass<const bt_field_class> {*this};
+    }
+
     FieldClassType type() const noexcept
     {
         return static_cast<FieldClassType>(bt_field_class_get_type(this->libObjPtr()));
@@ -476,6 +481,11 @@ public:
         return *this;
     }
 
+    CommonBitArrayFieldClass<const bt_field_class> asConst() const noexcept
+    {
+        return CommonBitArrayFieldClass<const bt_field_class> {*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<const bt_field_class> asConst() const noexcept
+    {
+        return CommonIntegerFieldClass<const bt_field_class> {*this};
+    }
+
     void fieldValueRange(const std::uint64_t n) const noexcept
     {
         static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
@@ -785,6 +800,11 @@ public:
         return *this;
     }
 
+    CommonBaseEnumerationFieldClass<const bt_field_class> asConst() const noexcept
+    {
+        return CommonBaseEnumerationFieldClass<const bt_field_class> {*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<const bt_field_class_structure_member> asConst() const noexcept
+    {
+        return CommonStructureFieldClassMember<const bt_field_class_structure_member> {*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<const bt_field_class> asConst() const noexcept
+    {
+        return CommonStructureFieldClass<const bt_field_class> {*this};
+    }
+
     void appendMember(const char * const name, const FieldClass fc) const
     {
         static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
@@ -1270,6 +1300,11 @@ public:
         return *this;
     }
 
+    CommonArrayFieldClass<const bt_field_class> asConst() const noexcept
+    {
+        return CommonArrayFieldClass<const bt_field_class> {*this};
+    }
+
     _FieldClass elementFieldClass() const noexcept
     {
         return _FieldClass {
@@ -1335,6 +1370,11 @@ public:
         return *this;
     }
 
+    CommonStaticArrayFieldClass<const bt_field_class> asConst() const noexcept
+    {
+        return CommonStaticArrayFieldClass<const bt_field_class> {*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<const bt_field_class> asConst() const noexcept
+    {
+        return CommonDynamicArrayWithLengthFieldClass<const bt_field_class> {*this};
+    }
+
     ConstFieldPath lengthFieldPath() const noexcept
     {
         return ConstFieldPath {
@@ -1498,6 +1543,11 @@ public:
         return *this;
     }
 
+    CommonOptionFieldClass<const bt_field_class> asConst() const noexcept
+    {
+        return CommonOptionFieldClass<const bt_field_class> {*this};
+    }
+
     _FieldClass fieldClass() const noexcept
     {
         return _FieldClass {
@@ -1567,6 +1617,11 @@ public:
         return *this;
     }
 
+    CommonOptionWithSelectorFieldClass<const bt_field_class> asConst() const noexcept
+    {
+        return CommonOptionWithSelectorFieldClass<const bt_field_class> {*this};
+    }
+
     ConstFieldPath selectorFieldPath() const noexcept
     {
         return ConstFieldPath {
@@ -1636,6 +1691,11 @@ public:
         return *this;
     }
 
+    CommonOptionWithBoolSelectorFieldClass<const bt_field_class> asConst() const noexcept
+    {
+        return CommonOptionWithBoolSelectorFieldClass<const bt_field_class> {*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<const bt_field_class_variant_option> asConst() const noexcept
+    {
+        return CommonVariantFieldClassOption<const bt_field_class_variant_option> {*this};
+    }
+
     nonstd::optional<bpstd::string_view> name() const noexcept
     {
         const auto name = bt_field_class_variant_option_get_name(this->libObjPtr());
@@ -2121,6 +2186,11 @@ public:
         return *this;
     }
 
+    CommonVariantFieldClass<const bt_field_class> asConst() const noexcept
+    {
+        return CommonVariantFieldClass<const bt_field_class> {*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<const bt_field_class> asConst() const noexcept
+    {
+        return CommonVariantWithoutSelectorFieldClass<const bt_field_class> {*this};
+    }
+
     void appendOption(const char * const name, const FieldClass fc) const
     {
         static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
@@ -2366,6 +2441,11 @@ public:
         return *this;
     }
 
+    CommonVariantWithSelectorFieldClass<const bt_field_class> asConst() const noexcept
+    {
+        return CommonVariantWithSelectorFieldClass<const bt_field_class> {*this};
+    }
+
     ConstFieldPath selectorFieldPath() const noexcept
     {
         return ConstFieldPath {
index d5048b6ea0b224aad6e5cc4e189070570bcd688f..4c270c2dc0cb127e18fae33085b97da78bd5f4d7 100644 (file)
@@ -121,6 +121,11 @@ public:
         return *this;
     }
 
+    CommonField<const bt_field> asConst() const noexcept
+    {
+        return CommonField<const bt_field> {*this};
+    }
+
     FieldClassType classType() const noexcept
     {
         return static_cast<FieldClassType>(bt_field_get_class_type(this->libObjPtr()));
@@ -273,6 +278,11 @@ public:
         return *this;
     }
 
+    CommonBoolField<const bt_field> asConst() const noexcept
+    {
+        return CommonBoolField<const bt_field> {*this};
+    }
+
     CommonBoolField<LibObjT> operator=(const Value val) const noexcept
     {
         static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
@@ -344,6 +354,11 @@ public:
         return *this;
     }
 
+    CommonBitArrayField<const bt_field> asConst() const noexcept
+    {
+        return CommonBitArrayField<const bt_field> {*this};
+    }
+
     ConstBitArrayFieldClass cls() const noexcept
     {
         return ConstBitArrayFieldClass {
@@ -434,6 +449,11 @@ public:
         return *this;
     }
 
+    CommonUnsignedIntegerField<const bt_field> asConst() const noexcept
+    {
+        return CommonUnsignedIntegerField<const bt_field> {*this};
+    }
+
     Class cls() const noexcept
     {
         return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
@@ -517,6 +537,11 @@ public:
         return *this;
     }
 
+    CommonSignedIntegerField<const bt_field> asConst() const noexcept
+    {
+        return CommonSignedIntegerField<const bt_field> {*this};
+    }
+
     Class cls() const noexcept
     {
         return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
@@ -628,6 +653,11 @@ public:
         return *this;
     }
 
+    CommonUnsignedEnumerationField<const bt_field> asConst() const noexcept
+    {
+        return CommonUnsignedEnumerationField<const bt_field> {*this};
+    }
+
     using CommonUnsignedIntegerField<LibObjT>::operator=;
 
     Class cls() const noexcept
@@ -705,6 +735,11 @@ public:
         return *this;
     }
 
+    CommonSignedEnumerationField<const bt_field> asConst() const noexcept
+    {
+        return CommonSignedEnumerationField<const bt_field> {*this};
+    }
+
     using CommonSignedIntegerField<LibObjT>::operator=;
 
     Class cls() const noexcept
@@ -781,6 +816,11 @@ public:
         return *this;
     }
 
+    CommonSinglePrecisionRealField<const bt_field> asConst() const noexcept
+    {
+        return CommonSinglePrecisionRealField<const bt_field> {*this};
+    }
+
     CommonSinglePrecisionRealField<LibObjT> operator=(const Value val) const noexcept
     {
         static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
@@ -854,6 +894,11 @@ public:
         return *this;
     }
 
+    CommonDoublePrecisionRealField<const bt_field> asConst() const noexcept
+    {
+        return CommonDoublePrecisionRealField<const bt_field> {*this};
+    }
+
     CommonDoublePrecisionRealField<LibObjT> operator=(const Value val) const noexcept
     {
         static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
@@ -921,6 +966,11 @@ public:
         return *this;
     }
 
+    CommonStringField<const bt_field> asConst() const noexcept
+    {
+        return CommonStringField<const bt_field> {*this};
+    }
+
     CommonStringField<LibObjT> operator=(const char * const val) const
     {
         static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
@@ -1058,6 +1108,11 @@ public:
         return *this;
     }
 
+    CommonStructureField<const bt_field> asConst() const noexcept
+    {
+        return CommonStructureField<const bt_field> {*this};
+    }
+
     Class cls() const noexcept
     {
         return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
@@ -1170,6 +1225,11 @@ public:
         return *this;
     }
 
+    CommonArrayField<const bt_field> asConst() const noexcept
+    {
+        return CommonArrayField<const bt_field> {*this};
+    }
+
     Class cls() const noexcept
     {
         return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
@@ -1237,6 +1297,11 @@ public:
         return *this;
     }
 
+    CommonDynamicArrayField<const bt_field> asConst() const noexcept
+    {
+        return CommonDynamicArrayField<const bt_field> {*this};
+    }
+
     std::uint64_t length() const noexcept
     {
         return _ThisCommonArrayField::length();
@@ -1329,6 +1394,11 @@ public:
         return *this;
     }
 
+    CommonOptionField<const bt_field> asConst() const noexcept
+    {
+        return CommonOptionField<const bt_field> {*this};
+    }
+
     Class cls() const noexcept
     {
         return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
@@ -1433,6 +1503,11 @@ public:
         return *this;
     }
 
+    CommonVariantField<const bt_field> asConst() const noexcept
+    {
+        return CommonVariantField<const bt_field> {*this};
+    }
+
     Class cls() const noexcept
     {
         return Class {internal::CommonFieldSpec<LibObjT>::cls(this->libObjPtr())};
index d50fe28558efcdfdd7fec789cdbe2f53858f70ba..27f5b00378da673fd86c713b073ae8744ea2d85f 100644 (file)
@@ -179,6 +179,11 @@ public:
         return *this;
     }
 
+    _ConstLibObjT asConst() const noexcept
+    {
+        return _ConstLibObjT {*this};
+    }
+
     template <typename OtherLibObjT>
     bool operator==(const CommonIntegerRangeSet<OtherLibObjT> other) const noexcept
     {
index 5812b06d6b0d73603b7aad42872fe8785ea7b625..2b210518b286aeb52535d5c2d89205ce754d22a6 100644 (file)
@@ -107,6 +107,11 @@ public:
         return *this;
     }
 
+    CommonMessage<const bt_message> asConst() const noexcept
+    {
+        return CommonMessage<const bt_message> {*this};
+    }
+
     MessageType type() const noexcept
     {
         return static_cast<MessageType>(bt_message_get_type(this->libObjPtr()));
@@ -253,6 +258,11 @@ public:
         return *this;
     }
 
+    CommonStreamBeginningMessage<const bt_message> asConst() const noexcept
+    {
+        return CommonStreamBeginningMessage<const bt_message> {*this};
+    }
+
     _Stream stream() const noexcept
     {
         return _Stream {
@@ -365,6 +375,11 @@ public:
         return *this;
     }
 
+    CommonStreamEndMessage<const bt_message> asConst() const noexcept
+    {
+        return CommonStreamEndMessage<const bt_message> {*this};
+    }
+
     _Stream stream() const noexcept
     {
         return _Stream {internal::CommonStreamEndMessageSpec<LibObjT>::stream(this->libObjPtr())};
@@ -476,6 +491,11 @@ public:
         return *this;
     }
 
+    CommonPacketBeginningMessage<const bt_message> asConst() const noexcept
+    {
+        return CommonPacketBeginningMessage<const bt_message> {*this};
+    }
+
     _Packet packet() const noexcept
     {
         return _Packet {
@@ -583,6 +603,11 @@ public:
         return *this;
     }
 
+    CommonPacketEndMessage<const bt_message> asConst() const noexcept
+    {
+        return CommonPacketEndMessage<const bt_message> {*this};
+    }
+
     _Packet packet() const noexcept
     {
         return _Packet {internal::CommonPacketEndMessageSpec<LibObjT>::packet(this->libObjPtr())};
@@ -688,6 +713,11 @@ public:
         return *this;
     }
 
+    CommonEventMessage<const bt_message> asConst() const noexcept
+    {
+        return CommonEventMessage<const bt_message> {*this};
+    }
+
     _Event event() const noexcept
     {
         return _Event {internal::CommonEventMessageSpec<LibObjT>::event(this->libObjPtr())};
@@ -787,6 +817,11 @@ public:
         return *this;
     }
 
+    CommonDiscardedEventsMessage<const bt_message> asConst() const noexcept
+    {
+        return CommonDiscardedEventsMessage<const bt_message> {*this};
+    }
+
     _Stream stream() const noexcept
     {
         return _Stream {
@@ -915,6 +950,11 @@ public:
         return *this;
     }
 
+    CommonDiscardedPacketsMessage<const bt_message> asConst() const noexcept
+    {
+        return CommonDiscardedPacketsMessage<const bt_message> {*this};
+    }
+
     _Stream stream() const noexcept
     {
         return _Stream {
@@ -1017,6 +1057,11 @@ public:
         return *this;
     }
 
+    CommonMessageIteratorInactivityMessage<const bt_message> asConst() const noexcept
+    {
+        return CommonMessageIteratorInactivityMessage<const bt_message> {*this};
+    }
+
     ConstClockSnapshot clockSnapshot() const noexcept
     {
         const auto libObjPtr =
index 5ee989601764d34fcea234c7eb46148e857cfa03..e00950818afb8863d62866f6a7748802403ebad7 100644 (file)
@@ -163,6 +163,11 @@ public:
         return *this;
     }
 
+    CommonEvent<const bt_event> asConst() const noexcept
+    {
+        return CommonEvent<const bt_event> {*this};
+    }
+
     Class cls() const noexcept;
     _Stream stream() const noexcept;
     nonstd::optional<_Packet> packet() const noexcept;
@@ -305,6 +310,11 @@ public:
         return *this;
     }
 
+    CommonPacket<const bt_packet> asConst() const noexcept
+    {
+        return CommonPacket<const bt_packet> {*this};
+    }
+
     _Stream stream() const noexcept;
 
     nonstd::optional<_StructureField> contextField() const noexcept
@@ -459,6 +469,11 @@ public:
         return *this;
     }
 
+    CommonStream<const bt_stream> asConst() const noexcept
+    {
+        return CommonStream<const bt_stream> {*this};
+    }
+
     Packet::Shared createPacket() const
     {
         static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
@@ -675,6 +690,11 @@ public:
         return *this;
     }
 
+    CommonTrace<const bt_trace> asConst() const noexcept
+    {
+        return CommonTrace<const bt_trace> {*this};
+    }
+
     Class cls() const noexcept;
 
     void name(const char * const name) const
@@ -993,6 +1013,11 @@ public:
         return *this;
     }
 
+    CommonEventClass<const bt_event_class> asConst() const noexcept
+    {
+        return CommonEventClass<const bt_event_class> {*this};
+    }
+
     _StreamClass streamClass() const noexcept;
 
     std::uint64_t id() const noexcept
@@ -1322,6 +1347,11 @@ public:
         return *this;
     }
 
+    CommonStreamClass<const bt_stream_class> asConst() const noexcept
+    {
+        return CommonStreamClass<const bt_stream_class> {*this};
+    }
+
     Stream::Shared instantiate(const Trace trace) const
     {
         static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
@@ -1736,6 +1766,11 @@ public:
         return *this;
     }
 
+    CommonTraceClass<const bt_trace_class> asConst() const noexcept
+    {
+        return CommonTraceClass<const bt_trace_class> {*this};
+    }
+
     Trace::Shared instantiate() const
     {
         static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
index c8e3dc791fce51678e5dc5301487fedcd3e58156..408710c0eb0150b6655aafd037191e6d7fecb0b0 100644 (file)
@@ -110,6 +110,11 @@ public:
         return *this;
     }
 
+    CommonValue<const bt_value> asConst() const noexcept
+    {
+        return CommonValue<const bt_value> {*this};
+    }
+
     ValueType type() const noexcept
     {
         return static_cast<ValueType>(bt_value_get_type(this->libObjPtr()));
@@ -247,6 +252,11 @@ public:
         return *this;
     }
 
+    CommonNullValue<const bt_value> asConst() const noexcept
+    {
+        return CommonNullValue<const bt_value> {*this};
+    }
+
     Shared shared() const noexcept
     {
         return Shared::createWithRef(*this);
@@ -312,6 +322,11 @@ public:
         return *this;
     }
 
+    CommonBoolValue<const bt_value> asConst() const noexcept
+    {
+        return CommonBoolValue<const bt_value> {*this};
+    }
+
     CommonBoolValue<LibObjT> operator=(const Value rawVal) const noexcept
     {
         static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
@@ -400,6 +415,11 @@ public:
         return *this;
     }
 
+    CommonUnsignedIntegerValue<const bt_value> asConst() const noexcept
+    {
+        return CommonUnsignedIntegerValue<const bt_value> {*this};
+    }
+
     CommonUnsignedIntegerValue<LibObjT> operator=(const Value rawVal) const noexcept
     {
         bt_value_integer_unsigned_set(this->libObjPtr(), rawVal);
@@ -484,6 +504,11 @@ public:
         return *this;
     }
 
+    CommonSignedIntegerValue<const bt_value> asConst() const noexcept
+    {
+        return CommonSignedIntegerValue<const bt_value> {*this};
+    }
+
     CommonSignedIntegerValue<LibObjT> operator=(const Value rawVal) const noexcept
     {
         static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
@@ -567,6 +592,11 @@ public:
         return *this;
     }
 
+    CommonRealValue<const bt_value> asConst() const noexcept
+    {
+        return CommonRealValue<const bt_value> {*this};
+    }
+
     CommonRealValue<LibObjT> operator=(const Value rawVal) const noexcept
     {
         static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
@@ -654,6 +684,11 @@ public:
         return *this;
     }
 
+    CommonStringValue<const bt_value> asConst() const noexcept
+    {
+        return CommonStringValue<const bt_value> {*this};
+    }
+
     CommonStringValue<LibObjT> operator=(const char * const rawVal) const
     {
         static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
@@ -766,6 +801,11 @@ public:
         return *this;
     }
 
+    CommonArrayValue<const bt_value> asConst() const noexcept
+    {
+        return CommonArrayValue<const bt_value> {*this};
+    }
+
     std::uint64_t length() const noexcept
     {
         return bt_value_array_get_length(this->libObjPtr());
@@ -1064,6 +1104,11 @@ public:
         return *this;
     }
 
+    CommonMapValue<const bt_value> asConst() const noexcept
+    {
+        return CommonMapValue<const bt_value> {*this};
+    }
+
     std::uint64_t length() const noexcept
     {
         return bt_value_map_get_size(this->libObjPtr());
This page took 0.034363 seconds and 4 git commands to generate.