1#ifndef DAGENERICINDEXEDCONTAINER_H
2#define DAGENERICINDEXEDCONTAINER_H
50template<
typename Container,
51 typename IndexType = int,
52 typename SFINAE_CHECK =
53 std::void_t<
typename Container::size_type,
54 typename Container::value_type,
55 decltype(std::declval< Container >()[ std::declval< typename Container::size_type >() ]) > >
166 template<
typename... Args >
216 m_index = (m_index + 1) %
static_cast< IndexType
>(size());
246 m_index = (m_index == 0) ?
static_cast< IndexType
>(size() - 1) : m_index - 1;
263 return m_container[
static_cast< size_type >(m_index) ];
273 return m_container[
static_cast< size_type >(m_index) ];
359 return m_container.empty();
368 return m_container.size();
392 template<
typename... Args >
395 m_container.emplace_back(std::forward< Args >(args)...);
415 return m_container.front();
424 return m_container.front();
440 return m_container.back();
449 return m_container.back();
469 throw std::out_of_range(
"Accessing first element of empty container");
471 return m_container.front();
481 throw std::out_of_range(
"Accessing first element of empty container");
483 return m_container.front();
494 throw std::out_of_range(
"Accessing last element of empty container");
496 return m_container.back();
506 throw std::out_of_range(
"Accessing last element of empty container");
508 return m_container.back();
558 return m_index >= 0 &&
static_cast< size_type >(m_index) < size();
567 return m_index == 0 || m_container.empty();
576 return (!m_container.empty()) && (m_index == m_container.size() - 1);
617 if (!m_container.empty()) {
619 if (oldIndex < m_container.size()) {
620 m_index =
static_cast< IndexType
>(oldIndex);
651 m_container = std::move(other);
654 if (!m_container.empty()) {
655 m_index = (oldIndex < m_container.size()) ?
static_cast< IndexType
>(oldIndex) : 0;
686 m_container = std::move(newContainer);
689 case IndexPolicy::AutoReset:
690 if (!m_container.empty()) {
691 m_index = (oldIndex < m_container.size()) ?
static_cast< IndexType
>(oldIndex) : 0;
696 case IndexPolicy::PreserveRaw:
699 case IndexPolicy::ForceReset:
730 return m_container[
static_cast< size_type >(index) ];
741 return m_container[
static_cast< size_type >(index) ];
768 throw std::out_of_range(
"Index out of range");
770 return m_container[ idx ];
781 throw std::out_of_range(
"Index out of range");
783 return m_container[ idx ];
802 return m_container.begin();
811 return m_container.end();
820 return m_container.begin();
829 return m_container.end();
837 return m_container.cbegin();
845 return m_container.cend();
860 return m_container.rbegin();
868 return m_container.rend();
876 return m_container.crbegin();
884 return m_container.crend();
888 const Container& container()
const
894 Container m_container;
895 IndexType m_index = 0;
通用索引容器模板类,为底层容器提供循环索引功能
Definition DAGenericIndexedContainer.hpp:57
const_reference lastChecked() const
安全获取尾元素(常量版本)
Definition DAGenericIndexedContainer.hpp:503
reference current()
获取当前元素(非常量)
Definition DAGenericIndexedContainer.hpp:261
reference at(IndexType index)
带边界检查的元素访问
Definition DAGenericIndexedContainer.hpp:764
DAGenericIndexedContainer(std::initializer_list< value_type > init)
初始化列表构造
Definition DAGenericIndexedContainer.hpp:181
const_reverse_iterator crbegin() const noexcept
获取常量反向起始迭代器
Definition DAGenericIndexedContainer.hpp:874
bool isFirstIndex() const
检查是否是第一个索引
Definition DAGenericIndexedContainer.hpp:565
DAGenericIndexedContainer()=default
默认构造空容器
const_reference first() const
获取首元素(常量版本)
Definition DAGenericIndexedContainer.hpp:422
const_reference firstChecked() const
安全获取首元素(常量版本)
Definition DAGenericIndexedContainer.hpp:478
bool isLastIndex() const
检查是否是最后一个索引
Definition DAGenericIndexedContainer.hpp:574
reference lastChecked()
安全获取尾元素(带边界检查)
Definition DAGenericIndexedContainer.hpp:491
const_reference last() const
获取尾元素(常量版本)
Definition DAGenericIndexedContainer.hpp:447
reference firstChecked()
安全获取首元素(带边界检查)
Definition DAGenericIndexedContainer.hpp:466
typename Container::value_type value_type
容器元素类型
Definition DAGenericIndexedContainer.hpp:60
void replace(Container newContainer, IndexPolicy policy=IndexPolicy::AutoReset)
替换容器内容并智能调整索引
Definition DAGenericIndexedContainer.hpp:682
typename Container::const_iterator const_iterator
常量迭代器类型
Definition DAGenericIndexedContainer.hpp:70
iterator begin() noexcept
获取指向容器首元素的迭代器
Definition DAGenericIndexedContainer.hpp:800
iterator end() noexcept
获取指向容器末尾的迭代器
Definition DAGenericIndexedContainer.hpp:809
const_iterator end() const noexcept
获取常量结束迭代器
Definition DAGenericIndexedContainer.hpp:827
void emplace_back(Args &&... args)
向容器末尾原位构造元素
Definition DAGenericIndexedContainer.hpp:393
const_reference at(IndexType index) const
带边界检查的常量元素访问
Definition DAGenericIndexedContainer.hpp:777
reference operator[](IndexType index)
下标访问操作符(非const版本)
Definition DAGenericIndexedContainer.hpp:728
const_reverse_iterator crend() const noexcept
获取常量反向结束迭代器
Definition DAGenericIndexedContainer.hpp:882
value_type operator--()
前缀递减(–obj)
Definition DAGenericIndexedContainer.hpp:324
typename Container::reverse_iterator reverse_iterator
反向迭代器类型
Definition DAGenericIndexedContainer.hpp:71
value_type previous()
移动后获取上一个元素
Definition DAGenericIndexedContainer.hpp:224
reference last()
获取尾元素(非常量版本)
Definition DAGenericIndexedContainer.hpp:438
const_iterator cbegin() const noexcept
获取常量起始迭代器(C++11风格)
Definition DAGenericIndexedContainer.hpp:835
const_iterator cend() const noexcept
获取常量结束迭代器(C++11风格)
Definition DAGenericIndexedContainer.hpp:843
typename Container::const_reference const_reference
常量元素引用类型
Definition DAGenericIndexedContainer.hpp:64
bool isValidIndex() const noexcept
检查当前索引是否有效
Definition DAGenericIndexedContainer.hpp:556
bool empty() const noexcept
检查容器是否为空
Definition DAGenericIndexedContainer.hpp:357
size_type size() const noexcept
获取元素数量
Definition DAGenericIndexedContainer.hpp:366
DAGenericIndexedContainer & operator=(const Container &other)
拷贝赋值底层容器
Definition DAGenericIndexedContainer.hpp:607
IndexPolicy
容器替换时的索引调整策略
Definition DAGenericIndexedContainer.hpp:83
IndexType currentIndex() const noexcept
获取当前索引值
Definition DAGenericIndexedContainer.hpp:521
typename Container::const_reverse_iterator const_reverse_iterator
常量反向迭代器类型
Definition DAGenericIndexedContainer.hpp:72
typename Container::reference reference
元素引用类型
Definition DAGenericIndexedContainer.hpp:62
DAGenericIndexedContainer & operator=(Container &&other) noexcept
移动赋值底层容器
Definition DAGenericIndexedContainer.hpp:645
reverse_iterator rend() noexcept
获取反向结束迭代器
Definition DAGenericIndexedContainer.hpp:866
void moveToNext()
移动到下一个位置(循环)
Definition DAGenericIndexedContainer.hpp:212
typename Container::size_type size_type
容器大小类型
Definition DAGenericIndexedContainer.hpp:66
value_type operator++(int)
后缀递增(obj++)
Definition DAGenericIndexedContainer.hpp:307
value_type operator++()
前缀递增(++obj)
Definition DAGenericIndexedContainer.hpp:290
const_iterator begin() const noexcept
获取常量起始迭代器
Definition DAGenericIndexedContainer.hpp:818
DAGenericIndexedContainer(Args &&... args)
完美转发构造底层容器
Definition DAGenericIndexedContainer.hpp:167
value_type operator--(int)
后缀递减(obj–)
Definition DAGenericIndexedContainer.hpp:341
void moveToPrevious()
移动到上一个位置(循环)
Definition DAGenericIndexedContainer.hpp:242
const_reference operator[](IndexType index) const
下标访问操作符(const版本)
Definition DAGenericIndexedContainer.hpp:739
reference first()
获取首元素(非常量版本)
Definition DAGenericIndexedContainer.hpp:413
void clear()
清空容器并重置索引
Definition DAGenericIndexedContainer.hpp:375
typename Container::iterator iterator
非常量迭代器类型
Definition DAGenericIndexedContainer.hpp:69
value_type next()
移动后获取下一个元素
Definition DAGenericIndexedContainer.hpp:194
reverse_iterator rbegin() noexcept
获取反向起始迭代器
Definition DAGenericIndexedContainer.hpp:858
void setCurrentIndex(IndexType index) noexcept
设置当前索引
Definition DAGenericIndexedContainer.hpp:539
const_reference current() const
获取当前元素(常量)
Definition DAGenericIndexedContainer.hpp:271
序列化类都是带异常的,使用中需要处理异常
Definition AppMainWindow.cpp:44