SimpleWindow
载入中...
搜索中...
未找到
ObservableCollection.h
1#pragma once
2
3#include "INotifyCollectionChanged.h"
4#include "List.h"
5#include "ObservableObject.h"
6#include <initializer_list>
7#include <stdexcept>
8
9namespace sw
10{
15 template <typename T>
17 public IListT<T>,
19 public IToString<ObservableCollection<T>>
20 {
21 private:
25 List<T> _items;
26
30 NotifyCollectionChangedEventHandler _collectionChanged;
31
32 public:
37
38 // 删除拷贝构造函数
40
41 // 删除移动构造函数
43
44 // 删除拷贝赋值运算符
45 ObservableCollection<T> &operator=(const ObservableCollection<T> &) = delete;
46
47 // 删除移动赋值运算符
49
54 ObservableCollection(std::initializer_list<T> list)
55 : _items(list)
56 {
57 }
58
64 : _items(capacity)
65 {
66 }
67
68 protected:
74 {
75 return _collectionChanged;
76 }
77
83 {
84 if (_collectionChanged) {
85 _collectionChanged(*this, args);
86 }
87 }
88
89 public:
95 {
96 return _items.Capacity();
97 }
98
104 {
105 _items.Reserve(newCapacity);
106 }
107
118
122 void Clear()
123 {
124 if (_items.Count() == 0) {
125 return;
126 }
127
128 _items.Clear();
129
132 args.list = this;
134 }
135
140 void Add(const T &value)
141 {
142 int index = _items.Count();
143 _items.Add(value);
144
147 args.list = this;
148 args.index = index;
150 }
151
156 void Add(T &&value)
157 {
158 int index = _items.Count();
159 _items.Add(std::move(value));
160
163 args.list = this;
164 args.index = index;
166 }
167
173 void RemoveAt(int index)
174 {
175 _items.RemoveAt(index);
176
179 args.list = this;
180 args.index = index;
182 }
183
190 void Insert(int index, const T &value)
191 {
192 _items.Insert(index, value);
193
196 args.list = this;
197 args.index = index;
199 }
200
207 void Insert(int index, T &&value)
208 {
209 _items.Insert(index, std::move(value));
210
213 args.list = this;
214 args.index = index;
216 }
217
224 void Move(int oldIndex, int newIndex)
225 {
226 int count = _items.Count();
228 throw std::out_of_range("Index out of range in ObservableCollection::Move.");
229 }
230
231 if (oldIndex == newIndex) {
232 return;
233 }
234
235 auto &items = _items.GetInternalVector();
236
237 T value = std::move(items[static_cast<size_t>(oldIndex)]);
238 items.erase(items.begin() + static_cast<size_t>(oldIndex));
239 items.insert(items.begin() + static_cast<size_t>(newIndex), std::move(value));
240
243 args.list = this;
244 args.index = newIndex;
245 args.oldIndex = oldIndex;
247 }
248
254 int IndexOf(const T &value) const
255 {
256 return _items.IndexOf(value);
257 }
258
264 int LastIndexOf(const T &value) const
265 {
266 return _items.LastIndexOf(value);
267 }
268
274 bool Contains(const T &value) const
275 {
276 return _items.Contains(value);
277 }
278
284 bool Remove(const T &value)
285 {
286 int index = _items.IndexOf(value);
287 if (index == -1) {
288 return false;
289 }
290
291 _items.RemoveAt(index);
292
295 args.list = this;
296 args.index = index;
298
299 return true;
300 }
301
306 std::wstring ToString() const
307 {
308 return _items.ToString();
309 }
310
315 std::vector<T> &GetInternalVector() noexcept
316 {
317 return _items.GetInternalVector();
318 }
319
324 const std::vector<T> &GetInternalVector() const noexcept
325 {
326 return _items.GetInternalVector();
327 }
328
329 public:
335 {
336 return _items.Count();
337 }
338
345 virtual T &GetAt(int index) override final
346 {
347 return _items.GetAt(index);
348 }
349
356 virtual const T &GetAt(int index) const override final
357 {
358 return _items.GetAt(index);
359 }
360
368 virtual void SetAt(int index, const T &value) override final
369 {
370 _items.SetAt(index, value);
371
374 args.list = this;
375 args.index = index;
377 }
378
386 virtual void SetAt(int index, T &&value) override final
387 {
388 _items.SetAt(index, std::move(value));
389
392 args.list = this;
393 args.index = index;
395 }
396 };
397}
类型安全的列表接口,继承IList并提供类型化的元素访问
Definition IList.h:71
支持集合变更通知的接口
Definition INotifyCollectionChanged.h:61
为支持ToString方法的类提供统一接口
Definition IToString.h:13
值转换器接口
Definition IValueConverter.h:14
集合变更事件参数类型
Definition INotifyCollectionChanged.h:33
支持集合变更通知的泛型集合类
Definition ObservableCollection.h:20
virtual void SetAt(int index, const T &value) override final
设置指定索引处的元素值
Definition ObservableCollection.h:368
bool Contains(const T &value) const
判断集合是否包含指定值
Definition ObservableCollection.h:274
int IndexOf(const T &value) const
查找指定值在集合中首次出现的索引
Definition ObservableCollection.h:254
int LastIndexOf(const T &value) const
查找指定值在集合中最后出现的索引
Definition ObservableCollection.h:264
void Move(int oldIndex, int newIndex)
将元素从一个索引移动到另一个索引,并触发移动通知
Definition ObservableCollection.h:224
virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs &args)
触发集合变更事件
Definition ObservableCollection.h:82
void Clear()
清空集合中的所有元素,并触发集合重置通知
Definition ObservableCollection.h:122
void Add(const T &value)
在集合末尾追加元素,并触发添加通知
Definition ObservableCollection.h:140
std::wstring ToString() const
将集合转换为字符串表示
Definition ObservableCollection.h:306
const std::vector< T > & GetInternalVector() const noexcept
获取底层std::vector的const引用
Definition ObservableCollection.h:324
virtual const T & GetAt(int index) const override final
获取指定索引处的const元素引用
Definition ObservableCollection.h:356
int Capacity() const noexcept
获取当前分配的容量
Definition ObservableCollection.h:94
void Insert(int index, T &&value)
在指定索引处插入元素(移动语义),并触发添加通知
Definition ObservableCollection.h:207
virtual void SetAt(int index, T &&value) override final
设置指定索引处的元素值(移动语义)
Definition ObservableCollection.h:386
virtual NotifyCollectionChangedEventHandler & GetCollectionChangedEventDelegate() override final
获取集合变更事件委托的引用
Definition ObservableCollection.h:73
ObservableCollection(int capacity)
指定初始容量构造
Definition ObservableCollection.h:63
void Insert(int index, const T &value)
在指定索引处插入元素,并触发添加通知
Definition ObservableCollection.h:190
bool Remove(const T &value)
移除集合中首次出现的指定值,并在成功移除时触发移除通知
Definition ObservableCollection.h:284
virtual T & GetAt(int index) override final
获取指定索引处的元素引用
Definition ObservableCollection.h:345
void Add(T &&value)
在集合末尾追加元素(移动语义),并触发添加通知
Definition ObservableCollection.h:156
virtual int Count() const noexcept override final
返回列表中的元素数量
Definition ObservableCollection.h:334
void Refresh()
刷新集合,触发集合重置通知
Definition ObservableCollection.h:111
void Reserve(int newCapacity)
预留至少指定数量的元素空间
Definition ObservableCollection.h:103
void RemoveAt(int index)
移除指定索引处的元素,并触发移除通知
Definition ObservableCollection.h:173
ObservableCollection(std::initializer_list< T > list)
使用初始化列表构造
Definition ObservableCollection.h:54
ObservableCollection()=default
默认构造函数,创建空集合
std::vector< T > & GetInternalVector() noexcept
获取底层std::vector的引用
Definition ObservableCollection.h:315
可观察对象基类,实现属性更改通知功能
Definition ObservableObject.h:14
SimpleWindow框架的顶级命名空间,所有公共类型、控件、枚举和工具函数均定义于此。
Definition Alignment.h:4