Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
ObjDefManager: Set replacement object's handle info after calling set()
Make gamedef optional when constructing an ObjDefManager
Add note about object ownership
  • Loading branch information
kwolekr committed May 4, 2015
1 parent 5704fb3 commit 5b237b4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
13 changes: 11 additions & 2 deletions src/mapgen.cpp
Expand Up @@ -434,7 +434,7 @@ void GenerateNotifier::getEvents(
ObjDefManager::ObjDefManager(IGameDef *gamedef, ObjDefType type)
{
m_objtype = type;
m_ndef = gamedef->getNodeDefManager();
m_ndef = gamedef ? gamedef->getNodeDefManager() : NULL;
}


Expand Down Expand Up @@ -471,7 +471,16 @@ ObjDef *ObjDefManager::get(ObjDefHandle handle) const
ObjDef *ObjDefManager::set(ObjDefHandle handle, ObjDef *obj)
{
u32 index = validateHandle(handle);
return (index != OBJDEF_INVALID_INDEX) ? setRaw(index, obj) : NULL;
if (index == OBJDEF_INVALID_INDEX)
return NULL;

ObjDef *oldobj = setRaw(index, obj);

obj->uid = oldobj->uid;
obj->index = oldobj->index;
obj->handle = oldobj->handle;

return oldobj;
}


Expand Down
5 changes: 4 additions & 1 deletion src/mapgen.h
Expand Up @@ -206,12 +206,15 @@ class ObjDef {
std::string name;
};

// 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
class ObjDefManager {
public:
ObjDefManager(IGameDef *gamedef, ObjDefType type);
virtual ~ObjDefManager();

virtual const char *getObjectTitle() const = 0;
virtual const char *getObjectTitle() const { return "ObjDef"; }

virtual void clear();
virtual ObjDef *getByName(const std::string &name) const;
Expand Down

0 comments on commit 5b237b4

Please sign in to comment.