From d715cf5431dc9f15684d6199e6db59f704506399 Mon Sep 17 00:00:00 2001 From: Marc Zinnschlag Date: Sat, 11 May 2013 17:34:18 +0200 Subject: [PATCH] replaced static adapter with a template --- apps/opencs/model/world/refidadapterimp.cpp | 44 ------------- apps/opencs/model/world/refidadapterimp.hpp | 69 ++++++++++++++++++--- apps/opencs/model/world/refidcollection.cpp | 10 ++- apps/opencs/model/world/refidcollection.hpp | 3 +- 4 files changed, 72 insertions(+), 54 deletions(-) diff --git a/apps/opencs/model/world/refidadapterimp.cpp b/apps/opencs/model/world/refidadapterimp.cpp index 5038797dd..b24be3da5 100644 --- a/apps/opencs/model/world/refidadapterimp.cpp +++ b/apps/opencs/model/world/refidadapterimp.cpp @@ -1,49 +1,5 @@ #include "refidadapterimp.hpp" -#include -#include -#include "record.hpp" -#include "refiddata.hpp" -#include "universalid.hpp" - -CSMWorld::StaticRefIdAdapter::StaticRefIdAdapter (const RefIdColumn *id, const RefIdColumn *modified) -: mId (id), mModified (modified) -{} - -QVariant CSMWorld::StaticRefIdAdapter::getData (const RefIdColumn *column, const RefIdData& data, - int index) const -{ - const Record& record = static_cast&> ( - data.getRecord (RefIdData::LocalIndex (index, UniversalId::Type_Static))); - - if (column==mId) - return QString::fromUtf8 (record.get().mId.c_str()); - - if (column==mModified) - { - if (record.mState==Record::State_Erased) - return static_cast (Record::State_Deleted); - - return static_cast (record.mState); - } - - return QVariant(); -} - -void CSMWorld::StaticRefIdAdapter::setData (const RefIdColumn *column, RefIdData& data, int index, - const QVariant& value) const -{ - Record& record = static_cast&> ( - data.getRecord (RefIdData::LocalIndex (index, UniversalId::Type_Static))); - - if (column==mModified) - record.mState = static_cast (value.toInt()); -} - -std::string CSMWorld::StaticRefIdAdapter::getId (const RecordBase& record) const -{ - return dynamic_cast&> (record).get().mId; -} \ No newline at end of file diff --git a/apps/opencs/model/world/refidadapterimp.hpp b/apps/opencs/model/world/refidadapterimp.hpp index c130e2f73..021fe58a9 100644 --- a/apps/opencs/model/world/refidadapterimp.hpp +++ b/apps/opencs/model/world/refidadapterimp.hpp @@ -1,28 +1,83 @@ #ifndef CSM_WOLRD_REFIDADAPTERIMP_H #define CSM_WOLRD_REFIDADAPTERIMP_H +#include + +#include "record.hpp" +#include "refiddata.hpp" +#include "universalid.hpp" #include "refidadapter.hpp" namespace CSMWorld { - class StaticRefIdAdapter : public RefIdAdapter + struct BaseColumns { - const RefIdColumn *mId; - const RefIdColumn *mModified; + const RefIdColumn *mId; + const RefIdColumn *mModified; + }; + + template + class BaseRefIdAdapter : public RefIdAdapter + { + UniversalId::Type mType; + BaseColumns mBase; public: - StaticRefIdAdapter (const RefIdColumn *id, const RefIdColumn *modified); + BaseRefIdAdapter (UniversalId::Type type, const BaseColumns& base); + + virtual std::string getId (const RecordBase& record) const; - virtual QVariant getData (const RefIdColumn *column, const RefIdData& data, int idnex) + virtual QVariant getData (const RefIdColumn *column, const RefIdData& data, int index) const; virtual void setData (const RefIdColumn *column, RefIdData& data, int index, const QVariant& value) const; ///< If the data type does not match an exception is thrown. - - virtual std::string getId (const RecordBase& record) const; }; + + template + BaseRefIdAdapter::BaseRefIdAdapter (UniversalId::Type type, const BaseColumns& base) + : mType (type), mBase (base) + {} + + template + std::string BaseRefIdAdapter::getId (const RecordBase& record) const + { + return dynamic_cast&> (record).get().mId; + } + + template + QVariant BaseRefIdAdapter::getData (const RefIdColumn *column, const RefIdData& data, + int index) const + { + const Record& record = static_cast&> ( + data.getRecord (RefIdData::LocalIndex (index, mType))); + + if (column==mBase.mId) + return QString::fromUtf8 (record.get().mId.c_str()); + + if (column==mBase.mModified) + { + if (record.mState==Record::State_Erased) + return static_cast (Record::State_Deleted); + + return static_cast (record.mState); + } + + return QVariant(); + } + + template + void BaseRefIdAdapter::setData (const RefIdColumn *column, RefIdData& data, int index, + const QVariant& value) const + { + Record& record = static_cast&> ( + data.getRecord (RefIdData::LocalIndex (index, mType))); + + if (column==mBase.mModified) + record.mState = static_cast (value.toInt()); + } } #endif diff --git a/apps/opencs/model/world/refidcollection.cpp b/apps/opencs/model/world/refidcollection.cpp index 1312a44e3..c2969bf9c 100644 --- a/apps/opencs/model/world/refidcollection.cpp +++ b/apps/opencs/model/world/refidcollection.cpp @@ -34,14 +34,20 @@ const CSMWorld::RefIdAdapter& CSMWorld::RefIdCollection::findAdaptor (UniversalI CSMWorld::RefIdCollection::RefIdCollection() { + BaseColumns baseColumns; + mColumns.push_back (RefIdColumn ("ID", ColumnBase::Display_String, ColumnBase::Flag_Table | ColumnBase::Flag_Dialogue, false, false)); + baseColumns.mId = &mColumns.back(); mColumns.push_back (RefIdColumn ("*", ColumnBase::Display_Integer, ColumnBase::Flag_Table | ColumnBase::Flag_Dialogue, false, false)); + baseColumns.mModified = &mColumns.back(); // mColumns.push_back (RefIdColumn ("Name", ColumnBase::Display_String)); - mAdapters.insert (std::make_pair (UniversalId::Type_Static, new StaticRefIdAdapter (&mColumns[0], - &mColumns[1]))); + + + mAdapters.insert (std::make_pair (UniversalId::Type_Static, + new BaseRefIdAdapter (UniversalId::Type_Static, baseColumns))); } diff --git a/apps/opencs/model/world/refidcollection.hpp b/apps/opencs/model/world/refidcollection.hpp index 4f8096d25..a55ad38a4 100644 --- a/apps/opencs/model/world/refidcollection.hpp +++ b/apps/opencs/model/world/refidcollection.hpp @@ -3,6 +3,7 @@ #include #include +#include #include "columnbase.hpp" #include "idcollection.hpp" @@ -33,7 +34,7 @@ namespace CSMWorld private: RefIdData mData; - std::vector mColumns; + std::deque mColumns; std::map mAdapters; private: