clang-tidy: add readability-simplify-boolean-expr
[lttng-tools.git] / src / bin / lttng-sessiond / field.hpp
index a774d133a0978d0c59e0ce08f3a2865f675e23b4..fdebe3acc3bd24ca69b52f71bf0b259363dd1920 100644 (file)
@@ -9,6 +9,7 @@
 #define LTTNG_FIELD_H
 
 #include <common/format.hpp>
+#include <common/make-unique.hpp>
 
 #include <vendor/optional.hpp>
 
@@ -65,12 +66,16 @@ public:
        bool operator==(const type& other) const noexcept;
        bool operator!=(const type& other) const noexcept;
        virtual ~type();
+
+       /* Obtain an independent copy of `type`. */
+       virtual type::cuptr copy() const = 0;
+
        virtual void accept(type_visitor& visitor) const = 0;
 
        const unsigned int alignment;
 
 protected:
-       type(unsigned int alignment);
+       explicit type(unsigned int alignment);
 
 private:
        virtual bool _is_equal(const type& rhs) const noexcept = 0;
@@ -133,7 +138,9 @@ public:
                        base base,
                        roles roles = {});
 
-       virtual void accept(type_visitor& visitor) const override;
+       type::cuptr copy() const override;
+
+       void accept(type_visitor& visitor) const override;
 
        const enum byte_order byte_order;
        const unsigned int size;
@@ -147,7 +154,7 @@ public:
        const roles roles_;
 
 protected:
-       virtual bool _is_equal(const type& other) const noexcept override;
+       bool _is_equal(const type& other) const noexcept override;
 };
 
 class floating_point_type : public type {
@@ -157,14 +164,16 @@ public:
                        unsigned int exponent_digits,
                        unsigned int mantissa_digits);
 
-       virtual void accept(type_visitor& visitor) const override final;
+       type::cuptr copy() const final;
+
+       void accept(type_visitor& visitor) const final;
 
        const enum byte_order byte_order;
        const unsigned int exponent_digits;
        const unsigned int mantissa_digits;
 
 private:
-       virtual bool _is_equal(const type& other) const noexcept override final;
+       bool _is_equal(const type& other) const noexcept final;
 };
 
 class enumeration_type : public integer_type {
@@ -176,7 +185,7 @@ protected:
                        enum base base,
                        integer_type::roles roles = {});
 
-       virtual void accept(type_visitor& visitor) const = 0;
+       void accept(type_visitor& visitor) const override = 0;
 };
 
 namespace details {
@@ -206,8 +215,8 @@ public:
        using range_t = enumeration_mapping_range<MappingIntegerType>;
 
        enumeration_mapping(const enumeration_mapping<MappingIntegerType>& other) = default;
-       enumeration_mapping(const enumeration_mapping<MappingIntegerType>&& other) :
-               name{std::move(other.name)}, range{other.range}
+       enumeration_mapping(const enumeration_mapping<MappingIntegerType>&& other) noexcept :
+               name{ std::move(other.name) }, range{ other.range }
        {
        }
 
@@ -236,7 +245,7 @@ bool operator==(const enumeration_mapping<MappingIntegerType>& lhs,
 }
 } /* namespace details */
 
-template <class MappingIntegerType>
+template <typename MappingIntegerType>
 class typed_enumeration_type : public enumeration_type {
 public:
        using mapping = details::enumeration_mapping<MappingIntegerType>;
@@ -264,12 +273,18 @@ public:
        {
        }
 
-       virtual void accept(type_visitor& visitor) const override final;
+       type::cuptr copy() const override
+       {
+               return lttng::make_unique<typed_enumeration_type<MappingIntegerType>>(
+                               alignment, byte_order, size, base_, mappings_, roles_);
+       }
+
+       void accept(type_visitor& visitor) const final;
 
        const std::shared_ptr<const mappings> mappings_;
 
 private:
-       virtual bool _is_equal(const type& base_other) const noexcept override final
+       bool _is_equal(const type& base_other) const noexcept final
        {
                const auto& other = static_cast<const typed_enumeration_type<MappingIntegerType>&>(
                                base_other);
@@ -289,7 +304,7 @@ public:
        const type::cuptr element_type;
 
 protected:
-       virtual bool _is_equal(const type& base_other) const noexcept override;
+       bool _is_equal(const type& base_other) const noexcept override;
 };
 
 class static_length_array_type : public array_type {
@@ -298,12 +313,14 @@ public:
                        type::cuptr element_type,
                        uint64_t in_length);
 
-       virtual void accept(type_visitor& visitor) const override final;
+       type::cuptr copy() const final;
+
+       void accept(type_visitor& visitor) const final;
 
        const uint64_t length;
 
 private:
-       virtual bool _is_equal(const type& base_other) const noexcept override final;
+       bool _is_equal(const type& base_other) const noexcept final;
 };
 
 class dynamic_length_array_type : public array_type {
@@ -312,12 +329,14 @@ public:
                        type::cuptr element_type,
                        field_location length_field_location);
 
