Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
managed_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
26#include <compare>
27
28namespace ext {
29
36template < class T >
42 T m_data;
43
44 std::vector < std::function < void ( const T & ) > > changeCallbacks;
45
46 void fireChange ( const T & element ) {
47 for ( const std::function < void ( const T & ) > & callback : changeCallbacks ) {
48 callback ( element );
49 }
50 }
51
52public:
53 void addChangeCallback ( const std::function < void ( const T & ) > & callback ) {
54 changeCallbacks.push_back ( callback );
55 }
56
61 typedef T value_type;
62
70 managed_value ( const T & value ) : m_data ( value ) {
71 }
72
80 managed_value ( T && value ) : m_data ( std::move ( value ) ) {
81 }
82
89 managed_value ( const managed_value & x ) : m_data ( x.m_data ) {
90 }
91
98 managed_value ( managed_value && x ) : m_data ( std::move ( x.m_data ) ) {
99 }
100
106 }
107
117 m_data = x.m_data;
118 fireChange ( m_data );
119 return *this;
120 }
121
131 m_data = std::move ( x.m_data );
132 fireChange ( m_data );
133 return *this;
134 }
135
142 void swap ( managed_value & x ) {
143 // intentionally swaped values the change fired is before its execution
144 this->fireChange ( x.getValue ( ) );
145 x.fireChange ( this->getValue ( ) );
146
147 using std::swap;
148 swap ( m_data, x.m_data );
149 }
150
159 auto operator <=> ( const managed_value < T > & other ) const = default;
160
161};
162
172template < class T >
174 x.swap ( y );
175}
176
188template< class T >
189std::ostream & operator << ( std::ostream & out, const ext::managed_value < T > & value ) {
190 out << value.getValue ( );
191 return out;
192}
193
194} /* namespace ext */
Implementation of managed value mimicking the behavior of the underlying value. The class is designed...
Definition: managed_value.hpp:37
auto operator<=>(const managed_value< T > &other) const =default
Compares two set instances by less relation.
T value_type
The type of values in the set.
Definition: managed_value.hpp:61
void addChangeCallback(const std::function< void(const T &) > &callback)
Definition: managed_value.hpp:53
~managed_value()
The destructor of the linear set.
Definition: managed_value.hpp:105
managed_value(const T &value)
Definition: managed_value.hpp:70
managed_value(const managed_value &x)
Copy constructor.
Definition: managed_value.hpp:89
managed_value & operator=(const managed_value &x)
Copy operator of assignmet.
Definition: managed_value.hpp:116
managed_value(managed_value &&x)
Move constructor.
Definition: managed_value.hpp:98
void swap(managed_value &x)
Swaps two instances of linear set.
Definition: managed_value.hpp:142
managed_value(T &&value)
Definition: managed_value.hpp:80
Definition: sigHandler.cpp:20
int callback(struct dl_phdr_info *info, size_t, void *data)
Definition: simpleStacktrace.cpp:25
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
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