]> git.tdb.fi Git - libs/game.git/blobdiff - source/game/accessguard.h
Decorate things which constitute the public API of the library
[libs/game.git] / source / game / accessguard.h
index c64efe75bee9de0ae04427f3b3589b295b8d6fdd..2d9a3053b8324a8e06abdb8713f8469bd5907d84 100644 (file)
@@ -6,21 +6,25 @@
 #include <string>
 #include <vector>
 #include <msp/core/noncopyable.h>
+#include <msp/debug/demangle.h>
+#include "mspgame_api.h"
 
 namespace Msp::Game {
 
-class invalid_access: public std::logic_error
+class MSPGAME_API invalid_access: public std::logic_error
 {
 public:
        invalid_access(const std::string &w): logic_error(w) { }
 };
 
 
-class AccessGuard
+class MSPGAME_API AccessGuard
 {
 public:
        struct Create { static std::string describe() { return "create"; } };
        struct Destroy { static std::string describe() { return "destroy"; } };
+       template<typename T> struct Read { static std::string describe(); };
+       template<typename T> struct Write { static std::string describe(); };
 
        template<typename T = void>
        struct BlockForScope: NonCopyable
@@ -66,7 +70,7 @@ public:
        void unblock() { get<T>() = UNBLOCKED; }
 
        template<typename T>
-       void check() { if(get<T>()!=UNBLOCKED) throw invalid_access(T::describe()); }
+       void check();
 };
 
 
@@ -94,6 +98,27 @@ template<>
 inline void AccessGuard::unblock<void>()
 { unblock_all(); }
 
+template<typename T>
+inline void AccessGuard::check()
+{
+       unsigned index = get_index<T>();
+       if((index<flags.size() ? flags[index] : default_flag) != UNBLOCKED)
+               throw invalid_access(T::describe());
+}
+
+
+template<typename T>
+inline std::string AccessGuard::Read<T>::describe()
+{
+       return "read "+Debug::demangle(typeid(T).name());
+}
+
+template<typename T>
+inline std::string AccessGuard::Write<T>::describe()
+{
+       return "write "+Debug::demangle(typeid(T).name());
+}
+
 } // namespace Msp::Game
 
 #endif