xtd 0.2.0
unique_ptr_object.hpp
Go to the documentation of this file.
1
4#pragma once
6#define __XTD_CORE_INTERNAL__
10#undef __XTD_CORE_INTERNAL__
11#include "hash_code.hpp"
12#include "icomparable.hpp"
13#include "iequatable.hpp"
14#include "null_ptr.hpp"
15#include "object.hpp"
16#include <memory>
17
19namespace xtd {
21 namespace linq {
22 class enumerable;
23 }
25
27
41 template<class type_t, class deleter_t>
42 class unique_ptr_object : public xtd::object, public xtd::icomparable<unique_ptr_object<type_t, deleter_t>>, public xtd::iequatable<unique_ptr_object<type_t, deleter_t >> {
43 public:
45
48 using base_type = std::unique_ptr<type_t, deleter_t>;
50 using deleter_type = typename base_type::deleter_type;
52 using element_type = typename base_type::element_type;
54 using pointer_type = typename base_type::pointer;
56
58
61 static const unique_ptr_object empty;
63
65
68 unique_ptr_object() noexcept = default;
71 unique_ptr_object(xtd::null_ptr null) noexcept : ptr_ {null} {} // Can't be explicit by design.
74 explicit unique_ptr_object(pointer_type ptr) noexcept : ptr_ {ptr} {}
78 unique_ptr_object(pointer_type ptr, const deleter_t& deleter) noexcept : ptr_ {ptr, deleter} {}
82 unique_ptr_object(pointer_type ptr, deleter_t&& deleter) noexcept : ptr_ {ptr, deleter} {}
85 unique_ptr_object(unique_ptr_object&& value) noexcept : ptr_ {std::move(value.ptr_)} {}
88 template<class value_t, class other_deleter_t>
89 unique_ptr_object(unique_ptr_object<value_t, other_deleter_t>&& value) : ptr_ {std::move(value)} {}
92 template<class value_t, class other_deleter_t>
93 unique_ptr_object(std::unique_ptr<value_t, other_deleter_t>&& value) : ptr_ {std::move(value)} {}
96 unique_ptr_object(const unique_ptr_object& value) = delete;
99 unique_ptr_object(const base_type& value) = delete;
100
103 template<class pointer_t>
104 explicit unique_ptr_object(pointer_t* ptr) noexcept : ptr_ {ptr} {}
108 template<class pointer_t, class other_deleter_t>
109 unique_ptr_object(pointer_t* ptr, other_deleter_t deleter) noexcept : ptr_ {ptr, deleter} {}
110
113 template<class value_t>
114 unique_ptr_object(unique_ptr_object<value_t>&& value) noexcept : ptr_ {std::move(value.ptr_)} {}
117 unique_ptr_object(base_type&& value) noexcept : ptr_ {std::move(value)} {}
120 template<class value_t>
121 unique_ptr_object(std::unique_ptr<value_t>&& value) noexcept : ptr_ {std::move(value)} {}
123
125
129 const base_type& pointer() const noexcept {return ptr_;}
132 base_type& pointer() noexcept {return ptr_;}
134
136
148 int32 compare_to(const unique_ptr_object& obj) const noexcept override {return to_pointer() < obj.to_pointer() ? -1 : to_pointer() > obj.to_pointer() ? 1 : 0;}
149
153 bool equals(const xtd::object& value) const noexcept override {return dynamic_cast<const unique_ptr_object*>(&value) && equals(static_cast<const unique_ptr_object&>(value));}
157 bool equals(const unique_ptr_object& value) const noexcept override {return ptr_ == value.ptr_;}
158
161 element_type* get() const noexcept {return ptr_.get();}
162
165 xtd::size get_hash_code() const noexcept override {return (ptr_ ? xtd::hash_code::combine(to_pointer()) : 0);}
166
169 pointer_type release() noexcept {return ptr_.release();}
170
173 void reset() noexcept {ptr_.reset();}
177 void reset(pointer_type ptr) noexcept {ptr_.reset(ptr);}
181 template<class pointer_t>
182 void reset(pointer_t* ptr) noexcept {ptr_.reset(ptr);}
185 void reset(xtd::null_ptr null) noexcept {ptr_.reset(null);}
186
187
190 void swap(unique_ptr_object& ptr) noexcept {ptr_.swap(ptr.ptr_);}
191
194 type_t& to_object() const noexcept {return *to_pointer();}
195
199 template<typename target_t>
200 target_t to_object() const;
201
204 element_type* to_pointer() const noexcept {return get();}
205
209 template<typename target_t>
210 target_t* to_pointer() const;
211
214 xtd::string to_string() const noexcept override;
216
218
222 unique_ptr_object& operator =(unique_ptr_object&& value) noexcept {
223 ptr_ = std::move(value.ptr_);
224 return *this;
225 }
226
229 template<class value_t>
230 unique_ptr_object& operator =(unique_ptr_object<value_t>&& value) noexcept {
231 ptr_ = std::move(value.ptr_);
232 return *this;
233 }
234
237 template<class value_t>
238 unique_ptr_object& operator =(std::unique_ptr<value_t>&& value) noexcept {
239 ptr_ = std::move(value);
240 return *this;
241 }
242
245 type_t& operator *() const noexcept {return ptr_.operator * ();}
246
249 type_t* operator ->() const noexcept {return ptr_.operator ->();}
250
254 element_type& operator[](std::ptrdiff_t index) const {return ptr_.operator [](index);}
255
258 explicit operator bool() const noexcept {return ptr_.operator bool();}
260
261 private:
262 template<class value_t, class other_deleter_t>
263 friend class xtd::unique_ptr_object;
264
265 template<class value_t, class allocator_t>
266 friend class xtd::basic_array;
267
268 template<class value_t, class allocator_t>
269 friend class xtd::collections::generic::list;
270
271 friend class xtd::linq::enumerable;
272
273 unique_ptr_object& operator =(const unique_ptr_object& value) noexcept {xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_operation);}
274
275 base_type ptr_;
276 };
277
278 template<class type_t, class deleter_t>
279 inline const unique_ptr_object<type_t, deleter_t> unique_ptr_object<type_t, deleter_t>::empty;
281
283 // C++17 deduction guides for xtd::reference_wrapper_object
284 // {
285 template<class type_t>
286 unique_ptr_object(type_t*) -> unique_ptr_object<type_t, std::default_delete<type_t>>;
287 template<class type_t, class deleter_t>
288 unique_ptr_object(type_t*, const deleter_t&) -> unique_ptr_object<type_t, deleter_t>;
289 template<class type_t, class deleter_t>
290 unique_ptr_object(xtd::unique_ptr_object<type_t, deleter_t>()) -> unique_ptr_object<type_t, deleter_t>;
291 template<class type_t, class deleter_t>
292 unique_ptr_object(std::unique_ptr<type_t, deleter_t>()) -> unique_ptr_object<type_t, deleter_t>;
293 // }
294}
295
296template<class object_t>
297xtd::unique_ptr_object<object_t> xtd::object::memberwise_clone() const {
298 auto object_ptr = dynamic_cast<const object_t*>(this);
300 return xtd::unique_ptr_object<object_t>(new object_t(*object_ptr));
301}
Contains xtd::array definitions.
Contains xtd::collections::generic::list definitions.
Contains xtd::unique_ptr_object definition.
static xtd::size combine(args_t... values) noexcept
Combines values into a hash code.
Definition hash_code.hpp:70
static void throws(xtd::helpers::exception_case exception_case, const source_location &location=source_location::current())
Throws an exption with specified exception case.
Provides a set of static methods for querying objects that implement ienumerable <type_t>.
Definition enumerable.hpp:58
xtd::unique_ptr_object< object_t > memberwise_clone() const
Creates a shallow copy of the current object.
@ invalid_cast
The cast is not valid.
Definition exception_case.hpp:61
@ invalid_operation
The operation is not valid.
Definition exception_case.hpp:63
xtd::basic_string< char > string
Represents text as a sequence of UTF-8 code units.
Definition __string_definitions.hpp:43
size_t size
Represents a size of any object in bytes.
Definition size.hpp:23
null_ptr null
Represents a null pointer value.
std::nullptr_t null_ptr
Represents the null_opt alias on std::nullptr_t.
Definition null_ptr.hpp:19
int32_t int32
Represents a 32-bit signed integer.
Definition int32.hpp:23
xtd::sptr< type_t > ptr
The xtd::ptr object is a shared pointer.
Definition ptr.hpp:27
@ release
Build type release.
Definition build_type.hpp:24
Contains xtd::hash_code class.
Contains xtd::icomparable interface.
Contains xtd::iequatable interface.
Provides classes and interfaces that support queries that use Language-Integrated Query (LINQ).
Definition enumerable.hpp:41
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
string to_string() const noexcept override
Returns the string representation of this xtd::read_only_span <type_t> object.
Definition read_only_span.hpp:375
bool equals(const object &obj) const noexcept override
Determines whether the specified object is equal to the current object.
Definition read_only_span.hpp:272
constexpr bool empty() const noexcept
Returns a value that indicates whether the current xtd::read_only_span <type_t> is empty.
Definition read_only_span.hpp:205
xtd::size get_hash_code() const noexcept override
Serves as a hash function for a particular type.
Definition read_only_span.hpp:296
Contains xtd::null_ptr alias.
Contains xtd::object class.
Contains xtd::helpers::exception_case enum class.