YSTest: ystorage.hpp 源文件

YSTest PreAlpha_b400_20130424 The YSLib Test Project

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

- 文件列表
- 文件成员

全部 类 命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义 组 页
ystorage.hpp
浏览该文件的文档.
1 ﻿
2 /*
3 Copyright (C) by Franksoft 2011 - 2012.
4
5 This file is part of the YSLib project, and may only be used,
6 modified, and distributed under the terms of the YSLib project
7 license, LICENSE.TXT. By continuing to use, modify, or distribute
8 this file you indicate that you have read the license and
9 understand and accept it fully.
10 */
11
29 #ifndef INCLUDED_CORE_YSTORAGE_HPP_
30 #define INCLUDED_CORE_YSTORAGE_HPP_ 1
31
32 #include "ycutil.h"
33
34 YSL_BEGIN
35
40 template<typename _type>
41 inline _type
42 FetchInstance()
43 {
44 return _type();
45 }
46
52 template<typename _type>
53 inline _type&
54 FetchStaticRef()
55 {
56 static _type _obj;
57
58 return _obj;
59 }
60
66 template<typename _type>
67 inline const _type&
68 FetchPrototype()
69 {
70 static _type _obj;
71
72 return _obj;
73 }
74
75
82 template<typename _type, typename _tPointer = _type*>
83 class GStaticCache
84 {
85 static_assert(std::is_nothrow_constructible<_tPointer>::value,
86 "Invalid pointer type found");
87
88 public:
89 typedef _tPointer PointerType;
90
91 private:
92 static PointerType _ptr;
93
94 GStaticCache();
95
99 static void
100 Check()
101 {
102 if(!_ptr)
103 _ptr = PointerType(new _type());
104 }
105
106 public:
107 static DefGetter(ynothrow, PointerType, PointerRaw, _ptr)
111 static PointerType
112 GetPointer()
113 {
114 Check();
115 return GetPointerRaw();
116 }
120 static _type&
121 GetInstance()
122 {
123 Check();
124 return *GetPointer();
125 }
126
131 static inline void
132 Release() ynothrow
133 {
134 safe_delete_obj()(_ptr);
135 }
136 };
137
138 template<typename _type, typename _tPointer>
139 typename GStaticCache<_type, _tPointer>::PointerType
140 GStaticCache<_type, _tPointer>::_ptr;
141
142
149 template<typename _type, typename _tPointer = _type*>
150 class GLocalStaticCache
151 {
152 static_assert(std::is_nothrow_constructible<_tPointer>::value,
153 "Invalid pointer type found");
154
155 public:
156 typedef _tPointer PointerType;
157
158 private:
159 static PointerType _ptr;
160
161 GLocalStaticCache();
162
167 static inline PointerType&
168 GetStaticPtrRef() ynothrow
169 {
170 static PointerType ptr;
171
172 return ptr;
173 }
174
178 static void
179 Check()
180 {
181 PointerType& ptr(GetStaticPtrRef());
182
183 if(!ptr)
184 ptr = PointerType(new _type());
185 }
186
187 public:
188 static DefGetter(ynothrow, PointerType, PointerRaw, GetStaticPtrRef())
192 static PointerType
193 GetPointer()
194 {
195 Check();
196 return GetPointerRaw();
197 }
201 static _type&
202 GetInstance()
203 {
204 Check();
205 return *GetPointer();
206 }
207
212 static inline void
213 Release() ynothrow
214 {
215 safe_delete_obj()(GetStaticPtrRef());
216 }
217 };
218
219 YSL_END
220
221 #endif
222