Реализации на С++
Property
Общая реализация на языке С++
#include <iostream>
#include <memory>
using namespace std;
template <typename Owner, typename Type>
class Property
{
using Getter = Type(Owner::*)() const;
using Setter = void (Owner::*)(const Type&);
private:
Owner* owner;
Getter methodGet;
Setter methodSet;
public:
Property() = default;
Property(Owner* const owr, Getter getmethod, Setter setmethod) : owner(owr), methodGet(getmethod), methodSet(setmethod) {}
void init(Owner* const owr, Getter getmethod, Setter setmethod)
{
owner = owr;
methodGet = getmethod;
methodSet = setmethod;
}
operator Type() // Getter
{
return (owner->*methodGet)();
}
void operator=(const Type& data) // Setter
{
(owner->*methodSet)(data);
}
};
class Object
{
private:
double value;
public:
Object(double v) : value(v)
{
Value.init(this, &Object::getValue, &Object::setValue);
}
double getValue() const
{
return value;
}
void setValue(const double& v)
{
value = v;
}
Property<Object, double> Value;
};
int main()
{
Object obj(5.);
cout << "value = " << obj.Value << endl;
obj.Value = 10.;
cout << "value = " << obj.Value << endl;
unique_ptr<Object> ptr = make_unique<Object>(15.);
cout << "value =" << ptr->Value << endl;
obj = *ptr;
obj.Value = ptr->Value;
}
Last updated
Was this helpful?