replaced static adapter with a template

actorid
Marc Zinnschlag 12 years ago
parent 9426eda47a
commit d715cf5431

@ -1,49 +1,5 @@
#include "refidadapterimp.hpp"
#include <QVariant>
#include <components/esm/loadstat.hpp>
#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<ESM::Static>& record = static_cast<const Record<ESM::Static>&> (
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<ESM::Static>::State_Erased)
return static_cast<int> (Record<ESM::Static>::State_Deleted);
return static_cast<int> (record.mState);
}
return QVariant();
}
void CSMWorld::StaticRefIdAdapter::setData (const RefIdColumn *column, RefIdData& data, int index,
const QVariant& value) const
{
Record<ESM::Static>& record = static_cast<Record<ESM::Static>&> (
data.getRecord (RefIdData::LocalIndex (index, UniversalId::Type_Static)));
if (column==mModified)
record.mState = static_cast<RecordBase::State> (value.toInt());
}
std::string CSMWorld::StaticRefIdAdapter::getId (const RecordBase& record) const
{
return dynamic_cast<const Record<ESM::Static>&> (record).get().mId;
}

@ -1,28 +1,83 @@
#ifndef CSM_WOLRD_REFIDADAPTERIMP_H
#define CSM_WOLRD_REFIDADAPTERIMP_H
#include <QVariant>
#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<typename RecordT>
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<typename RecordT>
BaseRefIdAdapter<RecordT>::BaseRefIdAdapter (UniversalId::Type type, const BaseColumns& base)
: mType (type), mBase (base)
{}
template<typename RecordT>
std::string BaseRefIdAdapter<RecordT>::getId (const RecordBase& record) const
{
return dynamic_cast<const Record<RecordT>&> (record).get().mId;
}
template<typename RecordT>
QVariant BaseRefIdAdapter<RecordT>::getData (const RefIdColumn *column, const RefIdData& data,
int index) const
{
const Record<RecordT>& record = static_cast<const Record<RecordT>&> (
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<RecordT>::State_Erased)
return static_cast<int> (Record<RecordT>::State_Deleted);
return static_cast<int> (record.mState);
}
return QVariant();
}
template<typename RecordT>
void BaseRefIdAdapter<RecordT>::setData (const RefIdColumn *column, RefIdData& data, int index,
const QVariant& value) const
{
Record<RecordT>& record = static_cast<Record<RecordT>&> (
data.getRecord (RefIdData::LocalIndex (index, mType)));
if (column==mBase.mModified)
record.mState = static_cast<RecordBase::State> (value.toInt());
}
}
#endif

@ -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<ESM::Static> (UniversalId::Type_Static, baseColumns)));
}

@ -3,6 +3,7 @@
#include <vector>
#include <map>
#include <deque>
#include "columnbase.hpp"
#include "idcollection.hpp"
@ -33,7 +34,7 @@ namespace CSMWorld
private:
RefIdData mData;
std::vector<RefIdColumn> mColumns;
std::deque<RefIdColumn> mColumns;
std::map<UniversalId::Type, RefIdAdapter *> mAdapters;
private:

Loading…
Cancel
Save