cpp-common/bt2: use more specific static assertion messages
[babeltrace.git] / src / cpp-common / bt2 / value.hpp
index 1aa413184c737478e08b9e1959bb85e6517a68f7..c0a60fe25c538eeda194fd14074d11993d028309 100644 (file)
@@ -29,12 +29,12 @@ namespace internal {
 
 struct ValueRefFuncs final
 {
-    static void get(const bt_value * const libObjPtr)
+    static void get(const bt_value * const libObjPtr) noexcept
     {
         bt_value_get_ref(libObjPtr);
     }
 
-    static void put(const bt_value * const libObjPtr)
+    static void put(const bt_value * const libObjPtr) noexcept
     {
         bt_value_put_ref(libObjPtr);
     }
@@ -81,45 +81,9 @@ enum class ValueType
     MAP = BT_VALUE_TYPE_MAP,
 };
 
-template <typename LibObjT>
-class CommonClockClass;
-
-template <typename LibObjT>
-class CommonFieldClass;
-
-template <typename LibObjT>
-class CommonTraceClass;
-
-template <typename LibObjT>
-class CommonStreamClass;
-
-template <typename LibObjT>
-class CommonEventClass;
-
-template <typename LibObjT>
-class CommonStream;
-
 template <typename LibObjT>
 class CommonValue : public BorrowedObject<LibObjT>
 {
-    /* Allow append() to call `val.libObjPtr()` */
-    friend class CommonArrayValue<bt_value>;
-
-    /* Allow insert() to call `val.libObjPtr()` */
-    friend class CommonMapValue<bt_value>;
-
-    /* Allow userAttributes() to call `val.libObjPtr()` */
-    friend class CommonClockClass<bt_clock_class>;
-    friend class CommonFieldClass<bt_field_class>;
-    friend class CommonTraceClass<bt_trace_class>;
-    friend class CommonStreamClass<bt_stream_class>;
-    friend class CommonEventClass<bt_event_class>;
-    friend class CommonStream<bt_stream>;
-
-    /* Allow operator==() to call `other.libObjPtr()` */
-    friend class CommonValue<bt_value>;
-    friend class CommonValue<const bt_value>;
-
 private:
     using typename BorrowedObject<LibObjT>::_ThisBorrowedObject;
 
@@ -146,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()));
@@ -283,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);
@@ -348,9 +322,14 @@ public:
         return *this;
     }
 
-    CommonBoolValue<LibObjT>& operator=(const Value rawVal) noexcept
+    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`.");
+        static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstBoolValue`.");
 
         bt_value_bool_set(this->libObjPtr(), static_cast<bt_bool>(rawVal));
         return *this;
@@ -430,13 +409,19 @@ public:
     CommonUnsignedIntegerValue<LibObjT>&
     operator=(const CommonUnsignedIntegerValue<OtherLibObjT> val) noexcept
     {
-        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+        static_assert(!std::is_const<LibObjT>::value,
+                      "Not available with `bt2::ConstUnsignedIntegerValue`.");
 
         _ThisCommonValue::operator=(val);
         return *this;
     }
 
-    CommonUnsignedIntegerValue<LibObjT>& operator=(const Value rawVal) noexcept
+    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);
         return *this;
@@ -513,16 +498,22 @@ public:
     }
 
     template <typename OtherLibObjT>
-    CommonSignedIntegerValue<LibObjT>&
+    CommonSignedIntegerValue<LibObjT>
     operator=(const CommonSignedIntegerValue<OtherLibObjT> val) noexcept
     {
         _ThisCommonValue::operator=(val);
         return *this;
     }
 
