2dkg/2dkg_zad5/2dgk_zad5/Property.h

55 lines
1.5 KiB
C
Raw Permalink Normal View History

2022-01-20 08:52:14 +01:00
#pragma once
namespace KapitanGame
{
template <class T, class C>
class ReadOnlyProperty {
protected:
C* Owner;
T(C::* Getter)();
public:
ReadOnlyProperty() : Owner(), Getter() {}
ReadOnlyProperty(C* owner, T(C::* getter)()) : Owner(owner), Getter(getter) {}
[[nodiscard]] T Get() const {
return (Owner->*Getter)();
}
// ReSharper disable once CppNonExplicitConversionOperator
operator T() const {
return Get();
}
};
template <class T, class C>
class Property : public ReadOnlyProperty<T,C> {
void(C::* Setter)(T);
public:
Property() : ReadOnlyProperty(), Setter() {}
Property(C* owner, T(C::* getter)(), void(C::* setter)(T)) : ReadOnlyProperty<T, C>(owner,getter), Setter(setter) {}
void Set(T value) const {
(this->Owner->*Setter)(value);
}
const Property<T, C>& operator = (T value) const { // NOLINT(misc-unconventional-assign-operator)
Set(value);
return *this;
}
const Property<T, C>& operator -= (T value) const { // NOLINT(misc-unconventional-assign-operator)
Set(this->Get() - value);
return *this;
}
const Property<T, C>& operator += (T value) const { // NOLINT(misc-unconventional-assign-operator)
Set(this->Get() + value);
return *this;
}
const Property<T, C>& operator *= (T value) const { // NOLINT(misc-unconventional-assign-operator)
Set(this->Get() * value);
return *this;
}
const Property<T, C>& operator /= (T value) const { // NOLINT(misc-unconventional-assign-operator)
Set(this->Get() / value);
return *this;
}
};
}