1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-04-01 13:36:40 +00:00

Use std::unique_ptr to store records in collections, RefidCollection and RefIdData.

- std::move support required (C++11)
- MSVC 2013 or later should be fine
This commit is contained in:
cc9cii 2015-12-06 15:14:05 +11:00
parent 257126ed69
commit 23e7e3c165
22 changed files with 248 additions and 229 deletions

View file

@ -2,6 +2,7 @@
#include <cassert> #include <cassert>
#include <fstream> #include <fstream>
#include <memory>
#include <boost/filesystem.hpp> #include <boost/filesystem.hpp>
@ -2091,10 +2092,10 @@ void CSMDoc::Document::addOptionalGmst (const ESM::GameSetting& gmst)
{ {
if (getData().getGmsts().searchId (gmst.mId)==-1) if (getData().getGmsts().searchId (gmst.mId)==-1)
{ {
CSMWorld::Record<ESM::GameSetting> record; std::unique_ptr<CSMWorld::Record<ESM::GameSetting> > record(new CSMWorld::Record<ESM::GameSetting>);
record.mBase = gmst; record->mBase = gmst;
record.mState = CSMWorld::RecordBase::State_BaseOnly; record->mState = CSMWorld::RecordBase::State_BaseOnly;
getData().getGmsts().appendRecord (record); getData().getGmsts().appendRecord (std::move(record));
} }
} }
@ -2102,10 +2103,10 @@ void CSMDoc::Document::addOptionalGlobal (const ESM::Global& global)
{ {
if (getData().getGlobals().searchId (global.mId)==-1) if (getData().getGlobals().searchId (global.mId)==-1)
{ {
CSMWorld::Record<ESM::Global> record; std::unique_ptr<CSMWorld::Record<ESM::Global> > record(new CSMWorld::Record<ESM::Global>);
record.mBase = global; record->mBase = global;
record.mState = CSMWorld::RecordBase::State_BaseOnly; record->mState = CSMWorld::RecordBase::State_BaseOnly;
getData().getGlobals().appendRecord (record); getData().getGlobals().appendRecord (std::move(record));
} }
} }
@ -2113,10 +2114,10 @@ void CSMDoc::Document::addOptionalMagicEffect (const ESM::MagicEffect& magicEffe
{ {
if (getData().getMagicEffects().searchId (magicEffect.mId)==-1) if (getData().getMagicEffects().searchId (magicEffect.mId)==-1)
{ {
CSMWorld::Record<ESM::MagicEffect> record; std::unique_ptr<CSMWorld::Record<ESM::MagicEffect> > record(new CSMWorld::Record<ESM::MagicEffect>);
record.mBase = magicEffect; record->mBase = magicEffect;
record.mState = CSMWorld::RecordBase::State_BaseOnly; record->mState = CSMWorld::RecordBase::State_BaseOnly;
getData().getMagicEffects().appendRecord (record); getData().getMagicEffects().appendRecord (std::move(record));
} }
} }

View file

@ -118,7 +118,7 @@ void CSMDoc::WriteDialogueCollectionStage::perform (int stage, Messages& message
for (CSMWorld::InfoCollection::RecordConstIterator iter (range.first); iter!=range.second; ++iter) for (CSMWorld::InfoCollection::RecordConstIterator iter (range.first); iter!=range.second; ++iter)
{ {
if (iter->isModified() || iter->mState == CSMWorld::RecordBase::State_Deleted) if ((*iter)->isModified() || (*iter)->mState == CSMWorld::RecordBase::State_Deleted)
{ {
infoModified = true; infoModified = true;
break; break;
@ -144,9 +144,9 @@ void CSMDoc::WriteDialogueCollectionStage::perform (int stage, Messages& message
// write modified selected info records // write modified selected info records
for (CSMWorld::InfoCollection::RecordConstIterator iter (range.first); iter!=range.second; ++iter) for (CSMWorld::InfoCollection::RecordConstIterator iter (range.first); iter!=range.second; ++iter)
{ {
if (iter->isModified() || iter->mState == CSMWorld::RecordBase::State_Deleted) if ((*iter)->isModified() || (*iter)->mState == CSMWorld::RecordBase::State_Deleted)
{ {
ESM::DialInfo info = iter->get(); ESM::DialInfo info = (*iter)->get();
info.mId = info.mId.substr (info.mId.find_last_of ('#')+1); info.mId = info.mId.substr (info.mId.find_last_of ('#')+1);
info.mPrev = ""; info.mPrev = "";
@ -155,7 +155,7 @@ void CSMDoc::WriteDialogueCollectionStage::perform (int stage, Messages& message
CSMWorld::InfoCollection::RecordConstIterator prev = iter; CSMWorld::InfoCollection::RecordConstIterator prev = iter;
--prev; --prev;
info.mPrev = prev->get().mId.substr (prev->get().mId.find_last_of ('#')+1); info.mPrev = (*prev)->get().mId.substr ((*prev)->get().mId.find_last_of ('#')+1);
} }
CSMWorld::InfoCollection::RecordConstIterator next = iter; CSMWorld::InfoCollection::RecordConstIterator next = iter;
@ -164,11 +164,11 @@ void CSMDoc::WriteDialogueCollectionStage::perform (int stage, Messages& message
info.mNext = ""; info.mNext = "";
if (next!=range.second) if (next!=range.second)
{ {
info.mNext = next->get().mId.substr (next->get().mId.find_last_of ('#')+1); info.mNext = (*next)->get().mId.substr ((*next)->get().mId.find_last_of ('#')+1);
} }
writer.startRecord (info.sRecordId); writer.startRecord (info.sRecordId);
info.save (writer, iter->mState == CSMWorld::RecordBase::State_Deleted); info.save (writer, (*iter)->mState == CSMWorld::RecordBase::State_Deleted);
writer.endRecord (info.sRecordId); writer.endRecord (info.sRecordId);
} }
} }

View file

@ -100,10 +100,9 @@ void CSMTools::MergeReferencesStage::perform (int stage, CSMDoc::Messages& messa
ref.mRefNum.mIndex = mIndex[Misc::StringUtils::lowerCase (ref.mCell)]++; ref.mRefNum.mIndex = mIndex[Misc::StringUtils::lowerCase (ref.mCell)]++;
ref.mRefNum.mContentFile = 0; ref.mRefNum.mContentFile = 0;
CSMWorld::Record<CSMWorld::CellRef> newRecord ( mState.mTarget->getData().getReferences().appendRecord (
CSMWorld::RecordBase::State_ModifiedOnly, 0, &ref); std::make_unique<CSMWorld::Record<CSMWorld::CellRef> >(
CSMWorld::Record<CSMWorld::CellRef>(CSMWorld::RecordBase::State_ModifiedOnly, 0, &ref)));
mState.mTarget->getData().getReferences().appendRecord (newRecord);
} }
} }
@ -189,10 +188,9 @@ void CSMTools::MergeLandTexturesStage::perform (int stage, CSMDoc::Messages& mes
texture.mIndex = mNext->second-1; texture.mIndex = mNext->second-1;
texture.mId = stream2.str(); texture.mId = stream2.str();
CSMWorld::Record<CSMWorld::LandTexture> newRecord ( mState.mTarget->getData().getLandTextures().appendRecord (
CSMWorld::RecordBase::State_ModifiedOnly, 0, &texture); std::make_unique<CSMWorld::Record<CSMWorld::LandTexture> >(
CSMWorld::Record<CSMWorld::LandTexture>(CSMWorld::RecordBase::State_ModifiedOnly, 0, &texture)));
mState.mTarget->getData().getLandTextures().appendRecord (newRecord);
found = true; found = true;
} }
@ -250,9 +248,8 @@ void CSMTools::MergeLandStage::perform (int stage, CSMDoc::Messages& messages)
} }
} }
CSMWorld::Record<CSMWorld::Land> newRecord ( mState.mTarget->getData().getLand().appendRecord (
CSMWorld::RecordBase::State_ModifiedOnly, 0, &newLand); std::make_unique<CSMWorld::Record<CSMWorld::Land> >(
CSMWorld::Record<CSMWorld::Land>(CSMWorld::RecordBase::State_ModifiedOnly, 0, &newLand)));
mState.mTarget->getData().getLand().appendRecord (newRecord);
} }
} }

View file

