diff --git a/src/core/multimap.hpp b/src/core/multimap.hpp index e906677141..da6a33ef45 100644 --- a/src/core/multimap.hpp +++ b/src/core/multimap.hpp @@ -15,7 +15,7 @@ #include #include -template +template class MultiMap; /** @@ -23,14 +23,15 @@ class MultiMap; * @tparam Tmap_iter Iterator type for the map in the MultiMap. * @tparam Tlist_iter Iterator type for the lists in the MultiMap. * @tparam Tkey Key type of the MultiMap. - * @tparam Tvalue Value type of the MultMap. + * @tparam Tvalue Value type of the MultiMap. + * @tparam Tcontainer Container type for the values of the MultiMap. * @tparam Tcompare Comparator type for keys of the MultiMap. */ -template +template class MultiMapIterator { protected: - friend class MultiMap; - typedef MultiMapIterator Self; + friend class MultiMap; + typedef MultiMapIterator Self; Tlist_iter list_iter; ///< Iterator pointing to current position in the current list of items with equal keys. Tmap_iter map_iter; ///< Iterator pointing to the position of the current list of items with equal keys in the map. @@ -201,8 +202,8 @@ public: * @param iter2 Second iterator to compare. * @return If iter1 and iter2 are equal. */ -template -bool operator==(const MultiMapIterator &iter1, const MultiMapIterator &iter2) +template +bool operator==(const MultiMapIterator &iter1, const MultiMapIterator &iter2) { if (iter1.GetMapIter() != iter2.GetMapIter()) return false; if (!iter1.ListValid()) return !iter2.ListValid(); @@ -218,8 +219,8 @@ bool operator==(const MultiMapIterator -bool operator!=(const MultiMapIterator &iter1, const MultiMapIterator &iter2) +template +bool operator!=(const MultiMapIterator &iter1, const MultiMapIterator &iter2) { return !(iter1 == iter2); } @@ -232,8 +233,8 @@ bool operator!=(const MultiMapIterator -bool operator==(const MultiMapIterator &iter1, const Tmap_iter2 &iter2) +template +bool operator==(const MultiMapIterator &iter1, const Tmap_iter2 &iter2) { return !iter1.ListValid() && iter1.GetMapIter() == iter2; } @@ -244,8 +245,8 @@ bool operator==(const MultiMapIterator -bool operator!=(const MultiMapIterator &iter1, const Tmap_iter2 &iter2) +template +bool operator!=(const MultiMapIterator &iter1, const Tmap_iter2 &iter2) { return iter1.ListValid() || iter1.GetMapIter() != iter2; } @@ -256,8 +257,8 @@ bool operator!=(const MultiMapIterator -bool operator==(const Tmap_iter2 &iter2, const MultiMapIterator &iter1) +template +bool operator==(const Tmap_iter2 &iter2, const MultiMapIterator &iter1) { return !iter1.ListValid() && iter1.GetMapIter() == iter2; } @@ -268,8 +269,8 @@ bool operator==(const Tmap_iter2 &iter2, const MultiMapIterator -bool operator!=(const Tmap_iter2 &iter2, const MultiMapIterator &iter1) +template +bool operator!=(const Tmap_iter2 &iter2, const MultiMapIterator &iter1) { return iter1.ListValid() || iter1.GetMapIter() != iter2; } @@ -282,10 +283,10 @@ bool operator!=(const Tmap_iter2 &iter2, const MultiMapIterator > -class MultiMap : public std::map, Tcompare > { +template, typename Tcompare = std::less > +class MultiMap : public std::map { public: - typedef typename std::list List; + typedef Tcontainer List; typedef typename List::iterator ListIterator; typedef typename List::const_iterator ConstListIterator; @@ -293,8 +294,8 @@ public: typedef typename Map::iterator MapIterator; typedef typename Map::const_iterator ConstMapIterator; - typedef MultiMapIterator iterator; - typedef MultiMapIterator const_iterator; + typedef MultiMapIterator iterator; + typedef MultiMapIterator const_iterator; /** * Erase the value pointed to by an iterator. The iterator may be invalid afterwards. diff --git a/src/openttd.cpp b/src/openttd.cpp index bfd9cd59fa..ede5cb4e51 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -255,7 +255,20 @@ static void WriteSavegameInfo(const char *name) char buf[8192]; char *p = buf; p += seprintf(p, lastof(buf), "Name: %s\n", name); - p += seprintf(p, lastof(buf), "Savegame ver: %d\n", _sl_version); + const char *type = ""; + extern bool _sl_is_faked_ext; + extern bool _sl_is_ext_version; + if (_sl_is_faked_ext) { + type = " (fake extended)"; + } else if (_sl_is_ext_version) { + type = " (extended)"; + } + p += seprintf(p, lastof(buf), "Savegame ver: %d%s\n", _sl_version, type); + for (size_t i = 0; i < XSLFI_SIZE; i++) { + if (_sl_xv_feature_versions[i] > 0) { + p += seprintf(p, lastof(buf), " Feature: %s = %d\n", SlXvGetFeatureName((SlXvFeatureIndex) i), _sl_xv_feature_versions[i]); + } + } p += seprintf(p, lastof(buf), "NewGRF ver: 0x%08X\n", last_ottd_rev); p += seprintf(p, lastof(buf), "Modified: %d\n", ever_modified); diff --git a/src/saveload/extended_ver_sl.cpp b/src/saveload/extended_ver_sl.cpp index 258a70bff2..4d2e262ffe 100644 --- a/src/saveload/extended_ver_sl.cpp +++ b/src/saveload/extended_ver_sl.cpp @@ -114,6 +114,20 @@ bool SlXvIsFeaturePresent(SlXvFeatureIndex feature, uint16 min_version, uint16 m return _sl_xv_feature_versions[feature] >= min_version && _sl_xv_feature_versions[feature] <= max_version; } +/** + * Returns true if @p feature is present and has a version inclusively bounded by @p min_version and @p max_version + */ +const char *SlXvGetFeatureName(SlXvFeatureIndex feature) +{ + const SlxiSubChunkInfo *info = _sl_xv_sub_chunk_infos; + for (; info->index != XSLFI_NULL; ++info) { + if (info->index == feature) { + return info->name; + } + } + return "(unknown feature)"; +} + /** * Resets all extended feature versions to 0 */ diff --git a/src/saveload/extended_ver_sl.h b/src/saveload/extended_ver_sl.h index c339d8a8b7..88ec923f37 100644 --- a/src/saveload/extended_ver_sl.h +++ b/src/saveload/extended_ver_sl.h @@ -97,6 +97,8 @@ inline bool SlXvIsFeatureMissing(SlXvFeatureIndex feature) return !SlXvIsFeaturePresent(feature); } +const char *SlXvGetFeatureName(SlXvFeatureIndex feature); + /** * sub chunk flags, this is saved as-is * (XSCF_EXTRA_DATA_PRESENT and XSCF_CHUNK_ID_LIST_PRESENT must only be set by the save code, and read by the load code) diff --git a/src/saveload/saveload.cpp b/src/saveload/saveload.cpp index 42d413a26d..40329504f3 100644 --- a/src/saveload/saveload.cpp +++ b/src/saveload/saveload.cpp @@ -54,6 +54,7 @@ #include "../safeguards.h" +#include #include /* @@ -1383,9 +1384,10 @@ static void *IntToReference(size_t index, SLRefType rt) * Return the size in bytes of a list * @param list The std::list to find the size of */ + template static inline size_t SlCalcListLen(const void *list) { - const std::list *l = (const std::list *) list; + const PtrList *l = (const PtrList *) list; int type_size = IsSavegameVersionBefore(69) ? 2 : 4; /* Each entry is saved as type_size bytes, plus type_size bytes are used for the length @@ -1399,23 +1401,23 @@ static inline size_t SlCalcListLen(const void *list) * @param list The list being manipulated * @param conv SLRefType type of the list (Vehicle *, Station *, etc) */ +template static void SlList(void *list, SLRefType conv) { /* Automatically calculate the length? */ if (_sl.need_length != NL_NONE) { - SlSetLength(SlCalcListLen(list)); + SlSetLength(SlCalcListLen(list)); /* Determine length only? */ if (_sl.need_length == NL_CALCLENGTH) return; } - typedef std::list PtrList; PtrList *l = (PtrList *)list; switch (_sl.action) { case SLA_SAVE: { SlWriteUint32((uint32)l->size()); - PtrList::iterator iter; + typename PtrList::iterator iter; for (iter = l->begin(); iter != l->end(); ++iter) { void *ptr = *iter; SlWriteUint32((uint32)ReferenceToInt(ptr, conv)); @@ -1437,7 +1439,7 @@ static void SlList(void *list, SLRefType conv) PtrList temp = *l; l->clear(); - PtrList::iterator iter; + typename PtrList::iterator iter; for (iter = temp.begin(); iter != temp.end(); ++iter) { void *ptr = IntToReference((size_t)*iter, conv); l->push_back(ptr); @@ -1503,6 +1505,8 @@ size_t SlCalcObjMemberLength(const void *object, const SaveLoad *sld) case SL_ARR: case SL_STR: case SL_LST: + case SL_DEQ: + case SL_VEC: /* CONDITIONAL saveload types depend on the savegame version */ if (!SlIsObjectValidInSavegame(sld)) break; @@ -1511,7 +1515,9 @@ size_t SlCalcObjMemberLength(const void *object, const SaveLoad *sld) case SL_REF: return SlCalcRefLen(); case SL_ARR: return SlCalcArrayLen(sld->length, sld->conv); case SL_STR: return SlCalcStringLen(GetVariableAddress(object, sld), sld->length, sld->conv); - case SL_LST: return SlCalcListLen(GetVariableAddress(object, sld)); + case SL_LST: return SlCalcListLen>(GetVariableAddress(object, sld)); + case SL_DEQ: return SlCalcListLen>(GetVariableAddress(object, sld)); + case SL_VEC: return SlCalcListLen>(GetVariableAddress(object, sld)); default: NOT_REACHED(); } break; @@ -1574,6 +1580,8 @@ bool SlObjectMember(void *ptr, const SaveLoad *sld) case SL_ARR: case SL_STR: case SL_LST: + case SL_DEQ: + case SL_VEC: /* CONDITIONAL saveload types depend on the savegame version */ if (!SlIsObjectValidInSavegame(sld)) return false; if (SlSkipVariableOnLoad(sld)) return false; @@ -1600,7 +1608,9 @@ bool SlObjectMember(void *ptr, const SaveLoad *sld) break; case SL_ARR: SlArray(ptr, sld->length, conv); break; case SL_STR: SlString(ptr, sld->length, sld->conv); break; - case SL_LST: SlList(ptr, (SLRefType)conv); break; + case SL_LST: SlList>(ptr, (SLRefType)conv); break; + case SL_DEQ: SlList>(ptr, (SLRefType)conv); break; + case SL_VEC: SlList>(ptr, (SLRefType)conv); break; default: NOT_REACHED(); } break; diff --git a/src/saveload/saveload.h b/src/saveload/saveload.h index 807ee8e7db..d1c2cec355 100644 --- a/src/saveload/saveload.h +++ b/src/saveload/saveload.h @@ -205,6 +205,8 @@ enum SaveLoadTypes { SL_ARR = 2, ///< Save/load an array. SL_STR = 3, ///< Save/load a string. SL_LST = 4, ///< Save/load a list. + SL_DEQ = 5, ///< Save/load a deque. + SL_VEC = 6, ///< Save/load a vector. /* non-normal save-load types */ SL_WRITEBYTE = 8, SL_VEH_INCLUDE = 9, @@ -310,6 +312,30 @@ typedef SaveLoad SaveLoadGlobVarList; #define SLE_CONDLST_X(base, variable, type, from, to, extver) SLE_GENERAL_X(SL_LST, base, variable, type, 0, from, to, extver) #define SLE_CONDLST(base, variable, type, from, to) SLE_CONDLST_X(base, variable, type, from, to, SlXvFeatureTest()) +/** + * Storage of a deque in some savegame versions. + * @param base Name of the class or struct containing the list. + * @param variable Name of the variable in the class or struct referenced by \a base. + * @param type Storage of the data in memory and in the savegame. + * @param from First savegame version that has the list. + * @param to Last savegame version that has the list. + * @param extver SlXvFeatureTest to test (along with from and to) which savegames have the field + */ +#define SLE_CONDDEQ_X(base, variable, type, from, to, extver) SLE_GENERAL_X(SL_DEQ, base, variable, type, 0, from, to, extver) +#define SLE_CONDDEQ(base, variable, type, from, to) SLE_CONDDEQ_X(base, variable, type, from, to, SlXvFeatureTest()) + +/** + * Storage of a vector in some savegame versions. + * @param base Name of the class or struct containing the list. + * @param variable Name of the variable in the class or struct referenced by \a base. + * @param type Storage of the data in memory and in the savegame. + * @param from First savegame version that has the list. + * @param to Last savegame version that has the list. + * @param extver SlXvFeatureTest to test (along with from and to) which savegames have the field + */ +#define SLE_CONDVEC_X(base, variable, type, from, to, extver) SLE_GENERAL_X(SL_VEC, base, variable, type, 0, from, to, extver) +#define SLE_CONDVEC(base, variable, type, from, to) SLE_CONDVEC_X(base, variable, type, from, to, SlXvFeatureTest()) + /** * Storage of a variable in every version of a savegame. * @param base Name of the class or struct containing the variable. @@ -352,6 +378,22 @@ typedef SaveLoad SaveLoadGlobVarList; */ #define SLE_LST(base, variable, type) SLE_CONDLST(base, variable, type, 0, SL_MAX_VERSION) +/** + * Storage of a deque in every savegame version. + * @param base Name of the class or struct containing the list. + * @param variable Name of the variable in the class or struct referenced by \a base. + * @param type Storage of the data in memory and in the savegame. + */ +#define SLE_DEQ(base, variable, type) SLE_CONDDEQ(base, variable, type, 0, SL_MAX_VERSION) + +/** + * Storage of a vector in every savegame version. + * @param base Name of the class or struct containing the list. + * @param variable Name of the variable in the class or struct referenced by \a base. + * @param type Storage of the data in memory and in the savegame. + */ +#define SLE_VEC(base, variable, type) SLE_CONDVEC(base, variable, type, 0, SL_MAX_VERSION) + /** * Empty space in every savegame version. * @param length Length of the empty space. @@ -447,6 +489,28 @@ typedef SaveLoad SaveLoadGlobVarList; #define SLEG_CONDLST_X(variable, type, from, to, extver) SLEG_GENERAL_X(SL_LST, variable, type, 0, from, to, extver) #define SLEG_CONDLST(variable, type, from, to) SLEG_CONDLST_X(variable, type, from, to, SlXvFeatureTest()) +/** + * Storage of a global deque in some savegame versions. + * @param variable Name of the global variable. + * @param type Storage of the data in memory and in the savegame. + * @param from First savegame version that has the list. + * @param to Last savegame version that has the list. + * @param extver SlXvFeatureTest to test (along with from and to) which savegames have the field + */ +#define SLEG_CONDDEQ_X(variable, type, from, to, extver) SLEG_GENERAL_X(SL_DEQ, variable, type, 0, from, to, extver) +#define SLEG_CONDDEQ(variable, type, from, to) SLEG_CONDDEQ_X(variable, type, from, to, SlXvFeatureTest()) + +/** + * Storage of a global vector in some savegame versions. + * @param variable Name of the global variable. + * @param type Storage of the data in memory and in the savegame. + * @param from First savegame version that has the list. + * @param to Last savegame version that has the list. + * @param extver SlXvFeatureTest to test (along with from and to) which savegames have the field + */ +#define SLEG_CONDVEC_X(variable, type, from, to, extver) SLEG_GENERAL_X(SL_VEC, variable, type, 0, from, to, extver) +#define SLEG_CONDVEC(variable, type, from, to) SLEG_CONDVEC_X(variable, type, from, to, SlXvFeatureTest()) + /** * Storage of a global variable in every savegame version. * @param variable Name of the global variable. @@ -482,6 +546,20 @@ typedef SaveLoad SaveLoadGlobVarList; */ #define SLEG_LST(variable, type) SLEG_CONDLST(variable, type, 0, SL_MAX_VERSION) +/** + * Storage of a global deque in every savegame version. + * @param variable Name of the global variable. + * @param type Storage of the data in memory and in the savegame. + */ +#define SLEG_DEQ(variable, type) SLEG_CONDDEQ(variable, type, 0, SL_MAX_VERSION) + +/** + * Storage of a global vector in every savegame version. + * @param variable Name of the global variable. + * @param type Storage of the data in memory and in the savegame. + */ +#define SLEG_VEC(variable, type) SLEG_CONDVEC(variable, type, 0, SL_MAX_VERSION) + /** * Empty global space in some savegame versions. * @param length Length of the empty space.