YSTest  PreAlpha_b500_20140530
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
viewer.hpp
浏览该文件的文档.
1 /*
2  © 2011-2014 FrankHB.
3 
4  This file is part of the YSLib project, and may only be used,
5  modified, and distributed under the terms of the YSLib project
6  license, LICENSE.TXT. By continuing to use, modify, or distribute
7  this file you indicate that you have read the license and
8  understand and accept it fully.
9 */
10 
28 #ifndef YSL_INC_UI_viewer_hpp_
29 #define YSL_INC_UI_viewer_hpp_ 1
30 
31 #include "YModules.h"
32 #include YFM_YSLib_UI_YComponent
33 #include <ystdex/algorithm.hpp>
34 
35 namespace YSLib
36 {
37 
38 namespace UI
39 {
40 
45 template<class _tCon>
47 {
48 public:
53  using ContainerType = _tCon;
54  using SizeType = typename _tCon::size_type;
55  using DifferenceType = typename _tCon::difference_type; \
57 
59  static_assert(std::is_unsigned<SizeType>::value,
60  "Invalid size type found!");
62  static_assert(std::is_signed<DifferenceType>::value,
63  "Invalid difference type found!");
64 
65 private:
71  _tCon* p_con;
75  bool is_selected;
76 
77 public:
83  explicit
85  : p_con(&con), head(0), selected(0), length(1), is_selected(false)
86  {}
87 
88  PDefHOp(GSequenceViewer&, ++, )
89  ImplRet(IncreaseSelected(1))
90  PDefHOp(GSequenceViewer&, --, )
91  ImplRet(IncreaseSelected(-1))
92  PDefHOp(GSequenceViewer&, ++, int) \
94  ImplRet(IncreaseHead(1))
95  PDefHOp(GSequenceViewer&, --, int) \
97  ImplRet(IncreaseHead(-1))
98 
103 
107  bool
109  {
110  return GetTotal() != 0 && GetLength() != 0
111  && IsInInterval<SizeType>(i - GetHeadIndex(), GetLength());
112  }
113 
114  DefGetter(const ynothrow, SizeType, Total, p_con->size()) \
117  DefGetter(const ynothrow, SizeType, HeadIndex, head)
119  DefGetter(const ynothrow, DifferenceType, Offset, IsSelected()
120  ? GetSelectedIndex() - GetHeadIndex() : -1) \
122  DefGetter(const ynothrow, SizeType, Valid, min(GetTotal() - GetHeadIndex(),
123  GetLength()))
124 
130  void
131  SetContainer(ContainerType& con)
132  {
133  if(YB_LIKELY(p_con != &con))
134  yunseq(p_con = &con, selected = 0, head = 0, length = 1);
135  }
139  bool
141  {
142  if(t < GetTotal() && t != head)
143  {
144  if(t == 0)
145  MoveViewerToBegin();
146  else if(length + t > GetTotal())
147  MoveViewerToEnd();
148  else
149  head = t;
150  return true;
151  }
152  return false;
153  }
157  bool
159  {
160  if(l != length)
161  {
162  length = l;
163  return true;
164  }
165  return false;
166  }
170  bool
172  {
173  if(t < GetTotal() && !(t == selected && is_selected))
174  {
175  selected = t;
176  RestrictView();
177  is_selected = true;
178  return true;
179  }
180  return false;
181  }
182 
190  bool
192  {
193  const auto total(GetTotal());
194 
195  if(total != 0)
196  {
197  if(!(selected < total))
198  selected = total - 1;
199  return RestrictView();
200  }
201  else
202  Reset();
203  return true;
204  }
205 
209  PDefH(void, ClearSelected, )
210  ImplExpr(is_selected = {})
211 
212  inline PDefH(GSequenceViewer&, DecreaseHead, DifferenceType d) \
214  ImplRet(IncreaseHead(-d))
215 
216  inline PDefH(GSequenceViewer&, DecreaseSelected, DifferenceType d) \
218  ImplRet(IncreaseSelected(-d))
219 
224  GSequenceViewer&
225  IncreaseHead(DifferenceType d)
226  {
227  int t(head + d);
228 
229  RestrictInInterval(t, 0, int(GetTotal()));
230  SetHeadIndex(t);
231  return *this;
232  }
233 
238  GSequenceViewer&
239  IncreaseSelected(DifferenceType d)
240  {
241  int t(selected + d);
242 
243  RestrictInInterval(t, 0, int(GetTotal()));
244  SetSelectedIndex(t);
245  return *this;
246  }
247 
251  bool
252  MoveViewerToBegin()
253  {
254  if(head)
255  {
256  head = 0;
257  return true;
258  }
259  return false;
260  }
261 
265  bool
266  MoveViewerToEnd()
267  {
268  if(GetTotal() < length)
269  return false;
270  head = GetTotal() - length;
271  return true;
272  }
273 
278  PDefH(void, Reset, )
279  ImplUnseq(head = 0, selected = 0, length = 1, is_selected = {})
280 
287  bool
288  RestrictSelected()
289  {
290  if(GetTotal() == 0)
291  return false;
292  if(selected < head)
293  selected = head;
294  else if(selected < head + length)
295  return false;
296  else
297  selected = head + length - 1;
298  return true;
299  }
300 
307  bool
308  RestrictView()
309  {
310  if(GetTotal() == 0)
311  return false;
312  if(selected < head)
313  head = selected;
314  else if(selected < head + length)
315  return false;
316  else
317  head = selected + 1 - length;
318  return true;
319  }
320 };
321 
322 } // namespace UI;
323 
324 } // namespace YSLib;
325 
326 #endif
327 
typename _tCon::difference_type DifferenceType
项目索引差值类型。
Definition: viewer.hpp:56
#define ImplUnseq(...)
Definition: YBaseMacro.h:107
#define DefPred(_q, _n,...)
Definition: YBaseMacro.h:172
PDefHOp(GSequenceViewer &,++,) ImplRet(IncreaseSelected(1)) PDefHOp(GSequenceViewer &
ImplRet(IncreaseSelected(-1)) PDefHOp(GSequenceViewer &
< 选中项目的索引自减。
bool SetSelectedIndex(SizeType t)
设置选中项目的索引。
Definition: viewer.hpp:171
bool SetHeadIndex(SizeType t)
设置视图中首个项目的索引。
Definition: viewer.hpp:140
SizeType head
视图中首个项目的索引,大于等于 GetTotal() 时无效。
Definition: viewer.hpp:72
length selected min(GetTotal()-GetHeadIndex(), GetLength())) void SetContainer(ContainerType &con)
< 取当前视图中有效项目个数。
Definition: viewer.hpp:122
PDefH(void, Activate, Console &console, Drawing::Color fc=Drawing::ColorSpace::White) ImplExpr(Activate(console
激活:使用指定屏幕、有效性、前景色和默认背景色。
序列视图类模板。
Definition: viewer.hpp:46
#define yunseq
无序列依赖表达式组求值。
Definition: ydef.h:748
#define ImplExpr(...)
Definition: YBaseMacro.h:93
#define ynothrow
YSLib 无异常抛出保证:若支持 noexcept 关键字, 指定特定的 noexcept 异常规范。
Definition: ydef.h:514
SizeType selected
选中项目的索引,大于等于 GetTotal() 时无效。
Definition: viewer.hpp:73
_tCon * p_con
序列容器指针。
Definition: viewer.hpp:60
bool AdjustForContent()
按序列内容大小依次调整选中和首个项目的索引。
Definition: viewer.hpp:191
typename _tCon::size_type SizeType
项目索引类型。
Definition: viewer.hpp:54
GSequenceViewer(ContainerType &con)
构造:使用指定容器。
Definition: viewer.hpp:84
auto total(size_t n, _fNow now, _fCallable &&f, _tParams &&...args) -> decltype(now()-now())
测试指定函数执行若干次的总时间。
Definition: timing.hpp:96
unsigned long Reset(COMPtr< _iCOM > &ptr) ynothrow
bool is_selected
选中状态。
Definition: viewer.hpp:75
length selected Valid
Definition: viewer.hpp:122
_tCon ContainerType
容器类型。
Definition: viewer.hpp:53
泛型算法。
void RestrictInInterval(_type &i, int a, int b) ynothrow
约束整数 i 在左闭右开区间 [a, b) 中。
Definition: ycutil.h:283
#define YB_LIKELY(expr)
Definition: ydef.h:297
bool SetLength(SizeType l)
设置长度。
Definition: viewer.hpp:158
SizeType length
视图长度:最大可视项目数。
Definition: viewer.hpp:74
int int is_selected bool Contains(SizeType i) const
判断是否在有效范围内包含指定项目索引。
Definition: viewer.hpp:108
DefGetter(const ynothrow, SizeType, Total, p_con->size()) DefGetter(const ynothrow