YSTest: YStorage.hpp 源文件

YSTest PreAlpha_b800_20170810 The YSLib Test Project

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

- 文件列表
- 文件成员

YStorage.hpp
浏览该文件的文档.
1 /*
2 © 2011-2015 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_ystorage_hpp_
29 #define YSL_INC_Core_ystorage_hpp_ 1
30
31 #include "YModules.h"
32 #include YFM_YSLib_Core_YCoreUtilities // for YSLib::safe_delete_obj;
33
34 namespace YSLib
35 {
36
41 template<typename _type>
42 inline _type
43 FetchInstance()
44 {
45 return _type();
46 }
47
52 template<typename _type>
53 inline _type&
54 FetchStaticRef()
55 {
56 return ystdex::parameterize_static_object<_type>();
57 }
58
63 template<typename _type>
64 inline const _type&
65 FetchPrototype()
66 {
67 return ystdex::parameterize_static_object<const _type>();
68 }
69
70
77 template<typename _type, typename _tPointer = _type*>
78 class GStaticCache : private noncopyable
79 {
80 static_assert(std::is_nothrow_constructible<_tPointer>(),
81 "Invalid pointer type found");
82
83 public:
84 using PointerType = _tPointer;
85
86 private:
87 static PointerType _ptr;
88
92 static void
93 Check()
94 {
95 if(!_ptr)
96 _ptr = PointerType(new _type());
97 }
98
99 public:
101 DefDelCtor(GStaticCache)
102
103 static DefGetter(ynothrow, PointerType, PointerRaw, _ptr)
107 static const PointerType&
108 GetPointer()
109 {
110 Check();
111 return GetPointerRaw();
112 }
116 static _type&
117 GetInstance()
118 {
119 Check();
120 return *GetPointer();
121 }
122
128 static void
129 Release() ynothrow
130 {
131 reset(_ptr);
132 }
133 };
134
135 template<typename _type, typename _tPointer>
136 typename GStaticCache<_type, _tPointer>::PointerType
137 GStaticCache<_type, _tPointer>::_ptr;
138
139
148 template<typename _type, typename _tPointer = _type*>
149 class GLocalStaticCache : private noncopyable
150 {
151 static_assert(std::is_nothrow_constructible<_tPointer>(),
152 "Invalid pointer type found");
153
154 public:
155 using PointerType = _tPointer;
156
157 private:
162 static PointerType&
163 GetStaticPtrRef() ynothrow
164 {
165 return ystdex::parameterize_static_object<PointerType,
166 GLocalStaticCache>();
167 }
168
172 static void
173 Check()
174 {
175 PointerType& ptr(GetStaticPtrRef());
176
177 if(!ptr)
178 ptr = PointerType(new _type());
179 }
180
181 public:
183 DefDelCtor(GLocalStaticCache)
184
185 static DefGetter(ynothrow, PointerType, PointerRaw, GetStaticPtrRef())
190 static const PointerType&
191 GetPointer()
192 {
193 Check();
194 return GetPointerRaw();
195 }
199 static _type&
200 GetInstance()
201 {
202 Check();
203 return *GetPointer();
204 }
205
211 static void
212 Release() ynothrow
213 {
214 reset(GetStaticPtrRef());
215 }
216 };
217
218 } // namespace YSLib;
219
220 #endif
221
YSLib::GLocalStaticCache
全局局部静态单态存储器。
Definition: YStorage.hpp:149
YSLib::GLocalStaticCache::PointerType
_tPointer PointerType
Definition: YStorage.hpp:155
YSLib::FetchInstance
_type FetchInstance()
取指定类型的无参数构造的对象。
Definition: YStorage.hpp:43
YSLib
YSLib 命名空间。
Definition: AndroidHost.h:45
YSLib::GLocalStaticCache::GetInstance
static _type & GetInstance()
取实例引用。
Definition: YStorage.hpp:200
YSLib::reset
bool reset(_type *&p) ynothrow
Definition: YShellDefinition.h:46
YSLib::GStaticCache
全局静态单态存储器。
Definition: YStorage.hpp:78
YSLib::GStaticCache::PointerRaw
PointerRaw
Definition: YStorage.hpp:103
YSLib::GStaticCache::GetPointer
static _ptr const PointerType & GetPointer()
取指针。
Definition: YStorage.hpp:108
YSLib::GLocalStaticCache::Check
static void Check()
检查是否已经初始化，否则构造新对象。
Definition: YStorage.hpp:173
YSLib::FetchStaticRef
_type & FetchStaticRef()
取指定类型的静态默认对象。
Definition: YStorage.hpp:54
YSLib::GLocalStaticCache::PointerRaw
PointerRaw
Definition: YStorage.hpp:185
YSLib::ynothrow
YB_PURE const _type size_t n ynothrow
Definition: YCoreUtilities.h:168
YSLib::GStaticCache::_ptr
static PointerType _ptr
Definition: YStorage.hpp:87
YSLib::GLocalStaticCache::Release
static void Release() ynothrow
删除对象并置指针为空值。
Definition: YStorage.hpp:212
DefGetter
#define DefGetter(_q, _t, _n,...)
Definition: YBaseMacro.h:241
YSLib::GStaticCache::Check
static void Check()
检查是否已经初始化，否则构造新对象。
Definition: YStorage.hpp:93
YSLib::GStaticCache::DefDelCtor
DefDelCtor(GStaticCache) static DefGetter(ynothrow
YModules.h
YSLib::FetchPrototype
const _type & FetchPrototype()
取指定类型的静态原型对象。
Definition: YStorage.hpp:65
ystdex::noncopyable
不可复制对象：禁止派生类调用默认原型的复制构造函数和复制赋值操作符。
Definition: base.h:41
YSLib::GStaticCache::Release
static void Release() ynothrow
删除对象并置指针为空值。
Definition: YStorage.hpp:129
YSLib::GLocalStaticCache::GetStaticPtrRef
static PointerType & GetStaticPtrRef() ynothrow
取静态指针引用。
Definition: YStorage.hpp:163
YSLib::GStaticCache::GetInstance
static _type & GetInstance()
取实例引用。
Definition: YStorage.hpp:117
YSLib::GStaticCache::PointerType
_tPointer PointerType
Definition: YStorage.hpp:84
ystdex::parameterize_static_object
_type & parameterize_static_object()
类型参数化静态对象。
Definition: utility.hpp:323
YSLib::GLocalStaticCache::DefDelCtor
DefDelCtor(GLocalStaticCache) static DefGetter(ynothrow