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
|
/*properties
- how use create 1 property with macro:
PROPERTY(TypeName,PropertyName,getfunction, setfunction)
- never forget to do 1 Copy Constructor inside of class's, that uses the property,
for avoid copy the 'this' value\adress and use a diferent memory adress
*/
template <typename T>
class property
{
private:
T PropertyValue;
std::function<T(void)> getf;
std::function<void(T)> setf;
public:
property(const T value)
{
getf=nullptr;
setf=nullptr;
PropertyValue=value;
};
property(const property &value) : PropertyValue(value.PropertyValue) , getf(value.getf)
{
}
property(std::function<T(void)> GetFunction=nullptr,std::function<void(T)> SetFunction=nullptr)
{
setf=SetFunction;
getf=GetFunction;
}
property& operator=(const T &value)
{
PropertyValue=value;
return *this;
}
property& operator=(const property &value)
{
PropertyValue = value.PropertyValue;
getf = value.getf;
return *this;
}
friend ostream& operator<<(ostream& os, property& dt)
{
if(dt.getf==nullptr && dt.setf==nullptr)
os << dt.PropertyValue;
else if (dt.getf!=nullptr)
os << dt.getf();
return os;
}
friend istream& operator>>(istream &input, property &dt)
{
input >> dt.PropertyValue;
if (dt.setf!=nullptr)
dt.setf(dt.PropertyValue);
return input;
}
friend istream &getline(istream &in, property &dt)
{
getline(in, dt.PropertyValue);
if (dt.setf!=nullptr)
dt.setf(dt.PropertyValue);
return in;
}
};
template<typename T, typename Fnc1_t, typename Fnc2_t, typename classthis>
property<T> GetProperty(Fnc1_t Getter, Fnc2_t Setter, classthis clsthis)
{
return property<T>(std::bind(Getter, clsthis), std::bind(Setter, clsthis, std::placeholders::_1));
}
#define PROPERTY(TypeName,PropertyName,getfunction, setfunction) \
property<TypeName> PropertyName{std::bind(&getfunction, *this),std::bind(&setfunction, *this, std::placeholders::_1)}
|