YSTest: YObject.h 源文件

YSTest PreAlpha_b700_20160611 The YSLib Test Project

- 首页
- 相关页面
- 模块
- 命名空间
- 类
- 文件

- 文件列表
- 文件成员

YObject.h
浏览该文件的文档.
1 /*
2 © 2009-2016 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 <ystdex/any.h> // for ystdex::any_ops::holder, ystdex::boxed_value,
34 // ystdex::any;
35 #include <ystdex/examiner.hpp> // for ystdex::examiners::equal_examiner;
36 #include <ystdex/operators.hpp> // for ystdex::equality_comparable
37
38 namespace YSLib
39 {
40
42 using ystdex::type_info;
43
44
49 template<typename>
50 struct OwnershipTag
51 {};
52
53
58 struct MoveTag
59 {};
60
61
66 struct PointerTag
67 {};
68
69
74 template<typename _type>
75 using InPlaceTag = ystdex::any_ops::in_place_t<_type>;
76
77
82 template<class _tOwner, typename _type>
83 struct HasOwnershipOf : ystdex::bool_constant<
84 std::is_base_of<OwnershipTag<_type>, _tOwner>::value>
85 {};
86
87
93 DeclDerivedI(YF_API, IValueHolder, ystdex::any_ops::holder)
94 DeclIEntry(bool operator==(const IValueHolder&) const)
95 EndDecl
96
97
99
100
105 template<typename _type1, typename _type2>
106 struct HeldEqual : private ystdex::examiners::equal_examiner
107 {
108 using ystdex::examiners::equal_examiner::are_equal;
109 };
110
111 template<typename _type1, typename _type2>
112 struct HeldEqual<weak_ptr<_type1>, weak_ptr<_type2>>
113 {
114 static bool
115 are_equal(const weak_ptr<_type1>& x, const weak_ptr<_type2>& y)
116 {
117 return x == y;
118 }
119 };
120
121 template<typename _type1, typename _type2, typename _type3, typename _type4>
122 struct HeldEqual<pair<_type1, _type2>, pair<_type3, _type4>>
123 {
124 static yconstfn bool
125 are_equal(const pair<_type1, _type2>& x, const pair<_type3, _type4>& y)
126 {
127 return x.first == y.first && x.second == y.second;
128 }
129 };
131
132
139 template<typename _type1, typename _type2>
140 yconstfn bool
141 AreEqualHeld(const _type1& x, const _type2& y)
142 {
143 return HeldEqual<_type1, _type2>::are_equal(x, y);
144 }
146
147
156 template<typename _type>
157 class ValueHolder
158 : protected ystdex::boxed_value<_type>, implements IValueHolder
159 {
160 static_assert(std::is_object<_type>(), "Non-object type found.");
161 static_assert(!ystdex::is_cv<_type>(), "Cv-qualified type found.");
162
163 public:
165 using value_type = _type;
166
168
169 DefDeCtor(ValueHolder)
170 template<typename _tParam,
171 yimpl(typename = ystdex::exclude_self_t<ValueHolder, _tParam>)>
172 ValueHolder(_tParam&& arg)
173 ynoexcept(std::is_nothrow_constructible<_type, _tParam&&>())
174 : ystdex::boxed_value<_type>(yforward(arg))
175 {}
176 using ystdex::boxed_value<_type>::boxed_value;
178 DefDeCopyCtor(ValueHolder)
179 DefDeMoveCtor(ValueHolder)
180
182 DefDeCopyAssignment(ValueHolder)
184 DefDeMoveAssignment(ValueHolder)
185
186 PDefHOp(bool, ==, const IValueHolder& obj) const ImplI(IValueHolder)
187 ImplRet(type() == obj.type() && AreEqualHeld(this->value,
188 static_cast<const ValueHolder&>(obj).value))
189
191 DefClone(const ImplI(IValueHolder), ValueHolder)
192
194 PDefH(void*, get, ) const ImplI(IValueHolder)
195 ImplRet(std::addressof(this->value))
196
198 PDefH(const type_info&, type, ) const ynothrow ImplI(IValueHolder)
199 ImplRet(ystdex::type_id<_type>())
200 };
201
202
213 template<typename _type, class _tPointer = std::unique_ptr<_type>>
214 class PointerHolder : implements IValueHolder
215 {
217 static_assert(std::is_object<_type>(), "Invalid type found.");
218
219 public:
221 using value_type = _type;
222 using holder_pointer = _tPointer;
223 using pointer = typename holder_pointer::pointer;
224
225 protected:
226 holder_pointer p_held;
227
228 public:
230 PointerHolder(pointer value)
231 : p_held(value)
232 {}
234
235 PointerHolder(const PointerHolder& h)
236 : PointerHolder(h.p_held ? new _type(*h.p_held) : nullptr)
237 {}
238 DefDeMoveCtor(PointerHolder)
240
242 DefDeCopyAssignment(PointerHolder)
243 DefDeMoveAssignment(PointerHolder)
244
246 PDefHOp(bool, ==, const IValueHolder& obj) const ImplI(IValueHolder)
247 ImplRet(type() == obj.type() && AreEqualHeld(*p_held,
248 Deref(static_cast<const PointerHolder&>(obj).p_held)))
249
251 DefClone(const ImplI(IValueHolder), PointerHolder)
252
254 PDefH(void*, get, ) const ImplI(IValueHolder)
255 ImplRet(p_held.get())
256
258 PDefH(const type_info&, type, ) const ynothrow ImplI(IValueHolder)
259 ImplRet(p_held ? ystdex::type_id<_type>() : ystdex::type_id<void>())
260 };
261
262
272 class YF_API ValueObject : private ystdex::equality_comparable<ValueObject>
273 {
274 public:
276 ystdex::any content;
277
283 DefDeCtor(ValueObject)
289 template<typename _type,
290 yimpl(typename = ystdex::exclude_self_t<ValueObject, _type>)>
291 ValueObject(_type&& obj)
292 : content(ystdex::any_ops::use_holder,
293 InPlaceTag<ValueHolder<ystdex::decay_t<_type>>>(), yforward(obj))
294 {}
302 template<typename _type, typename... _tParams>
303 ValueObject(InPlaceTag<_type>, _tParams&&... args)
304 : content(ystdex::any_ops::use_holder,
305 InPlaceTag<ValueHolder<_type>>(), yforward(args)...)
306 {}
313 template<typename _type>
314 ValueObject(_type* p, PointerTag)
315 : content(ystdex::any_ops::use_holder,
316 InPlaceTag<PointerHolder<_type>>(), p)
317 {}
324 template<typename _type>
325 ValueObject(unique_ptr<_type>&& p, PointerTag)
326 : ValueObject(p.get(), PointerTag())
327 {
328 p.release();
329 }
334 DefDeCopyCtor(ValueObject)
339 DefDeMoveCtor(ValueObject)
344 DefDeDtor(ValueObject)
345
347
348 DefDeCopyAssignment(ValueObject)
349 DefDeMoveAssignment(ValueObject)
351
356 DefBoolNeg(explicit, content.get_holder())
357
359
360 YF_API friend bool
362 operator==(const ValueObject&, const ValueObject&);
363
369 template<typename _type>
370 inline _type&
371 GetObject() ynothrowv
372 {
373 return Deref(ystdex::unchecked_any_cast<_type>(&content));
374 }
375 template<typename _type>
376 inline const _type&
377 GetObject() const ynothrowv
378 {
379 return Deref(ystdex::unchecked_any_cast<const _type>(&content));
380 }
382
383 DefGetter(const ynothrow, const type_info&, Type, content.type())
385
392 template<typename _type>
393 inline _type&
394 Access()
395 {
396 return ystdex::any_cast<_type&>(content);
397 }
398 template<typename _type>
399 inline const _type&
400 Access() const
401 {
402 return ystdex::any_cast<const _type&>(content);
403 }
405
411 template<typename _type>
412 inline observer_ptr<_type>
413 AccessPtr() ynothrow
414 {
415 return YSLib::make_observer(ystdex::any_cast<_type>(&content));
416 }
417 template<typename _type>
418 inline observer_ptr<const _type>
419 AccessPtr() const ynothrow
420 {
421 return YSLib::make_observer(ystdex::any_cast<const _type>(&content));
422 }
424
426
427 template<typename _type, typename... _tParams>
428 void
429 Emplace(_tParams&&... args)
430 {
431 using Holder = ValueHolder<ystdex::decay_t<_type>>;
432
433 content.emplace<Holder>(ystdex::any_ops::use_holder,
434 Holder(yforward(args)...));
435 }
436 template<typename _type>
437 void
438 Emplace(_type* p, PointerTag)
439 {
440 using Holder = PointerHolder<ystdex::decay_t<_type>>;
441
442 content.emplace<Holder>(ystdex::any_ops::use_holder, Holder(p));
443 }
444
445 template<typename _type, typename... _tParams>
446 _type&
447 EmplaceIfEmpty(_tParams&&... args)
448 {
449 if(!*this)
450 {
451 Emplace<_type>(yforward(args)...);
452 return GetObject<_type>();
453 }
454 return Access<_type>();
455 }
457
463 PDefH(void, Clear, ) ynothrow
464 ImplExpr(content.clear())
465
470 PDefH(void, swap, ValueObject& vo) ynothrow
471 ImplExpr(content.swap(vo.content))
472 };
473
478 inline DefSwap(ynothrow, ValueObject)
479
480
492 template<typename _type, class _tOwnerPointer = shared_ptr<_type>>
493 class GDependency
494 {
495 public:
496 using DependentType = _type;
497 using PointerType = _tOwnerPointer;
498 using ConstReferenceType = decltype(*PointerType());
499 using ReferentType = ystdex::remove_const_t<ystdex::remove_reference_t<
500 ConstReferenceType>>;
501 using ReferenceType = ReferentType&;
502
503 private:
504 PointerType ptr;
505
506 public:
507 inline
508 GDependency(PointerType p = PointerType())
509 : ptr(p)
510 {
511 GetCopyOnWritePtr();
512 }
513
514 DefDeCopyAssignment(GDependency)
515 DefDeMoveAssignment(GDependency)
516
517 DefCvt(const ynothrow, ConstReferenceType, *ptr)
518 DefCvt(ynothrow, ReferenceType, *ptr)
519 DefCvt(const ynothrow, bool, bool(ptr))
520
521 DefGetter(const ynothrow, ConstReferenceType, Ref,
522 operator ConstReferenceType())
523 DefGetter(ynothrow, ReferenceType, Ref, operator ReferenceType())
524 DefGetter(ynothrow, ReferenceType, NewRef, *GetCopyOnWritePtr())
525
526
527 PointerType
528 GetCopyOnWritePtr()
529 {
530 if(!ptr)
531 ptr = PointerType(new DependentType());
532 else if(!ptr.unique())
533 ptr = PointerType(ystdex::clone_monomorphic(ptr));
534 return Nonnull(ptr);
535 }
536
537 inline
538 void Reset()
539 {
540 reset(ptr);
541 }
542 };
543
544
550 template<typename _type>
551 class GMRange
552 {
553 public:
554 using ValueType = _type;
555
556 protected:
557 ValueType max_value;
558 ValueType value;
559
563 GMRange(ValueType m, ValueType v)
564 : max_value(m), value(v)
565 {}
566
567 public:
568 DefGetter(const ynothrow, ValueType, MaxValue, max_value)
569 DefGetter(const ynothrow, ValueType, Value, value)
570 };
571
572 } // namespace YSLib;
573
574 #endif
575
YSLib::PointerHolder::p_held
holder_pointer p_held
Definition: YObject.h:226
ystdex::clone_monomorphic
auto clone_monomorphic(const _type &p, _tAlloc &&a=_tAlloc()) -> decltype(std::addressof(*p))
使用分配器复制指定指针指向的对象。
Definition: memory.hpp:488
platform::p
while p p
Definition: YCLib/FileSystem.h:225
YSLib::ValueHolder
带等于接口的值类型动态泛型持有者。
Definition: YObject.h:157
YSLib::Drawing::x
The boundary is out of the const Rect SDst SPos x
Definition: YDraw.h:127
YSLib::GMRange::value
ValueType value
值。
Definition: YObject.h:558
platform::Nonnull
_type && Nonnull(_type &&p) ynothrow
断言并返回非空指针。
Definition: Debug.h:411
ystdex::any_ops::in_place_t
Definition: any.h:83
YModules.h
YSLib::GMRange::GMRange
GMRange(ValueType m, ValueType v)
构造：使用指定最大取值和值。
Definition: YObject.h:563
ystdex::swap
void swap(container_adaptor< _tSeqCon > &x, container_adaptor< _tSeqCon > &y)
Definition: container.hpp:179
YSLib::ValueObject::ValueObject
ValueObject(InPlaceTag< _type >, _tParams &&...args)
构造：使用对象初始化参数。
Definition: YObject.h:303
v
byte v
Definition: CharRenderer.cpp:75
YSLib::reset
bool reset(_type *&p) ynothrow
Definition: YShellDefinition.h:46
ystdex::any_ops::holder
抽象动态泛型持有者接口。
Definition: any.h:97
YSLib::HeldEqual
Definition: YObject.h:106
YSLib::PointerHolder::holder_pointer
_tPointer holder_pointer
Definition: YObject.h:222
ystdex::nullptr
const class ystdex::nullptr_t nullptr
YSLib::HasOwnershipOf
标签类型元运算。
Definition: YObject.h:83
YSLib::PointerHolder::pointer
typename holder_pointer::pointer pointer
Definition: YObject.h:223
YSLib::ValueHolder::DefDeCopyCtor
DefDeCopyCtor(ValueHolder) DefDeMoveCtor(ValueHolder) DefDeCopyAssignment(ValueHolder) DefDeMoveAssignment(ValueHolder) PDefHOp(bool
ystdex::boxed_value
包装类类型的值的对象。
Definition: utility.hpp:139
YSLib::PointerHolder
带等于接口的指针类型动态泛型持有者。
Definition: YObject.h:214
YSLib
YSLib 命名空间。
Definition: AndroidHost.h:45
ImplI
#define ImplI(...)
Definition: YBaseMacro.h:386
YSLib::PDefH
PDefH(Environment &, FetchEnvironment,) ImplRet(FetchGlobalInstance().GetEnvironmentRef()) inline PDefH(GUIHost &
取单一环境对象引用。
ystdex::cpp2014::remove_reference_t
typename remove_reference< _type >::type remove_reference_t
Definition: type_traits.hpp:304
ynothrowv
#define ynothrowv
YSLib 无异常抛出保证验证：有条件地使用无异常抛出规范。
Definition: ydef.h:760
DefDeCopyAssignment
#define DefDeCopyAssignment(_t)
Definition: YBaseMacro.h:185
YSLib::ValueObject::EmplaceIfEmpty
_type & EmplaceIfEmpty(_tParams &&...args)
Definition: YObject.h:447
ystdex::bool_constant
integral_constant< bool, _b > bool_constant
bool 常量。
Definition: type_traits.hpp:382
ystdex::cpp2014::remove_const_t
typename remove_const< _type >::type remove_const_t
ISO C++ 14 兼容类型操作别名。
Definition: type_traits.hpp:285
YSLib::vo
vo
Definition: YApplication.h:196
YSLib::PointerHolder::PointerHolder
PointerHolder(pointer value)
取得所有权。
Definition: YObject.h:230
YSLib::PointerHolder::value_type
_type value_type
Definition: YObject.h:221
YSLib::ValueHolder::value_type
_type value_type
Definition: YObject.h:165
ystdex::exclude_self_t
enable_if_t<!is_same< _tClass &, decay_t< _tParam > & >::value, _type > exclude_self_t
移除选择类类型的特定重载避免构造模板和复制/转移特殊成员函数冲突。
Definition: type_traits.hpp:708
DefGetter
#define DefGetter(_q, _t, _n,...)
Definition: YBaseMacro.h:240
DefCvt
#define DefCvt(_q, _t,...)
Definition: YBaseMacro.h:205
YSLib::DeclDerivedI
DeclDerivedI(YF_API, IValueHolder, ystdex::any_ops::holder) DeclIEntry(bool operator
带等于接口的动态泛型持有者接口。
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:115
ystdex::any::type
const type_info & type() const
Definition: any.h:1002
YSLib::ynothrow
YB_PURE const _type size_t n ynothrow
Definition: YCoreUtilities.h:168
ystdex::any
基于类型擦除的动态泛型对象。
Definition: any.h:823
YSLib::ValueObject::AccessPtr
observer_ptr< _type > AccessPtr() ynothrow
访问指定类型对象指针。
Definition: YObject.h:413
platform_ex::Reset
unsigned long Reset(COMPtr< _iCOM > &ptr) ynothrow
Definition: COM.h:408
DefClone
#define DefClone(_q, _t)
动态复制。
Definition: YBaseMacro.h:293
YSLib::ValueHolder::DefDeCtor
DefDeCtor(ValueHolder) template< typename _tParam
YSLib::ValueObject::ValueObject
ValueObject(_type *p, PointerTag)
构造：使用对象指针。
Definition: YObject.h:314
std
Definition: memory.hpp:1100
platform::Deref
yconstfn auto Deref(_type &&p) -> decltype(*p)
断言并解引用非空指针。
Definition: Debug.h:440
YSLib::ValueHolder::yimpl
yimpl(typename=ystdex::exclude_self_t< ValueHolder, _tParam >)> ValueHolder(_tParam &&arg) ynoexcept(std
Definition: YObject.h:171
EndDecl
#define EndDecl
Definition: YBaseMacro.h:396
YSLib::GMRange
范围模块类。
Definition: YObject.h:551
DefDeMoveAssignment
#define DefDeMoveAssignment(_t)
Definition: YBaseMacro.h:190
DeclIEntry
#define DeclIEntry(_sig)
Definition: YBaseMacro.h:394
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:125
ystdex::details::any_emplace::emplace
void emplace(_tParams &&...args)
Definition: any.h:787
YSLib::AreEqualHeld
yconstfn bool AreEqualHeld(const _type1 &x, const _type2 &y)
判断动态泛型的持有值是否相等。
Definition: YObject.h:141
ystdex
特性测试宏。
Definition: ydef.h:796
ystdex::cpp2014::decay_t
typename decay< _type >::type decay_t
Definition: type_traits.hpp:347
DefDeMoveCtor
#define DefDeMoveCtor(_t)
Definition: YBaseMacro.h:166
YSLib::ValueObject::ValueObject
ValueObject(unique_ptr< _type > &&p, PointerTag)
构造：使用对象 unique_ptr 指针。
Definition: YObject.h:325
YSLib::ValueObject::AccessPtr
observer_ptr< const _type > AccessPtr() const ynothrow
Definition: YObject.h:419
YSLib::OwnershipTag
指定对参数指定类型的成员具有所有权的标签。
Definition: YObject.h:50
YSLib::GMRange::max_value
ValueType max_value
最大取值。
Definition: YObject.h:557
YSLib::UI::y
SPos SPos y
Definition: YWidget.cpp:44
examiner.hpp
C++ 类型操作检测。
operators.hpp
重载操作符。
ynoexcept
#define ynoexcept(...)
YSLib 无异常抛出保证：指定特定的异常规范。
Definition: ydef.h:720
ystdex::examiners::equal::are_equal
static auto are_equal(_type1 &&x, _type2 &&y) -> decltype(bool(x==y))
Definition: examiner.hpp:53
ystdex::type_id
const type_info & type_id()
Definition: typeinfo.h:149
implements
#define implements
Definition: YBaseMacro.h:351
DefDeDtor
#define DefDeDtor(_t)
Definition: YBaseMacro.h:176
yconstfn
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:654
YSLib::PointerTag
指示指针的标记。
Definition: YObject.h:66
YSLib::ValueObject::content
ystdex::any content
Definition: YObject.h:276
YSLib::GMRange< float >::ValueType
float ValueType
Definition: YObject.h:554
yforward
#define yforward(_expr)
根据参数类型使用 std::forward 传递对应参数。
Definition: ydef.h:969
ystdex::boxed_value::value
_type value
Definition: utility.hpp:142
ystdex::is_cv
判断指定类型是否为 const 或 volatile 类型。
Definition: type_traits.hpp:842
ystdex::any_ops::use_holder
const struct ystdex::any_ops::use_holder_t use_holder
DefBoolNeg
#define DefBoolNeg(_spec,...)
定义表示显式转换 bool 操作符及其否定的 operator! 。
Definition: YBaseMacro.h:227
ystdex::make_observer
observer_ptr< _type > make_observer(_type *p)
Definition: memory.hpp:1042
YF_API
#define YF_API
Definition: Platform.h:56
YSLib::ValueObject::Access
const _type & Access() const
Definition: YObject.h:400
ystdex::dep_ops::equality_comparable
_t< details::ops_seq< _type, _type2, _tOpt, cond_t< is_same< _type, _type2 >, index_sequence< 7 >, index_sequence< 6, 7, 8 >>>> equality_comparable
Definition: operators.hpp:243
CHRLib::args
_tParams && args
Definition: CharacterProcessing.h:538
YSLib::ImplRet
ImplRet(FetchGlobalInstance().GetGUIHostRef()) YF_API void Execute(GUIApplication &
执行程序主消息循环。
any.h
动态泛型类型。
YSLib::ValueObject::Emplace
void Emplace(_type *p, PointerTag)
Definition: YObject.h:438
ystdex::boxed_value::boxed_value
boxed_value()
Definition: utility.hpp:147
YSLib::MoveTag
指示转移的标记。
Definition: YObject.h:58
YSLib::ValueObject
值类型对象类。
Definition: YObject.h:272
YSLib::ValueObject::Emplace
void Emplace(_tParams &&...args)
Definition: YObject.h:429
YSLib::PointerHolder::PointerHolder
PointerHolder(const PointerHolder &h)
Definition: YObject.h:235
YSLib::ValueObject::GetObject
const _type & GetObject() const ynothrowv
Definition: YObject.h:377