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 <memory>
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 inline 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 shared_ptr_object(pointer_t* ptr) noexcept : ptr_ {ptr} {}
70 template<class value_t>
71 explicit shared_ptr_object(shared_ptr_object<value_t>&& value) noexcept : ptr_ {std::move(value.ptr_)} {}
74 template<class value_t>
75 explicit shared_ptr_object(const shared_ptr_object<value_t>& value) noexcept : ptr_ {value.ptr_} {}
78 template<class value_t>
79 explicit shared_ptr_object(base_type&& value) noexcept : ptr_ {std::move(value)} {}
82 template<class value_t>
83 explicit shared_ptr_object(const std::shared_ptr<value_t>& value) noexcept : ptr_ {value} {}
85
87
91 bool is_unique() const noexcept {return use_count() == xtd::size {1};}
92
96 template<class value_t>
97 bool owner_before(const shared_ptr_object<value_t>& other) const noexcept {return ptr_.owner_before(other.ptr_);}
101 template<class value_t>
102 bool owner_before(const std::shared_ptr<value_t>& other) const noexcept {return ptr_.owner_before(other);}
106 template<class value_t>
107 bool owner_before( const std::weak_ptr<value_t>& other) const noexcept {return ptr_owner_before(other);}
108
111 base_type pointer() const noexcept {return ptr_;}
112
115 xtd::size use_count() const noexcept {return static_cast<xtd::size>(ptr_.use_count());}
117
119
131 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;}
132
136 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));}
140 bool equals(const shared_ptr_object& value) const noexcept override {return ptr_ == value.ptr_;}
141
144 element_type* get() const noexcept {return ptr_.get();}
145
148 xtd::size get_hash_code() const noexcept override {return (ptr_ ? xtd::hash_code::combine(*ptr_) : 0);}
149
152 void reset() noexcept {ptr_.reset();}
156 template<class pointer_t>
157 void reset(pointer_t* ptr) noexcept {ptr_.reset(ptr);}
158
161 void swap(shared_ptr_object& ptr) noexcept {ptr_.swap(ptr.ptr_);}
162
165 type_t& to_object() const noexcept {return *to_pointer();}
166
170 template<typename target_t>
171 target_t to_object() const;
172
175 type_t* to_pointer() const noexcept {return get();}
176
180 template<typename target_t>
181 target_t* to_pointer() const;
182
185 xtd::string to_string() const noexcept override;
187
189
193 shared_ptr_object& operator =(shared_ptr_object&& value) noexcept {
194 ptr_ = std::move(value.ptr_);
195 return *this;
196 }
197
201 ptr_ = value.ptr_;
202 return *this;
203 }
204
207 template<class value_t>
209 ptr_ = std::move(value.ptr_);
210 return *this;
211 }
212
215 template<class value_t>
217 ptr_ = value.ptr_;
218 return *this;
219 }
220
223 template<class value_t>
224 shared_ptr_object& operator =(std::shared_ptr<value_t>&& value) noexcept {
225 ptr_ = std::move(value);
226 return *this;
227 }
228
231 template<class value_t>
232 shared_ptr_object& operator =(const std::shared_ptr<value_t>& value) noexcept {
233 ptr_ = value;
234 return *this;
235 }
236
239 type_t& operator *() const noexcept {return ptr_.operator *();}
240
243 type_t* operator ->() const noexcept {return ptr_.operator ->();}
244
248 element_type& operator[](std::ptrdiff_t index) const {return ptr_.operator [](index);}
249
252 explicit operator bool() const noexcept {return ptr_.operator bool();}
253
256 operator base_type() const noexcept {return ptr_;}
258
259 private:
260 template<class other_t>
261 friend class shared_ptr_object;
262 mutable base_type ptr_;
263 };
265}
Represents text as a sequence of character units.
Definition basic_string.hpp:71
static xtd::size combine(args_t... values) noexcept
Combines values into a hash code.
Definition hash_code.hpp:70
Defines a generalized comparison method that a value type or class implements to create a type-specif...
Definition icomparable.hpp:21
Defines a generalized method that a value type or class implements to create a type-specific method f...
Definition iequatable.hpp:22
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.hpp:43
The xtd::shared_ptr_object is a shared pointer as std::shared_ptr.
Definition shared_ptr_object.hpp:30
element_type & operator[](std::ptrdiff_t index) const
Provides indexed access to the stored array.
Definition shared_ptr_object.hpp:248
bool equals(const shared_ptr_object &value) const noexcept override
Indicates whether the current object is equal to another object of the same type.
Definition shared_ptr_object.hpp:140
type_t * operator->() const noexcept
Gets the stored pointer.
Definition shared_ptr_object.hpp:243
type_t & operator*() const noexcept
Gets the stored object.
Definition shared_ptr_object.hpp:239
shared_ptr_object() noexcept=default
Initializes a new instance of the xtd::shared_ptr_object class.
shared_ptr_object(shared_ptr_object< value_t > &&value) noexcept
Initializes a new instance of the xtd::shared_ptr_object class with specified shared pointer object.
Definition shared_ptr_object.hpp:71
bool is_unique() const noexcept
Checks whether the managed object is managed only by the current xtd::shared_ptr_object object.
Definition shared_ptr_object.hpp:91
void reset() noexcept
Reset the current object. Set the current object to null.
Definition shared_ptr_object.hpp:152
bool equals(const xtd::object &value) const noexcept override
Determines whether the specified object is equal to the current object.
Definition shared_ptr_object.hpp:136
bool owner_before(const shared_ptr_object< value_t > &other) const noexcept
Provides owner-based ordering of shared pointers.
Definition shared_ptr_object.hpp:97
void reset(pointer_t *ptr) noexcept
Reset the current object. Set the current object with specified pointer.
Definition shared_ptr_object.hpp:157
shared_ptr_object & operator=(shared_ptr_object &&value) noexcept
Assignment operator with specified xtd::shared_ptr_object value.
Definition shared_ptr_object.hpp:193
type_t & to_object() const noexcept
Gets the stored object.
Definition shared_ptr_object.hpp:165
shared_ptr_object(const shared_ptr_object &value) noexcept
Initializes a new instance of the xtd::shared_ptr_object class with specified shared pointer object.
Definition shared_ptr_object.hpp:63
shared_ptr_object(pointer_t *ptr) noexcept
Initializes a new instance of the xtd::shared_ptr_object class with specified pointer.
Definition shared_ptr_object.hpp:67
base_type pointer() const noexcept
Returns the underlying base type pointer.
Definition shared_ptr_object.hpp:111
int32 compare_to(const shared_ptr_object &obj) const noexcept override
Compares the current instance with another object of the same type.
Definition shared_ptr_object.hpp:131
static const shared_ptr_object empty
Represents the empty xtd::shared_ptr_object. This field is constant.
Definition shared_ptr_object.hpp:47
element_type * get() const noexcept
Gets the stored pointer.
Definition shared_ptr_object.hpp:144
shared_ptr_object(shared_ptr_object &&value) noexcept
Initializes a new instance of the xtd::shared_ptr_object class with specified shared pointer object.
Definition shared_ptr_object.hpp:60
xtd::string to_string() const noexcept override
Returns a xtd::string that represents the current object.
typename base_type::weak_type weak_type
Represent the weak type.
Definition shared_ptr_object.hpp:40
void swap(shared_ptr_object &ptr) noexcept
Swaps this current instance with specified shared pointer object.
Definition shared_ptr_object.hpp:161
shared_ptr_object(const std::shared_ptr< value_t > &value) noexcept
Initializes a new instance of the xtd::shared_ptr_object class with specified shared pointer object.
Definition shared_ptr_object.hpp:83
shared_ptr_object(base_type &&value) noexcept
Initializes a new instance of the xtd::shared_ptr_object class with specified shared pointer object.
Definition shared_ptr_object.hpp:79
bool owner_before(const std::weak_ptr< value_t > &other) const noexcept
Provides owner-based ordering of shared pointers.
Definition shared_ptr_object.hpp:107
shared_ptr_object(const shared_ptr_object< value_t > &value) noexcept
Initializes a new instance of the xtd::shared_ptr_object class with specified shared pointer object.
Definition shared_ptr_object.hpp:75
bool owner_before(const std::shared_ptr< value_t > &other) const noexcept
Provides owner-based ordering of shared pointers.
Definition shared_ptr_object.hpp:102
std::shared_ptr< type_t > base_type
Represents the base type (std::shared_ptr<type_t>)
Definition shared_ptr_object.hpp:36
xtd::size get_hash_code() const noexcept override
Serves as a hash function for a particular type.
Definition shared_ptr_object.hpp:148
type_t * to_pointer() const noexcept
Gets the stored pointer.
Definition shared_ptr_object.hpp:175
target_t to_object() const
Gets the stored object with specified target_t type.
xtd::size use_count() const noexcept
Gets the number of xtd::shared_ptr_object objects referring to the same managed object.
Definition shared_ptr_object.hpp:115
target_t * to_pointer() const
Gets the stored pointer with specified target_t type.
typename base_type::element_type element_type
Represent the element type.
Definition shared_ptr_object.hpp:38
int32_t int32
Represents a 32-bit signed integer.
Definition int32.hpp:23
null_ptr null
Represents a null pointer value.
size_t size
Represents a size of any object in bytes.
Definition size.hpp:23
std::nullptr_t null_ptr
Represents the null_opt alias on std::nullptr_t.
Definition null_ptr.hpp:19
@ other
The operating system is other.
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 xtd_about_box.hpp:10
Contains xtd::null_ptr alias.
Contains xtd::object class.