DAWorkbench 0.0.1
DAWorkbench API
载入中...
搜索中...
未找到
DAGenericIndexedContainer.hpp
1#ifndef DAGENERICINDEXEDCONTAINER_H
2#define DAGENERICINDEXEDCONTAINER_H
3
4#include <type_traits>
5#include <utility>
6#include <stdexcept>
7namespace DA
8{
9
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 >() ]) > >
57{
58public:
60 using value_type = typename Container::value_type;
62 using reference = typename Container::reference;
64 using const_reference = typename Container::const_reference;
66 using size_type = typename Container::size_type;
69 using iterator = typename Container::iterator;
70 using const_iterator = typename Container::const_iterator;
71 using reverse_iterator = typename Container::reverse_iterator;
72 using const_reverse_iterator = typename Container::const_reverse_iterator;
74
82 enum class IndexPolicy
83 {
102 AutoReset,
103
122 PreserveRaw,
123
139 ForceReset
140 };
141
142public:
145
151
166 template< typename... Args >
167 explicit DAGenericIndexedContainer(Args&&... args) : m_container(std::forward< Args >(args)...)
168 {
169 }
170
181 DAGenericIndexedContainer(std::initializer_list< value_type > init) : m_container(init)
182 {
183 }
185
188
195 {
196 moveToNext();
197 return current();
198 }
199
213 {
214 if (empty())
215 return;
216 m_index = (m_index + 1) % static_cast< IndexType >(size());
217 }
218
225 {
226 moveToPrevious();
227 return current();
228 }
229
243 {
244 if (empty())
245 return;
246 m_index = (m_index == 0) ? static_cast< IndexType >(size() - 1) : m_index - 1;
247 }
248
262 {
263 return m_container[ static_cast< size_type >(m_index) ];
264 }
265
272 {
273 return m_container[ static_cast< size_type >(m_index) ];
274 }
276
279
291 {
292 moveToNext();
293 return current();
294 }
295
308 {
309 value_type tmp = current();
310 moveToNext();
311 return tmp;
312 }
313
325 {
326 moveToPrevious();
327 return current();
328 }
329
342 {
343 value_type tmp = current();
344 moveToPrevious();
345 return tmp;
346 }
348
351
357 bool empty() const noexcept
358 {
359 return m_container.empty();
360 }
361
366 size_type size() const noexcept
367 {
368 return m_container.size();
369 }
370
375 void clear()
376 {
377 m_container.clear();
378 m_index = 0;
379 }
380
392 template< typename... Args >
393 void emplace_back(Args&&... args)
394 {
395 m_container.emplace_back(std::forward< Args >(args)...);
396 }
398
401
414 {
415 return m_container.front();
416 }
417
423 {
424 return m_container.front();
425 }
426
439 {
440 return m_container.back();
441 }
442
448 {
449 return m_container.back();
450 }
451
467 {
468 if (empty()) {
469 throw std::out_of_range("Accessing first element of empty container");
470 }
471 return m_container.front();
472 }
473
479 {
480 if (empty()) {
481 throw std::out_of_range("Accessing first element of empty container");
482 }
483 return m_container.front();
484 }
485
492 {
493 if (empty()) {
494 throw std::out_of_range("Accessing last element of empty container");
495 }
496 return m_container.back();
497 }
498
504 {
505 if (empty()) {
506 throw std::out_of_range("Accessing last element of empty container");
507 }
508 return m_container.back();
509 }
510
512
515
521 IndexType currentIndex() const noexcept
522 {
523 return m_index;
524 }
525
539 void setCurrentIndex(IndexType index) noexcept
540 {
541 m_index = index;
542 }
543
556 bool isValidIndex() const noexcept
557 {
558 return m_index >= 0 && static_cast< size_type >(m_index) < size();
559 }
560
565 bool isFirstIndex() const
566 {
567 return m_index == 0 || m_container.empty();
568 }
569
574 bool isLastIndex() const
575 {
576 return (!m_container.empty()) && (m_index == m_container.size() - 1);
577 }
578
580
583
607 DAGenericIndexedContainer& operator=(const Container& other)
608 {
609
610 // 拷贝前保存原索引
611 const size_type oldIndex = static_cast< size_type >(m_index);
612
613 // 执行容器拷贝
614 m_container = other;
615
616 // 调整索引
617 if (!m_container.empty()) {
618 // 新容器非空时调整索引
619 if (oldIndex < m_container.size()) {
620 m_index = static_cast< IndexType >(oldIndex);
621 } else {
622 m_index = 0;
623 }
624 } else {
625 // 新容器为空时保持索引0(无效状态)
626 m_index = 0;
627 }
628
629 return *this;
630 }
631
645 DAGenericIndexedContainer& operator=(Container&& other) noexcept
646 {
647 // 移动前保存原索引
648 const size_type oldIndex = static_cast< size_type >(m_index);
649
650 // 执行容器移动
651 m_container = std::move(other);
652
653 // 调整索引(逻辑同拷贝版本)
654 if (!m_container.empty()) {
655 m_index = (oldIndex < m_container.size()) ? static_cast< IndexType >(oldIndex) : 0;
656 } else {
657 m_index = 0;
658 }
659 return *this;
660 }
661
663
666
682 void replace(Container newContainer, IndexPolicy policy = IndexPolicy::AutoReset)
683 {
684 const size_type oldIndex = static_cast< size_type >(m_index);
685
686 m_container = std::move(newContainer);
687
688 switch (policy) {
689 case IndexPolicy::AutoReset:
690 if (!m_container.empty()) {
691 m_index = (oldIndex < m_container.size()) ? static_cast< IndexType >(oldIndex) : 0;
692 } else {
693 m_index = 0;
694 }
695 break;
696 case IndexPolicy::PreserveRaw:
697 // 不调整,可能无效
698 break;
699 case IndexPolicy::ForceReset:
700 m_index = 0;
701 break;
702 }
703 }
705
708
728 reference operator[](IndexType index)
729 {
730 return m_container[ static_cast< size_type >(index) ];
731 }
732
739 const_reference operator[](IndexType index) const
740 {
741 return m_container[ static_cast< size_type >(index) ];
742 }
743
745
748
764 reference at(IndexType index)
765 {
766 const size_type idx = static_cast< size_type >(index);
767 if (idx >= size()) {
768 throw std::out_of_range("Index out of range");
769 }
770 return m_container[ idx ];
771 }
772
777 const_reference at(IndexType index) const
778 {
779 const size_type idx = static_cast< size_type >(index);
780 if (idx >= size()) {
781 throw std::out_of_range("Index out of range");
782 }
783 return m_container[ idx ];
784 }
786
789
800 iterator begin() noexcept
801 {
802 return m_container.begin();
803 }
804
809 iterator end() noexcept
810 {
811 return m_container.end();
812 }
813
818 const_iterator begin() const noexcept
819 {
820 return m_container.begin();
821 }
822
827 const_iterator end() const noexcept
828 {
829 return m_container.end();
830 }
831
835 const_iterator cbegin() const noexcept
836 {
837 return m_container.cbegin();
838 }
839
843 const_iterator cend() const noexcept
844 {
845 return m_container.cend();
846 }
847
859 {
860 return m_container.rbegin();
861 }
862
867 {
868 return m_container.rend();
869 }
870
875 {
876 return m_container.crbegin();
877 }
878
883 {
884 return m_container.crend();
885 }
887
888 const Container& container() const
889 {
890 return m_container;
891 }
892
893private:
894 Container m_container;
895 IndexType m_index = 0;
896};
897
898} // namespace DA
899
900#endif // DAGENERICINDEXEDCONTAINER_H
通用索引容器模板类,为底层容器提供循环索引功能
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