-    CommonSignedIntegerValue<LibObjT>& operator=(const Value rawVal) noexcept
+    CommonSignedIntegerValue<const bt_value> asConst() const noexcept
     {
-        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+        return CommonSignedIntegerValue<const bt_value> {*this};
+    }
+
+    CommonSignedIntegerValue<LibObjT> operator=(const Value rawVal) const noexcept
+    {
+        static_assert(!std::is_const<LibObjT>::value,
+                      "Not available with `bt2::ConstSignedIntegerValue`.");
 
         bt_value_integer_signed_set(this->libObjPtr(), rawVal);
         return *this;
@@ -603,9 +594,14 @@ public:
         return *this;
     }
 
-    CommonRealValue<LibObjT>& operator=(const Value rawVal) noexcept
+    CommonRealValue<const bt_value> asConst() const noexcept
     {
-        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+        return CommonRealValue<const bt_value> {*this};
+    }
+
+    CommonRealValue<LibObjT> operator=(const Value rawVal) const noexcept
+    {
+        static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstRealValue`.");
 
         bt_value_real_set(this->libObjPtr(), rawVal);
         return *this;
@@ -690,9 +686,15 @@ public:
         return *this;
     }
 
-    CommonStringValue<LibObjT>& operator=(const char * const rawVal)
+    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`.");
+        static_assert(!std::is_const<LibObjT>::value,
+                      "Not available with `bt2::ConstStringValue`.");
 
         const auto status = bt_value_string_set(this->libObjPtr(), rawVal);
 
@@ -703,7 +705,7 @@ public:
         return *this;
     }
 
-    CommonStringValue<LibObjT>& operator=(const std::string& rawVal) noexcept
+    CommonStringValue<LibObjT> operator=(const std::string& rawVal) const noexcept
     {
         return *this = rawVal.data();
     }
@@ -802,15 +804,14 @@ public:
         return *this;
     }
 
-    std::uint64_t length() const noexcept
+    CommonArrayValue<const bt_value> asConst() const noexcept
     {
-        return bt_value_array_get_length(this->libObjPtr());
+        return CommonArrayValue<const bt_value> {*this};
     }
 
-    /* Required by the `CommonIterator` template class */
-    std::uint64_t size() const noexcept
+    std::uint64_t length() const noexcept
     {
-        return this->length();
+        return bt_value_array_get_length(this->libObjPtr());
     }
 
     Iterator begin() const noexcept
@@ -828,30 +829,24 @@ public:
         return this->length() == 0;
     }
 
-    ConstValue operator[](const std::uint64_t index) const noexcept
-    {
-        return ConstValue {internal::CommonArrayValueSpec<const bt_value>::elementByIndex(
-            this->libObjPtr(), index)};
-    }
-
-    CommonValue<LibObjT> operator[](const std::uint64_t index) noexcept
+    CommonValue<LibObjT> operator[](const std::uint64_t index) const noexcept
     {
         return CommonValue<LibObjT> {
             internal::CommonArrayValueSpec<LibObjT>::elementByIndex(this->libObjPtr(), index)};
     }
 
-    void append(const Value val)
+    void append(const Value val) const
     {
-        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+        static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`.");
 
         const auto status = bt_value_array_append_element(this->libObjPtr(), val.libObjPtr());
 
         this->_handleAppendLibStatus(status);
     }
 
-    void append(const bool rawVal)
+    void append(const bool rawVal) const
     {
-        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+        static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`.");
 
         const auto status =
             bt_value_array_append_bool_element(this->libObjPtr(), static_cast<bt_bool>(rawVal));
@@ -859,9 +854,9 @@ public:
         this->_handleAppendLibStatus(status);
     }
 
-    void append(const std::uint64_t rawVal)
+    void append(const std::uint64_t rawVal) const
     {
-        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+        static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`.");
 
         const auto status =
             bt_value_array_append_unsigned_integer_element(this->libObjPtr(), rawVal);
@@ -869,72 +864,72 @@ public:
         this->_handleAppendLibStatus(status);
     }
 
-    void append(const std::int64_t rawVal)
+    void append(const std::int64_t rawVal) const
     {
-        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+        static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`.");
 
         const auto status = bt_value_array_append_signed_integer_element(this->libObjPtr(), rawVal);
 
         this->_handleAppendLibStatus(status);
     }
 
