mirror of
https://github.com/OpenMW/openmw.git
synced 2025-02-03 19:15:40 +00:00
Avoid using boost::any in MWWorld::Ptr
Reduces dependency on Boost, and should improve performance a bit when copying or constructing Ptr objects.
This commit is contained in:
parent
aee0336780
commit
b6d2888c48
2 changed files with 26 additions and 15 deletions
|
@ -1,6 +1,8 @@
|
|||
#ifndef GAME_MWWORLD_LIVECELLREF_H
|
||||
#define GAME_MWWORLD_LIVECELLREF_H
|
||||
|
||||
#include <typeinfo>
|
||||
|
||||
#include <components/esm/cellref.hpp>
|
||||
|
||||
#include "refdata.hpp"
|
||||
|
@ -10,6 +12,15 @@ namespace MWWorld
|
|||
class Ptr;
|
||||
class ESMStore;
|
||||
|
||||
/// Used to create pointers to hold any type of LiveCellRef<> object.
|
||||
struct LiveCellRefBase
|
||||
{
|
||||
std::string mTypeName;
|
||||
|
||||
LiveCellRefBase(std::string type) : mTypeName(type)
|
||||
{ }
|
||||
};
|
||||
|
||||
/// A reference to one object (of any type) in a cell.
|
||||
///
|
||||
/// Constructing this with a CellRef instance in the constructor means that
|
||||
|
@ -17,14 +28,14 @@ namespace MWWorld
|
|||
/// across to mData. If later adding data (such as position) to CellRef
|
||||
/// this would have to be manually copied across.
|
||||
template <typename X>
|
||||
struct LiveCellRef
|
||||
struct LiveCellRef : public LiveCellRefBase
|
||||
{
|
||||
LiveCellRef(const ESM::CellRef& cref, const X* b = NULL)
|
||||
: mBase(b), mRef(cref), mData(mRef)
|
||||
: LiveCellRefBase(typeid(X).name()), mBase(b), mRef(cref), mData(mRef)
|
||||
{}
|
||||
|
||||
LiveCellRef(const X* b = NULL)
|
||||
: mBase(b), mData(mRef)
|
||||
: LiveCellRefBase(typeid(X).name()), mBase(b), mData(mRef)
|
||||
{}
|
||||
|
||||
// The object that this instance is based on.
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
#ifndef GAME_MWWORLD_PTR_H
|
||||
#define GAME_MWWORLD_PTR_H
|
||||
|
||||
#include <boost/any.hpp>
|
||||
|
||||
#include "cellstore.hpp"
|
||||
|
||||
namespace MWWorld
|
||||
|
@ -18,7 +16,7 @@ namespace MWWorld
|
|||
typedef MWWorld::CellStore CellStore;
|
||||
///< \deprecated
|
||||
|
||||
boost::any mPtr;
|
||||
MWWorld::LiveCellRefBase *mPtr;
|
||||
ESM::CellRef *mCellRef;
|
||||
RefData *mRefData;
|
||||
CellStore *mCell;
|
||||
|
@ -27,17 +25,11 @@ namespace MWWorld
|
|||
|
||||
public:
|
||||
|
||||
Ptr() : mCellRef (0), mRefData (0), mCell (0), mContainerStore (0) {}
|
||||
Ptr() : mPtr (0), mCellRef (0), mRefData (0), mCell (0), mContainerStore (0) {}
|
||||
|
||||
bool isEmpty() const
|
||||
{
|
||||
return mPtr.empty();
|
||||
}
|
||||
|
||||
const std::type_info& getType() const
|
||||
{
|
||||
assert (!mPtr.empty());
|
||||
return mPtr.type();
|
||||
return mPtr == 0;
|
||||
}
|
||||
|
||||
const std::string& getTypeName() const
|
||||
|
@ -59,7 +51,15 @@ namespace MWWorld
|
|||
template<typename T>
|
||||
MWWorld::LiveCellRef<T> *get() const
|
||||
{
|
||||
return boost::any_cast<MWWorld::LiveCellRef<T>*> (mPtr);
|
||||
if(mPtr && mPtr->mTypeName == typeid(T).name())
|
||||
return static_cast<MWWorld::LiveCellRef<T>*>(mPtr);
|
||||
|
||||
std::stringstream str;
|
||||
str<< "Bad LiveCellRef cast to "<<typeid(T).name()<<" from ";
|
||||
if(mPtr != 0) str<< mPtr->mTypeName;
|
||||
else str<< "an empty object";
|
||||
|
||||
throw std::runtime_error(str.str());
|
||||
}
|
||||
|
||||
ESM::CellRef& getCellRef() const;
|
||||
|
|
Loading…
Reference in a new issue