DVarProperty.inl
Go to the documentation of this file.
1 namespace iv
2 {
3 
4 template< class T >
6  cm( inst, this, "DVarProperty<>" ),
7  inst( inst ),
8  dvs( inst->getSystem< DVarSystem >() ),
9  _dvar_id( id ),
10  _on_changed(),
11  _explicit_value()
12 {
13  if( this->dvs )
14  {
15  this->_cached_value = this->dvs->template get< T >( this->_dvar_id );
16  this->dvs->add_listener( this, this->_dvar_id );
17  }
18  else
19  {
20  this->_cached_value = this->_dvar_id.initial();
21  }
22 }
23 
24 template< class T >
26 {
27  if( this->dvs )
28  this->dvs->remove_listener( this, this->_dvar_id );
29 }
30 
31 template< class T >
33 {
34  return this->inst;
35 }
36 
37 template< class T >
38 void DVarProperty< T >::value( std::optional< T > const & val )
39 {
40  this->_explicit_value = val;
41  if( this->_on_changed )
42  this->_on_changed( this->value() );
43 }
44 
45 template< class T >
47 {
48  if( this->_explicit_value.has_value() )
49  return this->_explicit_value.value();
50  else
51  return this->_cached_value;
52 }
53 
54 template< class T >
55 void DVarProperty< T >::on_dvar_changed( DVarId id, std::type_index type, void * value )
56 {
57  if( type != typeid( T ) )
58  {
59  this->cm.warning( SRC_INFO, "Wrong type of DVar." );
60  return;
61  }
62 
63  this->_cached_value = *reinterpret_cast< T * >( value );
64 
65  if( this->_on_changed )
66  this->_on_changed( this->value() );
67 }
68 
69 template< class T >
70 void DVarProperty< T >::on_changed( std::function< void( T const & newval ) > const & fun, bool call_immediately )
71 {
72  this->_on_changed = fun;
73  if( call_immediately && this->_on_changed )
74  this->_on_changed( this->value() );
75 }
76 
77 }