From 45e0ded5cd688d00bd39feace0720cba4e63a298 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Thu, 19 May 2022 21:50:36 -0400 Subject: [PATCH] Add bt2::Common{Field,FieldClass,Value,Message}<>::as<>() 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 void g(bt2::Field field, const ValT val) { // ... field.as() = val; // ... } Signed-off-by: Philippe Proulx Change-Id: Ifc1f49487ac6ef9c143bf4c2faa3373f4eaccd9e Reviewed-on: https://review.lttng.org/c/babeltrace/+/8099 Reviewed-by: Francis Deslauriers Reviewed-on: https://review.lttng.org/c/babeltrace/+/10803 Tested-by: jenkins --- src/cpp-common/bt2/field-class.hpp | 6 ++++++ src/cpp-common/bt2/field.hpp | 6 ++++++ src/cpp-common/bt2/message.hpp | 6 ++++++ src/cpp-common/bt2/value.hpp | 6 ++++++ 4 files changed, 24 insertions(+) diff --git a/src/cpp-common/bt2/field-class.hpp b/src/cpp-common/bt2/field-class.hpp index 2a167b19..b630224f 100644 --- a/src/cpp-common/bt2/field-class.hpp +++ b/src/cpp-common/bt2/field-class.hpp @@ -373,6 +373,12 @@ public: return this->_libTypeIs(BT_FIELD_CLASS_TYPE_VARIANT_WITH_SIGNED_INTEGER_SELECTOR_FIELD); } + template + FieldClassT as() const noexcept + { + return FieldClassT {this->libObjPtr()}; + } + CommonBitArrayFieldClass asBitArray() const noexcept; CommonIntegerFieldClass asInteger() const noexcept; CommonBaseEnumerationFieldClass asEnumeration() const noexcept; diff --git a/src/cpp-common/bt2/field.hpp b/src/cpp-common/bt2/field.hpp index 25d0034f..f1908b99 100644 --- a/src/cpp-common/bt2/field.hpp +++ b/src/cpp-common/bt2/field.hpp @@ -204,6 +204,12 @@ public: return this->cls().isVariant(); } + template + FieldT as() const noexcept + { + return FieldT {this->libObjPtr()}; + } + CommonBoolField asBool() const noexcept; CommonBitArrayField asBitArray() const noexcept; CommonUnsignedIntegerField asUnsignedInteger() const noexcept; diff --git a/src/cpp-common/bt2/message.hpp b/src/cpp-common/bt2/message.hpp index cc0f20ea..bc83c4b2 100644 --- a/src/cpp-common/bt2/message.hpp +++ b/src/cpp-common/bt2/message.hpp @@ -157,6 +157,12 @@ public: return Shared::createWithRef(*this); } + template + MessageT as() const noexcept + { + return MessageT {this->libObjPtr()}; + } + CommonStreamBeginningMessage asStreamBeginning() const noexcept; CommonStreamEndMessage asStreamEnd() const noexcept; CommonEventMessage asEvent() const noexcept; diff --git a/src/cpp-common/bt2/value.hpp b/src/cpp-common/bt2/value.hpp index 5f265236..a065af6c 100644 --- a/src/cpp-common/bt2/value.hpp +++ b/src/cpp-common/bt2/value.hpp @@ -212,6 +212,12 @@ public: return Shared::createWithRef(*this); } + template + ValueT as() const noexcept + { + return ValueT {this->libObjPtr()}; + } + CommonNullValue asNull() const noexcept; CommonBoolValue asBool() const noexcept; CommonSignedIntegerValue asSignedInteger() const noexcept; -- 2.34.1