@ -3,6 +3,7 @@
#include <algorithm> #include <algorithm>
#include <map> #include <map>
#include <memory>
#include <components/to_utf8/to_utf8.hpp> #include <components/to_utf8/to_utf8.hpp>
@ -82,7 +83,8 @@ namespace CSMTools
const CSMWorld::Record<RecordType>& record = source.getRecord (stage); const CSMWorld::Record<RecordType>& record = source.getRecord (stage);
if (!record.isDeleted()) if (!record.isDeleted())
target.appendRecord (CSMWorld::Record<RecordType> (CSMWorld::RecordBase::State_ModifiedOnly, 0, &record.get())); target.appendRecord (std::make_unique<CSMWorld::Record<RecordType> >(
CSMWorld::Record<RecordType>(CSMWorld::RecordBase::State_ModifiedOnly, 0, &record.get())));
} }
class MergeRefIdsStage : public CSMDoc::Stage class MergeRefIdsStage : public CSMDoc::Stage

View file

@ -7,6 +7,7 @@
#include <cctype> #include <cctype>
#include <stdexcept> #include <stdexcept>
#include <functional> #include <functional>
#include <memory>
#include <QVariant> #include <QVariant>
@ -49,7 +50,7 @@ namespace CSMWorld
private: private:
std::vector<Record<ESXRecordT> > mRecords; std::vector<std::unique_ptr<Record<ESXRecordT> > > mRecords;
std::map<std::string, int> mIndex; std::map<std::string, int> mIndex;
std::vector<Column<ESXRecordT> *> mColumns; std::vector<Column<ESXRecordT> *> mColumns;
@ -61,7 +62,7 @@ namespace CSMWorld
const std::map<std::string, int>& getIdMap() const; const std::map<std::string, int>& getIdMap() const;
const std::vector<Record<ESXRecordT> >& getRecords() const; const std::vector<std::unique_ptr<Record<ESXRecordT> > >& getRecords() const;
bool reorderRowsImp (int baseIndex, const std::vector<int>& newOrder); bool reorderRowsImp (int baseIndex, const std::vector<int>& newOrder);
///< Reorder the rows [baseIndex, baseIndex+newOrder.size()) according to the indices ///< Reorder the rows [baseIndex, baseIndex+newOrder.size()) according to the indices
@ -112,12 +113,12 @@ namespace CSMWorld
////< Search record with \a id. ////< Search record with \a id.
/// \return index of record (if found) or -1 (not found) /// \return index of record (if found) or -1 (not found)
virtual void replace (int index, const RecordBase& record); virtual void replace (int index, std::unique_ptr<RecordBase> record);
///< If the record type does not match, an exception is thrown. ///< If the record type does not match, an exception is thrown.
/// ///
/// \attention \a record must not change the ID. /// \attention \a record must not change the ID.
virtual void appendRecord (const RecordBase& record, virtual void appendRecord (std::unique_ptr<RecordBase> record,
UniversalId::Type type = UniversalId::Type_None); UniversalId::Type type = UniversalId::Type_None);
///< If the record type does not match, an exception is thrown. ///< If the record type does not match, an exception is thrown.
///< \param type Will be ignored, unless the collection supports multiple record types ///< \param type Will be ignored, unless the collection supports multiple record types
@ -135,7 +136,7 @@ namespace CSMWorld
/// ///
/// \param listDeleted include deleted record in the list /// \param listDeleted include deleted record in the list
virtual void insertRecord (const RecordBase& record, int index, virtual void insertRecord (std::unique_ptr<RecordBase> record, int index,
UniversalId::Type type = UniversalId::Type_None); UniversalId::Type type = UniversalId::Type_None);
///< Insert record before index. ///< Insert record before index.
/// ///
@ -152,7 +153,7 @@ namespace CSMWorld
void addColumn (Column<ESXRecordT> *column); void addColumn (Column<ESXRecordT> *column);
void setRecord (int index, const Record<ESXRecordT>& record); void setRecord (int index, std::unique_ptr<Record<ESXRecordT> > record);
///< \attention This function must not change the ID. ///< \attention This function must not change the ID.
NestableColumn *getNestableColumn (int column) const; NestableColumn *getNestableColumn (int column) const;
@ -165,7 +166,7 @@ namespace CSMWorld
} }
template<typename ESXRecordT, typename IdAccessorT> template<typename ESXRecordT, typename IdAccessorT>
const std::vector<Record<ESXRecordT> >& Collection<ESXRecordT, IdAccessorT>::getRecords() const const std::vector<std::unique_ptr<Record<ESXRecordT> > >& Collection<ESXRecordT, IdAccessorT>::getRecords() const
{ {
return mRecords; return mRecords;
} }
@ -185,15 +186,15 @@ namespace CSMWorld
return false; return false;
// reorder records // reorder records
std::vector<Record<ESXRecordT> > buffer (size); std::vector<std::unique_ptr<Record<ESXRecordT> > > buffer (size);
for (int i=0; i<size; ++i) for (int i=0; i<size; ++i)
{ {
buffer[newOrder[i]] = mRecords [baseIndex+i]; buffer[newOrder[i]] = std::move(mRecords [baseIndex+i]);
buffer[newOrder[i]].setModified (buffer[newOrder[i]].get()); buffer[newOrder[i]]->setModified (buffer[newOrder[i]]->get());
} }
std::copy (buffer.begin(), buffer.end(), mRecords.begin()+baseIndex); std::move (buffer.begin(), buffer.end(), mRecords.begin()+baseIndex);
// adjust index // adjust index
for (std::map<std::string, int>::iterator iter (mIndex.begin()); iter!=mIndex.end(); for (std::map<std::string, int>::iterator iter (mIndex.begin()); iter!=mIndex.end();
@ -210,12 +211,12 @@ namespace CSMWorld
const std::string& destination, const std::string& destination,
const UniversalId::Type type) const UniversalId::Type type)
{ {
Record<ESXRecordT> copy; std::unique_ptr<Record<ESXRecordT> > copy(new Record<ESXRecordT>);
copy.mModified = getRecord(origin).get(); copy->mModified = getRecord(origin).get();
copy.mState = RecordBase::State_ModifiedOnly; copy->mState = RecordBase::State_ModifiedOnly;
copy.get().mId = destination; copy->get().mId = destination;
insertRecord(copy, getAppendIndex(destination, type)); insertRecord(std::move(copy), getAppendIndex(destination, type));
} }
template<typename ESXRecordT, typename IdAccessorT> template<typename ESXRecordT, typename IdAccessorT>
@ -238,15 +239,15 @@ namespace CSMWorld
if (iter==mIndex.end()) if (iter==mIndex.end())
{ {
Record<ESXRecordT> record2; std::unique_ptr<Record<ESXRecordT> > record2(new Record<ESXRecordT>);
record2.mState = Record<ESXRecordT>::State_ModifiedOnly; record2->mState = Record<ESXRecordT>::State_ModifiedOnly;
record2.mModified = record; record2->mModified = record;
insertRecord (record2, getAppendIndex (id)); insertRecord (std::move(record2), getAppendIndex (id));
} }
else else
{ {
mRecords[iter->second].setModified (record); mRecords[iter->second]->setModified (record);
} }
} }
@ -259,7 +260,7 @@ namespace CSMWorld
template<typename ESXRecordT, typename IdAccessorT> template<typename ESXRecordT, typename IdAccessorT>
std::string Collection<ESXRecordT, IdAccessorT>::getId (int index) const std::string Collection<ESXRecordT, IdAccessorT>::getId (int index) const
{ {
return IdAccessorT().getId (mRecords.at (index).get()); return IdAccessorT().getId (mRecords.at (index)->get());
} }
template<typename ESXRecordT, typename IdAccessorT> template<typename ESXRecordT, typename IdAccessorT>
@ -282,13 +283,13 @@ namespace CSMWorld
template<typename ESXRecordT, typename IdAccessorT> template<typename ESXRecordT, typename IdAccessorT>
QVariant Collection<ESXRecordT, IdAccessorT>::getData (int index, int column) const QVariant Collection<ESXRecordT, IdAccessorT>::getData (int index, int column) const
{ {
return mColumns.at (column)->get (mRecords.at (index)); return mColumns.at (column)->get (*mRecords.at (index));
} }
template<typename ESXRecordT, typename IdAccessorT> template<typename ESXRecordT, typename IdAccessorT>
void Collection<ESXRecordT, IdAccessorT>::setData (int index, int column, const QVariant& data) void Collection<ESXRecordT, IdAccessorT>::setData (int index, int column, const QVariant& data)
{ {
return mColumns.at (column)->set (mRecords.at (index), data); return mColumns.at (column)->set (*mRecords.at (index), data);
} }
template<typename ESXRecordT, typename IdAccessorT> template<typename ESXRecordT, typename IdAccessorT>
@ -315,8 +316,8 @@ namespace CSMWorld
template<typename ESXRecordT, typename IdAccessorT> template<typename ESXRecordT, typename IdAccessorT>
void Collection<ESXRecordT, IdAccessorT>::merge() void Collection<ESXRecordT, IdAccessorT>::merge()
{ {
for (typename std::vector<Record<ESXRecordT> >::iterator iter (mRecords.begin()); iter!=mRecords.end(); ++iter) for (typename std::vector<std::unique_ptr<Record<ESXRecordT> > >::iterator iter (mRecords.begin()); iter!=mRecords.end(); ++iter)
iter->merge(); (*iter)->merge();
purge(); purge();
} }
@ -328,7 +329,7 @@ namespace CSMWorld
while (i<static_cast<int> (mRecords.size())) while (i<static_cast<int> (mRecords.size()))
{ {
if (mRecords[i].isErased()) if (mRecords[i]->isErased())
removeRows (i, 1); removeRows (i, 1);
else else
++i; ++i;
@ -369,11 +370,11 @@ namespace CSMWorld
IdAccessorT().getId (record) = id; IdAccessorT().getId (record) = id;
record.blank(); record.blank();
Record<ESXRecordT> record2; std::unique_ptr<Record<ESXRecordT> > record2(new Record<ESXRecordT>);
record2.mState = Record<ESXRecordT>::State_ModifiedOnly; record2->mState = Record<ESXRecordT>::State_ModifiedOnly;
record2.mModified = record; record2->mModified = record;
insertRecord (record2, getAppendIndex (id, type), type); insertRecord (std::move(record2), getAppendIndex (id, type), type);
} }
template<typename ESXRecordT, typename IdAccessorT> template<typename ESXRecordT, typename IdAccessorT>
@ -390,18 +391,19 @@ namespace CSMWorld
} }
template<typename ESXRecordT, typename IdAccessorT> template<typename ESXRecordT, typename IdAccessorT>
void Collection<ESXRecordT, IdAccessorT>::replace (int index, const RecordBase& record) void Collection<ESXRecordT, IdAccessorT>::replace (int index, std::unique_ptr<RecordBase> record)
{ {
mRecords.at (index) = dynamic_cast<const Record<ESXRecordT>&> (record); std::unique_ptr<Record<ESXRecordT> > tmp(static_cast<Record<ESXRecordT>*>(record.release()));
mRecords.at (index) = std::move(tmp);
} }
template<typename ESXRecordT, typename IdAccessorT> template<typename ESXRecordT, typename IdAccessorT>
void Collection<ESXRecordT, IdAccessorT>::appendRecord (const RecordBase& record, void Collection<ESXRecordT, IdAccessorT>::appendRecord (std::unique_ptr<RecordBase> record,
UniversalId::Type type) UniversalId::Type type)
{ {
insertRecord (record, int index =
getAppendIndex (IdAccessorT().getId ( getAppendIndex(IdAccessorT().getId(static_cast<Record<ESXRecordT>*>(record.get())->get()), type);
dynamic_cast<const Record<ESXRecordT>&> (record).get()), type), type); insertRecord (std::move(record), index, type);
} }
template<typename ESXRecordT, typename IdAccessorT> template<typename ESXRecordT, typename IdAccessorT>
@ -419,8 +421,8 @@ namespace CSMWorld
for (typename std::map<std::string, int>::const_iterator iter = mIndex.begin(); for (typename std::map<std::string, int>::const_iterator iter = mIndex.begin();
iter!=mIndex.end(); ++iter) iter!=mIndex.end(); ++iter)
{ {
if (listDeleted || !mRecords[iter->second].isDeleted()) if (listDeleted || !mRecords[iter->second]->isDeleted())
ids.push_back (IdAccessorT().getId (mRecords[iter->second].get())); ids.push_back (IdAccessorT().getId (mRecords[iter->second]->get()));
} }
return ids; return ids;
@ -430,29 +432,30 @@ namespace CSMWorld
const Record<ESXRecordT>& Collection<ESXRecordT, IdAccessorT>::getRecord (const std::string& id) const const Record<ESXRecordT>& Collection<ESXRecordT, IdAccessorT>::getRecord (const std::string& id) const
{ {
int index = getIndex (id); int index = getIndex (id);
return mRecords.at (index); return *mRecords.at (index);
} }
template<typename ESXRecordT, typename IdAccessorT> template<typename ESXRecordT, typename IdAccessorT>
const Record<ESXRecordT>& Collection<ESXRecordT, IdAccessorT>::getRecord (int index) const const Record<ESXRecordT>& Collection<ESXRecordT, IdAccessorT>::getRecord (int index) const
{ {
return mRecords.at (index); return *mRecords.at (index);
} }
template<typename ESXRecordT, typename IdAccessorT> template<typename ESXRecordT, typename IdAccessorT>
void Collection<ESXRecordT, IdAccessorT>::insertRecord (const RecordBase& record, int index, void Collection<ESXRecordT, IdAccessorT>::insertRecord (std::unique_ptr<RecordBase> record, int index,
UniversalId::Type type) UniversalId::Type type)
{ {
int size = static_cast<int>(mRecords.size()); int size = static_cast<int>(mRecords.size());
if (index < 0 || index > size) if (index < 0 || index > size)
throw std::runtime_error ("index out of range"); throw std::runtime_error ("index out of range");
const Record<ESXRecordT>& record2 = dynamic_cast<const Record<ESXRecordT>&> (record); std::unique_ptr<Record<ESXRecordT> > record2(static_cast<Record<ESXRecordT>*>(record.release()));
std::string lowerId = Misc::StringUtils::lowerCase(IdAccessorT().getId(record2->get()));
if (index == size) if (index == size)
mRecords.push_back (record2); mRecords.push_back (std::move(record2));
else else
mRecords.insert (mRecords.begin()+index, record2); mRecords.insert (mRecords.begin()+index, std::move(record2));
if (index < size-1) if (index < size-1)
{ {
@ -463,18 +466,18 @@ namespace CSMWorld
} }
} }
mIndex.insert (std::make_pair (Misc::StringUtils::lowerCase (IdAccessorT().getId ( mIndex.insert (std::make_pair (lowerId, index));
record2.get())), index));
} }
template<typename ESXRecordT, typename IdAccessorT> template<typename ESXRecordT, typename IdAccessorT>
void Collection<ESXRecordT, IdAccessorT>::setRecord (int index, const Record<ESXRecordT>& record) void Collection<ESXRecordT, IdAccessorT>::setRecord (int index,
std::unique_ptr<Record<ESXRecordT> > record)
{ {
if (Misc::StringUtils::lowerCase (IdAccessorT().getId (mRecords.at (index).get()))!= if (Misc::StringUtils::lowerCase (IdAccessorT().getId (mRecords.at (index)->get())) !=
Misc::StringUtils::lowerCase (IdAccessorT().getId (record.get()))) Misc::StringUtils::lowerCase (IdAccessorT().getId (record->get())))
throw std::runtime_error ("attempt to change the ID of a record"); throw std::runtime_error ("attempt to change the ID of a record");
mRecords.at (index) = record; mRecords.at (index) = std::move(record);
} }
template<typename ESXRecordT, typename IdAccessorT> template<typename ESXRecordT, typename IdAccessorT>

