-
Notifications
You must be signed in to change notification settings - Fork 0
/
GetSet.h
108 lines (92 loc) · 2.55 KB
/
GetSet.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#pragma once
#include<functional>
#include<iostream>
using namespace std;
template<typename T>
class GetSet {
private:
T item;
//bool _useDefaultGetSet = false;
std::function<T(T &)> _getterFunction = nullptr;// [](T& currentItem)-> T& {return currentItem; };
std::function<void(T &, T const &)> _setterFunction = nullptr;// [](T& currentItem, T newValue) {curr = newValue; };
//COMMENT line below, if you want to have copy constructor
GetSet(const GetSet<T>& src) = delete;
public:
//DE-COMMENT lines below, if you want to have copy constructor and assignement
/*GetSet(const GetSet<T>& src) {
*this = src.factory_copyValueGetterSetter();
}
GetSet<T>& operator=(GetSet<T> & src) {
if (this != &src) {
try {
// if(this->item!=nullptr) delete this->item
}
catch (...) {}
*this = src.factory_copyValueGetterSetter();
}
return *this;
}
*/
GetSet() :item(T()) {}
GetSet(T tt) :item(tt) {}
GetSet(T *tt) :item(*tt){}
GetSet(GetSet<T>&& src) :
item(std::move(src.item)),
_getterFunction(std::move(src._getterFunction)),_setterFunction(std::move(src._setterFunction)) {}
void setGetter(std::function<T (T &)> getterFunction) { _getterFunction = getterFunction; }
void setSetter(std::function<void(T &, T const &)> setterFunction) { _setterFunction = setterFunction; }
//operator T&() {
operator T() {
if (_getterFunction != nullptr ) {
return _getterFunction(item);
}
else {
return item;
}
}
GetSet<T>& operator=(const T & new_value) {
if (_setterFunction != nullptr ) {
_setterFunction(item, new_value);
}else {
item = new_value;
}
return *this;
}
GetSet<T>& operator=(GetSet<T> && src) {
*this = std::move(src);
return nullptr;
}
GetSet<T> factory_copyValue(){
GetSet<T> retObj;
retObj.item = T(item);
return retObj;
}
GetSet<T> factory_copyValueGetterSetter(){
GetSet<T> retObj;
retObj.item = T(this->item);
retObj._getterFunction = this->_getterFunction;
retObj._setterFunction = this->_setterFunction;
return retObj;
}
/* GetSet<T>& GetSet<T>::operator=(T && new_value) {
if (_setterFunction != nullptr) {
_setterFunction(item, std::move(new_value));
}
else {
item =std::move(new_value);
}
return *this;
}*/
friend std::ostream& operator<< (std::ostream& stream, GetSet<T>& getSet) {
//T &aus = getSet;//recall the getter
T aus = getSet;//recall the getter
return stream << aus;
}
friend std::istream& operator >> (std::istream& stream, GetSet<T>& getSet) {
T aus;
stream >> aus;
getSet = std::move(aus);
return stream;
}
};
//ccAlgorithms2