-       virtual void accept(type_visitor& visitor) const override final;
+       type::cuptr copy() const final;
+
+       void accept(type_visitor& visitor) const final;
 
        const field_location length_field_location;
 
 private:
-       virtual bool _is_equal(const type& base_other) const noexcept override final;
+       bool _is_equal(const type& base_other) const noexcept final;
 };
 
 class static_length_blob_type : public type {
@@ -331,25 +350,29 @@ public:
 
        static_length_blob_type(unsigned int alignment, uint64_t in_length_bytes, roles roles = {});
 
-       virtual void accept(type_visitor& visitor) const override final;
+       type::cuptr copy() const final;
+
+       void accept(type_visitor& visitor) const final;
 
        const uint64_t length_bytes;
        const roles roles_;
 
 private:
-       virtual bool _is_equal(const type& base_other) const noexcept override final;
+       bool _is_equal(const type& base_other) const noexcept final;
 };
 
 class dynamic_length_blob_type : public type {
 public:
        dynamic_length_blob_type(unsigned int alignment, field_location length_field_location);
 
-       virtual void accept(type_visitor& visitor) const override final;
+       type::cuptr copy() const final;
+
+       void accept(type_visitor& visitor) const final;
 
        const field_location length_field_location;
 
 private:
-       virtual bool _is_equal(const type& base_other) const noexcept override final;
+       bool _is_equal(const type& base_other) const noexcept final;
 };
 
 class string_type : public type {
@@ -369,19 +392,22 @@ public:
        const encoding encoding_;
 
 protected:
-       virtual bool _is_equal(const type& base_other) const noexcept override;
+       bool _is_equal(const type& base_other) const noexcept override;
 };
 
 class static_length_string_type : public string_type {
 public:
        static_length_string_type(
                        unsigned int alignment, enum encoding in_encoding, uint64_t length);
-       virtual void accept(type_visitor& visitor) const override final;
+
+       type::cuptr copy() const final;
+
+       void accept(type_visitor& visitor) const final;
 
        const uint64_t length;
 
 private:
-       virtual bool _is_equal(const type& base_other) const noexcept override final;
+       bool _is_equal(const type& base_other) const noexcept final;
 };
 
 class dynamic_length_string_type : public string_type {
@@ -389,18 +415,24 @@ public:
        dynamic_length_string_type(unsigned int alignment,
                        enum encoding in_encoding,
                        field_location length_field_location);
-       virtual void accept(type_visitor& visitor) const override final;
+
+       type::cuptr copy() const final;
+
+       void accept(type_visitor& visitor) const final;
 
        const field_location length_field_location;
 
 private:
-       virtual bool _is_equal(const type& base_other) const noexcept override final;
+       bool _is_equal(const type& base_other) const noexcept final;
 };
 
 class null_terminated_string_type : public string_type {
 public:
        null_terminated_string_type(unsigned int alignment, enum encoding in_encoding);
-       virtual void accept(type_visitor& visitor) const override final;
+
+       type::cuptr copy() const final;
+
+       void accept(type_visitor& visitor) const final;
 };
 
 class structure_type : public type {
@@ -409,15 +441,17 @@ public:
 
        structure_type(unsigned int alignment, fields in_fields);
 
-       virtual void accept(type_visitor& visitor) const override final;
+       type::cuptr copy() const final;
+
+       void accept(type_visitor& visitor) const final;
 
        const fields fields_;
 
 private:
-       virtual bool _is_equal(const type& base_other) const noexcept override final;
+       bool _is_equal(const type& base_other) const noexcept final;
 };
 