View file

@ -3,6 +3,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <memory>
#include "universalid.hpp" #include "universalid.hpp"
#include "columns.hpp" #include "columns.hpp"
@ -64,13 +65,13 @@ namespace CSMWorld
////< Search record with \a id. ////< Search record with \a id.
/// \return index of record (if found) or -1 (not found) /// \return index of record (if found) or -1 (not found)
virtual void replace (int index, const RecordBase& record) = 0; virtual void replace (int index, std::unique_ptr<RecordBase> record) = 0;
///< If the record type does not match, an exception is thrown. ///< If the record type does not match, an exception is thrown.
/// ///
/// \attention \a record must not change the ID. /// \attention \a record must not change the ID.
///< \param type Will be ignored, unless the collection supports multiple record types ///< \param type Will be ignored, unless the collection supports multiple record types
virtual void appendRecord (const RecordBase& record, virtual void appendRecord (std::unique_ptr<RecordBase> record,
UniversalId::Type type = UniversalId::Type_None) = 0; UniversalId::Type type = UniversalId::Type_None) = 0;
///< If the record type does not match, an exception is thrown. ///< If the record type does not match, an exception is thrown.

View file

@ -124,16 +124,15 @@ void CSMWorld::CreateCommand::undo()
} }
CSMWorld::RevertCommand::RevertCommand (IdTable& model, const std::string& id, QUndoCommand* parent) CSMWorld::RevertCommand::RevertCommand (IdTable& model, const std::string& id, QUndoCommand* parent)
: QUndoCommand (parent), mModel (model), mId (id), mOld (0) : QUndoCommand (parent), mModel (model), mId (id)
{ {
setText (("Revert record " + id).c_str()); setText (("Revert record " + id).c_str());
mOld = model.getRecord (id).clone(); mOld = std::move(model.getRecord (id).clone());
} }
CSMWorld::RevertCommand::~RevertCommand() CSMWorld::RevertCommand::~RevertCommand()
{ {
delete mOld;
} }
void CSMWorld::RevertCommand::redo() void CSMWorld::RevertCommand::redo()
@ -155,21 +154,20 @@ void CSMWorld::RevertCommand::redo()
void CSMWorld::RevertCommand::undo() void CSMWorld::RevertCommand::undo()
{ {
mModel.setRecord (mId, *mOld); mModel.setRecord (mId, std::move(mOld));
} }
CSMWorld::DeleteCommand::DeleteCommand (IdTable& model, CSMWorld::DeleteCommand::DeleteCommand (IdTable& model,
const std::string& id, CSMWorld::UniversalId::Type type, QUndoCommand* parent) const std::string& id, CSMWorld::UniversalId::Type type, QUndoCommand* parent)
: QUndoCommand (parent), mModel (model), mId (id), mOld (0), mType(type) : QUndoCommand (parent), mModel (model), mId (id), mType(type)
{ {
setText (("Delete record " + id).c_str()); setText (("Delete record " + id).c_str());
mOld = model.getRecord (id).clone(); mOld = std::move(model.getRecord (id).clone());
} }
CSMWorld::DeleteCommand::~DeleteCommand() CSMWorld::DeleteCommand::~DeleteCommand()
{ {
delete mOld;
} }
void CSMWorld::DeleteCommand::redo() void CSMWorld::DeleteCommand::redo()
@ -191,7 +189,7 @@ void CSMWorld::DeleteCommand::redo()
void CSMWorld::DeleteCommand::undo() void CSMWorld::DeleteCommand::undo()
{ {
mModel.setRecord (mId, *mOld, mType); mModel.setRecord (mId, std::move(mOld), mType);
} }