-    void append(const double rawVal)
+    void append(const double rawVal) const
     {
-        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+        static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`.");
 
         const auto status = bt_value_array_append_real_element(this->libObjPtr(), rawVal);
 
         this->_handleAppendLibStatus(status);
     }
 
-    void append(const char * const rawVal)
+    void append(const char * const rawVal) const
     {
-        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+        static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`.");
 
         const auto status = bt_value_array_append_string_element(this->libObjPtr(), rawVal);
 
         this->_handleAppendLibStatus(status);
     }
 
-    void append(const std::string& rawVal)
+    void append(const std::string& rawVal) const
     {
         this->append(rawVal.data());
     }
 
-    CommonArrayValue<bt_value> appendEmptyArray();
-    CommonMapValue<bt_value> appendEmptyMap();
+    CommonArrayValue<bt_value> appendEmptyArray() const;
+    CommonMapValue<bt_value> appendEmptyMap() const;
 
-    void operator+=(const Value val)
+    void operator+=(const Value val) const
     {
         this->append(val);
     }
 
-    void operator+=(const bool rawVal)
+    void operator+=(const bool rawVal) const
     {
         this->append(rawVal);
     }
 
-    void operator+=(const std::uint64_t rawVal)
+    void operator+=(const std::uint64_t rawVal) const
     {
         this->append(rawVal);
     }
 
-    void operator+=(const std::int64_t rawVal)
+    void operator+=(const std::int64_t rawVal) const
     {
         this->append(rawVal);
     }
 
-    void operator+=(const double rawVal)
+    void operator+=(const double rawVal) const
     {
         this->append(rawVal);
     }
 
-    void operator+=(const char * const rawVal)
+    void operator+=(const char * const rawVal) const
     {
         this->append(rawVal);
     }
 
-    void operator+=(const std::string& rawVal)
+    void operator+=(const std::string& rawVal) const
     {
         this->append(rawVal);
     }
@@ -1112,34 +1107,22 @@ public:
         return *this;
     }
 
-    std::uint64_t size() const noexcept
+    CommonMapValue<const bt_value> asConst() const noexcept
     {
-        return bt_value_map_get_size(this->libObjPtr());
+        return CommonMapValue<const bt_value> {*this};
     }
 
-    bool isEmpty() const noexcept
-    {
-        return this->size() == 0;
-    }
-
-    nonstd::optional<ConstValue> operator[](const char * const key) const noexcept
+    std::uint64_t length() const noexcept
     {
-        const auto libObjPtr =
-            internal::CommonMapValueSpec<const bt_value>::entryByKey(this->libObjPtr(), key);
-
-        if (!libObjPtr) {
-            return nonstd::nullopt;
-        }
-
-        return ConstValue {libObjPtr};
+        return bt_value_map_get_size(this->libObjPtr());
     }
 
-    nonstd::optional<ConstValue> operator[](const std::string& key) const noexcept
+    bool isEmpty() const noexcept
     {
-        return (*this)[key.data()];
+        return this->length() == 0;
     }
 
-    nonstd::optional<CommonValue<LibObjT>> operator[](const char * const key) noexcept
+    nonstd::optional<CommonValue<LibObjT>> operator[](const char * const key) const noexcept
     {
         const auto libObjPtr =
             internal::CommonMapValueSpec<LibObjT>::entryByKey(this->libObjPtr(), key);
@@ -1151,7 +1134,7 @@ public:
         return CommonValue<LibObjT> {libObjPtr};
     }
 
-    nonstd::optional<CommonValue<LibObjT>> operator[](const std::string& key) noexcept
+    nonstd::optional<CommonValue<LibObjT>> operator[](const std::string& key) const noexcept
     {
         return (*this)[key.data()];
     }
@@ -1166,23 +1149,23 @@ public:
         return this->hasEntry(key.data());
     }
 
