lttng: move session_list to the lttng::cli namespace
[lttng-tools.git] / src / bin / lttng / utils.hpp
index 64605575e94c33da7c3fdb0f2e4d52bf67a817b0..dfbbc25b28056e71bbbeab4b7d44c6cbb46679d7 100644 (file)
@@ -9,10 +9,14 @@
 #define _LTTNG_UTILS_H
 
 #include <common/argpar/argpar.h>
+#include <common/container-wrapper.hpp>
 #include <common/dynamic-array.hpp>
+#include <common/make-unique-wrapper.hpp>
 
 #include <lttng/lttng.h>
 
+#include <iterator>
+#include <memory>
 #include <popt.h>
 
 extern char *opt_relayd_path;
@@ -22,6 +26,154 @@ extern pid_t sessiond_pid;
 
 struct cmd_struct;
 
+namespace lttng {
+namespace cli {
+
+struct session_spec {
+       enum class type {
+               NAME,
+               GLOB_PATTERN,
+               ALL,
+       };
+
+       explicit session_spec(type spec_type, const char *name_or_pattern = nullptr) noexcept :
+               type_(spec_type), value(name_or_pattern)
+       {
+       }
+
+       /* Disambiguate type enum from the member for buggy g++ versions. */
+       type type_;
+       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>;
+
+public:
+       session_list() : _sessions_count(0), _sessions(nullptr)
+       {
+       }
+
+       session_list(session_list&& original, std::size_t new_count)
+       {
+               _sessions_count = new_count;
+               _sessions = std::move(original._sessions);
+       }
+
+       session_list(struct lttng_session *raw_sessions, std::size_t raw_sessions_count)
+       {
+               _sessions_count = raw_sessions_count;
+               _sessions.reset(raw_sessions);
+       }
+
+       iterator begin() noexcept
+       {
+               return iterator(*this, 0);
+       }
+
+       iterator end() noexcept
+       {
+               return iterator(*this, _sessions_count);
+       }
+
+       const_iterator begin() const noexcept
+       {
+               return const_iterator(*this, 0);
+       }
+
+       const_iterator end() const noexcept
+       {
+               return const_iterator(*this, _sessions_count);
+       }
+
+       std::size_t size() const noexcept
+       {
+               return _sessions_count;
+       }
+
+       void resize(std::size_t new_size) noexcept
+       {
+               _sessions_count = new_size;
+       }
+
+       lttng_session& operator[](std::size_t index)
+       {
+               LTTNG_ASSERT(index < _sessions_count);
+               return _sessions.get()[index];
+       }
+
+       const lttng_session& operator[](std::size_t index) const
+       {
+               LTTNG_ASSERT(index < _sessions_count);
+               return _sessions.get()[index];
+       }
+
+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);
+} /* namespace cli */
+} /* namespace lttng */
+
 char *get_session_name(void);
 char *get_session_name_quiet(void);
 void list_commands(struct cmd_struct *commands, FILE *ofp);
This page took 0.02475 seconds and 4 git commands to generate.