YSTest: yobject.h 源文件

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>
47 struct OwnershipTag
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
82 DeclDerivedI(YF_API, IValueHolder, ystdex::any_ops::holder)
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 {
97 using ystdex::examiners::equal_examiner::are_equal;
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 {
132 return HeldEqual<_type1, _type2>::are_equal(x, y);
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 {}
168 DefDeCopyCtor(ValueHolder)
169 DefDeMoveCtor(ValueHolder)
170
172 DefDeCopyAssignment(ValueHolder)
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
226 PointerHolder(const PointerHolder& h)
227 : PointerHolder(h.p_held ? new _type(*h.p_held) : nullptr)
228 {}
229 PointerHolder(PointerHolder&& h)
230 : p_held(h.p_held)
231 {
232 h.p_held = {};
233 }
235 virtual
237 ~PointerHolder()
238 {
239 // TODO: Provide other deleters.
240 delete p_held;
241 }
242
244 DefDeCopyAssignment(PointerHolder)
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
281 class YF_API ValueObject
282 {
283 public:
285 ystdex::any content;
286
287 public:
293 DefDeCtor(ValueObject)
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>
312 ValueObject(_type* p, PointerTag)
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 }
332 DefDeCopyCtor(ValueObject)
337 DefDeMoveCtor(ValueObject)
342 DefDeDtor(ValueObject)
343
345
346 DefDeCopyAssignment(ValueObject)
347 DefDeMoveAssignment(ValueObject)
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&
386 GetObject()
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());
479 using ReferentType = ystdex::remove_const_t<ystdex::remove_reference_t<
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:
538 ValueType max_value;
539 ValueType value;
540
544 GMRange(ValueType m, ValueType v)
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
YSLib::GMRange
范围模块类。
Definition: yobject.h:532
ImplBodyMem
#define ImplBodyMem(_m, _n,...)
Definition: YBaseMacro.h:116
YSLib::ValueObject::AccessPtr
const _type * AccessPtr() const ynothrow
Definition: yobject.h:432
ystdex::any
基于类型擦除的动态泛型对象。
Definition: any.h:584
YSLib::HeldEqual< weak_ptr< _type1 >, weak_ptr< _type2 > >::are_equal
static bool are_equal(const weak_ptr< _type1 > &x, const weak_ptr< _type2 > &y)
Definition: yobject.h:104
YSLib::PointerHolder::PointerHolder
PointerHolder(_type *value)
Definition: yobject.h:221
YSLib::ValueObject::Access
const _type & Access() const
Definition: yobject.h:413
YSLib::PointerHolder::p_held
_type * p_held
Definition: yobject.h:217
DefDeDtor
#define DefDeDtor(_t)
定义默认析构函数。
Definition: YBaseMacro.h:146
ImplRet
#define ImplRet(...)
Definition: YBaseMacro.h:97
YModules.h
YSLib::OwnershipTag
指定对于参数指定类型的成员具有所有权的标签。
Definition: yobject.h:47
implements
#define implements
Definition: YBaseMacro.h:272
ystdex::remove_reference_t
typename remove_reference< _type >::type remove_reference_t
Definition: type_op.hpp:234
DefDeCtor
#define DefDeCtor(_t)
Definition: YBaseMacro.h:131
DefDeCopyCtor
#define DefDeCopyCtor(_t)
Definition: YBaseMacro.h:136
YSLib::ValueHolder::ValueHolder
ValueHolder(const _type &value)
Definition: yobject.h:161
YF_API
#define YF_API
Definition: Platform.h:64
YSLib::HasOwnershipOf
标签类型元运算。
Definition: yobject.h:72
YSLib::GMRange::max_value
ValueType max_value
最大取值。
Definition: yobject.h:538
ystdex::nullptr
const class ystdex::nullptr_t nullptr
ystdex::any_ops::holder
抽象动态泛型持有者接口。
Definition: any.h:162
YSLib::PointerHolder::PointerHolder
PointerHolder(PointerHolder &&h)
Definition: yobject.h:229
YSLib::ValueObject::ValueObject
ValueObject(unique_ptr< _type > &&p, PointerTag)
构造：使用对象 unique_ptr 指针。
Definition: yobject.h:323
YSLib::PointerHolder::~PointerHolder
virtual ~PointerHolder()
Definition: yobject.h:237
yforward
#define yforward(_expr)
根据参数类型使用 std::forward 传递对应参数。
Definition: ydef.h:722
DefGetter
#define DefGetter(_q, _t, _n,...)
Definition: YBaseMacro.h:180
ImplI
#define ImplI(...)
Definition: YBaseMacro.h:308
YSLib::PointerTag
指示指针的标记。
Definition: yobject.h:63
YSLib::ValueObject
值类型对象类。
Definition: yobject.h:281
YSLib::ValueObject::ValueObject
ValueObject(_type *p, PointerTag)
构造：使用对象指针。
Definition: yobject.h:312
ystdex::swap
void swap(any &x, any &y)
交换对象。
Definition: any.h:729
YSLib::AreEqualHeld
yconstfn bool AreEqualHeld(const _type1 &x, const _type2 &y)
判断动态泛型的持有值是否相等。
Definition: yobject.h:130
YSLib::ValueHolder::ValueHolder
ValueHolder(_type &&value)
Definition: yobject.h:165
DefDeMoveCtor
#define DefDeMoveCtor(_t)
Definition: YBaseMacro.h:141
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:514
YSLib::PointerHolder::value_type
_type value_type
Definition: yobject.h:214
YSLib::ValueObject::content
ystdex::any content
Definition: yobject.h:285
EndDecl
#define EndDecl
Definition: YBaseMacro.h:316
examiner.hpp
C++ 类型操作检测。
YSLib::DeclDerivedI
DeclDerivedI(, GIHEvent, ystdex::cloneable) DeclIEntry(size_t operator()(_tParams...) const ) DeclIEntry(GIHEvent *clone() const override) EndDecltemplate< typename > class GHEvent
事件处理器接口模板。
YSLib::ValueHolder::held
_type held
Definition: yobject.h:158
YAssertNonnull
#define YAssertNonnull(_expr)
Definition: cassert.h:81
YSLib::GMRange< float >::ValueType
float ValueType
Definition: yobject.h:535
yconstfn
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:463
ystdex::exclude_self_ctor_t
enable_if_t<!is_same< _tClass &, remove_rcv_t< _tParam > & >::value, _type > exclude_self_ctor_t
移除选择类类型的特定重载避免构造模板和复制/转移构造函数冲突。
Definition: type_op.hpp:766
platform_ex::Reset
unsigned long Reset(COMPtr< _iCOM > &ptr) ynothrow
Definition: include/YCLib/COM.h:406
YSLib::GMRange::value
ValueType value
值。
Definition: yobject.h:539
any.h
动态泛型类型。
DefClone
#define DefClone(_q, _t)
动态复制。
Definition: YBaseMacro.h:221
YSLib::PointerHolder::PointerHolder
PointerHolder(const PointerHolder &h)
Definition: yobject.h:226
YSLib::PointerHolder
带等于接口的指针类型动态泛型持有者。
Definition: yobject.h:208
DefDeMoveAssignment
#define DefDeMoveAssignment(_t)
Definition: YBaseMacro.h:159
DefCvt
#define DefCvt(_q, _t,...)
Definition: YBaseMacro.h:164
YSLib::MoveTag
指示转移的标记。
Definition: yobject.h:55
DeclIEntry
#define DeclIEntry(_sig)
Definition: YBaseMacro.h:314
YSLib::ValueObject::DefGetter
DefGetter(const ynothrow, const std::type_info &, Type, content.type()) template< typename _type > inline _type &Access()
访问指定类型对象。 空实例或类型检查失败 。
Definition: yobject.h:397
YSLib::ValueHolder::value_type
_type value_type
Definition: yobject.h:154
YSLib::ValueObject::GetObject
const _type & GetObject() const
Definition: yobject.h:392
ystdex::make_unique
enable_if_t<!is_array< _type >::value, std::unique_ptr< _type > > make_unique(_tParams &&...args)
使用 new 和指定参数构造指定类型的 std::unique_ptr 实例。
Definition: memory.hpp:213
YSLib::yimpl
YF_API yimpl(GUIApplication &) FetchGlobalInstance() ynothrow
取全局应用程序实例。
YSLib::GMRange::GMRange
GMRange(ValueType m, ValueType v)
构造：使用指定最大取值和值。
Definition: yobject.h:544
YSLib::ValueHolder
带等于接口的值类型动态泛型持有者。
Definition: yobject.h:146
YSLib::reset
bool reset(_type *&p) ynothrow
Definition: yref.hpp:69
YSLib::CloneNonpolymorphic
yconstfn auto CloneNonpolymorphic(const _type &p) -> decltype(&*p)
使用 new 复制指定指针指向的对象。
Definition: ycutil.h:467
v
byte v
Definition: CharRenderer.cpp:73
DefDeCopyAssignment
#define DefDeCopyAssignment(_t)
Definition: YBaseMacro.h:154
YSLib::ValueObject::AccessPtr
_type * AccessPtr() ynothrow
访问指定类型对象指针。
Definition: yobject.h:426
ystdex::remove_const_t
typename remove_const< _type >::type remove_const_t
ISO C++ 1y 兼容类型操作别名。
Definition: type_op.hpp:216
YAssert
#define YAssert(_expr, _msg)
Definition: cassert.h:73
YSLib::ValueObject::GetObject
_type & GetObject()
Definition: yobject.h:386
ystdex::examiners::equal::are_equal
static bool are_equal(_type1 &&x, _type2 &&y, decltype(x==y)={})
Definition: examiner.hpp:51
YSLib::HeldEqual< pair< _type1, _type2 >, pair< _type3, _type4 > >::are_equal
static yconstfn bool are_equal(const pair< _type1, _type2 > &x, const pair< _type3, _type4 > &y)
Definition: yobject.h:114
YSLib::HeldEqual
Definition: yobject.h:95