From d1b27418a6a39aa15d51465e61975c5230dcc9d6 Mon Sep 17 00:00:00 2001 From: Alexander Gavrilov Date: Thu, 5 Apr 2012 11:32:23 +0400 Subject: [PATCH] Add a World::GetPersistentData version that auto-adds if not found. --- library/include/DataFuncs.h | 4 ++++ library/include/DataIdentity.h | 4 +++- library/include/modules/World.h | 8 ++++++++ library/modules/World.cpp | 15 +++++++++++++++ 4 files changed, 30 insertions(+), 1 deletion(-) diff --git a/library/include/DataFuncs.h b/library/include/DataFuncs.h index 822dcd45d..109b2c176 100644 --- a/library/include/DataFuncs.h +++ b/library/include/DataFuncs.h @@ -32,6 +32,10 @@ distribution. #include "DataIdentity.h" #include "LuaWrapper.h" +#ifndef BUILD_DFHACK_LIB +#error Due to export issues this header is internal to the main library. +#endif + namespace df { // A very simple and stupid implementation of some stuff from boost template struct is_same_type { static const bool value = false; }; diff --git a/library/include/DataIdentity.h b/library/include/DataIdentity.h index c83b2092a..bab87e1bb 100644 --- a/library/include/DataIdentity.h +++ b/library/include/DataIdentity.h @@ -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 { using DFHack::function_identity_base; @@ -575,4 +577,4 @@ namespace df return &identity; } } - +#endif diff --git a/library/include/modules/World.h b/library/include/modules/World.h index 9ed6a3ed9..e7b17ce41 100644 --- a/library/include/modules/World.h +++ b/library/include/modules/World.h @@ -139,8 +139,16 @@ namespace DFHack PersistentDataItem AddPersistentData(const std::string &key); PersistentDataItem GetPersistentData(const std::string &key); 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 *vec, const std::string &key, bool prefix = false); + // Deletes the item; returns true if success. bool DeletePersistentData(const PersistentDataItem &item); void ClearPersistentCache(); diff --git a/library/modules/World.cpp b/library/modules/World.cpp index d570abaec..ca5d0aee0 100644 --- a/library/modules/World.cpp +++ b/library/modules/World.cpp @@ -300,6 +300,21 @@ PersistentDataItem World::GetPersistentData(int entry_id) 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 *vec, const std::string &key, bool prefix) { if (!BuildPersistentCache())