YSTest: ystorage.hpp 源文件

YSTest PreAlpha_b500_20140530 The YSLib Test Project

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

- 文件列表
- 文件成员

全部 类 命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义 组 页
ystorage.hpp
浏览该文件的文档.
1 /*
2 © 2011-2013 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
33
34 namespace YSLib
35 {
36
41 template<typename _type>
42 inline _type
43 FetchInstance()
44 {
45 return _type();
46 }
47
53 template<typename _type>
54 inline _type&
55 FetchStaticRef()
56 {
57 static _type _obj;
58
59 return _obj;
60 }
61
67 template<typename _type>
68 inline const _type&
69 FetchPrototype()
70 {
71 static _type _obj;
72
73 return _obj;
74 }
75
76
83 template<typename _type, typename _tPointer = _type*>
84 class GStaticCache
85 {
86 static_assert(std::is_nothrow_constructible<_tPointer>::value,
87 "Invalid pointer type found");
88
89 public:
90 using PointerType = _tPointer;
91
92 private:
93 static PointerType _ptr;
94
95 GStaticCache();
96
100 static void
101 Check()
102 {
103 if(!_ptr)
104 _ptr = PointerType(new _type());
105 }
106
107 public:
108 static DefGetter(ynothrow, PointerType, PointerRaw, _ptr)
112 static PointerType
113 GetPointer()
114 {
115 Check();
116 return GetPointerRaw();
117 }
121 static _type&
122 GetInstance()
123 {
124 Check();
125 return *GetPointer();
126 }
127
132 static inline void
133 Release() ynothrow
134 {
135 safe_delete_obj()(_ptr);
136 }
137 };
138
139 template<typename _type, typename _tPointer>
140 typename GStaticCache<_type, _tPointer>::PointerType
141 GStaticCache<_type, _tPointer>::_ptr;
142
143
150 template<typename _type, typename _tPointer = _type*>
151 class GLocalStaticCache
152 {
153 static_assert(std::is_nothrow_constructible<_tPointer>::value,
154 "Invalid pointer type found");
155
156 public:
157 using PointerType = _tPointer;
158
159 private:
160 static PointerType _ptr;
161
162 GLocalStaticCache();
163
168 static inline PointerType&
169 GetStaticPtrRef() ynothrow
170 {
171 static PointerType ptr;
172
173 return ptr;
174 }
175
179 static void
180 Check()
181 {
182 PointerType& ptr(GetStaticPtrRef());
183
184 if(!ptr)
185 ptr = PointerType(new _type());
186 }
187
188 public:
189 static DefGetter(ynothrow, PointerType, PointerRaw, GetStaticPtrRef())
193 static PointerType
194 GetPointer()
195 {
196 Check();
197 return GetPointerRaw();
198 }
202 static _type&
203 GetInstance()
204 {
205 Check();
206 return *GetPointer();
207 }
208
213 static inline void
214 Release() ynothrow
215 {
216 safe_delete_obj()(GetStaticPtrRef());
217 }
218 };
219
220 } // namespace YSLib;
221
222 #endif
223
YSLib::GLocalStaticCache
全局局部静态单态存储器。
Definition: ystorage.hpp:151
YSLib::FetchPrototype
const _type & FetchPrototype()
取指定类型的静态原型对象。
Definition: ystorage.hpp:69
YModules.h
YSLib::GStaticCache::DefGetter
static DefGetter(ynothrow, PointerType, PointerRaw, _ptr) static PointerType GetPointer()
取指针。
Definition: ystorage.hpp:108
YSLib::GStaticCache::PointerType
_tPointer PointerType
Definition: ystorage.hpp:90
YSLib::GLocalStaticCache::GetStaticPtrRef
static PointerType & GetStaticPtrRef() ynothrow
取静态指针引用。
Definition: ystorage.hpp:169
YSLib::GLocalStaticCache::GLocalStaticCache
GLocalStaticCache()
YSLib::GStaticCache::Release
static void Release() ynothrow
删除对象并置指针为空值。
Definition: ystorage.hpp:133
YSLib::FetchStaticRef
_type & FetchStaticRef()
取指定类型的静态默认对象。
Definition: ystorage.hpp:55
YSLib::GLocalStaticCache::_ptr
static PointerType _ptr
Definition: ystorage.hpp:160
YSLib::GStaticCache
全局静态单态存储器。
Definition: ystorage.hpp:84
YSLib::GStaticCache::Check
static void Check()
检查是否已经初始化，否则构造新对象。
Definition: ystorage.hpp:101
YSLib::safe_delete_obj
带置空指针操作的 delete 仿函数。
Definition: ycutil.h:447
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:514
YSLib::GStaticCache::GetInstance
static _type & GetInstance()
取实例引用。
Definition: ystorage.hpp:122
YSLib::GLocalStaticCache::DefGetter
static DefGetter(ynothrow, PointerType, PointerRaw, GetStaticPtrRef()) static PointerType GetPointer()
取指针。
Definition: ystorage.hpp:189
YSLib::GStaticCache::_ptr
static PointerType _ptr
Definition: ystorage.hpp:93
YSLib::GLocalStaticCache::Release
static void Release() ynothrow
删除对象并置指针为空值。
Definition: ystorage.hpp:214
YSLib::GLocalStaticCache::Check
static void Check()
检查是否已经初始化，否则构造新对象。
Definition: ystorage.hpp:180
YSLib::GStaticCache::GStaticCache
GStaticCache()
YSLib::FetchInstance
_type FetchInstance()
取指定类型的无参数构造的对象。
Definition: ystorage.hpp:43
YSLib::GLocalStaticCache::PointerType
_tPointer PointerType
Definition: ystorage.hpp:157
YSLib::GLocalStaticCache::GetInstance
static _type & GetInstance()
取实例引用。
Definition: ystorage.hpp:203