From e617cc1815918f412900039f3e8e42cf2b450140 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaakko=20Kera=CC=88nen?= Date: Fri, 1 Aug 2014 11:41:32 +0300 Subject: [PATCH] Refactor|libdoomsday|DED: Making copies of definitions DEDRegister now provides a method for making a copy of an existing definition so that all the appropriate members are excluded from the copy. --- .../include/doomsday/defs/dedregister.h | 18 +++++++++++++- doomsday/libdoomsday/src/defs/dedparser.cpp | 5 +--- doomsday/libdoomsday/src/defs/dedregister.cpp | 24 ++++++++++++++++++- 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/doomsday/libdoomsday/include/doomsday/defs/dedregister.h b/doomsday/libdoomsday/include/doomsday/defs/dedregister.h index 995be71826..666148f143 100644 --- a/doomsday/libdoomsday/include/doomsday/defs/dedregister.h +++ b/doomsday/libdoomsday/include/doomsday/defs/dedregister.h @@ -51,8 +51,9 @@ class LIBDOOMSDAY_PUBLIC DEDRegister { CaseSensitive = 0x1, ///< Looking up is done case sensitively. OnlyFirst = 0x2, ///< Only the first defined value is kept in lookup (otherwise last). + AllowCopy = 0x4, ///< When copying from an existing definition, include this lookup key. - DefaultLookup = 0 ///< Latest in order, case insensitive. + DefaultLookup = 0 ///< Latest in order, case insensitive, omitted from copies. }; Q_DECLARE_FLAGS(LookupFlags, LookupFlag) @@ -80,6 +81,21 @@ class LIBDOOMSDAY_PUBLIC DEDRegister */ de::Record &append(); + /** + * Adds a new definition as the last one, and copies contents into it from an + * existing definition at @a index. + * + * Double-underscore members will not be copied to the new definition, and neither + * will lookup keys flagged as non-copyable. + * + * @param index Index to copy from. + * + * @return Reference to the added definition. + */ + de::Record &appendCopy(int index); + + de::Record ©(int fromIndex, de::Record &to); + int size() const; bool has(de::String const &key, de::String const &value) const; diff --git a/doomsday/libdoomsday/src/defs/dedparser.cpp b/doomsday/libdoomsday/src/defs/dedparser.cpp index f42f10854b..bc488a0d18 100644 --- a/doomsday/libdoomsday/src/defs/dedparser.cpp +++ b/doomsday/libdoomsday/src/defs/dedparser.cpp @@ -1414,10 +1414,7 @@ DENG2_PIMPL(DEDParser) { prevModel = &ded->models[prevModelDefIdx]; - // Private members are used for metadata (like __order__) that should - // not be copied. Also don't copy the identifying keys, so that - // the new ones are used for indexing. - if(bCopyNext) mdl.assign(*prevModel, QRegExp("__.*|id|state")); + if(bCopyNext) ded->models.copy(prevModelDefIdx, mdl); } FINDBEGIN; diff --git a/doomsday/libdoomsday/src/defs/dedregister.cpp b/doomsday/libdoomsday/src/defs/dedregister.cpp index 8a80b74b45..339e08a4e4 100644 --- a/doomsday/libdoomsday/src/defs/dedregister.cpp +++ b/doomsday/libdoomsday/src/defs/dedregister.cpp @@ -38,7 +38,8 @@ DENG2_PIMPL(DEDRegister) LookupFlags flags; Key(LookupFlags const &f = DefaultLookup) : flags(f) {} }; - QMap keys; + typedef QMap Keys; + Keys keys; QMap parents; Instance(Public *i, Record &rec) : Base(i), names(&rec) @@ -260,6 +261,27 @@ Record &DEDRegister::append() return d->append(); } +Record &DEDRegister::appendCopy(int index) +{ + return copy(index, append()); +} + +Record &DEDRegister::copy(int fromIndex, Record &to) +{ + QStringList omitted; + omitted << "__.*"; // double-underscore + + // By default lookup keys are not copied, as they are used as identifiers and + // therefore duplicates should not occur. + DENG2_FOR_EACH_CONST(Instance::Keys, i, d->keys) + { + if(i.value().flags.testFlag(AllowCopy)) continue; + omitted << i.key(); + } + + return to.assign((*this)[fromIndex], QRegExp(omitted.join('|'))); +} + int DEDRegister::size() const { return d->order().size();