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);
*/
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)
{
}
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) }
{
}
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);
return ContainerOperations::get(_container, index);
}
-private:
+protected:
ContainerType _container;
};
} /* namespace utils */