sessiond: add a CTF 2-generating trace class visitor
[lttng-tools.git] / src / bin / lttng-sessiond / field.hpp
index dec5b103222008200b7adbcb8a019fa2ae3dbd3d..a774d133a0978d0c59e0ce08f3a2865f675e23b4 100644 (file)
@@ -12,6 +12,7 @@
 
 #include <vendor/optional.hpp>
 
+#include <algorithm>
 #include <memory>
 #include <string>
 #include <type_traits>
@@ -77,14 +78,20 @@ private:
 
 class field {
 public:
+       using uptr = std::unique_ptr<field>;
        using cuptr = std::unique_ptr<const field>;
 
        field(std::string name, type::cuptr type);
        void accept(field_visitor& visitor) const;
        bool operator==(const field& other) const noexcept;
 
+       const type& get_type() const;
+       type::cuptr move_type() noexcept;
+
        const std::string name;
-       const type::cuptr _type;
+
+private:
+       type::cuptr _type;
 };
 
 class integer_type : public type {
@@ -198,14 +205,13 @@ class enumeration_mapping {
 public:
        using range_t = enumeration_mapping_range<MappingIntegerType>;
 
-       enumeration_mapping(const enumeration_mapping<MappingIntegerType>& other) = delete;
+       enumeration_mapping(const enumeration_mapping<MappingIntegerType>& other) = default;
        enumeration_mapping(const enumeration_mapping<MappingIntegerType>&& other) :
                name{std::move(other.name)}, range{other.range}
        {
        }
 
-       /* Mapping with an implicit value. */
-       enumeration_mapping(std::string in_name) : name{std::move(in_name)}
+       enumeration_mapping(std::string in_name, MappingIntegerType value) : name{std::move(in_name)}, range{value, value}
        {
        }
 
@@ -214,7 +220,12 @@ public:
        }
 
        const std::string name;
-       const nonstd::optional<range_t> range;
+       /*
+        * Only one range per mapping is supported for the moment as
+        * the tracers (and CTF 1.8) can't express multiple ranges per
+        * mapping, which is allowed by CTF 2.
+        */
+       const range_t range;
 };
 
 template <class MappingIntegerType>
@@ -249,13 +260,13 @@ public:
                                                      integer_type::signedness::UNSIGNED,
                                in_base,
                                std::move(in_roles)),
-               _mappings{std::move(in_mappings)}
+               mappings_{std::move(in_mappings)}
        {
        }
 
        virtual void accept(type_visitor& visitor) const override final;
 
-       const std::shared_ptr<const mappings> _mappings;
+       const std::shared_ptr<const mappings> mappings_;
 
 private:
        virtual bool _is_equal(const type& base_other) const noexcept override final
@@ -263,7 +274,7 @@ private:
                const auto& other = static_cast<const typed_enumeration_type<MappingIntegerType>&>(
                                base_other);
 
-               return integer_type::_is_equal(base_other) && *this->_mappings == *other._mappings;
+               return integer_type::_is_equal(base_other) && *this->mappings_ == *other.mappings_;
        }
 };
 
@@ -313,7 +324,7 @@ class static_length_blob_type : public type {
 public:
        enum class role {
                /* Packet header field class specific role. */
-               TRACE_CLASS_UUID,
+               METADATA_STREAM_UUID,
        };
 
        using roles = std::vector<role>;
@@ -400,28 +411,64 @@ public:
 
        virtual void accept(type_visitor& visitor) const override final;
 
-       const fields _fields;
+       const fields fields_;
 
 private:
        virtual bool _is_equal(const type& base_other) const noexcept override final;
 };
 
+template <class MappingIntegerType>
 class variant_type : public type {
-public:
-       using choices = std::vector<field::cuptr>;
+       static_assert(std::is_same<MappingIntegerType,
+                                       unsigned_enumeration_type::mapping::range_t::
+                                                       range_integer_t>::value ||
+                                       std::is_same<MappingIntegerType,
+                                                       signed_enumeration_type::mapping::range_t::
+                                                                       range_integer_t>::value,
+                       "Variant mapping integer type must be one of those allowed by typed_enumeration_type");
 
-       variant_type(unsigned int alignment,
-                       field_location selector_field_location,
-                       choices in_choices);
+public:
+       using choice = std::pair<const details::enumeration_mapping<MappingIntegerType>, type::cuptr>;
+       using choices = std::vector<choice>;
+
+       variant_type(unsigned int in_alignment,
+                       field_location in_selector_field_location,
+                       choices in_choices) :
+               type(in_alignment),
+               selector_field_location{std::move(in_selector_field_location)},
+               choices_{std::move(in_choices)}
+       {
+       }
 
        virtual void accept(type_visitor& visitor) const override final;
 
        const field_location selector_field_location;
-       const choices _choices;
+       const choices choices_;
 ;
 
 private:
-       virtual bool _is_equal(const type& base_other) const noexcept override final;
+       static bool _choices_are_equal(const choices& a, const choices& b)
+       {
+               if (a.size() != b.size()) {
+                       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 &&
+                                               *choice_a.second == *choice_b.second;
+                               });
+       }
+
+       virtual bool _is_equal(const type& base_other) const noexcept override final
+       {
+               const auto& other = static_cast<decltype(*this)&>(base_other);
+
+               return selector_field_location == other.selector_field_location &&
+                               _choices_are_equal(choices_, other.choices_);
+       }
 };
 
 class field_visitor {
@@ -448,7 +495,8 @@ public:
        virtual void visit(const static_length_string_type& type) = 0;
        virtual void visit(const dynamic_length_string_type& type) = 0;
        virtual void visit(const structure_type& type) = 0;
-       virtual void visit(const variant_type& type) = 0;
+       virtual void visit(const variant_type<signed_enumeration_type::mapping::range_t::range_integer_t>& type) = 0;
+       virtual void visit(const variant_type<unsigned_enumeration_type::mapping::range_t::range_integer_t>& type) = 0;
 
 protected:
        type_visitor() = default;
This page took 0.026057 seconds and 4 git commands to generate.