mirror of
https://github.com/TES3MP/openmw-tes3mp.git
synced 2025-02-01 20:15:33 +00:00
Store the object class in the LiveCellRef
This commit is contained in:
parent
08d1d486a4
commit
21121d5ba5
5 changed files with 34 additions and 11 deletions
|
@ -245,9 +245,10 @@ namespace MWWorld
|
|||
throw std::runtime_error ("class does not support persistence");
|
||||
}
|
||||
|
||||
void Class::registerClass (const std::string& key, boost::shared_ptr<Class> instance)
|
||||
void Class::registerClass(const std::string& key, boost::shared_ptr<Class> instance)
|
||||
{
|
||||
sClasses.insert (std::make_pair (key, instance));
|
||||
instance->mTypeName = key;
|
||||
sClasses.insert(std::make_pair(key, instance));
|
||||
}
|
||||
|
||||
std::string Class::getUpSoundId (const Ptr& ptr) const
|
||||
|
|
|
@ -50,6 +50,8 @@ namespace MWWorld
|
|||
{
|
||||
static std::map<std::string, boost::shared_ptr<Class> > sClasses;
|
||||
|
||||
std::string mTypeName;
|
||||
|
||||
// not implemented
|
||||
Class (const Class&);
|
||||
Class& operator= (const Class&);
|
||||
|
@ -73,6 +75,10 @@ namespace MWWorld
|
|||
|
||||
virtual ~Class();
|
||||
|
||||
const std::string& getTypeName() const {
|
||||
return mTypeName;
|
||||
}
|
||||
|
||||
virtual std::string getId (const Ptr& ptr) const;
|
||||
///< Return ID of \a ptr or throw an exception, if class does not support ID retrieval
|
||||
/// (default implementation: throw an exception)
|
||||
|
@ -292,7 +298,7 @@ namespace MWWorld
|
|||
|
||||
static const Class& get (const Ptr& ptr)
|
||||
{
|
||||
return get(ptr.getTypeName());
|
||||
return ptr.getClass();
|
||||
}
|
||||
///< If there is no class for this pointer, an exception is thrown.
|
||||
|
||||
|
|
|
@ -11,11 +11,12 @@ namespace MWWorld
|
|||
{
|
||||
class Ptr;
|
||||
class ESMStore;
|
||||
class Class;
|
||||
|
||||
/// Used to create pointers to hold any type of LiveCellRef<> object.
|
||||
struct LiveCellRefBase
|
||||
{
|
||||
std::string mTypeName;
|
||||
const Class *mClass;
|
||||
|
||||
/** Information about this instance, such as 3D location and rotation
|
||||
* and individual type-dependent data.
|
||||
|
@ -25,9 +26,7 @@ namespace MWWorld
|
|||
/** runtime-data */
|
||||
RefData mData;
|
||||
|
||||
LiveCellRefBase(std::string type, const ESM::CellRef &cref=ESM::CellRef())
|
||||
: mTypeName(type), mRef(cref), mData(mRef)
|
||||
{ }
|
||||
LiveCellRefBase(std::string type, const ESM::CellRef &cref=ESM::CellRef());
|
||||
/* Need this for the class to be recognized as polymorphic */
|
||||
virtual ~LiveCellRefBase() { }
|
||||
};
|
||||
|
|
|
@ -4,8 +4,23 @@
|
|||
#include <cassert>
|
||||
|
||||
#include "containerstore.hpp"
|
||||
#include "class.hpp"
|
||||
|
||||
|
||||
/* This shouldn't really be here. */
|
||||
MWWorld::LiveCellRefBase::LiveCellRefBase(std::string type, const ESM::CellRef &cref)
|
||||
: mClass(&Class::get(type)), mRef(cref), mData(mRef)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
const std::string& MWWorld::Ptr::getTypeName() const
|
||||
{
|
||||
if(mRef != 0)
|
||||
return mRef->mClass->getTypeName();
|
||||
throw std::runtime_error("Can't get type name from an empty object.");
|
||||
}
|
||||
|
||||
ESM::CellRef& MWWorld::Ptr::getCellRef() const
|
||||
{
|
||||
assert(mRef);
|
||||
|
|
|
@ -32,11 +32,13 @@ namespace MWWorld
|
|||
return mRef == 0;
|
||||
}
|
||||
|
||||
const std::string& getTypeName() const
|
||||
const std::string& getTypeName() const;
|
||||
|
||||
const Class& getClass() const
|
||||
{
|
||||
if(mRef != 0)
|
||||
return mRef->mTypeName;
|
||||
throw std::runtime_error("Can't get type name from an empty object.");
|
||||
return *(mRef->mClass);
|
||||
throw std::runtime_error("Cannot get class of an empty object");
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
|
@ -47,7 +49,7 @@ namespace MWWorld
|
|||
|
||||
std::stringstream str;
|
||||
str<< "Bad LiveCellRef cast to "<<typeid(T).name()<<" from ";
|
||||
if(mRef != 0) str<< mRef->mTypeName;
|
||||
if(mRef != 0) str<< getTypeName();
|
||||
else str<< "an empty object";
|
||||
|
||||
throw std::runtime_error(str.str());
|
||||
|
|
Loading…
Reference in a new issue