Luanti 5.11.0-dev
 
Loading...
Searching...
No Matches
objdef.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2010-2015 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
4
5#pragma once
6
7#include "util/basic_macros.h"
8#include "porting.h"
9#include <vector>
10
11class IGameDef;
12class NodeDefManager;
13
14#define OBJDEF_INVALID_INDEX ((u32)(-1))
15#define OBJDEF_INVALID_HANDLE 0
16#define OBJDEF_HANDLE_SALT 0x00585e6fu
17#define OBJDEF_MAX_ITEMS (1 << 18)
18#define OBJDEF_UID_MASK ((1 << 7) - 1)
19
20typedef u32 ObjDefHandle;
21
29
30class ObjDef {
31public:
32 virtual ~ObjDef() = default;
33
34 // Only implemented by child classes (leafs in class hierarchy)
35 // Should create new object of its own type, call cloneTo() of parent class
36 // and copy its own instance variables over
37 virtual ObjDef *clone() const = 0;
38
39 u32 index;
40 u32 uid;
42 std::string name;
43
44protected:
45 // Only implemented by classes that have children themselves
46 // by copying the defintion and changing that argument type (!!!)
47 // Should defer to parent class cloneTo() if applicable and then copy
48 // over its own properties
49 void cloneTo(ObjDef *def) const;
50};
51
52// WARNING: Ownership of ObjDefs is transferred to the ObjDefManager it is
53// added/set to. Note that ObjDefs managed by ObjDefManager are NOT refcounted,
54// so the same ObjDef instance must not be referenced multiple
55// TODO: const correctness for getter methods
57public:
58 ObjDefManager(IGameDef *gamedef, ObjDefType type);
59 virtual ~ObjDefManager();
61
62 // T *clone() const; // implemented in child class with correct type
63
64 virtual const char *getObjectTitle() const { return "ObjDef"; }
65
66 virtual void clear();
67 virtual ObjDef *getByName(const std::string &name) const;
68
70 virtual ObjDefHandle add(ObjDef *obj);
71 virtual ObjDef *get(ObjDefHandle handle) const;
72 virtual ObjDef *set(ObjDefHandle handle, ObjDef *obj);
73
75 virtual u32 addRaw(ObjDef *obj);
76
77 // It is generally assumed that getRaw() will always return a valid object
78 // This won't be true if people do odd things such as call setRaw() with NULL
79 virtual ObjDef *getRaw(u32 index) const;
80 virtual ObjDef *setRaw(u32 index, ObjDef *obj);
81
82 size_t getNumObjects() const { return m_objects.size(); }
83 ObjDefType getType() const { return m_objtype; }
84 const NodeDefManager *getNodeDef() const { return m_ndef; }
85
86 u32 validateHandle(ObjDefHandle handle) const;
87 static ObjDefHandle createHandle(u32 index, ObjDefType type, u32 uid);
88 static bool decodeHandle(ObjDefHandle handle, u32 *index,
89 ObjDefType *type, u32 *uid);
90
91protected:
93 // Helper for child classes to implement clone()
94 void cloneTo(ObjDefManager *mgr) const;
95
97 std::vector<ObjDef *> m_objects;
99};
Definition gamedef.h:36
This class is for getting the actual properties of nodes from their content ID.
Definition nodedef.h:540
Definition objdef.h:56
void cloneTo(ObjDefManager *mgr) const
Definition objdef.cpp:181
const NodeDefManager * getNodeDef() const
Definition objdef.h:84
virtual ObjDefHandle add(ObjDef *obj)
Definition objdef.cpp:24
virtual ~ObjDefManager()
Definition objdef.cpp:17
static bool decodeHandle(ObjDefHandle handle, u32 *index, ObjDefType *type, u32 *uid)
Definition objdef.cpp:155
ObjDefManager()
Definition objdef.h:92
u32 validateHandle(ObjDefHandle handle) const
Definition objdef.cpp:124
virtual const char * getObjectTitle() const
Definition objdef.h:64
size_t getNumObjects() const
Definition objdef.h:82
static ObjDefHandle createHandle(u32 index, ObjDefType type, u32 uid)
Definition objdef.cpp:141
virtual ObjDef * getRaw(u32 index) const
Definition objdef.cpp:89
virtual void clear()
Definition objdef.cpp:115
virtual ObjDef * get(ObjDefHandle handle) const
Definition objdef.cpp:40
const NodeDefManager * m_ndef
Definition objdef.h:96
DISABLE_CLASS_COPY(ObjDefManager)
ObjDefType m_objtype
Definition objdef.h:98
virtual ObjDef * set(ObjDefHandle handle, ObjDef *obj)
Definition objdef.cpp:47
std::vector< ObjDef * > m_objects
Definition objdef.h:97
ObjDefType getType() const
Definition objdef.h:83
virtual u32 addRaw(ObjDef *obj)
Definition objdef.cpp:63
virtual ObjDef * setRaw(u32 index, ObjDef *obj)
Definition objdef.cpp:95
virtual ObjDef * getByName(const std::string &name) const
Definition objdef.cpp:103
Definition objdef.h:30
u32 uid
Definition objdef.h:40
void cloneTo(ObjDef *def) const
Definition objdef.cpp:173
virtual ~ObjDef()=default
virtual ObjDef * clone() const =0
u32 index
Definition objdef.h:39
ObjDefHandle handle
Definition objdef.h:41
std::string name
Definition objdef.h:42
u32 ObjDefHandle
Definition objdef.h:20
ObjDefType
Definition objdef.h:22
@ OBJDEF_SCHEMATIC
Definition objdef.h:27
@ OBJDEF_BIOME
Definition objdef.h:24
@ OBJDEF_DECORATION
Definition objdef.h:26
@ OBJDEF_GENERIC
Definition objdef.h:23
@ OBJDEF_ORE
Definition objdef.h:25