3 Copyright (C) 2010-2015 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "util/basic_macros.h"
28 #define OBJDEF_INVALID_INDEX ((u32)(-1))
29 #define OBJDEF_INVALID_HANDLE 0
30 #define OBJDEF_HANDLE_SALT 0x00585e6fu
31 #define OBJDEF_MAX_ITEMS (1 << 18)
32 #define OBJDEF_UID_MASK ((1 << 7) - 1)
34 typedef u32 ObjDefHandle
;
46 virtual ~ObjDef() = default;
48 // Only implemented by child classes (leafs in class hierarchy)
49 // Should create new object of its own type, call cloneTo() of parent class
50 // and copy its own instance variables over
51 virtual ObjDef
*clone() const = 0;
59 // Only implemented by classes that have children themselves
60 // by copying the defintion and changing that argument type (!!!)
61 // Should defer to parent class cloneTo() if applicable and then copy
62 // over its own properties
63 void cloneTo(ObjDef
*def
) const;
66 // WARNING: Ownership of ObjDefs is transferred to the ObjDefManager it is
67 // added/set to. Note that ObjDefs managed by ObjDefManager are NOT refcounted,
68 // so the same ObjDef instance must not be referenced multiple
69 // TODO: const correctness for getter methods
72 ObjDefManager(IGameDef
*gamedef
, ObjDefType type
);
73 virtual ~ObjDefManager();
74 DISABLE_CLASS_COPY(ObjDefManager
);
76 // T *clone() const; // implemented in child class with correct type
78 virtual const char *getObjectTitle() const { return "ObjDef"; }
81 virtual ObjDef
*getByName(const std::string
&name
) const;
83 //// Add new/get/set object definitions by handle
84 virtual ObjDefHandle
add(ObjDef
*obj
);
85 virtual ObjDef
*get(ObjDefHandle handle
) const;
86 virtual ObjDef
*set(ObjDefHandle handle
, ObjDef
*obj
);
88 //// Raw variants that work on indexes
89 virtual u32
addRaw(ObjDef
*obj
);
91 // It is generally assumed that getRaw() will always return a valid object
92 // This won't be true if people do odd things such as call setRaw() with NULL
93 virtual ObjDef
*getRaw(u32 index
) const;
94 virtual ObjDef
*setRaw(u32 index
, ObjDef
*obj
);
96 size_t getNumObjects() const { return m_objects
.size(); }
97 ObjDefType
getType() const { return m_objtype
; }
98 const NodeDefManager
*getNodeDef() const { return m_ndef
; }
100 u32
validateHandle(ObjDefHandle handle
) const;
101 static ObjDefHandle
createHandle(u32 index
, ObjDefType type
, u32 uid
);
102 static bool decodeHandle(ObjDefHandle handle
, u32
*index
,
103 ObjDefType
*type
, u32
*uid
);
107 // Helper for child classes to implement clone()
108 void cloneTo(ObjDefManager
*mgr
) const;
110 const NodeDefManager
*m_ndef
;
111 std::vector
<ObjDef
*> m_objects
;
112 ObjDefType m_objtype
;