YSTest  PreAlpha_b500_20140530
The YSLib Test Project
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
memory.hpp
浏览该文件的文档.
1 /*
2  © 2011-2014 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 YB_INC_ystdex_memory_hpp_
29 #define YB_INC_ystdex_memory_hpp_ 1
30 
31 #include "type_op.hpp" // for ../ydef.h, is_pointer, is_array, extent,
32 // enable_if_t and remove_extent_t;
33 #include <memory>
34 
35 namespace ystdex
36 {
37 
43 template<typename _type>
44 yconstfn _type*
45 get_raw(_type* const& p) ynothrow
46 {
47  return p;
48 }
49 template<typename _type>
50 yconstfn auto
51 get_raw(const std::unique_ptr<_type>& p) ynothrow -> decltype(p.get())
52 {
53  return p.get();
54 }
55 template<typename _type>
56 yconstfn _type*
57 get_raw(const std::shared_ptr<_type>& p) ynothrow
58 {
59  return p.get();
60 }
61 template<typename _type>
62 yconstfn _type*
63 get_raw(const std::weak_ptr<_type>& p) ynothrow
64 {
65  return p.lock().get();
66 }
68 
75 template<typename _type>
76 inline bool
77 reset(std::unique_ptr<_type>& p) ynothrow
78 {
79  if(p.get())
80  {
81  p.reset();
82  return true;
83  }
84  return false;
85 }
86 template<typename _type>
87 inline bool
88 reset(std::shared_ptr<_type>& p) ynothrow
89 {
90  if(p.get())
91  {
92  p.reset();
93  return true;
94  }
95  return false;
96 }
98 
99 
112 template<typename _type, typename _pSrc>
113 yconstfn std::unique_ptr<_type>
114 unique_raw(const _pSrc& p)
115 {
116  static_assert(is_pointer<_pSrc>::value, "Invalid type found.");
117 
118  return std::unique_ptr<_type>(p);
119 }
124 template<typename _type, typename _pSrc>
125 yconstfn std::unique_ptr<_type>
126 unique_raw(_pSrc&& p)
127 {
128  static_assert(is_pointer<_pSrc>::value, "Invalid type found.");
129 
130  return std::unique_ptr<_type>(p);
131 }
132 template<typename _type>
133 yconstfn std::unique_ptr<_type>
134 unique_raw(_type* p)
135 {
136  return std::unique_ptr<_type>(p);
137 }
142 template<typename _type>
143 yconstfn std::unique_ptr<_type>
145 {
146  return std::unique_ptr<_type>();
147 }
149 
150 
163 template<typename _type, typename _pSrc>
164 yconstfn std::shared_ptr<_type>
165 share_raw(const _pSrc& p)
166 {
167  static_assert(is_pointer<_pSrc>::value, "Invalid type found.");
168 
169  return std::shared_ptr<_type>(p);
170 }
175 template<typename _type, typename _pSrc>
176 yconstfn std::shared_ptr<_type>
177 share_raw(_pSrc&& p)
178 {
179  static_assert(is_pointer<_pSrc>::value, "Invalid type found.");
180 
181  return std::shared_ptr<_type>(p);
182 }
183 template<typename _type>
184 yconstfn std::shared_ptr<_type>
185 share_raw(_type* p)
186 {
187  return std::shared_ptr<_type>(p);
188 }
193 template<typename _type>
194 yconstfn std::shared_ptr<_type>
196 {
197  return std::shared_ptr<_type>();
198 }
200 
201 
211 template<typename _type, typename... _tParams>
212 yconstfn yimpl(enable_if_t<!is_array<_type>::value, std::unique_ptr<_type>>)
213 make_unique(_tParams&&... args)
214 {
215  return std::unique_ptr<_type>(new _type(yforward(args)...));
216 }
217 template<typename _type, typename... _tParams>
218 yconstfn yimpl(enable_if_t<is_array<_type>::value && extent<_type>::value == 0,
219  std::unique_ptr<_type>>)
220 make_unique(size_t size)
221 {
222  return std::unique_ptr<_type>(new remove_extent_t<_type>[size]());
223 }
224 template<typename _type, typename... _tParams>
225 yimpl(enable_if_t<extent<_type>::value != 0, void>)
226 make_unique(_tParams&&...) = delete;
228 
237 template<typename _type, typename... _tParams>
238 yconstfn std::shared_ptr<_type>
239 make_shared(_tParams&&... args)
240 {
241  return std::shared_ptr<_type>(new _type(yforward(args)...));
242 }
243 
244 } // namespace ystdex;
245 
246 #endif
247 
yconstfn const string _tParams && args
Definition: Loader.h:111
std::unique_ptr< _type > unique_raw(const _pSrc &p)
Definition: memory.hpp:114
std::shared_ptr< _type > make_shared(_tParams &&...args)
使用 new 和指定参数构造指定类型的 std::shared_ptr 实例。
Definition: memory.hpp:239
std::shared_ptr< _type > share_raw(const _pSrc &p)
Definition: memory.hpp:165
typename remove_extent< _type >::type remove_extent_t
Definition: type_op.hpp:249
#define yforward(_expr)
根据参数类型使用 std::forward 传递对应参数。
Definition: ydef.h:722
_type * get_raw(_type *const &p)
Definition: memory.hpp:45
#define yimpl(...)
实现标签。
Definition: ydef.h:177
#define ynothrow
YSLib 无异常抛出保证:若支持 noexcept 关键字, 指定特定的 noexcept 异常规范。
Definition: ydef.h:514
空指针类。
Definition: ydef.h:583
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:463
bool reset(std::unique_ptr< _type > &p)
Definition: memory.hpp:77
C++ 类型操作。
enable_if_t<!is_array< _type >::value, std::unique_ptr< _type > > make_unique(_tParams &&...args)
使用 new 和指定参数构造指定类型的 std::unique_ptr 实例。
Definition: memory.hpp:213
typename enable_if< _bCond, _type >::type enable_if_t
Definition: type_op.hpp:274