YSTest  PreAlpha_b500_20140530
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
yobject.h
浏览该文件的文档.
1 /*
2  © 2009-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_Core_yobject_h_
29 #define YSL_INC_Core_yobject_h_ 1
30 
31 #include "YModules.h"
32 #include YFM_YSLib_Core_YCoreUtilities
33 #include YFM_YSLib_Adaptor_YContainer
34 #include <ystdex/any.h> // for ystdex::any_ops::holder, ystdex::any;
35 #include <ystdex/examiner.hpp> // for ystdex::examiners::equal_examiner;
36 
37 namespace YSLib
38 {
39 
40 //特征类策略:对象类型标签模板。
41 
46 template<typename>
48 {};
49 
50 
55 struct MoveTag
56 {};
57 
58 
63 struct PointerTag
64 {};
65 
66 
71 template<class _tOwner, typename _type>
72 struct HasOwnershipOf : std::integral_constant<bool,
73  std::is_base_of<OwnershipTag<_type>, _tOwner>::value>
74 {};
75 
76 
83  DeclIEntry(bool operator==(const IValueHolder&) const)
84 EndDecl
85 
86 
88 
89 
94 template<typename _type1, typename _type2>
95 struct HeldEqual : private ystdex::examiners::equal_examiner
96 {
98 };
99 
100 template<typename _type1, typename _type2>
101 struct HeldEqual<weak_ptr<_type1>, weak_ptr<_type2>>
102 {
103  static inline bool
104  are_equal(const weak_ptr<_type1>& x, const weak_ptr<_type2>& y)
105  {
106  return x == y;
107  }
108 };
109 
110 template<typename _type1, typename _type2, typename _type3, typename _type4>
111 struct HeldEqual<pair<_type1, _type2>, pair<_type3, _type4>>
112 {
113  static yconstfn bool
114  are_equal(const pair<_type1, _type2>& x, const pair<_type3, _type4>& y)
115  {
116  return x.first == y.first && x.second == y.second;
117  }
118 };
120 
121 
128 template<typename _type1, typename _type2>
129 yconstfn bool
130 AreEqualHeld(const _type1& x, const _type2& y)
131 {
133 }
135 
136 
145 template<typename _type>
146 class ValueHolder : implements IValueHolder
147 {
148  static_assert(std::is_object<_type>::value, "Non-object type found.");
149  static_assert(!(std::is_const<_type>::value
150  || std::is_volatile<_type>::value), "Cv-qualified type found.");
151 
152 public:
154  using value_type = _type;
155 
156 protected:
158  mutable _type held;
159 
160 public:
161  ValueHolder(const _type& value)
162  : held(value)
163  {}
165  ValueHolder(_type&& value)
166  : held(std::move(value))
167  {}
170 
173 
174  ImplI(IValueHolder) bool
175  operator==(const IValueHolder& obj) const override
176  {
177  return AreEqualHeld(held, static_cast<const ValueHolder&>(obj).held);
178  }
179 
181  ImplI(IValueHolder) DefClone(const override, ValueHolder)
182 
184  ImplI(IValueHolder) void*
185  get() const override
186  {
187  return std::addressof(held);
188  }
189 
191  ImplI(IValueHolder) const std::type_info&
192  type() const ynothrow override
193  {
194  return typeid(_type);
195  }
196 };
197 
198 
207 template<typename _type>
208 class PointerHolder : implements IValueHolder
209 {
210  static_assert(std::is_object<_type>::value, "Invalid type found.");
211 
212 public:
214  using value_type = _type;
215 
216 protected:
217  _type* p_held;
218 
219 public:
221  PointerHolder(_type* value)
222  : p_held(value)
223  {}
225 
227  : PointerHolder(h.p_held ? new _type(*h.p_held) : nullptr)
228  {}
230  : p_held(h.p_held)
231  {
232  h.p_held = {};
233  }
235  virtual
238  {
239  // TODO: Provide other deleters.
240  delete p_held;
241  }
242 
245 
246  ImplI(IValueHolder) bool
247  operator==(const IValueHolder& obj) const
248  {
249  return AreEqualHeld(*p_held,
250  *static_cast<const PointerHolder&>(obj).p_held);
251  }
252 
254  ImplI(IValueHolder) DefClone(const override, PointerHolder)
255 
257  ImplI(IValueHolder) void*
258  get() const override
259  {
260  return p_held;
261  }
262 
264  ImplI(IValueHolder) const std::type_info&
265  type() const ynothrow override
266  {
267  return p_held ? typeid(_type) : typeid(void);
268  }
269 };
270 
271 
282 {
283 public:
286 
287 public:
299  template<typename _type,
300  yimpl(typename = ystdex::exclude_self_ctor_t<ValueObject, _type>)>
301  ValueObject(_type&& obj)
302  : content(ystdex::any_ops::holder_tag(), make_unique<ValueHolder<
303  typename ystdex::remove_rcv<_type>::type>>(yforward(obj)))
304  {}
311  template<typename _type>
313  : content(ystdex::any_ops::holder_tag(), make_unique<PointerHolder<
314  _type>>(p))
315  {}
322  template<typename _type>
323  ValueObject(unique_ptr<_type>&& p, PointerTag)
324  : ValueObject(p.get(), PointerTag())
325  {
326  p.release();
327  }
343 
345 
349 
354  PDefHOp(bool, !, ) const ynothrow
355  ImplRet(!content)
356 
357  bool
358  operator==(const ValueObject&) const;
359 
364  explicit DefCvt(const ynothrow, bool, content.get_holder())
365 
366 private:
373  template<typename _type>
374  inline _type&
375  GetMutableObject() const
376  {
377  YAssertNonnull(content);
378  YAssert(content.type() == typeid(_type), "Invalid type found.");
379 
380  return *static_cast<_type*>(content.get());
381  }
382 
383 public:
384  template<typename _type>
385  inline _type&
387  {
388  return GetMutableObject<_type>();
389  }
390  template<typename _type>
391  inline const _type&
392  GetObject() const
393  {
394  return GetMutableObject<_type>();
395  }
397  DefGetter(const ynothrow, const std::type_info&, Type, content.type())
398 
405  template<typename _type>
406  inline _type&
407  Access()
408  {
409  return ystdex::any_cast<_type&>(content);
410  }
411  template<typename _type>
412  inline const _type&
413  Access() const
414  {
415  return ystdex::any_cast<const _type&>(content);
416  }
418 
424  template<typename _type>
425  inline _type*
426  AccessPtr() ynothrow
427  {
428  return ystdex::any_cast<_type*>(&content);
429  }
430  template<typename _type>
431  inline const _type*
432  AccessPtr() const ynothrow
433  {
434  return ystdex::any_cast<const _type*>(&content);
435  }
437 
438  /*
439  \brief 清除。
440  \post <tt>*this == ValueObject()</tt> 。
441  \since build 296
442  */
443  PDefH(void, Clear, ) ynothrow
444  ImplBodyMem(content, clear, )
445 
450  PDefH(void, swap, ValueObject& vo) ynothrow
451  ImplBodyMem(content, swap, vo.content)
452 };
453 
458 inline DefSwap(ynothrow, ValueObject)
459 
460 
472 template<typename _type, class _tOwnerPointer = shared_ptr<_type>>
473 class GDependency
474 {
475 public:
476  using DependentType = _type;
477  using PointerType = _tOwnerPointer;
478  using ConstReferenceType = decltype(*PointerType());
480  ConstReferenceType>>;
481  using ReferenceType = ReferentType&;
482 
483 private:
484  PointerType ptr;
485 
486 public:
487  inline
488  GDependency(PointerType p = PointerType())
489  : ptr(p)
490  {
491  GetCopyOnWritePtr();
492  }
493 
494  DefDeCopyAssignment(GDependency)
495  DefDeMoveAssignment(GDependency)
496 
497  DefCvt(const ynothrow, ConstReferenceType, *ptr)
498  DefCvt(ynothrow, ReferenceType, *ptr)
499  DefCvt(const ynothrow, bool, bool(ptr))
500 
501  DefGetter(const ynothrow, ConstReferenceType, Ref,
502  operator ConstReferenceType())
503  DefGetter(ynothrow, ReferenceType, Ref, operator ReferenceType())
504  DefGetter(ynothrow, ReferenceType, NewRef, *GetCopyOnWritePtr())
505 
506 
507  PointerType
508  GetCopyOnWritePtr()
509  {
510  if(!ptr)
511  ptr = PointerType(new DependentType());
512  else if(!ptr.unique())
513  ptr = PointerType(CloneNonpolymorphic(ptr));
514  YAssertNonnull(ptr);
515  return ptr;
516  }
517 
518  inline
519  void Reset()
520  {
521  reset(ptr);
522  }
523 };
524 
525 
531 template<typename _type>
532 class GMRange
533 {
534 public:
535  using ValueType = _type;
536 
537 protected:
540 
545  : max_value(m), value(v)
546  {}
547 
548 public:
549  DefGetter(const ynothrow, ValueType, MaxValue, max_value)
550  DefGetter(const ynothrow, ValueType, Value, value)
551 };
552 
553 } // namespace YSLib;
554 
555 #endif
556 
范围模块类。
Definition: yobject.h:532
#define ImplBodyMem(_m, _n,...)
Definition: YBaseMacro.h:116
const _type * AccessPtr() const ynothrow
Definition: yobject.h:432
基于类型擦除的动态泛型对象。
Definition: any.h:584
static bool are_equal(const weak_ptr< _type1 > &x, const weak_ptr< _type2 > &y)
Definition: yobject.h:104
PointerHolder(_type *value)
Definition: yobject.h:221
const _type & Access() const
Definition: yobject.h:413
#define DefDeDtor(_t)
定义默认析构函数。
Definition: YBaseMacro.h:146
#define ImplRet(...)
Definition: YBaseMacro.h:97
指定对于参数指定类型的成员具有所有权的标签。
Definition: yobject.h:47
#define implements
Definition: YBaseMacro.h:272
typename remove_reference< _type >::type remove_reference_t
Definition: type_op.hpp:234
#define DefDeCtor(_t)
Definition: YBaseMacro.h:131
#define DefDeCopyCtor(_t)
Definition: YBaseMacro.h:136
ValueHolder(const _type &value)
Definition: yobject.h:161
#define YF_API
Definition: Platform.h:64
标签类型元运算。
Definition: yobject.h:72
ValueType max_value
最大取值。
Definition: yobject.h:538
const class ystdex::nullptr_t nullptr
抽象动态泛型持有者接口。
Definition: any.h:162
PointerHolder(PointerHolder &&h)
Definition: yobject.h:229
ValueObject(unique_ptr< _type > &&p, PointerTag)
构造:使用对象 unique_ptr 指针。
Definition: yobject.h:323
virtual ~PointerHolder()
Definition: yobject.h:237
#define yforward(_expr)
根据参数类型使用 std::forward 传递对应参数。
Definition: ydef.h:722
#define DefGetter(_q, _t, _n,...)
Definition: YBaseMacro.h:180
#define ImplI(...)
Definition: YBaseMacro.h:308
指示指针的标记。
Definition: yobject.h:63
值类型对象类。
Definition: yobject.h:281
ValueObject(_type *p, PointerTag)
构造:使用对象指针。
Definition: yobject.h:312
void swap(any &x, any &y)
交换对象。
Definition: any.h:729
yconstfn bool AreEqualHeld(const _type1 &x, const _type2 &y)
判断动态泛型的持有值是否相等。
Definition: yobject.h:130
ValueHolder(_type &&value)
Definition: yobject.h:165
#define DefDeMoveCtor(_t)
Definition: YBaseMacro.h:141
#define ynothrow
YSLib 无异常抛出保证:若支持 noexcept 关键字, 指定特定的 noexcept 异常规范。
Definition: ydef.h:514
ystdex::any content
Definition: yobject.h:285
#define EndDecl
Definition: YBaseMacro.h:316
C++ 类型操作检测。
DeclDerivedI(, GIHEvent, ystdex::cloneable) DeclIEntry(size_t operator()(_tParams...) const ) DeclIEntry(GIHEvent *clone() const override) EndDecltemplate< typename > class GHEvent
事件处理器接口模板。
#define YAssertNonnull(_expr)
Definition: cassert.h:81
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:463
enable_if_t<!is_same< _tClass &, remove_rcv_t< _tParam > & >::value, _type > exclude_self_ctor_t
移除选择类类型的特定重载避免构造模板和复制/转移构造函数冲突。
Definition: type_op.hpp:766
unsigned long Reset(COMPtr< _iCOM > &ptr) ynothrow
ValueType value
值。
Definition: yobject.h:539
动态泛型类型。
#define DefClone(_q, _t)
动态复制。
Definition: YBaseMacro.h:221
PointerHolder(const PointerHolder &h)
Definition: yobject.h:226
带等于接口的指针类型动态泛型持有者。
Definition: yobject.h:208
#define DefDeMoveAssignment(_t)
Definition: YBaseMacro.h:159
#define DefCvt(_q, _t,...)
Definition: YBaseMacro.h:164
指示转移的标记。
Definition: yobject.h:55
#define DeclIEntry(_sig)
Definition: YBaseMacro.h:314
DefGetter(const ynothrow, const std::type_info &, Type, content.type()) template< typename _type > inline _type &Access()
访问指定类型对象。 空实例或类型检查失败 。
Definition: yobject.h:397
const _type & GetObject() const
Definition: yobject.h:392
enable_if_t<!is_array< _type >::value, std::unique_ptr< _type > > make_unique(_tParams &&...args)
使用 new 和指定参数构造指定类型的 std::unique_ptr 实例。
Definition: memory.hpp:213
YF_API yimpl(GUIApplication &) FetchGlobalInstance() ynothrow
取全局应用程序实例。
GMRange(ValueType m, ValueType v)
构造:使用指定最大取值和值。
Definition: yobject.h:544
带等于接口的值类型动态泛型持有者。
Definition: yobject.h:146
bool reset(_type *&p) ynothrow
Definition: yref.hpp:69
yconstfn auto CloneNonpolymorphic(const _type &p) -> decltype(&*p)
使用 new 复制指定指针指向的对象。
Definition: ycutil.h:467
byte v
#define DefDeCopyAssignment(_t)
Definition: YBaseMacro.h:154
_type * AccessPtr() ynothrow
访问指定类型对象指针。
Definition: yobject.h:426
typename remove_const< _type >::type remove_const_t
ISO C++ 1y 兼容类型操作别名。
Definition: type_op.hpp:216
#define YAssert(_expr, _msg)
Definition: cassert.h:73
_type & GetObject()
Definition: yobject.h:386
static bool are_equal(_type1 &&x, _type2 &&y, decltype(x==y)={})
Definition: examiner.hpp:51
static yconstfn bool are_equal(const pair< _type1, _type2 > &x, const pair< _type3, _type4 > &y)
Definition: yobject.h:114