YSTest  PreAlpha_b400_20130424
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
memory.hpp
浏览该文件的文档.
1 /*
2  Copyright by FrankHB 2011 - 2013.
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 YB_INC_ystdex_memory_hpp_
29 #define YB_INC_ystdex_memory_hpp_ 1
30 
31 #include "type_op.hpp" // for ../ydef.h and is_pointer;
32 #include <memory>
33 
34 namespace ystdex
35 {
36 
45 template<typename _tIterator>
46 yconstfn bool
47 is_undereferenceable(const _tIterator&)
48 {
49  return false;
50 }
51 template<typename _type>
52 yconstfn bool
54 {
55  return !bool(p);
56 }
58 
59 
65 template<typename _type>
66 yconstfn _type*
67 raw(_type* const& p) ynothrow
68 {
69  return p;
70 }
71 template<typename _type>
73 raw(const std::unique_ptr<_type>& p) ynothrow -> decltype(p.get())
74 {
75  return p.get();
76 }
77 template<typename _type>
78 yconstfn _type*
79 raw(const std::shared_ptr<_type>& p) ynothrow
80 {
81  return p.get();
82 }
83 template<typename _type>
84 yconstfn _type*
85 raw(const std::weak_ptr<_type>& p) ynothrow
86 {
87  return p.lock().get();
88 }
90 
97 template<typename _type>
98 inline bool
99 reset(std::unique_ptr<_type>& p) ynothrow
100 {
101  if(p.get())
102  {
103  p.reset();
104  return true;
105  }
106  return false;
107 }
108 template<typename _type>
109 inline bool
110 reset(std::shared_ptr<_type>& p) ynothrow
111 {
112  if(p.get())
113  {
114  p.reset();
115  return true;
116  }
117  return false;
118 }
120 
121 
134 template<typename _type, typename _pSrc>
135 yconstfn std::unique_ptr<_type>
136 unique_raw(const _pSrc& p)
137 {
138  static_assert(is_pointer<_pSrc>::value, "Invalid type found.");
139 
140  return std::unique_ptr<_type>(p);
141 }
146 template<typename _type, typename _pSrc>
147 yconstfn std::unique_ptr<_type>
148 unique_raw(_pSrc&& p)
149 {
150  static_assert(is_pointer<_pSrc>::value, "Invalid type found.");
152  return std::unique_ptr<_type>(p);
153 }
154 template<typename _type>
155 yconstfn std::unique_ptr<_type>
156 unique_raw(_type* p)
157 {
158  return std::unique_ptr<_type>(p);
159 }
164 template<typename _type>
165 yconstfn std::unique_ptr<_type>
166 unique_raw(nullptr_t) ynothrow
167 {
168  return std::unique_ptr<_type>();
169 }
171 
172 
185 template<typename _type, typename _pSrc>
186 yconstfn std::shared_ptr<_type>
187 share_raw(const _pSrc& p)
188 {
189  static_assert(is_pointer<_pSrc>::value, "Invalid type found.");
190 
191  return std::shared_ptr<_type>(p);
192 }
197 template<typename _type, typename _pSrc>
198 yconstfn std::shared_ptr<_type>
199 share_raw(_pSrc&& p)
200 {
201  static_assert(is_pointer<_pSrc>::value, "Invalid type found.");
202 
203  return std::shared_ptr<_type>(p);
204 }
205 template<typename _type>
206 yconstfn std::shared_ptr<_type>
207 share_raw(_type* p)
208 {
209  return std::shared_ptr<_type>(p);
210 }
215 template<typename _type>
216 yconstfn std::shared_ptr<_type>
217 share_raw(nullptr_t) ynothrow
218 {
219  return std::shared_ptr<_type>();
220 }
222 
223 
232 template<typename _type, typename... _tParams>
233 yconstfn std::unique_ptr<_type>
234 make_unique(_tParams&&... args)
235 {
236  return std::unique_ptr<_type>(new _type(yforward(args)...));
237 }
238 
247 template<typename _type, typename... _tParams>
248 yconstfn std::shared_ptr<_type>
249 make_shared(_tParams&&... args)
250 {
251  return std::shared_ptr<_type>(new _type(yforward(args)...));
252 }
253 
254 } // namespace ystdex;
255 
256 #endif
257