Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
ptr_value.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
27#include <extensions/clone.hpp>
28
29namespace ext {
30
39template < class T >
40class ptr_value {
45 T * m_data;
46
47public:
52 explicit ptr_value ( const T & value ) : m_data ( ext::clone ( value ) ) {
53
54 }
55
60 explicit ptr_value ( T && value ) : m_data ( ext::clone ( std::forward < T > ( value ) ) ) {
61
62 }
63
72 template < class U >
73 ptr_value ( ptr_value < U > && other ) : m_data ( other.m_data ) {
74 other.m_data = nullptr;
75 }
76
83 ptr_value ( const ptr_value & other ) : m_data ( ext::clone ( other.m_data ) ) {
84
85 }
86
93 ptr_value ( ptr_value && other ) noexcept : m_data ( other.m_data ) {
94 other.m_data = nullptr;
95 };
96
103 ptr_value & operator = ( const ptr_value & other ) {
104 if ( this == & other )
105 return *this;
106
107 delete m_data;
108 m_data = ext::clone ( other.m_data );
109 return * this;
110 }
111
118 ptr_value & operator = ( ptr_value && other ) noexcept {
119 using std::swap;
120
121 swap ( m_data, other.m_data );
122
123 return *this;
124 }
125
130 ~ptr_value ( ) noexcept {
131 delete m_data;
132 }
133
140 T * operator ->( ) && {
141 return m_data;
142 }
143
150 T * operator ->( ) & {
151 return m_data;
152 }
153
160 const T * operator ->( ) const & {
161 return m_data;
162 }
163
170 operator T & ( ) & {
171 return * m_data;
172 }
173
182 operator const T && ( ) const & {
183 return std::move ( * m_data );
184 }
185
192 operator T && ( ) && {
193 return std::move ( * m_data );
194 }
195
202 const T & get ( ) const {
203 return * m_data;
204 }
205
206 template < class U >
207 friend class ptr_value;
208};
209
210} /* namespace ext */
211
Class representing wrapper of dynamically allocated object behaving like rvalue reference.
Definition: ptr_value.hpp:40
const T & get() const
Getter of the holded value.
Definition: ptr_value.hpp:202
ptr_value & operator=(const ptr_value &other)
Copy assignment will clone the pointer referenced value in source instance to create new one for the ...
Definition: ptr_value.hpp:103
T * operator->() &&
Arrow operator to chain dereference.
Definition: ptr_value.hpp:140
ptr_value(ptr_value< U > &&other)
User conversion constructor from ptr_value for type U where T is its predecessor.
Definition: ptr_value.hpp:73
ptr_value(T &&value)
Constructor of the ptr_value from dynaically allocated object. The class will take ownerhip of the po...
Definition: ptr_value.hpp:60
ptr_value(const T &value)
Constructor of the ptr_value from dynaically allocated object. The class will take ownerhip of the po...
Definition: ptr_value.hpp:52
ptr_value(ptr_value &&other) noexcept
Move construction will transfer ownership of the pointer from source instance to newly created one.
Definition: ptr_value.hpp:93
ptr_value(const ptr_value &other)
Copy construction will clone the pointer referenced value in source instance to create new one for th...
Definition: ptr_value.hpp:83
~ptr_value() noexcept
Destructor will free the owned instance.
Definition: ptr_value.hpp:130
Definition: sigHandler.cpp:20
auto clone(T &&tmp)
Wrapper around clone by means of using copy constructor or clone method if available.
Definition: clone.hpp:41
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
Definition: FordFulkerson.hpp:16
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