Add bt2::Common{Field,FieldClass,Value,Message}<>::as<>()
[babeltrace.git] / src / cpp-common / bt2 / value.hpp
index dd05fea53fcc4404fb4c961c63518d2d263205fd..a065af6c0c8715dec1c129847c6afde4c9d2ccb2 100644 (file)
@@ -24,7 +24,6 @@
 #include "lib-error.hpp"
 
 namespace bt2 {
-
 namespace internal {
 
 struct ValueRefFuncs final
@@ -210,7 +209,13 @@ public:
 
     Shared shared() const noexcept
     {
-        return Shared {*this};
+        return Shared::createWithRef(*this);
+    }
+
+    template <typename ValueT>
+    ValueT as() const noexcept
+    {
+        return ValueT {this->libObjPtr()};
     }
 
     CommonNullValue<LibObjT> asNull() const noexcept;
@@ -232,6 +237,26 @@ protected:
 using Value = CommonValue<bt_value>;
 using ConstValue = CommonValue<const bt_value>;
 
+namespace internal {
+
+struct ValueTypeDescr
+{
+    using Const = ConstValue;
+    using NonConst = Value;
+};
+
+template <>
+struct TypeDescr<Value> : public ValueTypeDescr
+{
+};
+
+template <>
+struct TypeDescr<ConstValue> : public ValueTypeDescr
+{
+};
+
+} /* namespace internal */
+
 template <typename LibObjT>
 class CommonNullValue final : public CommonValue<LibObjT>
 {
@@ -259,13 +284,33 @@ public:
 
     Shared shared() const noexcept
     {
-        return Shared {*this};
+        return Shared::createWithRef(*this);
     }
 };
 
 using NullValue = CommonNullValue<bt_value>;
 using ConstNullValue = CommonNullValue<const bt_value>;
 
+namespace internal {
+
+struct NullValueTypeDescr
+{
+    using Const = ConstNullValue;
+    using NonConst = NullValue;
+};
+
+template <>
+struct TypeDescr<NullValue> : public NullValueTypeDescr
+{
+};
+
+template <>
+struct TypeDescr<ConstNullValue> : public NullValueTypeDescr
+{
+};
+
+} /* namespace internal */
+
 template <typename LibObjT>
 class CommonBoolValue final : public CommonValue<LibObjT>
 {
@@ -292,7 +337,7 @@ public:
         const auto libObjPtr = bt_value_bool_create_init(static_cast<bt_bool>(rawVal));
 
         internal::validateCreatedObjPtr(libObjPtr);
-        return Shared {CommonBoolValue<LibObjT> {libObjPtr}};
+        return CommonBoolValue::Shared::createWithoutRef(libObjPtr);
     }
 
     template <typename OtherLibObjT>
@@ -322,13 +367,33 @@ public:
 
     Shared shared() const noexcept
     {
-        return Shared {*this};
+        return Shared::createWithRef(*this);
     }
 };
 
 using BoolValue = CommonBoolValue<bt_value>;
 using ConstBoolValue = CommonBoolValue<const bt_value>;
 
+namespace internal {
+
+struct BoolValueTypeDescr
+{
+    using Const = ConstBoolValue;
+    using NonConst = BoolValue;
+};
+
+template <>
+struct TypeDescr<BoolValue> : public BoolValueTypeDescr
+{
+};
+
+template <>
+struct TypeDescr<ConstBoolValue> : public BoolValueTypeDescr
+{
+};
+
+} /* namespace internal */
+
 template <typename LibObjT>
 class CommonUnsignedIntegerValue final : public CommonValue<LibObjT>
 {
@@ -351,7 +416,7 @@ public:
         const auto libObjPtr = bt_value_integer_unsigned_create_init(rawVal);
 
         internal::validateCreatedObjPtr(libObjPtr);
-        return Shared {CommonUnsignedIntegerValue<LibObjT> {libObjPtr}};
+        return CommonUnsignedIntegerValue::Shared::createWithoutRef(libObjPtr);
     }
 
     template <typename OtherLibObjT>
@@ -388,13 +453,33 @@ public:
 
     Shared shared() const noexcept
     {
-        return Shared {*this};
+        return Shared::createWithRef(*this);
     }
 };
 
 using UnsignedIntegerValue = CommonUnsignedIntegerValue<bt_value>;
 using ConstUnsignedIntegerValue = CommonUnsignedIntegerValue<const bt_value>;
 
+namespace internal {
+
+struct UnsignedIntegerValueTypeDescr
+{
+    using Const = ConstUnsignedIntegerValue;
+    using NonConst = UnsignedIntegerValue;
+};
+
+template <>
+struct TypeDescr<UnsignedIntegerValue> : public UnsignedIntegerValueTypeDescr
+{
+};
+
+template <>
+struct TypeDescr<ConstUnsignedIntegerValue> : public UnsignedIntegerValueTypeDescr
+{
+};
+
+} /* namespace internal */
+
 template <typename LibObjT>
 class CommonSignedIntegerValue final : public CommonValue<LibObjT>
 {
@@ -417,7 +502,7 @@ public:
         const auto libObjPtr = bt_value_integer_signed_create_init(rawVal);
 
         internal::validateCreatedObjPtr(libObjPtr);
-        return Shared {CommonSignedIntegerValue<LibObjT> {libObjPtr}};
+        return CommonSignedIntegerValue::Shared::createWithoutRef(libObjPtr);
     }
 
     template <typename OtherLibObjT>
@@ -454,13 +539,33 @@ public:
 
     Shared shared() const noexcept
     {
-        return Shared {*this};
+        return Shared::createWithRef(*this);
     }
 };
 
 using SignedIntegerValue = CommonSignedIntegerValue<bt_value>;
 using ConstSignedIntegerValue = CommonSignedIntegerValue<const bt_value>;
 
