From 677158c477da1fa08ad9353430eb9b1511c24b5e Mon Sep 17 00:00:00 2001 From: Marc Zinnschlag Date: Thu, 13 Sep 2012 10:41:55 +0200 Subject: [PATCH] added typesafe access functions for GMST values --- components/esm/loadgmst.cpp | 35 +++++++++++++++++++++++++++++++++++ components/esm/loadgmst.hpp | 9 +++++++++ 2 files changed, 44 insertions(+) diff --git a/components/esm/loadgmst.cpp b/components/esm/loadgmst.cpp index 677642e319..4bd464da69 100644 --- a/components/esm/loadgmst.cpp +++ b/components/esm/loadgmst.cpp @@ -1,8 +1,15 @@ #include "loadgmst.hpp" +#include + +#include "defs.hpp" + namespace ESM { +/// \todo Review GMST "fixing". Probably remove completely or at least make it optional. Its definitely not +/// working properly in its current state and I doubt it can be fixed without breaking other stuff. + // Some handy macros #define cI(s,x) { if(id == (s)) return (i == (x)); } #define cF(s,x) { if(id == (s)) return (f == (x)); } @@ -169,4 +176,32 @@ void GameSetting::load(ESMReader &esm) dirty = true; } +int GameSetting::getInt() const +{ + switch (type) + { + case VT_Float: return static_cast (f); + case VT_Int: return i; + default: throw std::runtime_error ("GMST " + id + " is not of a numeric type"); + } +} + +int GameSetting::getFloat() const +{ + switch (type) + { + case VT_Float: return f; + case VT_Int: return i; + default: throw std::runtime_error ("GMST " + id + " is not of a numeric type"); + } +} + +std::string GameSetting::getString() const +{ + if (type==VT_String) + return str; + + throw std::runtime_error ("GMST " + id + " is not a string"); +} + } diff --git a/components/esm/loadgmst.hpp b/components/esm/loadgmst.hpp index 01fbc30676..f63028731b 100644 --- a/components/esm/loadgmst.hpp +++ b/components/esm/loadgmst.hpp @@ -83,6 +83,15 @@ struct GameSetting bool isDirtyBloodmoon(); void load(ESMReader &esm); + + int getInt() const; + ///< Throws an exception if GMST is not of type int or float. + + int getFloat() const; + ///< Throws an exception if GMST is not of type int or float. + + std::string getString() const; + ///< Throwns an exception if GMST is not of type string. }; } #endif