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
44 {
45  return _type();
46 }
47 
53 template<typename _type>
54 inline _type&
56 {
57  static _type _obj;
58 
59  return _obj;
60 }
61 
67 template<typename _type>
68 inline const _type&
70 {
71  static _type _obj;
72 
73  return _obj;
74 }
75 
76 
83 template<typename _type, typename _tPointer = _type*>
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
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&
123  {
124  Check();
125  return *GetPointer();
126  }
127 
132  static inline void
134  {
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*>
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:
161 
163 
168  static inline PointerType&
170  {
171  static PointerType ptr;
172 
173  return ptr;
174  }
175 
179  static void
181  {
183 
184  if(!ptr)
185  ptr = PointerType(new _type());
186  }
187 
188 public:
193  static PointerType
194  GetPointer()
195  {
196  Check();
197  return GetPointerRaw();
198  }
202  static _type&
204  {
205  Check();
206  return *GetPointer();
207  }
208 
213  static inline void
215  {
217  }
218 };
219 
220 } // namespace YSLib;
221 
222 #endif
223 
全局局部静态单态存储器。
Definition: ystorage.hpp:151
const _type & FetchPrototype()
取指定类型的静态原型对象。
Definition: ystorage.hpp:69
static DefGetter(ynothrow, PointerType, PointerRaw, _ptr) static PointerType GetPointer()
取指针。
Definition: ystorage.hpp:108
_tPointer PointerType
Definition: ystorage.hpp:90
static PointerType & GetStaticPtrRef() ynothrow
取静态指针引用。
Definition: ystorage.hpp:169
static void Release() ynothrow
删除对象并置指针为空值。
Definition: ystorage.hpp:133
_type & FetchStaticRef()
取指定类型的静态默认对象。
Definition: ystorage.hpp:55
static PointerType _ptr
Definition: ystorage.hpp:160
全局静态单态存储器。
Definition: ystorage.hpp:84
static void Check()
检查是否已经初始化,否则构造新对象。
Definition: ystorage.hpp:101
带置空指针操作的 delete 仿函数。
Definition: ycutil.h:447
#define ynothrow
YSLib 无异常抛出保证:若支持 noexcept 关键字, 指定特定的 noexcept 异常规范。
Definition: ydef.h:514
static _type & GetInstance()
取实例引用。
Definition: ystorage.hpp:122
static DefGetter(ynothrow, PointerType, PointerRaw, GetStaticPtrRef()) static PointerType GetPointer()
取指针。
Definition: ystorage.hpp:189
static PointerType _ptr
Definition: ystorage.hpp:93
static void Release() ynothrow
删除对象并置指针为空值。
Definition: ystorage.hpp:214
static void Check()
检查是否已经初始化,否则构造新对象。
Definition: ystorage.hpp:180
_type FetchInstance()
取指定类型的无参数构造的对象。
Definition: ystorage.hpp:43
static _type & GetInstance()
取实例引用。
Definition: ystorage.hpp:203