-    void insert(const char * const key, const Value val)
+    void insert(const char * const key, const Value val) const
     {
-        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+        static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
 
         const auto status = bt_value_map_insert_entry(this->libObjPtr(), key, val.libObjPtr());
 
         this->_handleInsertLibStatus(status);
     }
 
-    void insert(const std::string& key, const Value val)
+    void insert(const std::string& key, const Value val) const
     {
         this->insert(key.data(), val);
     }
 
-    void insert(const char * const key, const bool rawVal)
+    void insert(const char * const key, const bool rawVal) const
     {
-        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+        static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
 
         const auto status =
             bt_value_map_insert_bool_entry(this->libObjPtr(), key, static_cast<bt_bool>(rawVal));
@@ -1190,14 +1173,14 @@ public:
         this->_handleInsertLibStatus(status);
     }
 
-    void insert(const std::string& key, const bool rawVal)
+    void insert(const std::string& key, const bool rawVal) const
     {
         this->insert(key.data(), rawVal);
     }
 
-    void insert(const char * const key, const std::uint64_t rawVal)
+    void insert(const char * const key, const std::uint64_t rawVal) const
     {
-        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+        static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
 
         const auto status =
             bt_value_map_insert_unsigned_integer_entry(this->libObjPtr(), key, rawVal);
@@ -1205,14 +1188,14 @@ public:
         this->_handleInsertLibStatus(status);
     }
 
-    void insert(const std::string& key, const std::uint64_t rawVal)
+    void insert(const std::string& key, const std::uint64_t rawVal) const
     {
         this->insert(key.data(), rawVal);
     }
 
-    void insert(const char * const key, const std::int64_t rawVal)
+    void insert(const char * const key, const std::int64_t rawVal) const
     {
-        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+        static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
 
         const auto status =
             bt_value_map_insert_signed_integer_entry(this->libObjPtr(), key, rawVal);
@@ -1220,60 +1203,55 @@ public:
         this->_handleInsertLibStatus(status);
     }
 
-    void insert(const std::string& key, const std::int64_t rawVal)
+    void insert(const std::string& key, const std::int64_t rawVal) const
     {
         this->insert(key.data(), rawVal);
     }
 
-    void insert(const char * const key, const double rawVal)
+    void insert(const char * const key, const double rawVal) const
     {
-        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+        static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
 
         const auto status = bt_value_map_insert_real_entry(this->libObjPtr(), key, rawVal);
 
         this->_handleInsertLibStatus(status);
     }
 
-    void insert(const std::string& key, const double rawVal)
+    void insert(const std::string& key, const double rawVal) const
     {
         this->insert(key.data(), rawVal);
     }
 
-    void insert(const char * const key, const char * const rawVal)
+    void insert(const char * const key, const char * const rawVal) const
     {
-        static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+        static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
 
         const auto status = bt_value_map_insert_string_entry(this->libObjPtr(), key, rawVal);
 
         this->_handleInsertLibStatus(status);
     }
 
-    void insert(const char * const key, const std::string& rawVal)
+    void insert(const char * const key, const std::string& rawVal) const
     {
         this->insert(key, rawVal.data());
     }
 
-    void insert(const std::string& key, const char * const rawVal)
+    void insert(const std::string& key, const char * const rawVal) const
     {
         this->insert(key.data(), rawVal);
     }
 
-    void insert(const std::string& key, const std::string& rawVal)
+    void insert(const std::string& key, const std::string& rawVal) const
     {
         this->insert(key.data(), rawVal.data());
     }
 
