#ifndef GAME_MWWORLD_PTR_H #define GAME_MWWORLD_PTR_H #include #include #include #include "livecellref.hpp" namespace MWWorld { class ContainerStore; class CellStore; struct LiveCellRefBase; /// \brief Pointer to a LiveCellRef class Ptr { public: MWWorld::LiveCellRefBase *mRef; CellStore *mCell; ContainerStore *mContainerStore; public: Ptr(MWWorld::LiveCellRefBase *liveCellRef=nullptr, CellStore *cell=nullptr) : mRef(liveCellRef), mCell(cell), mContainerStore(nullptr) { } bool isEmpty() const { return mRef == nullptr; } // Returns a 32-bit id of the ESM record this object is based on. // Specific values of ids are defined in ESM::RecNameInts. // 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 // prison marker reuses ESM::Door record. Player is ESM::NPC. 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 MWWorld::LiveCellRef *get() const { MWWorld::LiveCellRef *ref = dynamic_cast*>(mRef); if(ref) return ref; std::stringstream str; str<< "Bad LiveCellRef cast to "<getTypeDescription() : "nullptr"; } const Class& getClass() const { if(mRef != nullptr) return *(mRef->mClass); throw std::runtime_error("Cannot get class of an empty object"); } template const MWWorld::LiveCellRef *get() const { const MWWorld::LiveCellRef *ref = dynamic_cast*>(mRef); if(ref) return ref; std::stringstream str; str<< "Bad LiveCellRef cast to "<mRef; } const RefData& getRefData() const { assert(mRef); return mRef->mData; } const CellStore *getCell() const { assert(mCell); return mCell; } bool isInCell() const { return (mContainerStore == nullptr) && (mCell != nullptr); } void setContainerStore (const ContainerStore *store); ///< Must not be called on references that are in a cell. const 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 }; 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 left.mRef= (const Ptr& left, const Ptr& right) { return !(left (const Ptr& left, const Ptr& right) { return rightright); } 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= (const ConstPtr& left, const ConstPtr& right) { return !(left (const ConstPtr& left, const ConstPtr& right) { return rightright); } } #endif