First solution is not good, because will be many classes to add clone function to all, and user may forgot about it.
Second solution not works if handle is on base type.
I want it use for gui system, where i want clone gui classes to create multiinstance screens.
class GUIObject // Base class
{
GUIObject@ Clone() { return clone(this); } // I want clone whole object in base class
void Draw() { OnDraw(); }
void OnDraw() {}
void Move() { OnMove(); }
void OnMove() {}
}
class UserGUIObject : GUIObject // User can extend objects to store own data and behaviours, override base methods (used for callbacks)
{
UserGUIObject@ Clone() { UserGUIObject clone(this); return this; } // I want avoid this solution, bacause user can forgot about this implementation
void OnMove()
{
}
}
// GUI system to handle interaction
RegisterGUI(UserGUIWindows());
GUIObject@[] WindowObjPrototype;
GUIObject@[] WindowObjActive;
void RegisterGUI(GUIObject& obj)
{
WindowObjPrototype.insertLast(obj);
}
void InitGUI()
{
WindowObjActive.clear();
for(uint i = 0; i < WindowObjPrototype.length(); i++)
WindowObjActive.insertLast(WindowObjPrototype.Clone());
}
DrawGUI(GUIObject& obj)
{
for(uint i = 0; i < WindowObjActive.length(); i++)
WindowObjActive.Draw();
}
MoveGUI(GUIObject& obj)
{
for(uint i = 0; i < WindowObjActive.length(); i++)
WindowObjActive.Move();
}