lttng: reuse random_access_container_wrapper for session_list
authorJérémie Galarneau <jeremie.galarneau@efficios.com>
Tue, 6 Jun 2023 03:39:17 +0000 (23:39 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Tue, 6 Jun 2023 17:49:39 +0000 (13:49 -0400)
Reimplement lttng::cli::session_list in terms of the
random_access_container_wrapper utility since the code is essentially
duplicated.

Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
Change-Id: I99160a54d5d2dfa7cf65a6287b271e9c2238c510

src/bin/lttng/utils.cpp
src/bin/lttng/utils.hpp
src/common/container-wrapper.hpp

index e65c01753e04fec18ccd78a0fac5c6a24b3a316a..df03b1127ed96be5b96614d57389d3c60dc719d8 100644 (file)
@@ -668,9 +668,7 @@ template <typename FilterFunctionType>
 lttng::cli::session_list get_sessions(const FilterFunctionType& filter,
                                      bool return_first_match_only = false)
 {
-       lttng::cli::session_list list;
-
-       {
+       lttng::cli::session_list list = []() {
                int list_ret;
                struct lttng_session *psessions;
 
@@ -681,8 +679,8 @@ lttng::cli::session_list get_sessions(const FilterFunctionType& filter,
                                        static_cast<lttng_error_code>(list_ret));
                }
 
-               list = lttng::cli::session_list(psessions, list_ret);
-       }
+               return lttng::cli::session_list(psessions, list_ret);
+       }();
 
        std::size_t write_to = 0;
        for (std::size_t read_from = 0; read_from < list.size(); ++read_from) {
index dfbbc25b28056e71bbbeab4b7d44c6cbb46679d7..56706af13b4cfadafa273df885bc3ab801d62ecb 100644 (file)
@@ -46,128 +46,93 @@ struct session_spec {
        const char *value;
 };
 
-/*
- * We don't use a std::vector here because it would make a copy of the C array.
- */
-class session_list {
-       template <typename ContainerType, typename DereferenceReturnType>
-       class _iterator : public std::iterator<std::random_access_iterator_tag, std::size_t> {
-       public:
-               explicit _iterator(ContainerType& list, std::size_t k) : _list(list), _index(k)
-               {
-               }
-
-               _iterator& operator++() noexcept
-               {
-                       ++_index;
-                       return *this;
-               }
-
-               _iterator& operator--() noexcept
-               {
-                       --_index;
-                       return *this;
-               }
-
-               _iterator& operator++(int) noexcept
-               {
-                       _index++;
-                       return *this;
-               }
-
-               _iterator& operator--(int) noexcept
-               {
-                       _index--;
-                       return *this;
-               }
-
-               bool operator==(_iterator other) const noexcept
-               {
-                       return _index == other._index;
-               }
-
-               bool operator!=(_iterator other) const noexcept
-               {
-                       return !(*this == other);
-               }
-
-               DereferenceReturnType& operator*() const noexcept
-               {
-                       return _list[_index];
-               }
-
-       private:
-               ContainerType& _list;
-               std::size_t _index;
-       };
-
-       using iterator = _iterator<session_list, lttng_session>;
-       using const_iterator = _iterator<const session_list, const lttng_session>;
+class session_list;
 
+namespace details {
+class session_storage {
 public:
-       session_list() : _sessions_count(0), _sessions(nullptr)
+       session_storage(lttng_session *raw_sessions, std::size_t sessions_count) :
+               _array(raw_sessions), _count(sessions_count)
        {
        }
 
-       session_list(session_list&& original, std::size_t new_count)
+       session_storage(session_storage&& original) :
+               _array(std::move(original._array)), _count(original._count)
        {
-               _sessions_count = new_count;
-               _sessions = std::move(original._sessions);
        }
 
-       session_list(struct lttng_session *raw_sessions, std::size_t raw_sessions_count)
+       session_storage(session_storage&& original, std::size_t new_count) :
+               _array(std::move(original._array)), _count(new_count)
        {
-               _sessions_count = raw_sessions_count;
-               _sessions.reset(raw_sessions);
        }
 
-       iterator begin() noexcept
-       {
-               return iterator(*this, 0);
-       }
+       std::unique_ptr<lttng_session,
+                       lttng::memory::create_deleter_class<lttng_session, lttng::free>::deleter>
+               _array = nullptr;
+       std::size_t _count = 0;
+};
 
-       iterator end() noexcept
+class session_list_operations {
+public:
+       static lttng_session& get(const lttng::cli::details::session_storage& storage,
+                                 std::size_t index) noexcept
        {
-               return iterator(*this, _sessions_count);
+               return storage._array.get()[index];
        }
 
-       const_iterator begin() const noexcept
+       static std::size_t size(const lttng::cli::details::session_storage& storage)
        {
-               return const_iterator(*this, 0);
+               return storage._count;
        }
+};
+} /* namespace details */
+
+/*
+ * We don't use a std::vector here because it would make a copy of the C array.
+ */
+class session_list
+       : public lttng::utils::random_access_container_wrapper<details::session_storage,
+                                                              lttng_session&,
+                                                              details::session_list_operations> {
+public:
+       friend details::session_list_operations;
 
-       const_iterator end() const noexcept
+       session_list() :
+               lttng::utils::random_access_container_wrapper<details::session_storage,
+                                                             lttng_session&,
+                                                             details::session_list_operations>(
+                       { nullptr, 0 })
        {
-               return const_iterator(*this, _sessions_count);
        }
 
-       std::size_t size() const noexcept
+       session_list(session_list&& original) :
+               lttng::utils::random_access_container_wrapper<details::session_storage,
+                                                             lttng_session&,
+                                                             details::session_list_operations>(
+                       { std::move(original._container) })
        {
-               return _sessions_count;
        }
 
-       void resize(std::size_t new_size) noexcept
+       session_list(session_list&& original, std::size_t new_count) :
+               lttng::utils::random_access_container_wrapper<details::session_storage,
+                                                             lttng_session&,
+                                                             details::session_list_operations>(
+                       { std::move(original._container), new_count })
        {
-               _sessions_count = new_size;
        }
 
-       lttng_session& operator[](std::size_t index)
+       session_list(lttng_session *raw_sessions, std::size_t raw_sessions_count) :
+               lttng::utils::random_access_container_wrapper<details::session_storage,
+                                                             lttng_session&,
+                                                             details::session_list_operations>(
+                       { raw_sessions, raw_sessions_count })
        {
-               LTTNG_ASSERT(index < _sessions_count);
-               return _sessions.get()[index];
        }
 
-       const lttng_session& operator[](std::size_t index) const
+       void resize(std::size_t new_size) noexcept
        {
-               LTTNG_ASSERT(index < _sessions_count);
-               return _sessions.get()[index];
+               _container._count = new_size;
        }
-
-private:
-       std::size_t _sessions_count;
-       std::unique_ptr<lttng_session,
-                       lttng::memory::create_deleter_class<lttng_session, lttng::free>::deleter>
-               _sessions;
 };
 
 lttng::cli::session_list list_sessions(const struct session_spec& spec);
index 09097e9358855977837eca2efd86f689f872ea29..bccb23ddaeaf05efc27c51985ddd5a67004ceaa9 100644 (file)
@@ -24,10 +24,10 @@ namespace utils {
  */
 template <typename ContainerType, typename ElementType, typename ContainerOperations>
 class random_access_container_wrapper {
+       template <typename IteratorContainerType, typename IteratorElementType>
        class _iterator : public std::iterator<std::random_access_iterator_tag, std::size_t> {
        public:
-               explicit _iterator(const random_access_container_wrapper& container,
-                                  std::size_t start_index = 0) :
+               explicit _iterator(IteratorContainerType& container, std::size_t start_index = 0) :
                        _container(container), _index(start_index)
                {
                }
@@ -68,23 +68,25 @@ class random_access_container_wrapper {
                        return !(*this == other);
                }
 
-               typename std::conditional<std::is_pointer<ElementType>::value,
-                                         ElementType,
-                                         ElementType&>::type
+               typename std::conditional<std::is_pointer<IteratorElementType>::value,
+                                         IteratorElementType,
+                                         IteratorElementType&>::type
                operator*() const noexcept
                {
                        return _container[_index];
                }
 
        private:
-               const random_access_container_wrapper& _container;
+               IteratorContainerType& _container;
                std::size_t _index;
        };
 
-       using iterator = _iterator;
+       using iterator = _iterator<random_access_container_wrapper, ElementType>;
+       using const_iterator = _iterator<const random_access_container_wrapper, const ElementType>;
 
 public:
-       explicit random_access_container_wrapper(ContainerType container) : _container{ container }
+       explicit random_access_container_wrapper(ContainerType container) :
+               _container{ std::move(container) }
        {
        }
 
@@ -98,6 +100,16 @@ public:
                return iterator(*this, ContainerOperations::size(_container));
        }
 
+       const_iterator begin() const noexcept
+       {
+               return const_iterator(*this);
+       }
+
+       const_iterator end() const noexcept
+       {
+               return const_iterator(*this, ContainerOperations::size(_container));
+       }
+
        std::size_t size() const noexcept
        {
                return ContainerOperations::size(_container);
@@ -119,7 +131,7 @@ public:
                return ContainerOperations::get(_container, index);
        }
 
-private:
+protected:
        ContainerType _container;
 };
 } /* namespace utils */
This page took 0.035632 seconds and 4 git commands to generate.