-    CommonArrayValue<bt_value> insertEmptyArray(const char *key);
-    CommonArrayValue<bt_value> insertEmptyArray(const std::string& key);
-    CommonMapValue<bt_value> insertEmptyMap(const char *key);
-    CommonMapValue<bt_value> insertEmptyMap(const std::string& key);
-
-    void forEach(const internal::CommonMapValueForEachUserFunc<ConstValue>& func) const
-    {
-        internal::CommonMapValueSpec<const bt_value>::forEach(this->libObjPtr(), func);
-    }
+    CommonArrayValue<bt_value> insertEmptyArray(const char *key) const;
+    CommonArrayValue<bt_value> insertEmptyArray(const std::string& key) const;
+    CommonMapValue<bt_value> insertEmptyMap(const char *key) const;
+    CommonMapValue<bt_value> insertEmptyMap(const std::string& key) const;
 
-    void forEach(const internal::CommonMapValueForEachUserFunc<CommonValue<LibObjT>>& func)
+    void forEach(const internal::CommonMapValueForEachUserFunc<CommonValue<LibObjT>>& func) const
     {
         internal::CommonMapValueSpec<LibObjT>::forEach(this->libObjPtr(), func);
     }
@@ -1372,9 +1350,9 @@ CommonMapValue<LibObjT> CommonValue<LibObjT>::asMap() const noexcept
 }
 
 template <typename LibObjT>
-ArrayValue CommonArrayValue<LibObjT>::appendEmptyArray()
+ArrayValue CommonArrayValue<LibObjT>::appendEmptyArray() const
 {
-    static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+    static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`.");
 
     bt_value *libElemPtr;
     const auto status = bt_value_array_append_empty_array_element(this->libObjPtr(), &libElemPtr);
@@ -1384,9 +1362,9 @@ ArrayValue CommonArrayValue<LibObjT>::appendEmptyArray()
 }
 
 template <typename LibObjT>
-MapValue CommonArrayValue<LibObjT>::appendEmptyMap()
+MapValue CommonArrayValue<LibObjT>::appendEmptyMap() const
 {
-    static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+    static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstArrayValue`.");
 
     bt_value *libElemPtr;
     const auto status = bt_value_array_append_empty_map_element(this->libObjPtr(), &libElemPtr);
@@ -1396,9 +1374,9 @@ MapValue CommonArrayValue<LibObjT>::appendEmptyMap()
 }
 
 template <typename LibObjT>
-ArrayValue CommonMapValue<LibObjT>::insertEmptyArray(const char * const key)
+ArrayValue CommonMapValue<LibObjT>::insertEmptyArray(const char * const key) const
 {
-    static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+    static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
 
     bt_value *libEntryPtr;
     const auto status = bt_value_map_insert_empty_array_entry(this->libObjPtr(), key, &libEntryPtr);
@@ -1408,15 +1386,15 @@ ArrayValue CommonMapValue<LibObjT>::insertEmptyArray(const char * const key)
 }
 
 template <typename LibObjT>
-ArrayValue CommonMapValue<LibObjT>::insertEmptyArray(const std::string& key)
+ArrayValue CommonMapValue<LibObjT>::insertEmptyArray(const std::string& key) const
 {
     return this->insertEmptyArray(key.data());
 }
 
 template <typename LibObjT>
-MapValue CommonMapValue<LibObjT>::insertEmptyMap(const char * const key)
+MapValue CommonMapValue<LibObjT>::insertEmptyMap(const char * const key) const
 {
-    static_assert(!std::is_const<LibObjT>::value, "`LibObjT` must NOT be `const`.");
+    static_assert(!std::is_const<LibObjT>::value, "Not available with `bt2::ConstMapValue`.");
 
     bt_value *libEntryPtr;
     const auto status = bt_value_map_insert_empty_map_entry(this->libObjPtr(), key, &libEntryPtr);
@@ -1426,7 +1404,7 @@ MapValue CommonMapValue<LibObjT>::insertEmptyMap(const char * const key)
 }
 
 template <typename LibObjT>
-MapValue CommonMapValue<LibObjT>::insertEmptyMap(const std::string& key)
+MapValue CommonMapValue<LibObjT>::insertEmptyMap(const std::string& key) const
 {
     return this->insertEmptyMap(key.data());
 }
This page took 0.032606 seconds and 4 git commands to generate.