Skip to content

Commit 5b237b4

Browse files
committedMay 4, 2015
ObjDefManager: Set replacement object's handle info after calling set()
Make gamedef optional when constructing an ObjDefManager Add note about object ownership
1 parent 5704fb3 commit 5b237b4

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed
 

‎src/mapgen.cpp

+11-2
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ void GenerateNotifier::getEvents(
434434
ObjDefManager::ObjDefManager(IGameDef *gamedef, ObjDefType type)
435435
{
436436
m_objtype = type;
437-
m_ndef = gamedef->getNodeDefManager();
437+
m_ndef = gamedef ? gamedef->getNodeDefManager() : NULL;
438438
}
439439

440440

@@ -471,7 +471,16 @@ ObjDef *ObjDefManager::get(ObjDefHandle handle) const
471471
ObjDef *ObjDefManager::set(ObjDefHandle handle, ObjDef *obj)
472472
{
473473
u32 index = validateHandle(handle);
474-
return (index != OBJDEF_INVALID_INDEX) ? setRaw(index, obj) : NULL;
474+
if (index == OBJDEF_INVALID_INDEX)
475+
return NULL;
476+
477+
ObjDef *oldobj = setRaw(index, obj);
478+
479+
obj->uid = oldobj->uid;
480+
obj->index = oldobj->index;
481+
obj->handle = oldobj->handle;
482+
483+
return oldobj;
475484
}
476485

477486

‎src/mapgen.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,15 @@ class ObjDef {
206206
std::string name;
207207
};
208208

209+
// WARNING: Ownership of ObjDefs is transferred to the ObjDefManager it is
210+
// added/set to. Note that ObjDefs managed by ObjDefManager are NOT refcounted,
211+
// so the same ObjDef instance must not be referenced multiple
209212
class ObjDefManager {
210213
public:
211214
ObjDefManager(IGameDef *gamedef, ObjDefType type);
212215
virtual ~ObjDefManager();
213216

214-
virtual const char *getObjectTitle() const = 0;
217+
virtual const char *getObjectTitle() const { return "ObjDef"; }
215218

216219
virtual void clear();
217220
virtual ObjDef *getByName(const std::string &name) const;

0 commit comments

Comments
 (0)
Please sign in to comment.