mirror of
https://gitlab.com/OpenMW/openmw.git
synced 2025-02-04 21:40:03 +00:00
templatise ptr.hpp (#3185)
With this PR we consolidate identical logic for Ptr and ConstPtr with the help of a template.
This commit is contained in:
parent
41318a585f
commit
6fb376f1cf
@ -73,3 +73,8 @@ bool MWWorld::LiveCellRefBase::checkStateImp (const ESM::ObjectState& state)
|
|||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsigned int MWWorld::LiveCellRefBase::getType() const
|
||||||
|
{
|
||||||
|
return mClass->getType();
|
||||||
|
}
|
||||||
|
@ -43,6 +43,9 @@ namespace MWWorld
|
|||||||
|
|
||||||
virtual std::string_view getTypeDescription() const = 0;
|
virtual std::string_view getTypeDescription() const = 0;
|
||||||
|
|
||||||
|
unsigned int getType() const;
|
||||||
|
///< @see MWWorld::Class::getType
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
void loadImp (const ESM::ObjectState& state);
|
void loadImp (const ESM::ObjectState& state);
|
||||||
|
@ -1,89 +0,0 @@
|
|||||||
#include "ptr.hpp"
|
|
||||||
|
|
||||||
#include <cassert>
|
|
||||||
|
|
||||||
#include "containerstore.hpp"
|
|
||||||
#include "class.hpp"
|
|
||||||
#include "livecellref.hpp"
|
|
||||||
|
|
||||||
unsigned int MWWorld::Ptr::getType() const
|
|
||||||
{
|
|
||||||
if(mRef != nullptr)
|
|
||||||
return mRef->mClass->getType();
|
|
||||||
throw std::runtime_error("Can't get type name from an empty object.");
|
|
||||||
}
|
|
||||||
|
|
||||||
MWWorld::LiveCellRefBase *MWWorld::Ptr::getBase() const
|
|
||||||
{
|
|
||||||
if (!mRef)
|
|
||||||
throw std::runtime_error ("Can't access cell ref pointed to by null Ptr");
|
|
||||||
|
|
||||||
return mRef;
|
|
||||||
}
|
|
||||||
|
|
||||||
MWWorld::CellRef& MWWorld::Ptr::getCellRef() const
|
|
||||||
{
|
|
||||||
assert(mRef);
|
|
||||||
|
|
||||||
return mRef->mRef;
|
|
||||||
}
|
|
||||||
|
|
||||||
MWWorld::RefData& MWWorld::Ptr::getRefData() const
|
|
||||||
{
|
|
||||||
assert(mRef);
|
|
||||||
|
|
||||||
return mRef->mData;
|
|
||||||
}
|
|
||||||
|
|
||||||
void MWWorld::Ptr::setContainerStore (ContainerStore *store)
|
|
||||||
{
|
|
||||||
assert (store);
|
|
||||||
assert (!mCell);
|
|
||||||
|
|
||||||
mContainerStore = store;
|
|
||||||
}
|
|
||||||
|
|
||||||
MWWorld::ContainerStore *MWWorld::Ptr::getContainerStore() const
|
|
||||||
{
|
|
||||||
return mContainerStore;
|
|
||||||
}
|
|
||||||
|
|
||||||
MWWorld::Ptr::operator const void *()
|
|
||||||
{
|
|
||||||
return mRef;
|
|
||||||
}
|
|
||||||
|
|
||||||
// -------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
unsigned int MWWorld::ConstPtr::getType() const
|
|
||||||
{
|
|
||||||
if(mRef != nullptr)
|
|
||||||
return mRef->mClass->getType();
|
|
||||||
throw std::runtime_error("Can't get type name from an empty object.");
|
|
||||||
}
|
|
||||||
|
|
||||||
const MWWorld::LiveCellRefBase *MWWorld::ConstPtr::getBase() const
|
|
||||||
{
|
|
||||||
if (!mRef)
|
|
||||||
throw std::runtime_error ("Can't access cell ref pointed to by null Ptr");
|
|
||||||
|
|
||||||
return mRef;
|
|
||||||
}
|
|
||||||
|
|
||||||
void MWWorld::ConstPtr::setContainerStore (const ContainerStore *store)
|
|
||||||
{
|
|
||||||
assert (store);
|
|
||||||
assert (!mCell);
|
|
||||||
|
|
||||||
mContainerStore = store;
|
|
||||||
}
|
|
||||||
|
|
||||||
const MWWorld::ContainerStore *MWWorld::ConstPtr::getContainerStore() const
|
|
||||||
{
|
|
||||||
return mContainerStore;
|
|
||||||
}
|
|
||||||
|
|
||||||
MWWorld::ConstPtr::operator const void *()
|
|
||||||
{
|
|
||||||
return mRef;
|
|
||||||
}
|
|
@ -2,7 +2,7 @@
|
|||||||
#define GAME_MWWORLD_PTR_H
|
#define GAME_MWWORLD_PTR_H
|
||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
#include <type_traits>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
@ -15,20 +15,19 @@ namespace MWWorld
|
|||||||
struct LiveCellRefBase;
|
struct LiveCellRefBase;
|
||||||
|
|
||||||
/// \brief Pointer to a LiveCellRef
|
/// \brief Pointer to a LiveCellRef
|
||||||
|
/// @note PtrBase is never used directly and needed only to define Ptr and ConstPtr
|
||||||
class Ptr
|
template <template<class> class TypeTransform>
|
||||||
|
class PtrBase
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
MWWorld::LiveCellRefBase *mRef;
|
typedef TypeTransform<MWWorld::LiveCellRefBase> LiveCellRefBaseType;
|
||||||
CellStore *mCell;
|
typedef TypeTransform<CellStore> CellStoreType;
|
||||||
ContainerStore *mContainerStore;
|
typedef TypeTransform<ContainerStore> ContainerStoreType;
|
||||||
|
|
||||||
public:
|
LiveCellRefBaseType *mRef;
|
||||||
Ptr(MWWorld::LiveCellRefBase *liveCellRef=nullptr, CellStore *cell=nullptr)
|
CellStoreType *mCell;
|
||||||
: mRef(liveCellRef), mCell(cell), mContainerStore(nullptr)
|
ContainerStoreType *mContainerStore;
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
bool isEmpty() const
|
bool isEmpty() const
|
||||||
{
|
{
|
||||||
@ -40,7 +39,12 @@ namespace MWWorld
|
|||||||
// Note 1: ids are not sequential. E.g. for a creature `getType` returns 0x41455243.
|
// Note 1: ids are not sequential. E.g. for a creature `getType` returns 0x41455243.
|
||||||
// Note 2: Life is not easy and full of surprises. For example
|
// Note 2: Life is not easy and full of surprises. For example
|
||||||
// prison marker reuses ESM::Door record. Player is ESM::NPC.
|
// prison marker reuses ESM::Door record. Player is ESM::NPC.
|
||||||
unsigned int getType() const;
|
unsigned int getType() const
|
||||||
|
{
|
||||||
|
if(mRef != nullptr)
|
||||||
|
return mRef->getType();
|
||||||
|
throw std::runtime_error("Can't get type name from an empty object.");
|
||||||
|
}
|
||||||
|
|
||||||
std::string_view getTypeDescription() const
|
std::string_view getTypeDescription() const
|
||||||
{
|
{
|
||||||
@ -55,9 +59,9 @@ namespace MWWorld
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
MWWorld::LiveCellRef<T> *get() const
|
TypeTransform<MWWorld::LiveCellRef<T>> *get() const
|
||||||
{
|
{
|
||||||
MWWorld::LiveCellRef<T> *ref = dynamic_cast<MWWorld::LiveCellRef<T>*>(mRef);
|
TypeTransform<MWWorld::LiveCellRef<T>> *ref = dynamic_cast<TypeTransform<MWWorld::LiveCellRef<T>>*>(mRef);
|
||||||
if(ref) return ref;
|
if(ref) return ref;
|
||||||
|
|
||||||
std::stringstream str;
|
std::stringstream str;
|
||||||
@ -68,102 +72,26 @@ namespace MWWorld
|
|||||||
throw std::runtime_error(str.str());
|
throw std::runtime_error(str.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
MWWorld::LiveCellRefBase *getBase() const;
|
LiveCellRefBaseType *getBase() const
|
||||||
|
|
||||||
MWWorld::CellRef& getCellRef() const;
|
|
||||||
|
|
||||||
RefData& getRefData() const;
|
|
||||||
|
|
||||||
CellStore *getCell() const
|
|
||||||
{
|
{
|
||||||
assert(mCell);
|
if (!mRef)
|
||||||
return mCell;
|
throw std::runtime_error ("Can't access cell ref pointed to by null Ptr");
|
||||||
|
return mRef;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isInCell() const
|
TypeTransform<MWWorld::CellRef>& getCellRef() const
|
||||||
{
|
|
||||||
return (mContainerStore == nullptr) && (mCell != nullptr);
|
|
||||||
}
|
|
||||||
|
|
||||||
void setContainerStore (ContainerStore *store);
|
|
||||||
///< Must not be called on references that are in a cell.
|
|
||||||
|
|
||||||
ContainerStore *getContainerStore() const;
|
|
||||||
///< May return a 0-pointer, if reference is not in a container.
|
|
||||||
|
|
||||||
operator const void *();
|
|
||||||
///< Return a 0-pointer, if Ptr is empty; return a non-0-pointer, if Ptr is not empty
|
|
||||||
};
|
|
||||||
|
|
||||||
/// \brief Pointer to a const LiveCellRef
|
|
||||||
/// @note a Ptr can be implicitely converted to a ConstPtr, but you can not convert a ConstPtr to a Ptr.
|
|
||||||
class ConstPtr
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
|
|
||||||
const MWWorld::LiveCellRefBase *mRef;
|
|
||||||
const CellStore *mCell;
|
|
||||||
const ContainerStore *mContainerStore;
|
|
||||||
|
|
||||||
public:
|
|
||||||
ConstPtr(const MWWorld::LiveCellRefBase *liveCellRef=nullptr, const CellStore *cell=nullptr)
|
|
||||||
: mRef(liveCellRef), mCell(cell), mContainerStore(nullptr)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
ConstPtr(const MWWorld::Ptr& ptr)
|
|
||||||
: mRef(ptr.mRef), mCell(ptr.mCell), mContainerStore(ptr.mContainerStore)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
bool isEmpty() const
|
|
||||||
{
|
|
||||||
return mRef == nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned int getType() const;
|
|
||||||
|
|
||||||
std::string_view getTypeDescription() const
|
|
||||||
{
|
|
||||||
return mRef ? mRef->getTypeDescription() : "nullptr";
|
|
||||||
}
|
|
||||||
|
|
||||||
const Class& getClass() const
|
|
||||||
{
|
|
||||||
if(mRef != nullptr)
|
|
||||||
return *(mRef->mClass);
|
|
||||||
throw std::runtime_error("Cannot get class of an empty object");
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
const MWWorld::LiveCellRef<T> *get() const
|
|
||||||
{
|
|
||||||
const MWWorld::LiveCellRef<T> *ref = dynamic_cast<const MWWorld::LiveCellRef<T>*>(mRef);
|
|
||||||
if(ref) return ref;
|
|
||||||
|
|
||||||
std::stringstream str;
|
|
||||||
str<< "Bad LiveCellRef cast to "<<T::getRecordType()<<" from ";
|
|
||||||
if(mRef != nullptr) str<< getTypeDescription();
|
|
||||||
else str<< "an empty object";
|
|
||||||
|
|
||||||
throw std::runtime_error(str.str());
|
|
||||||
}
|
|
||||||
|
|
||||||
const MWWorld::LiveCellRefBase *getBase() const;
|
|
||||||
|
|
||||||
const MWWorld::CellRef& getCellRef() const
|
|
||||||
{
|
{
|
||||||
assert(mRef);
|
assert(mRef);
|
||||||
return mRef->mRef;
|
return mRef->mRef;
|
||||||
}
|
}
|
||||||
|
|
||||||
const RefData& getRefData() const
|
TypeTransform<RefData>& getRefData() const
|
||||||
{
|
{
|
||||||
assert(mRef);
|
assert(mRef);
|
||||||
return mRef->mData;
|
return mRef->mData;
|
||||||
}
|
}
|
||||||
|
|
||||||
const CellStore *getCell() const
|
CellStoreType *getCell() const
|
||||||
{
|
{
|
||||||
assert(mCell);
|
assert(mCell);
|
||||||
return mCell;
|
return mCell;
|
||||||
@ -174,75 +102,47 @@ namespace MWWorld
|
|||||||
return (mContainerStore == nullptr) && (mCell != nullptr);
|
return (mContainerStore == nullptr) && (mCell != nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setContainerStore (const ContainerStore *store);
|
void setContainerStore (ContainerStoreType *store)
|
||||||
///< Must not be called on references that are in a cell.
|
///< Must not be called on references that are in a cell.
|
||||||
|
{
|
||||||
|
assert (store);
|
||||||
|
assert (!mCell);
|
||||||
|
mContainerStore = store;
|
||||||
|
}
|
||||||
|
|
||||||
const ContainerStore *getContainerStore() const;
|
ContainerStoreType *getContainerStore() const
|
||||||
///< May return a 0-pointer, if reference is not in a container.
|
///< May return a 0-pointer, if reference is not in a container.
|
||||||
|
{
|
||||||
|
return mContainerStore;
|
||||||
|
}
|
||||||
|
|
||||||
operator const void *();
|
operator const void *() const
|
||||||
///< Return a 0-pointer, if Ptr is empty; return a non-0-pointer, if Ptr is not empty
|
///< Return a 0-pointer, if Ptr is empty; return a non-0-pointer, if Ptr is not empty
|
||||||
|
{
|
||||||
|
return mRef;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
PtrBase(LiveCellRefBaseType *liveCellRef, CellStoreType *cell, ContainerStoreType* containerStore) : mRef(liveCellRef), mCell(cell), mContainerStore(containerStore) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
inline bool operator== (const Ptr& left, const Ptr& right)
|
/// @note It is possible to get mutable values from const Ptr. So if a function accepts const Ptr&, the object is still mutable.
|
||||||
|
/// To make it really const the argument should be const ConstPtr&.
|
||||||
|
class Ptr : public PtrBase<std::remove_const_t>
|
||||||
{
|
{
|
||||||
return left.mRef==right.mRef;
|
public:
|
||||||
}
|
Ptr(LiveCellRefBase *liveCellRef=nullptr, CellStoreType *cell=nullptr) : PtrBase(liveCellRef, cell, nullptr) {}
|
||||||
|
};
|
||||||
|
|
||||||
inline bool operator!= (const Ptr& left, const Ptr& right)
|
/// @note The difference between Ptr and ConstPtr is that the second one adds const to the underlying pointers.
|
||||||
|
/// @note a Ptr can be implicitely converted to a ConstPtr, but you can not convert a ConstPtr to a Ptr.
|
||||||
|
class ConstPtr : public PtrBase<std::add_const_t>
|
||||||
{
|
{
|
||||||
return !(left==right);
|
public:
|
||||||
}
|
ConstPtr(const Ptr& ptr) : PtrBase(ptr.mRef, ptr.mCell, ptr.mContainerStore) {}
|
||||||
|
ConstPtr(const LiveCellRefBase *liveCellRef=nullptr, const CellStoreType *cell=nullptr) : PtrBase(liveCellRef, cell, nullptr) {}
|
||||||
|
};
|
||||||
|
|
||||||
inline bool operator< (const Ptr& left, const Ptr& right)
|
|
||||||
{
|
|
||||||
return left.mRef<right.mRef;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline bool operator>= (const Ptr& left, const Ptr& right)
|
|
||||||
{
|
|
||||||
return !(left<right);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline bool operator> (const Ptr& left, const Ptr& right)
|
|
||||||
{
|
|
||||||
return right<left;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline bool operator<= (const Ptr& left, const Ptr& right)
|
|
||||||
{
|
|
||||||
return !(left>right);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline bool operator== (const ConstPtr& left, const ConstPtr& right)
|
|
||||||
{
|
|
||||||
return left.mRef==right.mRef;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline bool operator!= (const ConstPtr& left, const ConstPtr& right)
|
|
||||||
{
|
|
||||||
return !(left==right);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline bool operator< (const ConstPtr& left, const ConstPtr& right)
|
|
||||||
{
|
|
||||||
return left.mRef<right.mRef;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline bool operator>= (const ConstPtr& left, const ConstPtr& right)
|
|
||||||
{
|
|
||||||
return !(left<right);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline bool operator> (const ConstPtr& left, const ConstPtr& right)
|
|
||||||
{
|
|
||||||
return right<left;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline bool operator<= (const ConstPtr& left, const ConstPtr& right)
|
|
||||||
{
|
|
||||||
return !(left>right);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user