Add bt2::Common{Field,FieldClass,Value,Message}<>::as<>()
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Fri, 20 May 2022 01:50:36 +0000 (21:50 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Mon, 11 Sep 2023 15:24:02 +0000 (11:24 -0400)
Those new as() method templates conceptually cast their object to the
first template parameter. Those are shorthands to use specific types
within a template function, for example:

    template <typename FieldT, typename ValT>
    void g(bt2::Field field, const ValT val)
    {
        // ...
        field.as<FieldT>() = val;
        // ...
    }

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: Ifc1f49487ac6ef9c143bf4c2faa3373f4eaccd9e
Reviewed-on: https://review.lttng.org/c/babeltrace/+/8099
Reviewed-by: Francis Deslauriers <francis.deslauriers@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/10803
Tested-by: jenkins <jenkins@lttng.org>
src/cpp-common/bt2/field-class.hpp
src/cpp-common/bt2/field.hpp
src/cpp-common/bt2/message.hpp
src/cpp-common/bt2/value.hpp

index 2a167b1907774c59a5d5bcd4c7c8b196ede23409..b630224f929fda5b0a851065bebd0c5981b26103 100644 (file)
@@ -373,6 +373,12 @@ public:
         return this->_libTypeIs(BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD);
     }
 
+    template <typename FieldClassT>
+    FieldClassT as() const noexcept
+    {
+        return FieldClassT {this->libObjPtr()};
+    }
+
     CommonBitArrayFieldClass<LibObjT> asBitArray() const noexcept;
     CommonIntegerFieldClass<LibObjT> asInteger() const noexcept;
     CommonBaseEnumerationFieldClass<LibObjT> asEnumeration() const noexcept;
index 25d0034ffe4bbb18f3869abc8bc545c9dfea6579..f1908b9923a84760d196deacdcd499b6d3c7ef47 100644 (file)
@@ -204,6 +204,12 @@ public:
         return this->cls().isVariant();
     }
 
+    template <typename FieldT>
+    FieldT as() const noexcept
+    {
+        return FieldT {this->libObjPtr()};
+    }
+
     CommonBoolField<LibObjT> asBool() const noexcept;
     CommonBitArrayField<LibObjT> asBitArray() const noexcept;
     CommonUnsignedIntegerField<LibObjT> asUnsignedInteger() const noexcept;
index cc0f20ead672c26937020fa4e17b091bf5869b32..bc83c4b2181cbe9645bf5e9d1d9ab41ea2bfea08 100644 (file)
@@ -157,6 +157,12 @@ public:
         return Shared::createWithRef(*this);
     }
 
+    template <typename MessageT>
+    MessageT as() const noexcept
+    {
+        return MessageT {this->libObjPtr()};
+    }
+
     CommonStreamBeginningMessage<LibObjT> asStreamBeginning() const noexcept;
     CommonStreamEndMessage<LibObjT> asStreamEnd() const noexcept;
     CommonEventMessage<LibObjT> asEvent() const noexcept;
index 5f265236489bda91b73f0ba62f1f3c4fda8f685d..a065af6c0c8715dec1c129847c6afde4c9d2ccb2 100644 (file)
@@ -212,6 +212,12 @@ public:
         return Shared::createWithRef(*this);
     }
 
+    template <typename ValueT>
+    ValueT as() const noexcept
+    {
+        return ValueT {this->libObjPtr()};
+    }
+
     CommonNullValue<LibObjT> asNull() const noexcept;
     CommonBoolValue<LibObjT> asBool() const noexcept;
     CommonSignedIntegerValue<LibObjT> asSignedInteger() const noexcept;
This page took 0.027175 seconds and 4 git commands to generate.