]> git.tdb.fi Git - libs/game.git/blobdiff - source/game/component.h
Enforce correct access to buffered components
[libs/game.git] / source / game / component.h
index 44927ffd2e73c640f9d8a609f4e4b66984633572..6406cb5743e3f417f0cb7f27a08c92e439aecef8 100644 (file)
@@ -2,6 +2,7 @@
 #define MSP_GAME_COMPONENT_H_
 
 #include <msp/time/timedelta.h>
+#include "accessguard.h"
 #include "handle.h"
 
 namespace Msp::Game {
@@ -34,7 +35,7 @@ protected:
 
        BufferedComponent(Handle<Entity> e): Component(e) { }
 
-       const T &read() const { return data[read_index]; }
+       const T &read() const;
        T &write();
 
 public:
@@ -42,9 +43,21 @@ public:
        virtual void commit_tick() { if(written) read_index = write_index; }
 };
 
+template<typename T>
+const T &BufferedComponent<T>::read() const
+{
+#ifdef DEBUG
+       AccessGuard::get_instance().check<AccessGuard::Read<T>>();
+#endif
+       return data[read_index];
+}
+
 template<typename T>
 T &BufferedComponent<T>::write()
 {
+#ifdef DEBUG
+       AccessGuard::get_instance().check<AccessGuard::Write<T>>();
+#endif
        if(!written && write_index!=read_index)
        {
                data[write_index] = data[read_index];