-template <class MappingIntegerType>
+template <typename MappingIntegerType>
 class variant_type : public type {
        static_assert(std::is_same<MappingIntegerType,
                                        unsigned_enumeration_type::mapping::range_t::
@@ -440,11 +474,24 @@ public:
        {
        }
 
-       virtual void accept(type_visitor& visitor) const override final;
+       type::cuptr copy() const final
+       {
+               choices copy_of_choices;
+
+               copy_of_choices.reserve(choices_.size());
+               for (const auto& current_choice : choices_) {
+                       copy_of_choices.emplace_back(
+                                       current_choice.first, current_choice.second->copy());
+               }
+
+               return lttng::make_unique<variant_type<MappingIntegerType>>(
+                       alignment, selector_field_location, std::move(copy_of_choices));
+       }
+
+       void accept(type_visitor& visitor) const final;
 
        const field_location selector_field_location;
        const choices choices_;
-;
 
 private:
        static bool _choices_are_equal(const choices& a, const choices& b)
@@ -453,8 +500,6 @@ private:
                        return false;
                }
 
-               return true;
-
                return std::equal(a.cbegin(), a.cend(), b.cbegin(),
                                [](const choice& choice_a, const choice& choice_b) {
                                        return choice_a.first == choice_b.first &&
@@ -462,7 +507,7 @@ private:
                                });
        }
 
-       virtual bool _is_equal(const type& base_other) const noexcept override final
+       bool _is_equal(const type& base_other) const noexcept final
        {
                const auto& other = static_cast<decltype(*this)&>(base_other);
 
@@ -507,6 +552,8 @@ protected:
 } /* namespace lttng */
 
 /*
+ * Field formatters for libfmt.
+ *
  * Due to a bug in g++ < 7.1, this specialization must be enclosed in the fmt namespace,
  * see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56480.
  */
@@ -548,6 +595,52 @@ struct formatter<lttng::sessiond::trace::field_location> : formatter<std::string
                return format_to(ctx.out(), location_str);
        }
 };
+
+namespace details {
+template <typename MappingIntegerType>
+::std::string format_mapping_range(typename lttng::sessiond::trace::typed_enumeration_type<
+               MappingIntegerType>::mapping::range_t range)
+{
+       if (range.begin == range.end) {
+               return ::fmt::format("[{}]", range.begin);
+       } else {
+               return ::fmt::format("[{}, {}]", range.begin, range.end);
+       }
+}
+} /* namespace details */
+
+template <>
+struct formatter<typename lttng::sessiond::trace::signed_enumeration_type::mapping::range_t>
+       : formatter<std::string> {
+       template <typename FormatCtx>
+       typename FormatCtx::iterator
+       format(typename lttng::sessiond::trace::signed_enumeration_type::mapping::range_t range,
+                       FormatCtx& ctx)
+       {
+               return format_to(ctx.out(),
+                               details::format_mapping_range<
+                                               lttng::sessiond::trace::signed_enumeration_type::
+                                                               mapping::range_t::range_integer_t>(
+                                               range));
+       }
+};
+
+template <>
+struct formatter<typename lttng::sessiond::trace::unsigned_enumeration_type::mapping::range_t>
+       : formatter<std::string> {
+       template <typename FormatCtx>
+       typename FormatCtx::iterator
+       format(typename lttng::sessiond::trace::unsigned_enumeration_type::mapping::range_t range,
+                       FormatCtx& ctx)
+       {
+               return format_to(ctx.out(),
+                               details::format_mapping_range<
+                                               lttng::sessiond::trace::unsigned_enumeration_type::
+                                                               mapping::range_t::range_integer_t>(
+                                               range));
+       }
+};
+
 } /* namespace fmt */
 
 #endif /* LTTNG_FIELD_H */
This page took 0.030834 seconds and 4 git commands to generate.