DVar.inl
Go to the documentation of this file.
1 namespace iv
2 {
3 
4 template< class Type >
6  cm( inst, this, "DVar" ),
7  inst( inst ),
8  dvs( inst->getSystemContainer()->getSystem< DVarSystem >() ),
9  id( id )
10 {
11 }
12 
13 template< class Type >
15 {
16  if( this->dvs && this->_on_changed )
17  this->dvs->remove_listener( this, this->id );
18 }
19 
20 template< class Type >
22 {
23  return this->inst;
24 }
25 
26 template< class Type >
28 {
29  if( this->dvs && this->id.valid() && this->_on_changed )
30  this->dvs->remove_listener( this, this->id );
31 
32  this->id = id;
33 
34  if( this->dvs && this->id.valid() && this->_on_changed )
35  this->dvs->add_listener( this, this->id );
36 
37  if( this->_on_changed )
38  {
39  this->cached = this->get_uncached();
40  this->_on_changed( this->get() );
41  }
42 }
43 
44 template< class Type >
46 {
47  return this->id;
48 }
49 
50 template< class Type >
52 {
53  if( !this->id.valid() )
54  return Type();
55 
56  if( this->dvs )
57  return this->dvs->get( this->id );
58  else
59  return this->id.initial();
60 }
61 
62 template< class Type >
64 {
65  if( !this->id.valid() )
66  return Type();
67 
68  if( this->_on_changed )
69  return this->cached;
70  else
71  return this->get_uncached();
72 }
73 
74 template< class Type >
75 void DVar< Type >::on_changed( std::function< void( Type const & newval ) > const & fun, bool call_immediately )
76 {
77  if( this->dvs && this->id.valid() )
78  {
79  if( this->_on_changed && !fun )
80  this->dvs->remove_listener( this, this->id );
81 
82  if( fun && !this->_on_changed )
83  this->dvs->add_listener( this, this->id );
84  }
85 
86  if( fun )
87  this->cached = this->get_uncached();
88  else
89  this->cached = Type();
90 
91  this->_on_changed = fun;
92 
93  if( call_immediately && this->id.valid() && this->_on_changed )
94  this->_on_changed( this->get() );
95 }
96 
97 template< class Type >
98 void DVar< Type >::on_dvar_changed( DVarId id, std::type_index type, void * value )
99 {
100  if( this->_on_changed )
101  {
102  if( type != typeid( Type ) )
103  {
104  this->cm.warning( SRC_INFO, "Unexpected DVar type." );
105  return;
106  }
107 
108  this->cached = *reinterpret_cast< Type * >( value );
109  this->_on_changed( this->get() );
110  }
111 }
112 
113 
114 }