mirror of
https://github.com/OpenMW/openmw.git
synced 2025-02-23 21:09:42 +00:00
major refactorisation
This commit is contained in:
parent
dcd90faaef
commit
24eb034ba3
6 changed files with 210 additions and 132 deletions
|
@ -1 +1,8 @@
|
|||
#include "nestedadaptors.hpp"
|
||||
|
||||
CSMWorld::HelperBase::HelperBase(CSMWorld::UniversalId::Type type)
|
||||
: mType(type)
|
||||
{}
|
||||
|
||||
CSMWorld::HelperBase::~HelperBase()
|
||||
{}
|
||||
|
|
|
@ -6,45 +6,85 @@
|
|||
|
||||
#include "universalid.hpp"
|
||||
#include "nestedtablewrapper.hpp"
|
||||
#include <components/esm/loadcont.hpp>
|
||||
#include "record.hpp"
|
||||
#include "refiddata.hpp"
|
||||
#include "refidadapter.hpp"
|
||||
#include <components/esm/loadcont.hpp>
|
||||
|
||||
#include <QVariant>
|
||||
|
||||
namespace CSMWorld
|
||||
{
|
||||
template <typename ESXRecordT>
|
||||
class InventoryHelper
|
||||
class RefIdColumn;
|
||||
|
||||
class HelperBase
|
||||
{
|
||||
CSMWorld::UniversalId::Type mType;
|
||||
protected:
|
||||
const CSMWorld::UniversalId::Type mType;
|
||||
|
||||
public:
|
||||
HelperBase(CSMWorld::UniversalId::Type type);
|
||||
|
||||
virtual ~HelperBase();
|
||||
|
||||
virtual void setNestedTable(RefIdData& data,
|
||||
int index,
|
||||
const NestedTableWrapperBase& nestedTable) = 0;
|
||||
|
||||
virtual NestedTableWrapperBase* nestedTable(const RefIdData& data,
|
||||
int index) const = 0;
|
||||
|
||||
virtual QVariant getNestedData(const CSMWorld::RefIdData& data,
|
||||
int index,
|
||||
int subRowIndex,
|
||||
int subColIndex) const = 0;
|
||||
|
||||
InventoryHelper(CSMWorld::UniversalId::Type type) : mType(type) {};
|
||||
virtual void removeNestedRow (RefIdData& data,
|
||||
int index,
|
||||
int rowToRemove) const = 0;
|
||||
|
||||
void setNestedTable(const RefIdColumn* column,
|
||||
RefIdData& data,
|
||||
int index,
|
||||
const NestedTableWrapperBase& nestedTable)
|
||||
virtual void setNestedData (RefIdData& data,
|
||||
int index,
|
||||
const QVariant& value,
|
||||
int subRowIndex,
|
||||
int subColIndex) const = 0;
|
||||
|
||||
virtual void addNestedRow (RefIdData& data,
|
||||
int index,
|
||||
int position) const = 0;
|
||||
|
||||
virtual int getNestedColumnsCount(const RefIdData& data) const = 0;
|
||||
|
||||
virtual int getNestedRowsCount(const RefIdData& data,
|
||||
int index) const = 0;
|
||||
};
|
||||
|
||||
template <typename ESXRecordT>
|
||||
class InventoryHelper : public HelperBase
|
||||
{
|
||||
public:
|
||||
|
||||
InventoryHelper(CSMWorld::UniversalId::Type type)
|
||||
: HelperBase(type) {}
|
||||
|
||||
virtual void setNestedTable(RefIdData& data,
|
||||
int index,
|
||||
const NestedTableWrapperBase& nestedTable)
|
||||
{
|
||||
getRecord(data, index).get().mInventory.mList =
|
||||
(static_cast<const NestedTableWrapper<std::vector<ESM::ContItem> >&>(nestedTable)).mNestedTable;
|
||||
}
|
||||
|
||||
NestedTableWrapperBase* nestedTable(const RefIdColumn* column,
|
||||
const RefIdData& data,
|
||||
int index) const
|
||||
virtual NestedTableWrapperBase* nestedTable(const RefIdData& data,
|
||||
int index) const
|
||||
{
|
||||
return new NestedTableWrapper<std::vector<ESM::ContItem> >(getRecord(data, index).get().mInventory.mList);
|
||||
}
|
||||
|
||||
QVariant getNestedData(const CSMWorld::RefIdColumn* column,
|
||||
const CSMWorld::RefIdData& data,
|
||||
int index,
|
||||
int subRowIndex,
|
||||
int subColIndex) const
|
||||
virtual QVariant getNestedData(const CSMWorld::RefIdData& data,
|
||||
int index,
|
||||
int subRowIndex,
|
||||
int subColIndex) const
|
||||
{
|
||||
const ESM::ContItem& content = getRecord(data, index).get().mInventory.mList.at(subRowIndex);
|
||||
|
||||
|
@ -61,15 +101,14 @@ namespace CSMWorld
|
|||
}
|
||||
}
|
||||
|
||||
void removeNestedRow (const RefIdColumn *column, RefIdData& data, int index, int rowToRemove) const
|
||||
virtual void removeNestedRow (RefIdData& data, int index, int rowToRemove) const
|
||||
{
|
||||
std::vector<ESM::ContItem>& list = getRecord(data, index).get().mInventory.mList;
|
||||
|
||||
list.erase (list.begin () + rowToRemove);
|
||||
}
|
||||
|
||||
void setNestedData (const RefIdColumn *column,
|
||||
RefIdData& data,
|
||||
void setNestedData (RefIdData& data,
|
||||
int index,
|
||||
const QVariant& value,
|
||||
int subRowIndex,
|
||||
|
@ -90,7 +129,7 @@ namespace CSMWorld
|
|||
}
|
||||
}
|
||||
|
||||
void addNestedRow (const RefIdColumn *column, RefIdData& data, int index, int position) const
|
||||
virtual void addNestedRow (RefIdData& data, int index, int position) const
|
||||
{
|
||||
std::vector<ESM::ContItem>& list = getRecord(data, index).get().mInventory.mList;
|
||||
|
||||
|
@ -104,6 +143,18 @@ namespace CSMWorld
|
|||
list.insert(list.begin()+position, newRow);
|
||||
}
|
||||
|
||||
virtual int getNestedColumnsCount(const RefIdData& data) const
|
||||
{
|
||||
return 2;
|
||||
}
|
||||
|
||||
|
||||
virtual int getNestedRowsCount(const RefIdData& data,
|
||||
int index) const
|
||||
{
|
||||
return getRecord(data, index).get().mInventory.mList.size();
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
const Record<ESXRecordT>& getRecord(const RefIdData& data, int index) const
|
||||
|
|
|
@ -1,13 +1,89 @@
|
|||
|
||||
#include "refidadapter.hpp"
|
||||
|
||||
#include "cassert"
|
||||
#include "nestedtablewrapper.hpp"
|
||||
|
||||
#include <QVariant>
|
||||
|
||||
CSMWorld::RefIdAdapter::RefIdAdapter() {}
|
||||
|
||||
CSMWorld::RefIdAdapter::~RefIdAdapter() {}
|
||||
|
||||
CSMWorld::NestedRefIdAdapter::NestedRefIdAdapter() {}
|
||||
CSMWorld::NestedRefIdAdapterBase::NestedRefIdAdapterBase() {}
|
||||
|
||||
CSMWorld::NestedRefIdAdapter::~NestedRefIdAdapter() {}
|
||||
CSMWorld::NestedRefIdAdapterBase::~NestedRefIdAdapterBase() {}
|
||||
|
||||
CSMWorld::NestedRefIdAdapter::NestedRefIdAdapter()
|
||||
{}
|
||||
|
||||
CSMWorld::NestedRefIdAdapter::~NestedRefIdAdapter()
|
||||
{
|
||||
for (unsigned i = 0; i < mAssociatedColumns.size(); ++i)
|
||||
{
|
||||
delete mAssociatedColumns[i].second;
|
||||
}
|
||||
}
|
||||
|
||||
void CSMWorld::NestedRefIdAdapter::setNestedData (const RefIdColumn *column, RefIdData& data, int row,
|
||||
const QVariant& value, int subRowIndex, int subColIndex) const
|
||||
{
|
||||
getHelper(column)->setNestedData(data, row, value, subRowIndex, subColIndex);
|
||||
}
|
||||
|
||||
QVariant CSMWorld::NestedRefIdAdapter::getNestedData(const RefIdColumn *column, const RefIdData& data,
|
||||
int index, int subRowIndex, int subColIndex) const
|
||||
{
|
||||
return getHelper(column)->getNestedData(data, index, subRowIndex, subColIndex);
|
||||
}
|
||||
|
||||
int CSMWorld::NestedRefIdAdapter::getNestedColumnsCount(const RefIdColumn *column, const RefIdData& data) const
|
||||
{
|
||||
return getHelper(column)->getNestedColumnsCount(data);
|
||||
}
|
||||
|
||||
|
||||
int CSMWorld::NestedRefIdAdapter::getNestedRowsCount(const RefIdColumn *column, const RefIdData& data, int index) const
|
||||
{
|
||||
return getHelper(column)->getNestedRowsCount(data, index);
|
||||
}
|
||||
|
||||
|
||||
void CSMWorld::NestedRefIdAdapter::removeNestedRow (const RefIdColumn *column, RefIdData& data, int index, int rowToRemove) const
|
||||
{
|
||||
getHelper(column)->removeNestedRow(data, index, rowToRemove);
|
||||
}
|
||||
|
||||
void CSMWorld::NestedRefIdAdapter::addNestedRow (const RefIdColumn *column, RefIdData& data, int index, int position) const
|
||||
{
|
||||
getHelper(column)->addNestedRow(data, index, position); //This code grows more boring and boring. I would love some macros.
|
||||
}
|
||||
|
||||
void CSMWorld::NestedRefIdAdapter::setNestedTable (const RefIdColumn* column, RefIdData& data, int index, const NestedTableWrapperBase& nestedTable)
|
||||
{
|
||||
getHelper(column)->setNestedTable(data, index, nestedTable);
|
||||
}
|
||||
|
||||
|
||||
CSMWorld::NestedTableWrapperBase* CSMWorld::NestedRefIdAdapter::nestedTable (const RefIdColumn* column, const RefIdData& data, int index) const
|
||||
{
|
||||
return getHelper(column)->nestedTable(data, index);
|
||||
}
|
||||
|
||||
CSMWorld::HelperBase* CSMWorld::NestedRefIdAdapter::getHelper(const RefIdColumn *column) const
|
||||
{
|
||||
for (unsigned i = 0; i < mAssociatedColumns.size(); ++i)
|
||||
{
|
||||
if (mAssociatedColumns[i].first == column)
|
||||
{
|
||||
return mAssociatedColumns[i].second;
|
||||
}
|
||||
}
|
||||
|
||||
throw std::logic_error("No such column in the nestedrefidadapter");
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void CSMWorld::NestedRefIdAdapter::setAssocColumns(const std::vector<std::pair <const RefIdColumn*, HelperBase*> >& assocColumns)
|
||||
{
|
||||
mAssociatedColumns = assocColumns;
|
||||
}
|
||||
|
|
|
@ -2,6 +2,9 @@
|
|||
#define CSM_WOLRD_REFIDADAPTER_H
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "nestedadaptors.hpp"
|
||||
|
||||
class QVariant;
|
||||
|
||||
|
@ -11,6 +14,7 @@ namespace CSMWorld
|
|||
class RefIdData;
|
||||
class RecordBase;
|
||||
class NestedTableWrapperBase;
|
||||
class HelperBase;
|
||||
|
||||
class RefIdAdapter
|
||||
{
|
||||
|
@ -36,12 +40,12 @@ namespace CSMWorld
|
|||
virtual void setId(RecordBase& record, const std::string& id) = 0;
|
||||
};
|
||||
|
||||
class NestedRefIdAdapter
|
||||
class NestedRefIdAdapterBase
|
||||
{
|
||||
public:
|
||||
NestedRefIdAdapter();
|
||||
NestedRefIdAdapterBase();
|
||||
|
||||
virtual ~NestedRefIdAdapter();
|
||||
virtual ~NestedRefIdAdapterBase();
|
||||
|
||||
virtual void setNestedData (const RefIdColumn *column, RefIdData& data, int row,
|
||||
const QVariant& value, int subRowIndex, int subColIndex) const = 0;
|
||||
|
@ -61,6 +65,41 @@ namespace CSMWorld
|
|||
|
||||
virtual NestedTableWrapperBase* nestedTable (const RefIdColumn* column, const RefIdData& data, int index) const = 0;
|
||||
};
|
||||
|
||||
class NestedRefIdAdapter : public NestedRefIdAdapterBase
|
||||
{
|
||||
std::vector<std::pair <const RefIdColumn*, HelperBase*> > mAssociatedColumns; //basicly, i wanted map, but with pointer key
|
||||
|
||||
public:
|
||||
NestedRefIdAdapter();
|
||||
|
||||
virtual ~NestedRefIdAdapter();
|
||||
|
||||
virtual void setNestedData (const RefIdColumn *column, RefIdData& data, int row,
|
||||
const QVariant& value, int subRowIndex, int subColIndex) const;
|
||||
|
||||
virtual QVariant getNestedData (const RefIdColumn *column, const RefIdData& data,
|
||||
int index, int subRowIndex, int subColIndex) const;
|
||||
|
||||
virtual int getNestedColumnsCount(const RefIdColumn *column, const RefIdData& data) const;
|
||||
|
||||
virtual int getNestedRowsCount(const RefIdColumn *column, const RefIdData& data, int index) const;
|
||||
|
||||
virtual void removeNestedRow (const RefIdColumn *column, RefIdData& data, int index, int rowToRemove) const;
|
||||
|
||||
virtual void addNestedRow (const RefIdColumn *column, RefIdData& data, int index, int position) const;
|
||||
|
||||
virtual void setNestedTable (const RefIdColumn* column, RefIdData& data, int index, const NestedTableWrapperBase& nestedTable);
|
||||
|
||||
virtual NestedTableWrapperBase* nestedTable (const RefIdColumn* column, const RefIdData& data, int index) const;
|
||||
|
||||
protected:
|
||||
void setAssocColumns(const std::vector<std::pair <const RefIdColumn*, HelperBase*> >& assocColumns);
|
||||
|
||||
private:
|
||||
|
||||
HelperBase* getHelper(const RefIdColumn *column) const;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
#include <cassert>
|
||||
#include <stdexcept>
|
||||
#include <utility>
|
||||
|
||||
#include <components/esm/loadcont.hpp>
|
||||
#include "nestedtablewrapper.hpp"
|
||||
|
@ -180,28 +181,18 @@ void CSMWorld::ClothingRefIdAdapter::setData (const RefIdColumn *column, RefIdDa
|
|||
CSMWorld::ContainerRefIdAdapter::ContainerRefIdAdapter (const NameColumns& columns,
|
||||
const RefIdColumn *weight, const RefIdColumn *organic, const RefIdColumn *respawn, const RefIdColumn *content)
|
||||
: NameRefIdAdapter<ESM::Container> (UniversalId::Type_Container, columns), mWeight (weight),
|
||||
mOrganic (organic), mRespawn (respawn), mContent(content), mHelper(InventoryHelper<ESM::Container>(UniversalId::Type_Container))
|
||||
{}
|
||||
|
||||
int CSMWorld::ContainerRefIdAdapter::getNestedColumnsCount(const RefIdColumn *column, const RefIdData& data) const
|
||||
mOrganic (organic), mRespawn (respawn), mContent(content)
|
||||
{
|
||||
assert(column==mContent);
|
||||
std::vector<std::pair <const RefIdColumn*, HelperBase*> > assoCol;
|
||||
|
||||
return 2;
|
||||
assoCol.push_back(std::make_pair(content, new InventoryHelper<ESM::Container>(UniversalId::Type_Container)));
|
||||
|
||||
setAssocColumns(assoCol);
|
||||
}
|
||||
|
||||
int CSMWorld::ContainerRefIdAdapter::getNestedRowsCount(const RefIdColumn *column, const RefIdData& data, int index) const
|
||||
{
|
||||
assert(column==mContent);
|
||||
|
||||
const Record<ESM::Container>& record = static_cast<const Record<ESM::Container>&> (
|
||||
data.getRecord(RefIdData::LocalIndex (index, UniversalId::Type_Container)));
|
||||
|
||||
return record.get().mInventory.mList.size();
|
||||
}
|
||||
|
||||
QVariant CSMWorld::ContainerRefIdAdapter::getData (const RefIdColumn *column, const RefIdData& data,
|
||||
int index) const
|
||||
QVariant CSMWorld::ContainerRefIdAdapter::getData (const RefIdColumn *column,
|
||||
const RefIdData& data,
|
||||
int index) const
|
||||
{
|
||||
const Record<ESM::Container>& record = static_cast<const Record<ESM::Container>&> (
|
||||
data.getRecord (RefIdData::LocalIndex (index, UniversalId::Type_Container)));
|
||||
|
@ -221,24 +212,6 @@ QVariant CSMWorld::ContainerRefIdAdapter::getData (const RefIdColumn *column, co
|
|||
return NameRefIdAdapter<ESM::Container>::getData (column, data, index);
|
||||
}
|
||||
|
||||
void CSMWorld::ContainerRefIdAdapter::removeNestedRow (const RefIdColumn *column, RefIdData& data, int index, int rowToRemove) const
|
||||
{
|
||||
if(column!=mContent)
|
||||
{
|
||||
throw std::logic_error("This column does not hold multiple values.");
|
||||
}
|
||||
mHelper.removeNestedRow(column, data, index, rowToRemove);
|
||||
}
|
||||
|
||||
void CSMWorld::ContainerRefIdAdapter::addNestedRow (const RefIdColumn *column, RefIdData& data, int index, int position) const
|
||||
{
|
||||
if(column!=mContent)
|
||||
{
|
||||
throw std::logic_error("This column does not hold multiple values.");
|
||||
}
|
||||
|
||||
mHelper.addNestedRow(column, data, index, position);
|
||||
}
|
||||
|
||||
void CSMWorld::ContainerRefIdAdapter::setData (const RefIdColumn *column, RefIdData& data, int index,
|
||||
const QVariant& value) const
|
||||
|
@ -266,54 +239,6 @@ void CSMWorld::ContainerRefIdAdapter::setData (const RefIdColumn *column, RefIdD
|
|||
NameRefIdAdapter<ESM::Container>::setData (column, data, index, value);
|
||||
}
|
||||
|
||||
void CSMWorld::ContainerRefIdAdapter::setNestedData(const RefIdColumn *column,
|
||||
RefIdData& data,
|
||||
int index,
|
||||
const QVariant& value,
|
||||
int subRowIndex,
|
||||
int subColIndex) const
|
||||
{
|
||||
|
||||
if (column==mContent)
|
||||
{
|
||||
mHelper.setNestedData(column, data, index, value, subRowIndex, subColIndex);
|
||||
} else
|
||||
{
|
||||
throw std::logic_error("This column do not nest other columns");
|
||||
}
|
||||
}
|
||||
|
||||
void CSMWorld::ContainerRefIdAdapter::setNestedTable(const RefIdColumn* column,
|
||||
RefIdData& data,
|
||||
int index,
|
||||
const NestedTableWrapperBase& nestedTable)
|
||||
{
|
||||
mHelper.setNestedTable(column, data, index, nestedTable);
|
||||
}
|
||||
|
||||
CSMWorld::NestedTableWrapperBase* CSMWorld::ContainerRefIdAdapter::nestedTable (const RefIdColumn* column,
|
||||
const RefIdData& data,
|
||||
int index) const
|
||||
{
|
||||
return mHelper.nestedTable(column, data, index);
|
||||
}
|
||||
|
||||
QVariant CSMWorld::ContainerRefIdAdapter::getNestedData (const CSMWorld::RefIdColumn* column,
|
||||
const CSMWorld::RefIdData& data,
|
||||
int index,
|
||||
int subRowIndex,
|
||||
int subColIndex) const
|
||||
{
|
||||
if (column==mContent)
|
||||
{
|
||||
return mHelper.getNestedData(column, data, index, subRowIndex, subColIndex);
|
||||
} else
|
||||
{
|
||||
throw std::logic_error("This column do not nest other columns");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
CSMWorld::CreatureColumns::CreatureColumns (const ActorColumns& actorColumns)
|
||||
: ActorColumns (actorColumns)
|
||||
{}
|
||||
|
|
|
@ -616,36 +616,16 @@ namespace CSMWorld
|
|||
const RefIdColumn *mRespawn;
|
||||
const RefIdColumn *mContent;
|
||||
|
||||
InventoryHelper<ESM::Container> mHelper;
|
||||
|
||||
public:
|
||||
|
||||
ContainerRefIdAdapter (const NameColumns& columns, const RefIdColumn *weight,
|
||||
const RefIdColumn *organic, const RefIdColumn *respawn, const RefIdColumn *content);
|
||||
|
||||
virtual QVariant getNestedData (const RefIdColumn *column, const RefIdData& data, int index,
|
||||
int subRowIndex, int subColIndex) const;
|
||||
|
||||
virtual QVariant getData (const RefIdColumn *column, const RefIdData& data, int index) const;
|
||||
|
||||
virtual void setNestedData (const RefIdColumn *column, RefIdData& data, int index,
|
||||
const QVariant& value, int subRowIndex, int subColIndex) 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 int getNestedColumnsCount(const RefIdColumn *column, const RefIdData& data) const;
|
||||
|
||||
virtual int getNestedRowsCount(const RefIdColumn *column, const RefIdData& data, int index) const;
|
||||
|
||||
virtual void removeNestedRow (const RefIdColumn *column, RefIdData& data, int index, int rowToRemove) const;
|
||||
|
||||
virtual void addNestedRow (const RefIdColumn *column, RefIdData& data, int index, int position) const;
|
||||
|
||||
virtual NestedTableWrapperBase* nestedTable (const RefIdColumn * column, const RefIdData& data, int index) const;
|
||||
|
||||
virtual void setNestedTable (const RefIdColumn* column, RefIdData& data, int index, const NestedTableWrapperBase& nestedTable);
|
||||
};
|
||||
|
||||
struct CreatureColumns : public ActorColumns
|
||||
|
|
Loading…
Reference in a new issue