xtd 0.2.0
shared_ptr_object.hpp
Go to the documentation of this file.
1
4#pragma once
5#include "hash_code.hpp"
6#include "icomparable.hpp"
7#include "iequatable.hpp"
8#include "null_ptr.hpp"
9#include "object.hpp"
10#include "unique_ptr_object.hpp"
11
13namespace xtd {
15
29 template<class type_t>
30 class shared_ptr_object : public xtd::object, public xtd::icomparable<shared_ptr_object<type_t>>, public xtd::iequatable<shared_ptr_object<type_t>> {
31 public:
33
36 using base_type = std::shared_ptr<type_t>;
38 using element_type = typename base_type::element_type;
40 using weak_type = typename base_type::weak_type;
42
44
47 static const shared_ptr_object empty;
49
51
54 shared_ptr_object() noexcept = default;
57 shared_ptr_object(xtd::null_ptr null) noexcept : ptr_ {null} {} // Can't be explicit by design.
60 shared_ptr_object(shared_ptr_object&& value) noexcept : ptr_ {std::move(value.ptr_)} {}
63 shared_ptr_object(const shared_ptr_object& value) noexcept : ptr_ {value.ptr_} {}
66 template<class pointer_t>
67 explicit shared_ptr_object(pointer_t* ptr) noexcept : ptr_ {ptr} {}
70 template<class value_t>
71 shared_ptr_object(shared_ptr_object<value_t>&& value) noexcept : ptr_ {std::move(value.ptr_)} {}
74 template<class value_t>
75 shared_ptr_object(const shared_ptr_object<value_t>& value) noexcept : ptr_ {value.ptr_} {}
78 shared_ptr_object(const base_type& value) noexcept : ptr_ {value} {}
81 shared_ptr_object(base_type&& value) noexcept : ptr_ {std::move(value)} {}
84 template<class value_t>
85 shared_ptr_object(const std::shared_ptr<value_t>& value) noexcept : ptr_ {value} {}
88 template<class value_t>
89 explicit shared_ptr_object(const std::weak_ptr<value_t>& value) noexcept : ptr_ {value} {}
92 template<class value_t, class deleter_t>
93 shared_ptr_object(xtd::unique_ptr_object<value_t, deleter_t>&& value) : ptr_ {std::move(value.pointer())} {}
96 template<class value_t, class deleter_t>
97 shared_ptr_object(std::unique_ptr<value_t, deleter_t>&& value) : ptr_ {std::move(value)} {}
99
101
105 bool is_unique() const noexcept {return use_count() == xtd::size {1};}
106
110 template<class value_t>
111 bool owner_before(const shared_ptr_object<value_t>& other) const noexcept {return ptr_.owner_before(other.ptr_);}
115 template<class value_t>
116 bool owner_before(const std::shared_ptr<value_t>& other) const noexcept {return ptr_.owner_before(other);}
120 template<class value_t>
121 bool owner_before( const std::weak_ptr<value_t>& other) const noexcept {return ptr_.owner_before(other);}
122
125 const base_type& pointer() const noexcept {return ptr_;}
126
129 base_type& pointer() noexcept {return ptr_;}
130
133 xtd::size use_count() const noexcept {return static_cast<xtd::size>(ptr_.use_count());}
135
137
149 int32 compare_to(const shared_ptr_object& obj) const noexcept override {return to_pointer() < obj.to_pointer() ? -1 : to_pointer() > obj.to_pointer() ? 1 : 0;}
150
154 bool equals(const xtd::object& value) const noexcept override {return dynamic_cast<const shared_ptr_object*>(&value) && equals(static_cast<const shared_ptr_object&>(value));}
158 bool equals(const shared_ptr_object& value) const noexcept override {return ptr_ == value.ptr_;}
159
162 element_type* get() const noexcept {return ptr_.get();}
163
166 xtd::size get_hash_code() const noexcept override {return (ptr_ ? xtd::hash_code::combine(to_pointer()) : 0);}
167
170 void reset() noexcept {ptr_.reset();}
174 template<class pointer_t>
175 void reset(pointer_t* ptr) noexcept {ptr_.reset(ptr);}
178 void reset(xtd::null_ptr null) noexcept {ptr_.reset();}
179
182 void swap(shared_ptr_object& ptr) noexcept {ptr_.swap(ptr.ptr_);}
183
186 type_t& to_object() const noexcept {return *to_pointer();}
187
191 template<typename target_t>
192 target_t to_object() const;
193
196 element_type* to_pointer() const noexcept {return get();}
197
201 template<typename target_t>
202 target_t* to_pointer() const;
203
206 xtd::string to_string() const noexcept override;
208
210
214 shared_ptr_object& operator =(shared_ptr_object&& value) noexcept {
215 ptr_ = std::move(value.ptr_);
216 return *this;
217 }
218
221 shared_ptr_object& operator =(const shared_ptr_object& value) noexcept {
222 ptr_ = value.ptr_;
223 return *this;
224 }
225
228 template<class value_t>
229 shared_ptr_object& operator =(shared_ptr_object<value_t>&& value) noexcept {
230 ptr_ = std::move(value.ptr_);
231 return *this;
232 }
233
236 template<class value_t>
237 shared_ptr_object& operator =(const shared_ptr_object<value_t>& value) noexcept {
238 ptr_ = value.ptr_;
239 return *this;
240 }
241
244 template<class value_t>
245 shared_ptr_object& operator =(std::shared_ptr<value_t>&& value) noexcept {
246 ptr_ = std::move(value);
247 return *this;
248 }
249
252 template<class value_t>
253 shared_ptr_object& operator =(const std::shared_ptr<value_t>& value) noexcept {
254 ptr_ = value;
255 return *this;
256 }
257
260 type_t& operator *() const noexcept {return ptr_.operator *();}
261
264 type_t* operator ->() const noexcept {return ptr_.operator ->();}
265
269 element_type& operator[](std::ptrdiff_t index) const {return ptr_.operator [](index);}
270
273 explicit operator bool() const noexcept {return ptr_.operator bool();}
274
277 operator base_type() const noexcept {return ptr_;}
278
281 operator weak_type() const noexcept {return weak_type {ptr_};}
283
284 private:
285 template<class other_t>
286 friend class shared_ptr_object;
287 base_type ptr_;
288 };
289
290 template<class type_t>
291 inline const shared_ptr_object<type_t> shared_ptr_object<type_t>::empty;
293
295 // C++17 deduction guides for xtd::reference_wrapper_object
296 // {
297 template<class type_t>
298 shared_ptr_object(type_t*) -> shared_ptr_object<type_t>;
299 template<class type_t>
300 shared_ptr_object(std::weak_ptr<type_t>()) -> shared_ptr_object<type_t>;
301 template<class type_t, class deleter_t>
302 shared_ptr_object(xtd::unique_ptr_object<type_t, deleter_t>()) -> shared_ptr_object<type_t>;
303 template<class type_t, class deleter_t>
304 shared_ptr_object(std::unique_ptr<type_t, deleter_t>()) -> shared_ptr_object<type_t>;
305 // }
306}
static xtd::size combine(args_t... values) noexcept
Combines values into a hash code.
Definition hash_code.hpp:70
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
@ other
The operating system is other.
Definition platform_id.hpp:58
Contains xtd::hash_code class.
Contains xtd::icomparable interface.
Contains xtd::iequatable interface.
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::unique_ptr_object class.