View file

@ -6,6 +6,7 @@
#include <string> #include <string>
#include <map> #include <map>
#include <vector> #include <vector>
#include <memory>
#include <QVariant> #include <QVariant>
#include <QUndoCommand> #include <QUndoCommand>
@ -97,7 +98,7 @@ namespace CSMWorld
{ {
IdTable& mModel; IdTable& mModel;
std::string mId; std::string mId;
RecordBase *mOld; std::unique_ptr<RecordBase> mOld;
// not implemented // not implemented
RevertCommand (const RevertCommand&); RevertCommand (const RevertCommand&);
@ -118,7 +119,7 @@ namespace CSMWorld
{ {
IdTable& mModel; IdTable& mModel;
std::string mId; std::string mId;
RecordBase *mOld; std::unique_ptr<RecordBase> mOld;
UniversalId::Type mType; UniversalId::Type mType;
// not implemented // not implemented

View file

@ -861,8 +861,8 @@ const CSMWorld::MetaData& CSMWorld::Data::getMetaData() const
void CSMWorld::Data::setMetaData (const MetaData& metaData) void CSMWorld::Data::setMetaData (const MetaData& metaData)
{ {
Record<MetaData> record (RecordBase::State_ModifiedOnly, 0, &metaData); mMetaData.setRecord (0, std::make_unique<Record<MetaData> >(
mMetaData.setRecord (0, record); Record<MetaData>(RecordBase::State_ModifiedOnly, 0, &metaData)));
} }
const CSMWorld::NpcAutoCalc& CSMWorld::Data::getNpcAutoCalc() const const CSMWorld::NpcAutoCalc& CSMWorld::Data::getNpcAutoCalc() const
@ -920,7 +920,8 @@ int CSMWorld::Data::startLoading (const boost::filesystem::path& path, bool base
metaData.mId = "sys::meta"; metaData.mId = "sys::meta";
metaData.load (*mReader); metaData.load (*mReader);
mMetaData.setRecord (0, Record<MetaData> (RecordBase::State_ModifiedOnly, 0, &metaData)); mMetaData.setRecord (0, std::make_unique<Record<MetaData> >(
Record<MetaData> (RecordBase::State_ModifiedOnly, 0, &metaData)));
} }
return mReader->getRecordCount(); return mReader->getRecordCount();

View file

@ -66,9 +66,9 @@ namespace CSMWorld
return -1; return -1;
} }
Record<ESXRecordT> baseRecord = this->getRecord (index); std::unique_ptr<Record<ESXRecordT> > baseRecord(new Record<ESXRecordT>(this->getRecord(index)));
baseRecord.mState = RecordBase::State_Deleted; baseRecord->mState = RecordBase::State_Deleted;
this->setRecord (index, baseRecord); this->setRecord(index, std::move(baseRecord));
return index; return index;
} }
@ -79,30 +79,31 @@ namespace CSMWorld
int IdCollection<ESXRecordT, IdAccessorT>::load (const ESXRecordT& record, bool base, int IdCollection<ESXRecordT, IdAccessorT>::load (const ESXRecordT& record, bool base,
int index) int index)
{ {
if (index==-2) if (index==-2) // index unknown
index = this->searchId (IdAccessorT().getId (record)); index = this->searchId (IdAccessorT().getId (record));
if (index==-1) if (index==-1)
{ {
// new record // new record
Record<ESXRecordT> record2; std::unique_ptr<Record<ESXRecordT> > record2(new Record<ESXRecordT>);
record2.mState = base ? RecordBase::State_BaseOnly : RecordBase::State_ModifiedOnly; record2->mState = base ? RecordBase::State_BaseOnly : RecordBase::State_ModifiedOnly;
(base ? record2.mBase : record2.mModified) = record; (base ? record2->mBase : record2->mModified) = record;
index = this->getSize(); index = this->getSize();
this->appendRecord (record2); this->appendRecord(std::move(record2));
} }
else else
{ {
// old record // old record
Record<ESXRecordT> record2 = Collection<ESXRecordT, IdAccessorT>::getRecord (index); std::unique_ptr<Record<ESXRecordT> > record2(
new Record<ESXRecordT>(Collection<ESXRecordT, IdAccessorT>::getRecord(index)));
if (base) if (base)
record2.mBase = record; record2->mBase = record;
else else
record2.setModified (record); record2->setModified(record);
this->setRecord (index, record2); this->setRecord(index, std::move(record2));
} }
return index; return index;
@ -116,7 +117,7 @@ namespace CSMWorld
if (index==-1) if (index==-1)
return false; return false;
Record<ESXRecordT> record = Collection<ESXRecordT, IdAccessorT>::getRecord (index); const Record<ESXRecordT>& record = Collection<ESXRecordT, IdAccessorT>::getRecord (index);
if (record.isDeleted()) if (record.isDeleted())
return false; return false;
@ -127,8 +128,10 @@ namespace CSMWorld
} }
else else
{ {
record.mState = RecordBase::State_Deleted; std::unique_ptr<Record<ESXRecordT> > record2(
this->setRecord (index, record); new Record<ESXRecordT>(Collection<ESXRecordT, IdAccessorT>::getRecord(index)));
record2->mState = RecordBase::State_Deleted;
this->setRecord(index, std::move(record2));
} }
return true; return true;

