X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Fobjdef.h;h=e40324a88cdad81f1185c54bf8c538bfbc91ffe0;hb=5c89a0e12a1e679180b14bf92bdcdb1614e3982e;hp=65e5c0176fe1ffb02c719488d155b92bbeddd70a;hpb=39869aaa25ca577faa0da2b6cb83db6db7234342;p=dragonfireclient.git diff --git a/src/objdef.h b/src/objdef.h index 65e5c0176..e40324a88 100644 --- a/src/objdef.h +++ b/src/objdef.h @@ -17,13 +17,13 @@ with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -#ifndef OBJDEF_HEADER -#define OBJDEF_HEADER +#pragma once +#include "util/basic_macros.h" #include "porting.h" class IGameDef; -class INodeDefManager; +class NodeDefManager; #define OBJDEF_INVALID_INDEX ((u32)(-1)) #define OBJDEF_INVALID_HANDLE 0 @@ -43,21 +43,37 @@ enum ObjDefType { class ObjDef { public: - virtual ~ObjDef() {} + virtual ~ObjDef() = default; + + // Only implemented by child classes (leafs in class hierarchy) + // Should create new object of its own type, call cloneTo() of parent class + // and copy its own instance variables over + virtual ObjDef *clone() const = 0; u32 index; u32 uid; ObjDefHandle handle; std::string name; + +protected: + // Only implemented by classes that have children themselves + // by copying the defintion and changing that argument type (!!!) + // Should defer to parent class cloneTo() if applicable and then copy + // over its own properties + void cloneTo(ObjDef *def) const; }; // WARNING: Ownership of ObjDefs is transferred to the ObjDefManager it is // added/set to. Note that ObjDefs managed by ObjDefManager are NOT refcounted, // so the same ObjDef instance must not be referenced multiple +// TODO: const correctness for getter methods class ObjDefManager { public: ObjDefManager(IGameDef *gamedef, ObjDefType type); virtual ~ObjDefManager(); + DISABLE_CLASS_COPY(ObjDefManager); + + // T *clone() const; // implemented in child class with correct type virtual const char *getObjectTitle() const { return "ObjDef"; } @@ -79,7 +95,7 @@ class ObjDefManager { size_t getNumObjects() const { return m_objects.size(); } ObjDefType getType() const { return m_objtype; } - INodeDefManager *getNodeDef() const { return m_ndef; } + const NodeDefManager *getNodeDef() const { return m_ndef; } u32 validateHandle(ObjDefHandle handle) const; static ObjDefHandle createHandle(u32 index, ObjDefType type, u32 uid); @@ -87,9 +103,11 @@ class ObjDefManager { ObjDefType *type, u32 *uid); protected: - INodeDefManager *m_ndef; + ObjDefManager() {}; + // Helper for child classes to implement clone() + void cloneTo(ObjDefManager *mgr) const; + + const NodeDefManager *m_ndef; std::vector m_objects; ObjDefType m_objtype; }; - -#endif