From 19af94b73eb4d8974f16f48fbda7ed87d561508c Mon Sep 17 00:00:00 2001 From: cc9cii Date: Sun, 6 Dec 2015 15:20:45 +1100 Subject: [PATCH] Reduce copying further by adding move constructors and move assignment operators to CellRef structs. --- apps/opencs/model/world/ref.cpp | 22 ++++++++ apps/opencs/model/world/ref.hpp | 5 ++ apps/opencs/model/world/refcollection.cpp | 4 +- components/esm/cellref.cpp | 61 +++++++++++++++++++++++ components/esm/cellref.hpp | 9 ++++ 5 files changed, 99 insertions(+), 2 deletions(-) diff --git a/apps/opencs/model/world/ref.cpp b/apps/opencs/model/world/ref.cpp index 638f7ec9c..8c6f1d836 100644 --- a/apps/opencs/model/world/ref.cpp +++ b/apps/opencs/model/world/ref.cpp @@ -4,10 +4,32 @@ CSMWorld::CellRef::CellRef() { + mId.clear(); + mCell.clear(); + mOriginalCell.clear(); + mRefNum.mIndex = 0; mRefNum.mContentFile = 0; } +CSMWorld::CellRef::CellRef (CSMWorld::CellRef&& other) : ESM::CellRef (other) +{ + *this = std::move(other); +} + +CSMWorld::CellRef& CSMWorld::CellRef::operator= (CSMWorld::CellRef&& other) +{ + if (this != &other) + { + ESM::CellRef::operator= (other); + mId = std::move(other.mId); + mCell = std::move(other.mCell); + mOriginalCell = std::move(other.mOriginalCell); + } + + return *this; +} + std::pair CSMWorld::CellRef::getCellIndex() const { const int cellSize = 8192; diff --git a/apps/opencs/model/world/ref.hpp b/apps/opencs/model/world/ref.hpp index c60392221..c439e7ca5 100644 --- a/apps/opencs/model/world/ref.hpp +++ b/apps/opencs/model/world/ref.hpp @@ -15,6 +15,11 @@ namespace CSMWorld std::string mOriginalCell; CellRef(); + CellRef(const CellRef&) = default; + CellRef& operator= (const CellRef&) = default; + + CellRef (CellRef&& other); + CellRef& operator= (CellRef&& other); /// Calculate cell index based on coordinates (x and y) std::pair getCellIndex() const; diff --git a/apps/opencs/model/world/refcollection.cpp b/apps/opencs/model/world/refcollection.cpp index 2568a5120..117aeb012 100644 --- a/apps/opencs/model/world/refcollection.cpp +++ b/apps/opencs/model/world/refcollection.cpp @@ -114,7 +114,7 @@ void CSMWorld::RefCollection::load (ESM::ESMReader& reader, int cellIndex, bool std::unique_ptr > record(new Record); record->mState = base ? RecordBase::State_BaseOnly : RecordBase::State_ModifiedOnly; - (base ? record->mBase : record->mModified) = ref; + (base ? record->mBase : record->mModified) = std::move(ref); appendRecord(std::move(record)); @@ -129,7 +129,7 @@ void CSMWorld::RefCollection::load (ESM::ESMReader& reader, int cellIndex, bool std::unique_ptr > record(new Record(getRecord(index))); record->mState = base ? RecordBase::State_BaseOnly : RecordBase::State_Modified; - (base ? record->mBase : record->mModified) = ref; + (base ? record->mBase : record->mModified) = std::move(ref); setRecord(index, std::move(record)); } diff --git a/components/esm/cellref.cpp b/components/esm/cellref.cpp index 76a82fe23..b3fb410e3 100644 --- a/components/esm/cellref.cpp +++ b/components/esm/cellref.cpp @@ -206,6 +206,67 @@ void ESM::CellRef::blank() } } +ESM::CellRef::CellRef () +{ + blank(); +} + +ESM::CellRef::CellRef (const CellRef& other) + : mRefNum(other.mRefNum) + , mRefID(other.mRefID) + , mScale(other.mScale) + , mOwner(other.mOwner) + , mGlobalVariable(other.mGlobalVariable) + , mSoul(other.mSoul) + , mFaction(other.mFaction) + , mFactionRank(other.mFactionRank) + , mChargeInt(other.mChargeInt) + , mEnchantmentCharge(other.mEnchantmentCharge) + , mGoldValue(other.mGoldValue) + , mTeleport(other.mTeleport) + , mDoorDest(other.mDoorDest) + , mDestCell(other.mDestCell) + , mLockLevel(other.mLockLevel) + , mKey(other.mKey) + , mTrap(other.mTrap) + , mReferenceBlocked(other.mReferenceBlocked) + , mPos(other.mPos) +{ +} + +ESM::CellRef::CellRef (CellRef&& other) +{ + *this = std::move(other); +} + +ESM::CellRef& ESM::CellRef::operator= (CellRef&& other) +{ + if (this != &other) + { + mRefNum = other.mRefNum; // RefNum + mRefID = std::move(other.mRefID); + mScale = other.mScale; + mOwner = std::move(other.mOwner); + mGlobalVariable = std::move(other.mGlobalVariable); + mSoul = std::move(other.mSoul); + mFaction = std::move(other.mFaction); + mFactionRank = other.mFactionRank; + mChargeInt = other.mChargeInt; + mEnchantmentCharge = other.mEnchantmentCharge; + mGoldValue = other.mGoldValue; + mTeleport = other.mTeleport; + mDoorDest = other.mDoorDest; // Position + mDestCell = std::move(other.mDestCell); + mLockLevel = other.mLockLevel; + mKey = std::move(other.mKey); + mTrap = std::move(other.mTrap); + mReferenceBlocked = other.mReferenceBlocked; + mPos = other.mPos; // Position + } + + return *this; +} + bool ESM::operator== (const RefNum& left, const RefNum& right) { return left.mIndex==right.mIndex && left.mContentFile==right.mContentFile; diff --git a/components/esm/cellref.hpp b/components/esm/cellref.hpp index c371a4f01..f84386b3a 100644 --- a/components/esm/cellref.hpp +++ b/components/esm/cellref.hpp @@ -109,6 +109,15 @@ namespace ESM void save (ESMWriter &esm, bool wideRefNum = false, bool inInventory = false, bool isDeleted = false) const; void blank(); + + CellRef(); + ~CellRef() = default; + + CellRef(const CellRef&); + CellRef& operator=(const CellRef&) = default; + + CellRef (CellRef&& other); + CellRef& operator=(CellRef&& other); }; bool operator== (const RefNum& left, const RefNum& right);