From a70c3876a27da394d2f33ff6d1977f93172aa7e4 Mon Sep 17 00:00:00 2001 From: Marc Zinnschlag Date: Tue, 3 Aug 2010 11:14:57 +0200 Subject: [PATCH] added foundation for esm record class hierarchy --- apps/openmw/CMakeLists.txt | 36 +++++++++++++++++------------- apps/openmw/engine.cpp | 2 ++ apps/openmw/mwworld/activator.cpp | 14 ++++++++++++ apps/openmw/mwworld/activator.hpp | 17 ++++++++++++++ apps/openmw/mwworld/class.cpp | 28 +++++++++++++++++++++++ apps/openmw/mwworld/class.hpp | 37 +++++++++++++++++++++++++++++++ apps/openmw/mwworld/classes.cpp | 12 ++++++++++ apps/openmw/mwworld/classes.hpp | 10 +++++++++ apps/openmw/mwworld/ptr.hpp | 7 ++++++ 9 files changed, 148 insertions(+), 15 deletions(-) create mode 100644 apps/openmw/mwworld/activator.cpp create mode 100644 apps/openmw/mwworld/activator.hpp create mode 100644 apps/openmw/mwworld/class.cpp create mode 100644 apps/openmw/mwworld/class.hpp create mode 100644 apps/openmw/mwworld/classes.cpp create mode 100644 apps/openmw/mwworld/classes.hpp diff --git a/apps/openmw/CMakeLists.txt b/apps/openmw/CMakeLists.txt index 260ebcd70f..2dc4473ba8 100644 --- a/apps/openmw/CMakeLists.txt +++ b/apps/openmw/CMakeLists.txt @@ -2,33 +2,33 @@ project(OpenMW) # local files -set(GAME +set(GAME main.cpp engine.cpp) -set(GAME_HEADER +set(GAME_HEADER engine.hpp) source_group(game FILES ${GAME} ${GAME_HEADER}) -set(GAMEREND +set(GAMEREND mwrender/mwscene.cpp mwrender/cellimp.cpp mwrender/interior.cpp mwrender/sky.cpp) -set(GAMEREND_HEADER +set(GAMEREND_HEADER mwrender/cell.hpp mwrender/cellimp.hpp mwrender/mwscene.hpp mwrender/interior.hpp mwrender/playerpos.hpp mwrender/sky.hpp) -source_group(apps\\openmw\\mwrender FILES ${GAMEREND} ${GAMEREND_HEADER}) +source_group(apps\\openmw\\mwrender FILES ${GAMEREND} ${GAMEREND_HEADER}) set(GAMEINPUT mwinput/inputmanager.cpp ) -set(GAMEINPUT_HEADER +set(GAMEINPUT_HEADER mwinput/inputmanager.hpp) -source_group(apps\\openmw\\mwinput FILES ${GAMEINPUT} ${GAMEINPUT_HEADER}) +source_group(apps\\openmw\\mwinput FILES ${GAMEINPUT} ${GAMEINPUT_HEADER}) set(GAMEGUI_HEADER mwgui/mw_layouts.hpp @@ -54,7 +54,7 @@ set(GAMESCRIPT mwscript/extensions.cpp mwscript/globalscripts.cpp ) -set(GAMESCRIPT_HEADER +set(GAMESCRIPT_HEADER mwscript/locals.hpp mwscript/scriptmanager.hpp mwscript/compilercontext.hpp @@ -68,35 +68,41 @@ set(GAMESCRIPT_HEADER mwscript/extensions.hpp mwscript/globalscripts.hpp ) -source_group(apps\\openmw\\mwscript FILES ${GAMESCRIPT} ${GAMESCRIPT_HEADER}) +source_group(apps\\openmw\\mwscript FILES ${GAMESCRIPT} ${GAMESCRIPT_HEADER}) set(GAMESOUND mwsound/soundmanager.cpp) -set(GAMESOUND_HEADER +set(GAMESOUND_HEADER mwsound/soundmanager.hpp) -source_group(apps\\openmw\\mwsound FILES ${GAMESOUND} ${GAMESOUND_HEADER}) +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 +set(GAMEWORLD_HEADER mwworld/refdata.hpp mwworld/world.hpp 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}) +source_group(apps\\openmw\\mwworld FILES ${GAMEWORLD} ${GAMEWORLD_HEADER}) set(GAMEMECHANICS mwmechanics/mechanicsmanager.cpp) -set(GAMEMECHANICS_HEADER +set(GAMEMECHANICS_HEADER mwmechanics/mechanicsmanager.hpp mwmechanics/stat.hpp mwmechanics/creaturestats.hpp ) -source_group(apps\\openmw\\mwmechanics FILES ${GAMEMECHANICS} ${GAMEMECHANICS_HEADER}) +source_group(apps\\openmw\\mwmechanics FILES ${GAMEMECHANICS} ${GAMEMECHANICS_HEADER}) set(OPENMW_CPP ${GAME} ${GAMEREND} ${GAMEINPUT} ${GAMESCRIPT} ${GAMESOUND} ${GAMEGUI} ${GAMEWORLD} ${GAMEMECHANICS} diff --git a/apps/openmw/engine.cpp b/apps/openmw/engine.cpp index 43a01b271e..f49216cdaf 100644 --- a/apps/openmw/engine.cpp +++ b/apps/openmw/engine.cpp @@ -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() diff --git a/apps/openmw/mwworld/activator.cpp b/apps/openmw/mwworld/activator.cpp new file mode 100644 index 0000000000..27bf00e10b --- /dev/null +++ b/apps/openmw/mwworld/activator.cpp @@ -0,0 +1,14 @@ + +#include "activator.hpp" + +#include + +namespace MWWorld +{ + void Activator::registerSelf() + { + boost::shared_ptr instance (new Activator); + + registerClass (typeid (ESM::Activator).name(), instance); + } +} diff --git a/apps/openmw/mwworld/activator.hpp b/apps/openmw/mwworld/activator.hpp new file mode 100644 index 0000000000..023790e6f0 --- /dev/null +++ b/apps/openmw/mwworld/activator.hpp @@ -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 diff --git a/apps/openmw/mwworld/class.cpp b/apps/openmw/mwworld/class.cpp new file mode 100644 index 0000000000..1520916bee --- /dev/null +++ b/apps/openmw/mwworld/class.cpp @@ -0,0 +1,28 @@ + +#include "class.hpp" + +#include + +namespace MWWorld +{ + std::map > Class::sClasses; + + Class::Class() {} + + Class::~Class() {} + + const Class& Class::get (const std::string& key) + { + std::map >::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 instance) + { + sClasses.insert (std::make_pair (key, instance)); + } +} diff --git a/apps/openmw/mwworld/class.hpp b/apps/openmw/mwworld/class.hpp new file mode 100644 index 0000000000..c7c6a26f97 --- /dev/null +++ b/apps/openmw/mwworld/class.hpp @@ -0,0 +1,37 @@ +#ifndef GAME_MWWORLD_CLASS_H +#define GAME_MWWORLD_CLASS_H + +#include +#include + +#include + +namespace MWWorld +{ + /// \brief Base class for referenceable esm records + class Class + { + static std::map > 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 instance); + }; +} + +#endif diff --git a/apps/openmw/mwworld/classes.cpp b/apps/openmw/mwworld/classes.cpp new file mode 100644 index 0000000000..79f9b52d70 --- /dev/null +++ b/apps/openmw/mwworld/classes.cpp @@ -0,0 +1,12 @@ + +#include "classes.hpp" + +#include "activator.hpp" + +namespace MWWorld +{ + void registerClasses() + { + Activator::registerSelf(); + } +} diff --git a/apps/openmw/mwworld/classes.hpp b/apps/openmw/mwworld/classes.hpp new file mode 100644 index 0000000000..9b83b8d73a --- /dev/null +++ b/apps/openmw/mwworld/classes.hpp @@ -0,0 +1,10 @@ +#ifndef GAME_MWWORLD_CLASSES_H +#define GAME_MWWORLD_CLASSES_H + +namespace MWWorld +{ + void registerClasses(); + ///< register all known classes +} + +#endif diff --git a/apps/openmw/mwworld/ptr.hpp b/apps/openmw/mwworld/ptr.hpp index f05a9bef29..92eed194c7 100644 --- a/apps/openmw/mwworld/ptr.hpp +++ b/apps/openmw/mwworld/ptr.hpp @@ -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 Ptr (ESMS::LiveCellRef *liveCellRef, CellStore *cell) { @@ -50,6 +56,7 @@ namespace MWWorld mCellRef = &liveCellRef->ref; mRefData = &liveCellRef->mData; mCell = cell; + mTypeName = typeid (T).name(); } template