Store the object class in the LiveCellRef

This commit is contained in:
Chris Robinson 2013-08-16 04:18:48 -07:00
parent 08d1d486a4
commit 21121d5ba5
5 changed files with 34 additions and 11 deletions

View file

@ -245,9 +245,10 @@ namespace MWWorld
throw std::runtime_error ("class does not support persistence"); 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 std::string Class::getUpSoundId (const Ptr& ptr) const

View file

@ -50,6 +50,8 @@ namespace MWWorld
{ {
static std::map<std::string, boost::shared_ptr<Class> > sClasses; static std::map<std::string, boost::shared_ptr<Class> > sClasses;
std::string mTypeName;
// not implemented // not implemented
Class (const Class&); Class (const Class&);
Class& operator= (const Class&); Class& operator= (const Class&);
@ -73,6 +75,10 @@ namespace MWWorld
virtual ~Class(); virtual ~Class();
const std::string& getTypeName() const {
return mTypeName;
}
virtual std::string getId (const Ptr& ptr) const; virtual std::string getId (const Ptr& ptr) const;
///< Return ID of \a ptr or throw an exception, if class does not support ID retrieval ///< Return ID of \a ptr or throw an exception, if class does not support ID retrieval
/// (default implementation: throw an exception) /// (default implementation: throw an exception)
@ -292,7 +298,7 @@ namespace MWWorld
static const Class& get (const Ptr& ptr) 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. ///< If there is no class for this pointer, an exception is thrown.

View file

@ -11,11 +11,12 @@ namespace MWWorld
{ {
class Ptr; class Ptr;
class ESMStore; class ESMStore;
class Class;
/// Used to create pointers to hold any type of LiveCellRef<> object. /// Used to create pointers to hold any type of LiveCellRef<> object.
struct LiveCellRefBase struct LiveCellRefBase
{ {
std::string mTypeName; const Class *mClass;
/** Information about this instance, such as 3D location and rotation /** Information about this instance, such as 3D location and rotation
* and individual type-dependent data. * and individual type-dependent data.
@ -25,9 +26,7 @@ namespace MWWorld
/** runtime-data */ /** runtime-data */
RefData mData; RefData mData;
LiveCellRefBase(std::string type, const ESM::CellRef &cref=ESM::CellRef()) LiveCellRefBase(std::string type, const ESM::CellRef &cref=ESM::CellRef());
: mTypeName(type), mRef(cref), mData(mRef)
{ }
/* Need this for the class to be recognized as polymorphic */ /* Need this for the class to be recognized as polymorphic */
virtual ~LiveCellRefBase() { } virtual ~LiveCellRefBase() { }
}; };

View file

@ -4,8 +4,23 @@
#include <cassert> #include <cassert>
#include "containerstore.hpp" #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 ESM::CellRef& MWWorld::Ptr::getCellRef() const
{ {
assert(mRef); assert(mRef);

View file

@ -32,11 +32,13 @@ namespace MWWorld
return mRef == 0; return mRef == 0;
} }
const std::string& getTypeName() const const std::string& getTypeName() const;
const Class& getClass() const
{ {
if(mRef != 0) if(mRef != 0)
return mRef->mTypeName; return *(mRef->mClass);
throw std::runtime_error("Can't get type name from an empty object."); throw std::runtime_error("Cannot get class of an empty object");
} }
template<typename T> template<typename T>
@ -47,7 +49,7 @@ namespace MWWorld
std::stringstream str; std::stringstream str;
str<< "Bad LiveCellRef cast to "<<typeid(T).name()<<" from "; 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"; else str<< "an empty object";
throw std::runtime_error(str.str()); throw std::runtime_error(str.str());