DVarSystem.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "DVarId.hpp"
5 #include <typeindex>
6 
7 namespace iv
8 {
9 
11 {
12 public:
16  virtual void on_dvar_changed( DVarId id, std::type_index type, void * value ) = 0;
17 };
18 
22 class DVarSystem : public System
23 {
24 public:
29  ~DVarSystem();
30 
31  virtual std::string debug_name() const override { return "DVarSystem"; }
32 
36  void lock( Instance * setter, DVarId id );
37 
42  void unlock( Instance * setter, DVarId id );
43 
47  Instance * locked( DVarId id );
48 
53  template< class Type >
54  void set( Instance * setter, DVarIdT< Type > id, Type const & value );
55 
56  template< class Type >
57  Type get( DVarIdT< Type > id );
58 
59  void add_listener( DVarListenerI * listener, DVarId id );
60  void remove_listener( DVarListenerI * listener, DVarId id );
61 
62 private:
63  struct Var
64  {
66  std::type_index type;
67  void * val;
68  std::function< void() > deleter;
69  Instance * locked;
70 
71  Var() : listeners(), type( typeid(void) ), val( nullptr ), deleter(), locked( nullptr ){}
72  };
73 
74  std::unordered_map< DVarId, Var > vars;
75 };
76 
77 }
78 
79 #include "DVarSystem.inl"