mirror of
https://github.com/TES3MP/openmw-tes3mp.git
synced 2025-01-21 09:53:50 +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
|
#ifndef GAME_MWWORLD_LIVECELLREF_H
|
||||||
#define GAME_MWWORLD_LIVECELLREF_H
|
#define GAME_MWWORLD_LIVECELLREF_H
|
||||||
|
|
||||||
|
#include <typeinfo>
|
||||||
|
|
||||||
#include <components/esm/cellref.hpp>
|
#include <components/esm/cellref.hpp>
|
||||||
|
|
||||||
#include "refdata.hpp"
|
#include "refdata.hpp"
|
||||||
|
@ -10,6 +12,15 @@ namespace MWWorld
|
||||||
class Ptr;
|
class Ptr;
|
||||||
class ESMStore;
|
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.
|
/// A reference to one object (of any type) in a cell.
|
||||||
///
|
///
|
||||||
/// Constructing this with a CellRef instance in the constructor means that
|
/// 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
|
/// across to mData. If later adding data (such as position) to CellRef
|
||||||
/// this would have to be manually copied across.
|
/// this would have to be manually copied across.
|
||||||
template <typename X>
|
template <typename X>
|
||||||
struct LiveCellRef
|
struct LiveCellRef : public LiveCellRefBase
|
||||||
{
|
{
|
||||||
LiveCellRef(const ESM::CellRef& cref, const X* b = NULL)
|
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)
|
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.
|
// The object that this instance is based on.
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
#ifndef GAME_MWWORLD_PTR_H
|
#ifndef GAME_MWWORLD_PTR_H
|
||||||
#define GAME_MWWORLD_PTR_H
|
#define GAME_MWWORLD_PTR_H
|
||||||
|
|
||||||
#include <boost/any.hpp>
|
|
||||||
|
|
||||||
#include "cellstore.hpp"
|
#include "cellstore.hpp"
|
||||||
|
|
||||||
namespace MWWorld
|
namespace MWWorld
|
||||||
|
@ -18,7 +16,7 @@ namespace MWWorld
|
||||||
typedef MWWorld::CellStore CellStore;
|
typedef MWWorld::CellStore CellStore;
|
||||||
///< \deprecated
|
///< \deprecated
|
||||||
|
|
||||||
boost::any mPtr;
|
MWWorld::LiveCellRefBase *mPtr;
|
||||||
ESM::CellRef *mCellRef;
|
ESM::CellRef *mCellRef;
|
||||||
RefData *mRefData;
|
RefData *mRefData;
|
||||||
CellStore *mCell;
|
CellStore *mCell;
|
||||||
|
@ -27,17 +25,11 @@ namespace MWWorld
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
Ptr() : mCellRef (0), mRefData (0), mCell (0), mContainerStore (0) {}
|
Ptr() : mPtr (0), mCellRef (0), mRefData (0), mCell (0), mContainerStore (0) {}
|
||||||
|
|
||||||
bool isEmpty() const
|
bool isEmpty() const
|
||||||
{
|
{
|
||||||
return mPtr.empty();
|
return mPtr == 0;
|
||||||
}
|
|
||||||
|
|
||||||
const std::type_info& getType() const
|
|
||||||
{
|
|
||||||
assert (!mPtr.empty());
|
|
||||||
return mPtr.type();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string& getTypeName() const
|
const std::string& getTypeName() const
|
||||||
|
@ -59,7 +51,15 @@ namespace MWWorld
|
||||||
template<typename T>
|
template<typename T>
|
||||||
MWWorld::LiveCellRef<T> *get() const
|
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;
|
ESM::CellRef& getCellRef() const;
|
||||||
|
|
Loading…
Reference in a new issue