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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
template <typename T>
class Vector2
{
public:
Vector2() : __mX(0), __mY(0) {}
Vector2(T __X, T __Y) { set(__X, __Y); }
void set(T __X, T __Y) { __mX = __X; __mY = __Y; }
T x() const { return __mX; }
T y() const { return __mY; }
private:
T __mX;
T __mY;
};
class LObject
{
public:
LObject(const LObject* parent = nullptr, int PropertyId = -1) :
__mParent(parent),
__mId(PropertyId)
{ }
virtual void PropertyChanged(int __PropertyId) const
{
std::cout << __PropertyId << std::endl;
}
protected:
const LObject* __mParent;
int __mId;
};
class Property : protected LObject
{
public:
Property(const LObject* parent = nullptr, int PropertyId = -1) :
LObject(parent, PropertyId)
{}
};
template <typename T>
class Position : protected Property
{
public:
Position(const LObject* parent = nullptr, int PropertyId = -1) :
Property(parent, PropertyId)
{}
Position(T __X, T __Y, const LObject* parent = nullptr, int PropertyId = -1) :
Property(parent, PropertyId),
__mPosition(__X, __Y)
{}
void set(T __X, T __Y)
{
__mPosition.set(__X, __Y);
if(__mParent != nullptr)
__mParent->PropertyChanged(__mId);
}
Vector2<T> operator()() const { return __mPosition; }
private:
Vector2<T> __mPosition;
};
template <typename T>
class Origin : protected Property
{
public:
Origin(const LObject* parent = nullptr, int PropertyId = -1) :
Property(parent, PropertyId)
{}
Origin(T __X, T __Y, const LObject* parent = nullptr, int PropertyId = -1) :
Property(parent, PropertyId),
__mOrigin(__X, __Y)
{}
void set(T __X, T __Y)
{
__mOrigin.set(__X, __Y);
if(__mParent != nullptr)
__mParent->PropertyChanged(__mId);
}
Vector2<T> operator()() const { return __mOrigin; }
private:
Vector2<T> __mOrigin;
};
class Angle : protected Property
{
public:
Angle(const LObject* parent = nullptr, int PropertyId = -1) :
Property(parent, PropertyId)
{}
Angle(float __Angle, const LObject* parent = nullptr, int PropertyId = -1) :
Property(parent, PropertyId),
__mAngle(__Angle)
{}
void set(float __Angle)
{
__mAngle = __Angle;
if(__mParent != nullptr)
__mParent->PropertyChanged(__mId);
}
float operator()() const { return __mAngle; }
private:
float __mAngle;
};
template <typename T>
class BaseObject2D : public LObject
{
public:
BaseObject2D() :
position(this, __POSITION_ID),
origin(this, __ORIGIN_ID),
angle(this, __ANGLE_ID)
{
}
Position<T> position;
Origin<T> origin;
Angle angle;
virtual void PropertyChanged(int __PropertyId) { /* do stuff */ }
private:
enum
{
__POSITION_ID,
__ORIGIN_ID,
__ANGLE_ID
};
};
int main(void)
{
BaseObject2D<float> obj;
obj.position.set(50, 20);
obj.origin.set(-1, 1);
obj.angle.set(180);
return 0;
}
|