View file

@ -166,7 +166,8 @@ QModelIndex CSMWorld::IdTable::getModelIndex (const std::string& id, int column)
return index(mIdCollection->getIndex (id), column); return index(mIdCollection->getIndex (id), column);
} }
void CSMWorld::IdTable::setRecord (const std::string& id, const RecordBase& record, CSMWorld::UniversalId::Type type) void CSMWorld::IdTable::setRecord (const std::string& id,
std::unique_ptr<RecordBase> record, CSMWorld::UniversalId::Type type)
{ {
int index = mIdCollection->searchId (id); int index = mIdCollection->searchId (id);
@ -176,13 +177,13 @@ void CSMWorld::IdTable::setRecord (const std::string& id, const RecordBase& reco
beginInsertRows (QModelIndex(), index2, index2); beginInsertRows (QModelIndex(), index2, index2);
mIdCollection->appendRecord (record, type); mIdCollection->appendRecord (std::move(record), type);
endInsertRows(); endInsertRows();
} }
else else
{ {
mIdCollection->replace (index, record); mIdCollection->replace (index, std::move(record));
emit dataChanged (CSMWorld::IdTable::index (index, 0), emit dataChanged (CSMWorld::IdTable::index (index, 0),
CSMWorld::IdTable::index (index, mIdCollection->getColumns()-1)); CSMWorld::IdTable::index (index, mIdCollection->getColumns()-1));
} }

View file

@ -2,6 +2,7 @@
#define CSM_WOLRD_IDTABLE_H #define CSM_WOLRD_IDTABLE_H
#include <vector> #include <vector>
#include <memory>
#include "idtablebase.hpp" #include "idtablebase.hpp"
#include "universalid.hpp" #include "universalid.hpp"
@ -59,7 +60,7 @@ namespace CSMWorld
virtual QModelIndex getModelIndex (const std::string& id, int column) const; virtual QModelIndex getModelIndex (const std::string& id, int column) const;
void setRecord (const std::string& id, const RecordBase& record, void setRecord (const std::string& id, std::unique_ptr<RecordBase> record,
UniversalId::Type type = UniversalId::Type_None); UniversalId::Type type = UniversalId::Type_None);
///< Add record or overwrite existing recrod. ///< Add record or overwrite existing recrod.

View file

@ -15,25 +15,25 @@ void CSMWorld::InfoCollection::load (const Info& record, bool base)
if (index==-1) if (index==-1)
{ {
// new record // new record
Record<Info> record2; std::unique_ptr<Record<Info> > record2(new Record<Info>);
record2.mState = base ? RecordBase::State_BaseOnly : RecordBase::State_ModifiedOnly; record2->mState = base ? RecordBase::State_BaseOnly : RecordBase::State_ModifiedOnly;
(base ? record2.mBase : record2.mModified) = record; (base ? record2->mBase : record2->mModified) = record;
int index2 = -1; int index2 = -1;
std::string topic = Misc::StringUtils::lowerCase (record2.get().mTopicId); std::string topic = Misc::StringUtils::lowerCase (record2->get().mTopicId);
if (!record2.get().mPrev.empty()) if (!record2->get().mPrev.empty())
{ {
index2 = getInfoIndex (record2.get().mPrev, topic); index2 = getInfoIndex (record2->get().mPrev, topic);
if (index2!=-1) if (index2!=-1)
++index2; ++index2;
} }
if (index2==-1 && !record2.get().mNext.empty()) if (index2==-1 && !record2->get().mNext.empty())
{ {
index2 = getInfoIndex (record2.get().mNext, topic); index2 = getInfoIndex (record2->get().mNext, topic);
} }
if (index2==-1) if (index2==-1)
@ -43,19 +43,19 @@ void CSMWorld::InfoCollection::load (const Info& record, bool base)
index2 = std::distance (getRecords().begin(), range.second); index2 = std::distance (getRecords().begin(), range.second);
} }
insertRecord (record2, index2); insertRecord (std::move(record2), index2);
} }
else else
{ {
// old record // old record
Record<Info> record2 = getRecord (index); std::unique_ptr<Record<Info> > record2(new Record<Info>(getRecord(index)));
if (base) if (base)
record2.mBase = record; record2->mBase = record;
else else
record2.setModified (record); record2->setModified (record);
setRecord (index, record2); setRecord (index, std::move(record2));
} }
} }
@ -66,7 +66,7 @@ int CSMWorld::InfoCollection::getInfoIndex (const std::string& id, const std::st
std::pair<RecordConstIterator, RecordConstIterator> range = getTopicRange (topic); std::pair<RecordConstIterator, RecordConstIterator> range = getTopicRange (topic);
for (; range.first!=range.second; ++range.first) for (; range.first!=range.second; ++range.first)
if (Misc::StringUtils::ciEqual(range.first->get().mId, fullId)) if (Misc::StringUtils::ciEqual((*range.first).get()->get().mId, fullId))
return std::distance (getRecords().begin(), range.first); return std::distance (getRecords().begin(), range.first);
return -1; return -1;
@ -128,9 +128,9 @@ void CSMWorld::InfoCollection::load (ESM::ESMReader& reader, bool base, const ES
} }
else else
{ {
Record<Info> record = getRecord (index); std::unique_ptr<Record<Info> > record(new Record<Info>(getRecord(index)));
record.mState = RecordBase::State_Deleted; record->mState = RecordBase::State_Deleted;
setRecord (index, record); setRecord (index, std::move(record));
} }
} }
else else
@ -171,7 +171,7 @@ CSMWorld::InfoCollection::Range CSMWorld::InfoCollection::getTopicRange (const s
while (begin != getRecords().begin()) while (begin != getRecords().begin())
{ {
if (!Misc::StringUtils::ciEqual(begin->get().mTopicId, topic2)) if (!Misc::StringUtils::ciEqual((*begin)->get().mTopicId, topic2))
{ {
// we've gone one too far, go back // we've gone one too far, go back
++begin; ++begin;
@ -184,7 +184,7 @@ CSMWorld::InfoCollection::Range CSMWorld::InfoCollection::getTopicRange (const s
RecordConstIterator end = begin; RecordConstIterator end = begin;
for (; end!=getRecords().end(); ++end) for (; end!=getRecords().end(); ++end)
if (!Misc::StringUtils::ciEqual(end->get().mTopicId, topic2)) if (!Misc::StringUtils::ciEqual((*end)->get().mTopicId, topic2))
break; break;
return Range (begin, end); return Range (begin, end);
@ -199,7 +199,7 @@ void CSMWorld::InfoCollection::removeDialogueInfos(const std::string& dialogueId
std::map<std::string, int>::const_iterator end = getIdMap().end(); std::map<std::string, int>::const_iterator end = getIdMap().end();
for (; current != end; ++current) for (; current != end; ++current)
{ {
Record<Info> record = getRecord(current->second); const Record<Info>& record = getRecord(current->second);
if (Misc::StringUtils::ciEqual(dialogueId, record.get().mTopicId)) if (Misc::StringUtils::ciEqual(dialogueId, record.get().mTopicId))
{ {
@ -209,8 +209,9 @@ void CSMWorld::InfoCollection::removeDialogueInfos(const std::string& dialogueId
} }
else else
{ {
record.mState = RecordBase::State_Deleted; std::unique_ptr<Record<Info> > record2(new Record<Info>(record));
setRecord(current->second, record); record2->mState = RecordBase::State_Deleted;
setRecord(current->second, std::move(record2));
} }
} }
else else

View file

@ -15,7 +15,7 @@ namespace CSMWorld
{ {
public: public:
typedef std::vector<Record<Info> >::const_iterator RecordConstIterator; typedef std::vector<std::unique_ptr<Record<Info> > >::const_iterator RecordConstIterator;
typedef std::pair<RecordConstIterator, RecordConstIterator> Range; typedef std::pair<RecordConstIterator, RecordConstIterator> Range;
private: private:

View file

@ -90,23 +90,23 @@ namespace CSMWorld
template<typename ESXRecordT, typename IdAccessorT> template<typename ESXRecordT, typename IdAccessorT>
void NestedIdCollection<ESXRecordT, IdAccessorT>::addNestedRow(int row, int column, int position) void NestedIdCollection<ESXRecordT, IdAccessorT>::addNestedRow(int row, int column, int position)
{ {
Record<ESXRecordT> record; std::unique_ptr<Record<ESXRecordT> > record(new Record<ESXRecordT>);
record.assign(Collection<ESXRecordT, IdAccessorT>::getRecord(row)); record->assign(Collection<ESXRecordT, IdAccessorT>::getRecord(row));
getAdapter(Collection<ESXRecordT, IdAccessorT>::getColumn(column)).addRow(record, position); getAdapter(Collection<ESXRecordT, IdAccessorT>::getColumn(column)).addRow(*record, position);
Collection<ESXRecordT, IdAccessorT>::setRecord(row, record); Collection<ESXRecordT, IdAccessorT>::setRecord(row, std::move(record));
} }
template<typename ESXRecordT, typename IdAccessorT> template<typename ESXRecordT, typename IdAccessorT>
void NestedIdCollection<ESXRecordT, IdAccessorT>::removeNestedRows(int row, int column, int subRow) void NestedIdCollection<ESXRecordT, IdAccessorT>::removeNestedRows(int row, int column, int subRow)
{ {
Record<ESXRecordT> record; std::unique_ptr<Record<ESXRecordT> > record(new Record<ESXRecordT>);
record.assign(Collection<ESXRecordT, IdAccessorT>::getRecord(row)); record->assign(Collection<ESXRecordT, IdAccessorT>::getRecord(row));
getAdapter(Collection<ESXRecordT, IdAccessorT>::getColumn(column)).removeRow(record, subRow); getAdapter(Collection<ESXRecordT, IdAccessorT>::getColumn(column)).removeRow(*record, subRow);
Collection<ESXRecordT, IdAccessorT>::setRecord(row, record); Collection<ESXRecordT, IdAccessorT>::setRecord(row, std::move(record));
} }
template<typename ESXRecordT, typename IdAccessorT> template<typename ESXRecordT, typename IdAccessorT>
@ -121,13 +121,13 @@ namespace CSMWorld
void NestedIdCollection<ESXRecordT, IdAccessorT>::setNestedData(int row, void NestedIdCollection<ESXRecordT, IdAccessorT>::setNestedData(int row,
int column, const QVariant& data, int subRow, int subColumn) int column, const QVariant& data, int subRow, int subColumn)
{ {
Record<ESXRecordT> record; std::unique_ptr<Record<ESXRecordT> > record(new Record<ESXRecordT>);
record.assign(Collection<ESXRecordT, IdAccessorT>::getRecord(row)); record->assign(Collection<ESXRecordT, IdAccessorT>::getRecord(row));
getAdapter(Collection<ESXRecordT, IdAccessorT>::getColumn(column)).setData( getAdapter(Collection<ESXRecordT, IdAccessorT>::getColumn(column)).setData(
record, data, subRow, subColumn); *record, data, subRow, subColumn);
Collection<ESXRecordT, IdAccessorT>::setRecord(row, record); Collection<ESXRecordT, IdAccessorT>::setRecord(row, std::move(record));
} }
template<typename ESXRecordT, typename IdAccessorT> template<typename ESXRecordT, typename IdAccessorT>
@ -142,13 +142,13 @@ namespace CSMWorld
void NestedIdCollection<ESXRecordT, IdAccessorT>::setNestedTable(int row, void NestedIdCollection<ESXRecordT, IdAccessorT>::setNestedTable(int row,
int column, const CSMWorld::NestedTableWrapperBase& nestedTable) int column, const CSMWorld::NestedTableWrapperBase& nestedTable)
{ {
Record<ESXRecordT> record; std::unique_ptr<Record<ESXRecordT> > record(new Record<ESXRecordT>);
record.assign(Collection<ESXRecordT, IdAccessorT>::getRecord(row)); record->assign(Collection<ESXRecordT, IdAccessorT>::getRecord(row));
getAdapter(Collection<ESXRecordT, IdAccessorT>::getColumn(column)).setTable( getAdapter(Collection<ESXRecordT, IdAccessorT>::getColumn(column)).setTable(
record, nestedTable); *record, nestedTable);
Collection<ESXRecordT, IdAccessorT>::setRecord(row, record); Collection<ESXRecordT, IdAccessorT>::setRecord(row, std::move(record));
} }
template<typename ESXRecordT, typename IdAccessorT> template<typename ESXRecordT, typename IdAccessorT>

View file

@ -35,22 +35,22 @@ namespace CSMWorld
void NestedInfoCollection::addNestedRow(int row, int column, int position) void NestedInfoCollection::addNestedRow(int row, int column, int position)
{ {
Record<Info> record; std::unique_ptr<Record<Info> > record(new Record<Info>);
record.assign(Collection<Info, IdAccessor<Info> >::getRecord(row)); record->assign(Collection<Info, IdAccessor<Info> >::getRecord(row));
getAdapter(Collection<Info, IdAccessor<Info> >::getColumn(column)).addRow(record, position); getAdapter(Collection<Info, IdAccessor<Info> >::getColumn(column)).addRow(*record, position);
Collection<Info, IdAccessor<Info> >::setRecord(row, record); Collection<Info, IdAccessor<Info> >::setRecord(row, std::move(record));
} }
void NestedInfoCollection::removeNestedRows(int row, int column, int subRow) void NestedInfoCollection::removeNestedRows(int row, int column, int subRow)
{ {
Record<Info> record; std::unique_ptr<Record<Info> > record(new Record<Info>);
record.assign(Collection<Info, IdAccessor<Info> >::getRecord(row)); record->assign(Collection<Info, IdAccessor<Info> >::getRecord(row));
getAdapter(Collection<Info, IdAccessor<Info> >::getColumn(column)).removeRow(record, subRow); getAdapter(Collection<Info, IdAccessor<Info> >::getColumn(column)).removeRow(*record, subRow);
Collection<Info, IdAccessor<Info> >::setRecord(row, record); Collection<Info, IdAccessor<Info> >::setRecord(row, std::move(record));
} }
QVariant NestedInfoCollection::getNestedData (int row, QVariant NestedInfoCollection::getNestedData (int row,
@ -63,13 +63,13 @@ namespace CSMWorld
void NestedInfoCollection::setNestedData(int row, void NestedInfoCollection::setNestedData(int row,
int column, const QVariant& data, int subRow, int subColumn) int column, const QVariant& data, int subRow, int subColumn)
{ {
Record<Info> record; std::unique_ptr<Record<Info> > record(new Record<Info>);
record.assign(Collection<Info, IdAccessor<Info> >::getRecord(row)); record->assign(Collection<Info, IdAccessor<Info> >::getRecord(row));
getAdapter(Collection<Info, IdAccessor<Info> >::getColumn(column)).setData( getAdapter(Collection<Info, IdAccessor<Info> >::getColumn(column)).setData(
record, data, subRow, subColumn); *record, data, subRow, subColumn);
Collection<Info, IdAccessor<Info> >::setRecord(row, record); Collection<Info, IdAccessor<Info> >::setRecord(row, std::move(record));
} }
CSMWorld::NestedTableWrapperBase* NestedInfoCollection::nestedTable(int row, CSMWorld::NestedTableWrapperBase* NestedInfoCollection::nestedTable(int row,
@ -82,13 +82,13 @@ namespace CSMWorld
void NestedInfoCollection::setNestedTable(int row, void NestedInfoCollection::setNestedTable(int row,
int column, const CSMWorld::NestedTableWrapperBase& nestedTable) int column, const CSMWorld::NestedTableWrapperBase& nestedTable)
{ {
Record<Info> record; std::unique_ptr<Record<Info> > record(new Record<Info>);
record.assign(Collection<Info, IdAccessor<Info> >::getRecord(row)); record->assign(Collection<Info, IdAccessor<Info> >::getRecord(row));
getAdapter(Collection<Info, IdAccessor<Info> >::getColumn(column)).setTable( getAdapter(Collection<Info, IdAccessor<Info> >::getColumn(column)).setTable(
record, nestedTable); *record, nestedTable);
Collection<Info, IdAccessor<Info> >::setRecord(row, record); Collection<Info, IdAccessor<Info> >::setRecord(row, std::move(record));
} }
int NestedInfoCollection::getNestedRowsCount(int row, int column) const int NestedInfoCollection::getNestedRowsCount(int row, int column) const

View file

@ -1,6 +1,7 @@
#ifndef CSM_WOLRD_RECORD_H #ifndef CSM_WOLRD_RECORD_H
#define CSM_WOLRD_RECORD_H #define CSM_WOLRD_RECORD_H
#include <memory>
#include <stdexcept> #include <stdexcept>
namespace CSMWorld namespace CSMWorld
@ -20,9 +21,9 @@ namespace CSMWorld
virtual ~RecordBase(); virtual ~RecordBase();
virtual RecordBase *clone() const = 0; virtual std::unique_ptr<RecordBase> clone() const = 0;
virtual RecordBase *modifiedCopy() const = 0; virtual std::unique_ptr<RecordBase> modifiedCopy() const = 0;
virtual void assign (const RecordBase& record) = 0; virtual void assign (const RecordBase& record) = 0;
///< Will throw an exception if the types don't match. ///< Will throw an exception if the types don't match.
@ -45,9 +46,9 @@ namespace CSMWorld
Record(State state, Record(State state,
const ESXRecordT *base = 0, const ESXRecordT *modified = 0); const ESXRecordT *base = 0, const ESXRecordT *modified = 0);
virtual RecordBase *clone() const; virtual std::unique_ptr<RecordBase> clone() const;
virtual RecordBase *modifiedCopy() const; virtual std::unique_ptr<RecordBase> modifiedCopy() const;
virtual void assign (const RecordBase& record); virtual void assign (const RecordBase& record);
@ -85,15 +86,16 @@ namespace CSMWorld
} }
template <typename ESXRecordT> template <typename ESXRecordT>
RecordBase *Record<ESXRecordT>::modifiedCopy() const std::unique_ptr<RecordBase> Record<ESXRecordT>::modifiedCopy() const
{ {
return new Record<ESXRecordT> (State_ModifiedOnly, 0, &(this->get())); return std::make_unique<Record<ESXRecordT> >(
Record<ESXRecordT>(State_ModifiedOnly, 0, &(this->get())));
} }
template <typename ESXRecordT> template <typename ESXRecordT>
RecordBase *Record<ESXRecordT>::clone() const std::unique_ptr<RecordBase> Record<ESXRecordT>::clone() const
{ {
return new Record<ESXRecordT> (*this); return std::make_unique<Record<ESXRecordT> >(Record<ESXRecordT>(*this));
} }
template <typename ESXRecordT> template <typename ESXRecordT>

View file

@ -92,8 +92,6 @@ void CSMWorld::RefCollection::load (ESM::ESMReader& reader, int cellIndex, bool
int index = getIndex (iter->second); int index = getIndex (iter->second);
Record<CellRef> record = getRecord (index);
if (base) if (base)
{ {
removeRows (index, 1); removeRows (index, 1);
@ -101,8 +99,9 @@ void CSMWorld::RefCollection::load (ESM::ESMReader& reader, int cellIndex, bool
} }
else else
{ {
record.mState = RecordBase::State_Deleted; std::unique_ptr<Record<CellRef> > record2(new Record<CellRef>(getRecord(index)));
setRecord (index, record); record2->mState = RecordBase::State_Deleted;
setRecord(index, std::move(record2));
} }
continue; continue;
@ -113,11 +112,11 @@ void CSMWorld::RefCollection::load (ESM::ESMReader& reader, int cellIndex, bool
// new reference // new reference
ref.mId = getNewId(); ref.mId = getNewId();
Record<CellRef> record; std::unique_ptr<Record<CellRef> > record(new Record<CellRef>);
record.mState = base ? RecordBase::State_BaseOnly : RecordBase::State_ModifiedOnly; record->mState = base ? RecordBase::State_BaseOnly : RecordBase::State_ModifiedOnly;
(base ? record.mBase : record.mModified) = ref; (base ? record->mBase : record->mModified) = ref;
appendRecord (record); appendRecord(std::move(record));
cache.insert (std::make_pair (ref.mRefNum, ref.mId)); cache.insert (std::make_pair (ref.mRefNum, ref.mId));
} }
@ -128,11 +127,11 @@ void CSMWorld::RefCollection::load (ESM::ESMReader& reader, int cellIndex, bool
int index = getIndex (ref.mId); int index = getIndex (ref.mId);
Record<CellRef> record = getRecord (index); std::unique_ptr<Record<CellRef> > record(new Record<CellRef>(getRecord(index)));
record.mState = base ? RecordBase::State_BaseOnly : RecordBase::State_Modified; record->mState = base ? RecordBase::State_BaseOnly : RecordBase::State_Modified;
(base ? record.mBase : record.mModified) = ref; (base ? record->mBase : record->mModified) = ref;
setRecord (index, record); setRecord(index, std::move(record));
} }
} }
} }

View file

@ -810,30 +810,31 @@ int CSMWorld::RefIdCollection::searchId (const std::string& id) const
return mData.localToGlobalIndex (localIndex); return mData.localToGlobalIndex (localIndex);
} }
void CSMWorld::RefIdCollection::replace (int index, const RecordBase& record) void CSMWorld::RefIdCollection::replace (int index, std::unique_ptr<RecordBase> record)
{ {
mData.getRecord (mData.globalToLocalIndex (index)).assign (record); mData.getRecord (mData.globalToLocalIndex (index)).assign (*record.release());
} }
void CSMWorld::RefIdCollection::cloneRecord(const std::string& origin, void CSMWorld::RefIdCollection::cloneRecord(const std::string& origin,
const std::string& destination, const std::string& destination,
const CSMWorld::UniversalId::Type type) const CSMWorld::UniversalId::Type type)
{ {
std::auto_ptr<RecordBase> newRecord(mData.getRecord(mData.searchId(origin)).modifiedCopy()); std::unique_ptr<RecordBase> newRecord =
std::move(mData.getRecord(mData.searchId(origin)).modifiedCopy());
mAdapters.find(type)->second->setId(*newRecord, destination); mAdapters.find(type)->second->setId(*newRecord, destination);
mData.insertRecord(*newRecord, type, destination); mData.insertRecord(std::move(newRecord), type, destination);
} }
void CSMWorld::RefIdCollection::appendRecord (const RecordBase& record, void CSMWorld::RefIdCollection::appendRecord (std::unique_ptr<RecordBase> record,
UniversalId::Type type) UniversalId::Type type)
{ {
std::string id = findAdapter (type).getId (record); std::string id = findAdapter (type).getId (*record.get());
int index = mData.getAppendIndex (type); int index = mData.getAppendIndex (type);
mData.appendRecord (type, id, false); mData.appendRecord (type, id, false);
mData.getRecord (mData.globalToLocalIndex (index)).assign (record); mData.getRecord (mData.globalToLocalIndex (index)).assign (*record.release());
} }
const CSMWorld::RecordBase& CSMWorld::RefIdCollection::getRecord (const std::string& id) const const CSMWorld::RecordBase& CSMWorld::RefIdCollection::getRecord (const std::string& id) const

View file

@ -89,12 +89,12 @@ namespace CSMWorld
////< Search record with \a id. ////< Search record with \a id.
/// \return index of record (if found) or -1 (not found) /// \return index of record (if found) or -1 (not found)
virtual void replace (int index, const RecordBase& record); virtual void replace (int index, std::unique_ptr<RecordBase> record);
///< If the record type does not match, an exception is thrown. ///< If the record type does not match, an exception is thrown.
/// ///
/// \attention \a record must not change the ID. /// \attention \a record must not change the ID.
virtual void appendRecord (const RecordBase& record, UniversalId::Type type); virtual void appendRecord (std::unique_ptr<RecordBase> record, UniversalId::Type type);
///< If the record type does not match, an exception is thrown. ///< If the record type does not match, an exception is thrown.
/// ///
///< \param type Will be ignored, unless the collection supports multiple record types ///< \param type Will be ignored, unless the collection supports multiple record types

View file

@ -367,7 +367,7 @@ const CSMWorld::RefIdDataContainer< ESM::Static >& CSMWorld::RefIdData::getStati
return mStatics; return mStatics;
} }
void CSMWorld::RefIdData::insertRecord (CSMWorld::RecordBase& record, CSMWorld::UniversalId::Type type, const std::string& id) void CSMWorld::RefIdData::insertRecord (std::unique_ptr<CSMWorld::RecordBase> record, CSMWorld::UniversalId::Type type, const std::string& id)
{ {
std::map<UniversalId::Type, RefIdDataContainerBase *>::iterator iter = std::map<UniversalId::Type, RefIdDataContainerBase *>::iterator iter =
mRecordContainers.find (type); mRecordContainers.find (type);
@ -375,7 +375,7 @@ void CSMWorld::RefIdData::insertRecord (CSMWorld::RecordBase& record, CSMWorld::
if (iter==mRecordContainers.end()) if (iter==mRecordContainers.end())
throw std::logic_error ("invalid local index type"); throw std::logic_error ("invalid local index type");
iter->second->insertRecord(record); iter->second->insertRecord(std::move(record));
mIndex.insert (std::make_pair (Misc::StringUtils::lowerCase (id), mIndex.insert (std::make_pair (Misc::StringUtils::lowerCase (id),
LocalIndex (iter->second->getSize()-1, type))); LocalIndex (iter->second->getSize()-1, type)));
@ -387,9 +387,7 @@ void CSMWorld::RefIdData::copyTo (int index, RefIdData& target) const
RefIdDataContainerBase *source = mRecordContainers.find (localIndex.second)->second; RefIdDataContainerBase *source = mRecordContainers.find (localIndex.second)->second;
std::string id = source->getId (localIndex.first); target.insertRecord(source->getRecord(localIndex.first).modifiedCopy(),
localIndex.second,
std::auto_ptr<CSMWorld::RecordBase> newRecord (source->getRecord (localIndex.first).modifiedCopy()); source->getId(localIndex.first));
target.insertRecord (*newRecord, localIndex.second, id);
} }

View file

@ -3,6 +3,7 @@
#include <vector> #include <vector>
#include <map> #include <map>
#include <memory>
#include <components/esm/loadacti.hpp> #include <components/esm/loadacti.hpp>
#include <components/esm/loadalch.hpp> #include <components/esm/loadalch.hpp>
@ -49,7 +50,7 @@ namespace CSMWorld
virtual void appendRecord (const std::string& id, bool base) = 0; virtual void appendRecord (const std::string& id, bool base) = 0;
virtual void insertRecord (RecordBase& record) = 0; virtual void insertRecord (std::unique_ptr<RecordBase> record) = 0;
virtual int load (ESM::ESMReader& reader, bool base) = 0; virtual int load (ESM::ESMReader& reader, bool base) = 0;
///< \return index of a loaded record or -1 if no record was loaded ///< \return index of a loaded record or -1 if no record was loaded
@ -64,7 +65,7 @@ namespace CSMWorld
template<typename RecordT> template<typename RecordT>
struct RefIdDataContainer : public RefIdDataContainerBase struct RefIdDataContainer : public RefIdDataContainerBase
{ {
std::vector<Record<RecordT> > mContainer; std::vector<std::unique_ptr<Record<RecordT> > > mContainer;
virtual int getSize() const; virtual int getSize() const;
@ -74,7 +75,7 @@ namespace CSMWorld
virtual void appendRecord (const std::string& id, bool base); virtual void appendRecord (const std::string& id, bool base);
virtual void insertRecord (RecordBase& record); virtual void insertRecord (std::unique_ptr<RecordBase> record);
virtual int load (ESM::ESMReader& reader, bool base); virtual int load (ESM::ESMReader& reader, bool base);
///< \return index of a loaded record or -1 if no record was loaded ///< \return index of a loaded record or -1 if no record was loaded
@ -87,10 +88,18 @@ namespace CSMWorld
}; };
template<typename RecordT> template<typename RecordT>
void RefIdDataContainer<RecordT>::insertRecord(RecordBase& record) void RefIdDataContainer<RecordT>::insertRecord(std::unique_ptr<RecordBase> record)
{ {
Record<RecordT>& newRecord = dynamic_cast<Record<RecordT>& >(record); Record<RecordT> *tmp = dynamic_cast<Record<RecordT>*>(record.get());
mContainer.push_back(newRecord); if(tmp != nullptr)
{
record.release();
std::unique_ptr<Record<RecordT> > newRecord;
newRecord.reset(tmp);
mContainer.push_back(std::move(newRecord));
}
else
throw std::runtime_error ("invalid record for RefIdDataContainer");
} }
template<typename RecordT> template<typename RecordT>
@ -102,27 +111,27 @@ namespace CSMWorld
template<typename RecordT> template<typename RecordT>
const RecordBase& RefIdDataContainer<RecordT>::getRecord (int index) const const RecordBase& RefIdDataContainer<RecordT>::getRecord (int index) const
{ {
return mContainer.at (index); return *mContainer.at (index);
} }
template<typename RecordT> template<typename RecordT>
RecordBase& RefIdDataContainer<RecordT>::getRecord (int index) RecordBase& RefIdDataContainer<RecordT>::getRecord (int index)
{ {
return mContainer.at (index); return *mContainer.at (index);
} }
template<typename RecordT> template<typename RecordT>
void RefIdDataContainer<RecordT>::appendRecord (const std::string& id, bool base) void RefIdDataContainer<RecordT>::appendRecord (const std::string& id, bool base)
{ {
Record<RecordT> record; std::unique_ptr<Record<RecordT> > record(new Record<RecordT>);
record.mState = base ? RecordBase::State_BaseOnly : RecordBase::State_ModifiedOnly; record->mState = base ? RecordBase::State_BaseOnly : RecordBase::State_ModifiedOnly;
record.mBase.mId = id; record->mBase.mId = id;
record.mModified.mId = id; record->mModified.mId = id;
(base ? record.mBase : record.mModified).blank(); (base ? record->mBase : record->mModified).blank();
mContainer.push_back (record); mContainer.push_back (std::move(record));
} }
template<typename RecordT> template<typename RecordT>
@ -137,7 +146,7 @@ namespace CSMWorld
int numRecords = static_cast<int>(mContainer.size()); int numRecords = static_cast<int>(mContainer.size());
for (; index < numRecords; ++index) for (; index < numRecords; ++index)
{ {
if (Misc::StringUtils::ciEqual(mContainer[index].get().mId, record.mId)) if (Misc::StringUtils::ciEqual(mContainer[index]->get().mId, record.mId))
{ {
break; break;
} }
@ -155,7 +164,7 @@ namespace CSMWorld
// Flag the record as Deleted even for a base content file. // Flag the record as Deleted even for a base content file.
// RefIdData is responsible for its erasure. // RefIdData is responsible for its erasure.
mContainer[index].mState = RecordBase::State_Deleted; mContainer[index]->mState = RecordBase::State_Deleted;
} }
else else
{ {
@ -164,16 +173,16 @@ namespace CSMWorld
appendRecord(record.mId, base); appendRecord(record.mId, base);
if (base) if (base)
{ {
mContainer.back().mBase = record; mContainer.back()->mBase = record;
} }
else else
{ {
mContainer.back().mModified = record; mContainer.back()->mModified = record;
} }
} }
else if (!base) else if (!base)
{ {
mContainer[index].setModified(record); mContainer[index]->setModified(record);
} }
} }
@ -192,13 +201,13 @@ namespace CSMWorld
template<typename RecordT> template<typename RecordT>
std::string RefIdDataContainer<RecordT>::getId (int index) const std::string RefIdDataContainer<RecordT>::getId (int index) const
{ {
return mContainer.at (index).get().mId; return mContainer.at (index)->get().mId;
} }
template<typename RecordT> template<typename RecordT>
void RefIdDataContainer<RecordT>::save (int index, ESM::ESMWriter& writer) const void RefIdDataContainer<RecordT>::save (int index, ESM::ESMWriter& writer) const
{ {
Record<RecordT> record = mContainer.at(index); const Record<RecordT>& record = *mContainer.at(index);
if (record.isModified() || record.mState == RecordBase::State_Deleted) if (record.isModified() || record.mState == RecordBase::State_Deleted)
{ {
@ -260,7 +269,7 @@ namespace CSMWorld
void erase (int index, int count); void erase (int index, int count);
void insertRecord (CSMWorld::RecordBase& record, CSMWorld::UniversalId::Type type, void insertRecord (std::unique_ptr<RecordBase> record, CSMWorld::UniversalId::Type type,
const std::string& id); const std::string& id);
const RecordBase& getRecord (const LocalIndex& index) const; const RecordBase& getRecord (const LocalIndex& index) const;