Add a World::GetPersistentData version that auto-adds if not found.

develop
Alexander Gavrilov 2012-04-05 11:32:23 +04:00
parent 330118ee27
commit d1b27418a6
4 changed files with 30 additions and 1 deletions

@ -32,6 +32,10 @@ distribution.
#include "DataIdentity.h" #include "DataIdentity.h"
#include "LuaWrapper.h" #include "LuaWrapper.h"
#ifndef BUILD_DFHACK_LIB
#error Due to export issues this header is internal to the main library.
#endif
namespace df { namespace df {
// A very simple and stupid implementation of some stuff from boost // A very simple and stupid implementation of some stuff from boost
template<class U, class V> struct is_same_type { static const bool value = false; }; template<class U, class V> struct is_same_type { static const bool value = false; };

@ -160,6 +160,8 @@ namespace DFHack
}; };
} }
// Due to export issues, this stuff can only work in the main dll
#ifdef BUILD_DFHACK_LIB
namespace df namespace df
{ {
using DFHack::function_identity_base; using DFHack::function_identity_base;
@ -575,4 +577,4 @@ namespace df
return &identity; return &identity;
} }
} }
#endif

@ -139,8 +139,16 @@ namespace DFHack
PersistentDataItem AddPersistentData(const std::string &key); PersistentDataItem AddPersistentData(const std::string &key);
PersistentDataItem GetPersistentData(const std::string &key); PersistentDataItem GetPersistentData(const std::string &key);
PersistentDataItem GetPersistentData(int entry_id); PersistentDataItem GetPersistentData(int entry_id);
// Calls GetPersistentData(key); if not found, adds and sets added to true.
// The result can still be not isValid() e.g. if the world is not loaded.
PersistentDataItem GetPersistentData(const std::string &key, bool *added);
// Lists all items with the given key.
// If prefix is true, search for keys starting with key+"/".
// GetPersistentData(&vec,"",true) returns all items.
// Items have alphabetic order by key; same key ordering is undefined.
void GetPersistentData(std::vector<PersistentDataItem> *vec, void GetPersistentData(std::vector<PersistentDataItem> *vec,
const std::string &key, bool prefix = false); const std::string &key, bool prefix = false);
// Deletes the item; returns true if success.
bool DeletePersistentData(const PersistentDataItem &item); bool DeletePersistentData(const PersistentDataItem &item);
void ClearPersistentCache(); void ClearPersistentCache();

@ -300,6 +300,21 @@ PersistentDataItem World::GetPersistentData(int entry_id)
return PersistentDataItem(); return PersistentDataItem();
} }
PersistentDataItem World::GetPersistentData(const std::string &key, bool *added)
{
*added = false;
PersistentDataItem rv = GetPersistentData(key);
if (!rv.isValid())
{
*added = true;
rv = AddPersistentData(key);
}
return rv;
}
void World::GetPersistentData(std::vector<PersistentDataItem> *vec, const std::string &key, bool prefix) void World::GetPersistentData(std::vector<PersistentDataItem> *vec, const std::string &key, bool prefix)
{ {
if (!BuildPersistentCache()) if (!BuildPersistentCache())