DVarSystem.inl
Go to the documentation of this file.
1 namespace iv
2 {
3 
4 template< class Type >
5 void DVarSystem::set( Instance * setter, DVarIdT< Type > id, Type const & value )
6 {
7  Var & var = this->vars[ id ];
8 
9  if( var.locked && var.locked != setter )
10  {
11  this->warning( SRC_INFO, "DVar ", id, " is locked to an instance. Can not change it using different setter instance." );
12  return;
13  }
14 
15  if( !var.val )
16  {
17  auto val = new Type;
18  var.val = val;
19  var.type = typeid( Type );
20  var.deleter = [ val ]()
21  {
22  delete val;
23  };
24  }
25 
26  if( var.type != typeid( Type ) )
27  {
28  this->warning( SRC_INFO, "DVar ", id, " was already set with different type. Skipping this set." );
29  return;
30  }
31 
32  ( *reinterpret_cast< Type * >( var.val ) ) = value;
33 
34  var.listeners.foreach(
35  [&]( DVarListenerI * listener )
36  {
37  listener->on_dvar_changed( id, var.type, var.val );
38  }
39  );
40 }
41 
42 template< class Type >
44 {
45  Var & var = this->vars[ id ];
46  if( !var.val )
47  {
48  return id.initial();
49  }
50 
51  std::type_index type = typeid( Type );
52  if( type != var.type )
53  {
54  this->warning( SRC_INFO, "Trying to get DVar value using id of different type than with which it was set. There are probably two ids with the same persistent id '", id, "'." );
55  return id.initial();
56  }
57 
58  return *reinterpret_cast< Type * >( var.val );
59 }
60 
61 
62 
63 }