Add ConstPtr

openmw-38
scrawl 9 years ago
parent c9d02c67c0
commit d4eba794c3

@ -52,3 +52,25 @@ MWWorld::Ptr::operator const void *()
{
return mRef;
}
// -------------------------------------------------------------------------------
const std::string &MWWorld::ConstPtr::getTypeName() const
{
if(mRef != 0)
return mRef->mClass->getTypeName();
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;
}
MWWorld::ConstPtr::operator const void *()
{
return mRef;
}

@ -85,6 +85,87 @@ namespace MWWorld
///< 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=0, const CellStore *cell=0)
: mRef(liveCellRef), mCell(cell), mContainerStore(0)
{
}
ConstPtr(const MWWorld::Ptr& ptr)
: mRef(ptr.mRef), mCell(ptr.mCell), mContainerStore(ptr.mContainerStore)
{
}
bool isEmpty() const
{
return mRef == 0;
}
const std::string& getTypeName() const;
const Class& getClass() const
{
if(mRef != 0)
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 "<<typeid(T).name()<<" from ";
if(mRef != 0) str<< getTypeName();
else str<< "an empty object";
throw std::runtime_error(str.str());
}
const MWWorld::LiveCellRefBase *getBase() const;
const MWWorld::CellRef& getCellRef() const
{
assert(mRef);
return mRef->mRef;
}
const RefData& getRefData() const
{
assert(mRef);
return mRef->mData;
}
const CellStore *getCell() const
{
assert(mCell);
return mCell;
}
bool isInCell() const
{
return (mContainerStore == 0) && (mCell != 0);
}
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;

Loading…
Cancel
Save