added foundation for esm record class hierarchy

This commit is contained in:
Marc Zinnschlag 2010-08-03 11:14:57 +02:00
parent b584215680
commit a70c3876a2
9 changed files with 148 additions and 15 deletions

View file

@ -79,6 +79,9 @@ source_group(apps\\openmw\\mwsound FILES ${GAMESOUND} ${GAMESOUND_HEADER})
set(GAMEWORLD
mwworld/world.cpp
mwworld/globals.cpp
mwworld/class.cpp
mwworld/classes.cpp
mwworld/activator.cpp
)
set(GAMEWORLD_HEADER
mwworld/refdata.hpp
@ -86,6 +89,9 @@ set(GAMEWORLD_HEADER
mwworld/ptr.hpp
mwworld/environment.hpp
mwworld/globals.hpp
mwworld/class.hpp
mwworld/classes.hpp
mwworld/activator.hpp
)
source_group(apps\\openmw\\mwworld FILES ${GAMEWORLD} ${GAMEWORLD_HEADER})

View file

@ -23,6 +23,7 @@
#include "mwworld/world.hpp"
#include "mwworld/ptr.hpp"
#include "mwworld/environment.hpp"
#include "mwworld/classes.hpp"
#include "mwmechanics/mechanicsmanager.hpp"
@ -78,6 +79,7 @@ OMW::Engine::Engine()
, mScriptManager (0)
, mScriptContext (0)
{
MWWorld::registerClasses();
}
OMW::Engine::~Engine()

View file

@ -0,0 +1,14 @@
#include "activator.hpp"
#include <components/esm/loadacti.hpp>
namespace MWWorld
{
void Activator::registerSelf()
{
boost::shared_ptr<Class> instance (new Activator);
registerClass (typeid (ESM::Activator).name(), instance);
}
}

View file

@ -0,0 +1,17 @@
#ifndef GAME_MWWORLD_ACTIVATOR_H
#define GAME_MWWORLD_ACTIVATOR_H
#include "class.hpp"
namespace MWWorld
{
class Activator : public Class
{
public:
static void registerSelf();
};
}
#endif

View file

@ -0,0 +1,28 @@
#include "class.hpp"
#include <stdexcept>
namespace MWWorld
{
std::map<std::string, boost::shared_ptr<Class> > Class::sClasses;
Class::Class() {}
Class::~Class() {}
const Class& Class::get (const std::string& key)
{
std::map<std::string, boost::shared_ptr<Class> >::const_iterator iter = sClasses.find (key);
if (iter==sClasses.end())
throw std::logic_error ("unknown class key: " + key);
return *iter->second;
}
void Class::registerClass (const std::string& key, boost::shared_ptr<Class> instance)
{
sClasses.insert (std::make_pair (key, instance));
}
}

View file

@ -0,0 +1,37 @@
#ifndef GAME_MWWORLD_CLASS_H
#define GAME_MWWORLD_CLASS_H
#include <map>
#include <string>
#include <boost/shared_ptr.hpp>
namespace MWWorld
{
/// \brief Base class for referenceable esm records
class Class
{
static std::map<std::string, boost::shared_ptr<Class> > sClasses;
// not implemented
Class (const Class&);
Class& operator= (const Class&);
protected:
Class();
public:
virtual ~Class();
static const Class& get (const std::string& key);
///< If there is no class for this \a key, an exception is thrown.
static void registerClass (const std::string& key, boost::shared_ptr<Class> instance);
};
}
#endif

View file

@ -0,0 +1,12 @@
#include "classes.hpp"
#include "activator.hpp"
namespace MWWorld
{
void registerClasses()
{
Activator::registerSelf();
}
}

View file

@ -0,0 +1,10 @@
#ifndef GAME_MWWORLD_CLASSES_H
#define GAME_MWWORLD_CLASSES_H
namespace MWWorld
{
void registerClasses();
///< register all known classes
}
#endif

View file

@ -27,6 +27,7 @@ namespace MWWorld
ESM::CellRef *mCellRef;
RefData *mRefData;
CellStore *mCell;
std::string mTypeName;
public:
@ -43,6 +44,11 @@ namespace MWWorld
return mPtr.type();
}
const std::string& getTypeName() const
{
return mTypeName;
}
template<typename T>
Ptr (ESMS::LiveCellRef<T, RefData> *liveCellRef, CellStore *cell)
{
@ -50,6 +56,7 @@ namespace MWWorld
mCellRef = &liveCellRef->ref;
mRefData = &liveCellRef->mData;
mCell = cell;
mTypeName = typeid (T).name();
}
template<typename T>