+namespace internal {
+
+struct SignedIntegerValueTypeDescr
+{
+    using Const = ConstSignedIntegerValue;
+    using NonConst = SignedIntegerValue;
+};
+
+template <>
+struct TypeDescr<SignedIntegerValue> : public SignedIntegerValueTypeDescr
+{
+};
+
+template <>
+struct TypeDescr<ConstSignedIntegerValue> : public SignedIntegerValueTypeDescr
+{
+};
+
+} /* namespace internal */
+
 template <typename LibObjT>
 class CommonRealValue final : public CommonValue<LibObjT>
 {
@@ -482,7 +587,7 @@ public:
         const auto libObjPtr = bt_value_real_create_init(rawVal);
 
         internal::validateCreatedObjPtr(libObjPtr);
-        return Shared {CommonRealValue<LibObjT> {libObjPtr}};
+        return CommonRealValue::Shared::createWithoutRef(libObjPtr);
     }
 
     template <typename OtherLibObjT>
@@ -517,13 +622,33 @@ public:
 
     Shared shared() const noexcept
     {
-        return Shared {*this};
+        return Shared::createWithRef(*this);
     }
 };
 
 using RealValue = CommonRealValue<bt_value>;
 using ConstRealValue = CommonRealValue<const bt_value>;
 
+namespace internal {
+
+struct RealValueTypeDescr
+{
+    using Const = ConstRealValue;
+    using NonConst = RealValue;
+};
+
+template <>
+struct TypeDescr<RealValue> : public RealValueTypeDescr
+{
+};
+
+template <>
+struct TypeDescr<ConstRealValue> : public RealValueTypeDescr
+{
+};
+
+} /* namespace internal */
+
 template <typename LibObjT>
 class CommonStringValue final : public CommonValue<LibObjT>
 {
@@ -544,12 +669,12 @@ public:
         const auto libObjPtr = bt_value_string_create_init(rawVal);
 
         internal::validateCreatedObjPtr(libObjPtr);
-        return Shared {CommonStringValue<LibObjT> {libObjPtr}};
+        return CommonStringValue::Shared::createWithoutRef(libObjPtr);
     }
 
     static Shared create(const std::string& rawVal)
     {
-        return CommonStringValue<LibObjT>::create(rawVal.data());
+        return CommonStringValue::create(rawVal.data());
     }
 
     template <typename OtherLibObjT>
@@ -589,7 +714,7 @@ public:
 
     Shared shared() const noexcept
     {
-        return Shared {*this};
+        return Shared::createWithRef(*this);
     }
 };
 
@@ -598,6 +723,22 @@ using ConstStringValue = CommonStringValue<const bt_value>;
 
 namespace internal {
 
+struct StringValueTypeDescr
+{
+    using Const = ConstStringValue;
+    using NonConst = StringValue;
+};
+
+template <>
+struct TypeDescr<StringValue> : public StringValueTypeDescr
+{
+};
+
+template <>
+struct TypeDescr<ConstStringValue> : public StringValueTypeDescr
+{
+};
+
 template <typename LibObjT>
 struct CommonArrayValueSpec;
 
@@ -645,7 +786,7 @@ public:
         const auto libObjPtr = bt_value_array_create();
 
         internal::validateCreatedObjPtr(libObjPtr);
-        return Shared {CommonArrayValue<LibObjT> {libObjPtr}};
+        return CommonArrayValue::Shared::createWithoutRef(libObjPtr);
     }
 
     template <typename OtherLibObjT>
@@ -799,7 +940,7 @@ public:
 
     Shared shared() const noexcept
     {
-        return Shared {*this};
+        return Shared::createWithRef(*this);
     }
 
 private:
@@ -816,6 +957,22 @@ using ConstArrayValue = CommonArrayValue<const bt_value>;
 
 namespace internal {
 
+struct ArrayValueTypeDescr
+{
+    using Const = ConstArrayValue;
+    using NonConst = ArrayValue;
+};
+
+template <>
+struct TypeDescr<ArrayValue> : public ArrayValueTypeDescr
+{
+};
+
+template <>
+struct TypeDescr<ConstArrayValue> : public ArrayValueTypeDescr
+{
+};
+
 /*
  * Type of a user function passed to `CommonMapValue<ObjT>::forEach()`.
  *
@@ -939,7 +1096,7 @@ public:
         const auto libObjPtr = bt_value_map_create();
 
         internal::validateCreatedObjPtr(libObjPtr);
-        return Shared {CommonMapValue<LibObjT> {libObjPtr}};
+        return CommonMapValue::Shared::createWithoutRef(libObjPtr);
     }
 
     template <typename OtherLibObjT>
@@ -1122,7 +1279,7 @@ public:
 
     Shared shared() const noexcept
     {
-        return Shared {*this};
+        return Shared::createWithRef(*this);
     }
 
 private:
@@ -1137,6 +1294,26 @@ private:
 using MapValue = CommonMapValue<bt_value>;
 using ConstMapValue = CommonMapValue<const bt_value>;
 
+namespace internal {
+
+struct MapValueTypeDescr
+{
+    using Const = ConstMapValue;
+    using NonConst = MapValue;
+};
+
+template <>
+struct TypeDescr<MapValue> : public MapValueTypeDescr
+{
+};
+
+template <>
+struct TypeDescr<ConstMapValue> : public MapValueTypeDescr
+{
+};
+
+} /* namespace internal */
+
 template <typename LibObjT>
 CommonNullValue<LibObjT> CommonValue<LibObjT>::asNull() const noexcept
 {
This page took 0.028228 seconds and 4 git commands to generate.