2010-07-03 17:46:55 +02:00
|
|
|
#ifndef GAME_MWWORLD_PTR_H
|
|
|
|
#define GAME_MWWORLD_PTR_H
|
|
|
|
|
2014-02-23 20:11:05 +01:00
|
|
|
#include <cassert>
|
2021-10-25 07:08:50 +00:00
|
|
|
#include <type_traits>
|
2014-02-23 20:11:05 +01:00
|
|
|
#include <string>
|
2021-11-04 10:44:28 -04:00
|
|
|
#include <string_view>
|
2014-02-23 20:11:05 +01:00
|
|
|
#include <sstream>
|
|
|
|
|
2015-01-10 01:00:52 +01:00
|
|
|
#include "livecellref.hpp"
|
2010-07-03 17:46:55 +02:00
|
|
|
|
|
|
|
namespace MWWorld
|
|
|
|
{
|
2012-03-21 12:20:19 +01:00
|
|
|
class ContainerStore;
|
2014-02-23 20:11:05 +01:00
|
|
|
class CellStore;
|
2015-03-06 21:36:42 +13:00
|
|
|
struct LiveCellRefBase;
|
2012-03-21 12:20:19 +01:00
|
|
|
|
2010-07-03 17:46:55 +02:00
|
|
|
/// \brief Pointer to a LiveCellRef
|
2021-10-25 07:08:50 +00:00
|
|
|
/// @note PtrBase is never used directly and needed only to define Ptr and ConstPtr
|
|
|
|
template <template<class> class TypeTransform>
|
|
|
|
class PtrBase
|
2010-07-03 17:46:55 +02:00
|
|
|
{
|
2010-07-10 13:19:04 +02:00
|
|
|
public:
|
2010-07-28 18:27:46 +02:00
|
|
|
|
2021-10-25 07:08:50 +00:00
|
|
|
typedef TypeTransform<MWWorld::LiveCellRefBase> LiveCellRefBaseType;
|
|
|
|
typedef TypeTransform<CellStore> CellStoreType;
|
|
|
|
typedef TypeTransform<ContainerStore> ContainerStoreType;
|
2010-07-28 18:27:46 +02:00
|
|
|
|
2021-10-25 07:08:50 +00:00
|
|
|
LiveCellRefBaseType *mRef;
|
|
|
|
CellStoreType *mCell;
|
|
|
|
ContainerStoreType *mContainerStore;
|
2010-07-28 18:27:46 +02:00
|
|
|
|
2010-07-05 12:09:04 +02:00
|
|
|
bool isEmpty() const
|
|
|
|
{
|
2020-11-13 11:39:47 +04:00
|
|
|
return mRef == nullptr;
|
2010-07-27 14:43:46 +02:00
|
|
|
}
|
2010-07-28 18:27:46 +02:00
|
|
|
|
2021-10-12 21:25:34 +02:00
|
|
|
// 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.
|
2021-10-25 07:08:50 +00:00
|
|
|
unsigned int getType() const
|
|
|
|
{
|
|
|
|
if(mRef != nullptr)
|
|
|
|
return mRef->getType();
|
|
|
|
throw std::runtime_error("Can't get type name from an empty object.");
|
|
|
|
}
|
2021-10-11 11:46:21 +00:00
|
|
|
|
2021-10-12 00:18:23 +02:00
|
|
|
std::string_view getTypeDescription() const
|
2021-10-11 11:46:21 +00:00
|
|
|
{
|
|
|
|
return mRef ? mRef->getTypeDescription() : "nullptr";
|
|
|
|
}
|
2013-08-16 04:18:48 -07:00
|
|
|
|
|
|
|
const Class& getClass() const
|
2010-08-03 11:14:57 +02:00
|
|
|
{
|
2020-11-13 11:39:47 +04:00
|
|
|
if(mRef != nullptr)
|
2013-08-16 04:18:48 -07:00
|
|
|
return *(mRef->mClass);
|
|
|
|
throw std::runtime_error("Cannot get class of an empty object");
|
2010-07-03 17:46:55 +02:00
|
|
|
}
|
2010-07-28 18:27:46 +02:00
|
|
|
|
2010-07-03 17:46:55 +02:00
|
|
|
template<typename T>
|
2021-10-25 07:08:50 +00:00
|
|
|
TypeTransform<MWWorld::LiveCellRef<T>> *get() const
|
2010-07-03 17:46:55 +02:00
|
|
|
{
|
2021-10-25 07:08:50 +00:00
|
|
|
TypeTransform<MWWorld::LiveCellRef<T>> *ref = dynamic_cast<TypeTransform<MWWorld::LiveCellRef<T>>*>(mRef);
|
2013-08-14 20:26:50 -07:00
|
|
|
if(ref) return ref;
|
2013-08-13 06:32:20 -07:00
|
|
|
|
|
|
|
std::stringstream str;
|
2021-10-11 11:46:21 +00:00
|
|
|
str<< "Bad LiveCellRef cast to "<<T::getRecordType()<<" from ";
|
|
|
|
if(mRef != nullptr) str<< getTypeDescription();
|
2013-08-13 06:32:20 -07:00
|
|
|
else str<< "an empty object";
|
|
|
|
|
|
|
|
throw std::runtime_error(str.str());
|
2010-07-03 17:46:55 +02:00
|
|
|
}
|
2010-07-28 18:27:46 +02:00
|
|
|
|
2021-10-25 07:08:50 +00:00
|
|
|
LiveCellRefBaseType *getBase() const
|
|
|
|
{
|
|
|
|
if (!mRef)
|
|
|
|
throw std::runtime_error ("Can't access cell ref pointed to by null Ptr");
|
|
|
|
return mRef;
|
|
|
|
}
|
2014-01-31 13:25:32 +01:00
|
|
|
|
2021-10-25 07:08:50 +00:00
|
|
|
TypeTransform<MWWorld::CellRef>& getCellRef() const
|
|
|
|
{
|
|
|
|
assert(mRef);
|
|
|
|
return mRef->mRef;
|
|
|
|
}
|
2010-07-28 18:27:46 +02:00
|
|
|
|
2021-10-25 07:08:50 +00:00
|
|
|
TypeTransform<RefData>& getRefData() const
|
|
|
|
{
|
|
|
|
assert(mRef);
|
|
|
|
return mRef->mData;
|
|
|
|
}
|
2010-07-28 18:27:46 +02:00
|
|
|
|
2021-10-25 07:08:50 +00:00
|
|
|
CellStoreType *getCell() const
|
2010-07-10 13:19:04 +02:00
|
|
|
{
|
2013-05-15 17:54:18 +02:00
|
|
|
assert(mCell);
|
2010-07-10 13:19:04 +02:00
|
|
|
return mCell;
|
|
|
|
}
|
2012-03-21 12:20:19 +01:00
|
|
|
|
2012-05-15 18:05:53 +02:00
|
|
|
bool isInCell() const
|
|
|
|
{
|
2020-11-13 11:39:47 +04:00
|
|
|
return (mContainerStore == nullptr) && (mCell != nullptr);
|
2012-05-15 18:05:53 +02:00
|
|
|
}
|
|
|
|
|
2021-10-25 07:08:50 +00:00
|
|
|
void setContainerStore (ContainerStoreType *store)
|
2012-03-21 12:20:19 +01:00
|
|
|
///< Must not be called on references that are in a cell.
|
2021-10-25 07:08:50 +00:00
|
|
|
{
|
|
|
|
assert (store);
|
|
|
|
assert (!mCell);
|
|
|
|
mContainerStore = store;
|
|
|
|
}
|
2012-03-21 12:20:19 +01:00
|
|
|
|
2021-10-25 07:08:50 +00:00
|
|
|
ContainerStoreType *getContainerStore() const
|
2012-03-21 12:20:19 +01:00
|
|
|
///< May return a 0-pointer, if reference is not in a container.
|
2021-10-25 07:08:50 +00:00
|
|
|
{
|
|
|
|
return mContainerStore;
|
|
|
|
}
|
2014-02-23 18:17:41 +01:00
|
|
|
|
2021-10-25 07:08:50 +00:00
|
|
|
operator const void *() const
|
2014-02-23 18:17:41 +01:00
|
|
|
///< Return a 0-pointer, if Ptr is empty; return a non-0-pointer, if Ptr is not empty
|
2021-10-25 07:08:50 +00:00
|
|
|
{
|
|
|
|
return mRef;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
PtrBase(LiveCellRefBaseType *liveCellRef, CellStoreType *cell, ContainerStoreType* containerStore) : mRef(liveCellRef), mCell(cell), mContainerStore(containerStore) {}
|
2010-07-03 17:46:55 +02:00
|
|
|
};
|
2010-07-28 18:27:46 +02:00
|
|
|
|
2021-10-25 07:08:50 +00:00
|
|
|
/// @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>
|
2015-12-17 23:34:52 +01:00
|
|
|
{
|
|
|
|
public:
|
2021-10-25 07:08:50 +00:00
|
|
|
Ptr(LiveCellRefBase *liveCellRef=nullptr, CellStoreType *cell=nullptr) : PtrBase(liveCellRef, cell, nullptr) {}
|
2015-12-17 23:34:52 +01:00
|
|
|
};
|
|
|
|
|
2021-10-25 07:08:50 +00:00
|
|
|
/// @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>
|
2015-12-18 17:00:31 +01:00
|
|
|
{
|
2021-10-25 07:08:50 +00:00
|
|
|
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) {}
|
|
|
|
};
|
2015-12-18 17:00:31 +01:00
|
|
|
|
2010-07-03 17:46:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|