Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Concepts
memory.hpp
Go to the documentation of this file.
1
6/*
7 * This file is part of Algorithms library toolkit.
8 * Copyright (C) 2017 Jan Travnicek (jan.travnicek@fit.cvut.cz)
9
10 * Algorithms library toolkit is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14
15 * Algorithms library toolkit is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19
20 * You should have received a copy of the GNU General Public License
21 * along with Algorithms library toolkit. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24#pragma once
25
26#include <memory>
27#include <utility>
28#include <compare>
29
30#include "type_traits.hpp"
31#include "clone.hpp"
32
33namespace ext {
34
45template < class T >
46class cow_shared_ptr : std::shared_ptr < T > {
47public:
48 using std::shared_ptr < T >::shared_ptr;
49
50 using std::shared_ptr < T >::operator =;
51
52#ifndef __clang__
59 cow_shared_ptr ( const cow_shared_ptr & other ) = default;
60
67 cow_shared_ptr ( cow_shared_ptr && other ) noexcept = default;
68
73 ~cow_shared_ptr ( ) noexcept = default;
74
81 cow_shared_ptr & operator =( const cow_shared_ptr & other ) = default;
82
89 cow_shared_ptr & operator =( cow_shared_ptr && other ) noexcept = default;
90#endif
91
96 void reset ( ) noexcept {
98 }
99
106 template < class Y >
107 void reset( Y * ptr ) {
108 return std::shared_ptr < T >::reset ( ptr );
109 }
110
117 T * operator ->( ) {
118 return this->get ( );
119 }
120
127 T const * operator ->( ) const {
128 return this->get ( );
129 }
130
137 T & operator *( ) {
138 return * this->get ( );
139 }
140
147 T const & operator *( ) const {
148 return * this->get ( );
149 }
150
156 T * get ( ) {
157 make_unique ( );
159 }
160
166 T const * get ( ) const {
168 }
169
176 bool unique ( ) const {
178 }
179
186 unsigned use_count ( ) const {
187 return std::shared_ptr < T >::use_count ( );
188 }
189
195 explicit operator bool( ) const {
196 return use_count ( ) != 0;
197 }
198
199private:
200
205 void make_unique ( ) {
206 if ( unique ( ) ) return;
207
208 static_cast < std::shared_ptr < T > & > ( * this ) = std::shared_ptr < T > ( ext::clone ( std::shared_ptr < T >::operator * ( ) ) );
209 }
210
218 friend void swap ( cow_shared_ptr & first, cow_shared_ptr & second ) {
219 std::swap ( static_cast < std::shared_ptr < T > & > ( first ), static_cast < std::shared_ptr < T > & > ( second ) );
220 }
221
222};
223
232template < class T >
238 T * m_Data;
239
240public:
245 explicit smart_ptr ( ) : m_Data ( nullptr ) {
246 }
247
252 explicit smart_ptr ( T * data ) : m_Data ( data ) {
253 }
254
263 template < class R >
264 smart_ptr ( smart_ptr < R > other ) : m_Data ( other.release ( ) ) {
265 }
266
273 smart_ptr ( const smart_ptr & other ) : m_Data ( ext::clone ( * other.m_Data ) ) {
274 }
275
282 smart_ptr ( smart_ptr && other ) noexcept : m_Data ( other.release ( ) ) {
283 }
284
289 ~smart_ptr ( ) noexcept {
290 delete m_Data;
291 }
292
299 smart_ptr & operator =( const smart_ptr & other ) {
300 if ( this == & other ) return * this;
301
302 delete m_Data;
303 m_Data = ext::clone ( * other.m_Data );
304
305 return * this;
306 }
307
314 smart_ptr & operator =( smart_ptr && other ) noexcept {
315 using std::swap;
316
317 swap ( this->m_Data, other.m_Data );
318 return * this;
319 }
320
327 T * operator ->( ) {
328 return m_Data;
329 }
330
337 T * operator ->( ) const {
338 return m_Data;
339 }
340
347 T & operator *( ) {
348 return * m_Data;
349 }
350
357 T & operator *( ) const {
358 return * m_Data;
359 }
360
367 T * get ( ) {
368 return m_Data;
369 }
370
377 T * get ( ) const {
378 return m_Data;
379 }
380
387 T * release ( ) {
388 T * res = m_Data;
389
390 m_Data = nullptr;
391 return res;
392 }
393
400 explicit operator bool( ) const {
401 return static_cast < bool > ( m_Data );
402 }
403
404};
405
417template < class T >
418std::ostream & operator <<( std::ostream & out, const ext::cow_shared_ptr < T > & ptr ) {
419 out << * ptr;
420 return out;
421}
422
434template < class T >
435std::ostream & operator <<( std::ostream & out, const ext::smart_ptr < T > & ptr ) {
436 out << * ptr;
437 return out;
438}
439
440} /* namespace ext */
441
Specialisation of copy on write pointer for classes based with copy on write pointer base.
Definition: memory.hpp:46
void reset() noexcept
Sets the shared pointer to nullptr.
Definition: memory.hpp:96
cow_shared_ptr(cow_shared_ptr &&other) noexcept=default
Move constructor to create new instance of shared pointer reffering the same data.
T & operator*()
Operator dereference to access the inner managed pointer.
Definition: memory.hpp:137
T * get()
Definition: memory.hpp:156
bool unique() const
Tests whether the managed pointer is referenced from one location only (or the class stores null poin...
Definition: memory.hpp:176
friend void swap(cow_shared_ptr &first, cow_shared_ptr &second)
Specialisation of swap method to copy on write shared pointers.
Definition: memory.hpp:218
T const * get() const
Definition: memory.hpp:166
cow_shared_ptr(const cow_shared_ptr &other)=default
Copy constructor to create new instance of shared pointer reffering the same data.
unsigned use_count() const
Getter of the number how many times the managed pointer is referenced.
Definition: memory.hpp:186
void reset(Y *ptr)
Sets the shared pointer to value.
Definition: memory.hpp:107
T * operator->()
Operator arrow to chain dereferece to inner managed pointer.
Definition: memory.hpp:117
~cow_shared_ptr() noexcept=default
The destructor of the shared pointer.
Managed pointer simulating value like behavior.
Definition: memory.hpp:233
smart_ptr(smart_ptr &&other) noexcept
Move constructor of the smart pointer. Passes ownership of the managed resource from source to constr...
Definition: memory.hpp:282
smart_ptr()
Default initialization to null.
Definition: memory.hpp:245
T * operator->()
Operator arrow to chain dereferece to inner managed pointer.
Definition: memory.hpp:327
smart_ptr(const smart_ptr &other)
Copy constructor of the smart pointer. Internally uses clone (if available) or copy constructor of th...
Definition: memory.hpp:273
smart_ptr(T *data)
Constructor which takes ownership of the provided pointer.
Definition: memory.hpp:252
T * get() const
Getter of the raw managed pointer.
Definition: memory.hpp:377
~smart_ptr() noexcept
The destructor of the shared pointer, responsible for freeing the managed resource.
Definition: memory.hpp:289
T & operator*()
Operator dereference to access the inner managed pointer.
Definition: memory.hpp:347
smart_ptr(smart_ptr< R > other)
Conversion constructor to simplify type casting.
Definition: memory.hpp:264
T * release()
Releases the shared resource and returns it.
Definition: memory.hpp:387
T * get()
Getter of the raw managed pointer.
Definition: memory.hpp:367
smart_ptr & operator=(const smart_ptr &other)
Copy operator of assignment. Internally uses clone (if available) or copy constructor of the managed ...
Definition: memory.hpp:299
p second
Definition: ToRegExpAlgebraic.h:126
return res
Definition: MinimizeByPartitioning.h:145
Definition: sigHandler.cpp:20
ForwardIterator unique(ForwardIterator first, ForwardIterator last)
Shuffles values in a sequece so that consecutive duplicate values are pushed to the front and others ...
Definition: algorithm.hpp:384
auto clone(T &&tmp)
Wrapper around clone by means of using copy constructor or clone method if available.
Definition: clone.hpp:41
std::ostream & operator<<(ext::reference_wrapper< std::ostream > &os, std::ostream &(*const func)(std::ostream &))
Overloaded function allowing same operations on wrapped output stream as on the actual output stream,...
Definition: GlobalData.cpp:33
void swap(ext::linear_set< T, Compare, Alloc > &x, ext::linear_set< T, Compare, Alloc > &y)
Specialisation of swap for linear set.
Definition: linear_set.hpp:846
void reset()
Definition: measurements.cpp:24
auto & get(ext::ptr_array< Type, N > &tpl)
Specialisation of get function for pointer arrays.
Definition: ptr_array.hpp:693
void swap(ext::managed_linear_set< T, Compare, Alloc > &x, ext::managed_linear_set< T, Compare, Alloc > &y)
Specialisation of swap for linear set.
Definition: managed_linear_set.hpp:864