From: Jérémie Galarneau Date: Tue, 6 Jun 2023 03:39:17 +0000 (-0400) Subject: lttng: reuse random_access_container_wrapper for session_list X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=f74e820c5e8f81d6416f4557bce8b56ee6be4746;p=lttng-tools.git lttng: reuse random_access_container_wrapper for session_list 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 Change-Id: I99160a54d5d2dfa7cf65a6287b271e9c2238c510 --- diff --git a/src/bin/lttng/utils.cpp b/src/bin/lttng/utils.cpp index e65c01753..df03b1127 100644 --- a/src/bin/lttng/utils.cpp +++ b/src/bin/lttng/utils.cpp @@ -668,9 +668,7 @@ template 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(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) { diff --git a/src/bin/lttng/utils.hpp b/src/bin/lttng/utils.hpp index dfbbc25b2..56706af13 100644 --- a/src/bin/lttng/utils.hpp +++ b/src/bin/lttng/utils.hpp @@ -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 - class _iterator : public std::iterator { - 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; - using const_iterator = _iterator; +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::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 { +public: + friend details::session_list_operations; - const_iterator end() const noexcept + session_list() : + lttng::utils::random_access_container_wrapper( + { 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( + { 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( + { 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( + { 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::deleter> - _sessions; }; lttng::cli::session_list list_sessions(const struct session_spec& spec); diff --git a/src/common/container-wrapper.hpp b/src/common/container-wrapper.hpp index 09097e935..bccb23dda 100644 --- a/src/common/container-wrapper.hpp +++ b/src/common/container-wrapper.hpp @@ -24,10 +24,10 @@ namespace utils { */ template class random_access_container_wrapper { + template class _iterator : public std::iterator { 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::value, - ElementType, - ElementType&>::type + typename std::conditional::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; + using const_iterator = _iterator; 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 */