Watch.cpp
Go to the documentation of this file.
1 #include "Watch.hpp"
2 
3 #include "TimeSystem.hpp"
4 
5 namespace iv
6 {
7 
8 Watch::Watch( Instance * inst, TimeId time_type ) :
9  cm( inst, this, "Watch" ),
10  inst( inst ),
11  ts( inst->getSystemContainer()->getSystem< TimeSystem >() ),
12  _time_type( time_type ),
13  delta_cache( 0 ),
14  timeout_it(),
15  timeout_it_valid( false )
16 {
17  if( !this->ts )
18  return;
19 
20  if( !this->ts->is_valid( this->_time_type ) )
21  {
22  this->cm.warning( SRC_INFO, "This TimeId value (", this->_time_type.persistent_value(), ") can not be processed by TimeSystem. TimeId::create on this TimeId value was probably called after TimeSystem was initialized. TimeSystem can be changed to support this at cost of small performance decrease." );
23  this->_time_type = TimeId();
24  }
25 
26  this->delta_cache = this->ts->get_time_ms( this->_time_type );
27 }
28 
30 {
31  return this->inst;
32 }
33 
35 {
36  return this->_time_type;
37 }
38 
40 {
41  if( !this->ts )
42  return;
43 
44  if( !this->ts->is_valid( tt ) )
45  {
46  this->cm.warning( SRC_INFO, "This TimeId value (", this->_time_type.persistent_value(), ") can not be processed by TimeSystem. TimeId::create on this TimeId value was probably called after TimeSystem was initialized. TimeSystem can be changed to support this at cost of small performance decrease." );
47  tt = TimeId();
48  }
49 
50  // adjust delta_cache to new time (so that next call to Watch::delta_ms gives sane value)
51  int cache_change = this->ts->get_time_ms( this->_time_type ) - this->delta_cache;
52  double real_cache_change = cache_change / this->ts->speed( this->_time_type );
53  int new_cache_change = real_cache_change * this->ts->speed( tt );
54  this->delta_cache = this->ts->get_time_ms( tt ) - new_cache_change;
55 
56  // set new
57  this->_time_type = tt;
58 }
59 
61 {
62  if( this->ts )
63  return this->ts->get_time_ms( this->_time_type );
64  else
65  return 0;
66 }
67 
69 {
70  if( this->ts )
71  {
72  int time = this->ts->get_time_ms( this->_time_type );
73  int result = time - this->delta_cache;
74  this->delta_cache = time;
75  return result;
76  }
77 
78  return 0;
79 }
80 
81 void Watch::timeout( int delta_ms, std::function< void() > const & fun )
82 {
83  if( this->ts )
84  this->ts->timeout( this, this->_time_type, this->ts->get_time_ms( this->_time_type ) + delta_ms, fun );
85 }
86 
87 void Watch::timeout_unique( int delta_ms, std::function< void() > const & fun )
88 {
89  if( !this->ts )
90  return;
91 
92  if( this->timeout_it_valid )
93  {
94  this->timeout_it_valid = false;
95  this->ts->cancel_timeout( this->_time_type, this->timeout_it );
96  }
97 
98  this->timeout_it_valid = true;
99  this->timeout_it = this->ts->timeout( this, this->_time_type, this->ts->get_time_ms( this->_time_type ) + delta_ms,
100  [ this, fun ]()
101  {
102  this->timeout_it_valid = false;
103  fun();
104  }
105  );
106 }
107 
109 {
110  if( !this->ts )
111  return;
112 
113  if( this->timeout_it_valid )
114  {
115  this->timeout_it_valid = false;
116  this->ts->cancel_timeout( this->_time_type, this->timeout_it );
117  }
118 }
119 
120 void Watch::delay( std::function< void() > const & fun )
121 {
122  if( this->ts )
123  this->ts->delay( this, fun );
124 }
125 
127 {
128  if( this->ts )
129  return this->ts->get_time_to_next_frame_ms( this->_time_type );
130  else
131  return 1;
132 }
133 
134 }