My Project
ValueTracker.h
1 #pragma once
2 #include <string>
3 #include <vector>
4 
5 namespace ParaEngine
6 {
7 
13  template <class T>
15  {
16  public:
17  CValueTracker(void)
18  {
19  m_nCurrentPos = 0;
20  SetSize(2); // default size is always 2
21  }
22  ~CValueTracker(void)
23  {
24 
25  }
26 
27  public:
29  void Reset()
30  {
31  int nSize = GetSize();
32  for (int i=0;i<nSize;++i)
33  {
34  m_times[i] = INVALID_TIME;
35  }
36  m_nCurrentPos = 0;
37  }
39  int GetSize()
40  {
41  return m_nSize;
42  }
44  void SetSize(int nSize)
45  {
46  m_nSize = nSize;
47 
48  if(m_nCurrentPos<0 || m_nCurrentPos>=m_nSize)
49  m_nCurrentPos = 0;
50  m_times.resize(m_nSize, INVALID_TIME);
51  m_values.resize(m_nSize);
52  }
53 
54 
60  void Push(int time, const T& value)
61  {
62  ++m_nCurrentPos;
63  if(m_nCurrentPos>=m_nSize)
64  m_nCurrentPos = 0;
65 
66  Update(time, value);
67  }
68 
74  void Update(int time, const T& value)
75  {
76  m_times[m_nCurrentPos] = time;
77  m_values[m_nCurrentPos] = value;
78  }
79 
84  int GetTime(int nPos=0)
85  {
86  nPos = m_nCurrentPos+nPos;
87  if(nPos<0)
88  {
89  nPos += m_nSize;
90  if(nPos>=0)
91  return m_times[nPos];
92  }
93  else if(nPos>=m_nSize)
94  {
95  nPos -= m_nSize;
96  if(nPos>=0)
97  return m_times[nPos];
98  }
99  else
100  {
101  return m_times[nPos];
102  }
103  return INVALID_TIME;
104  }
105 
110  const T& GetValue(int nPos=0)
111  {
112  nPos = m_nCurrentPos+nPos;
113  if(nPos<0)
114  {
115  nPos += m_nSize;
116  if(nPos>=0)
117  return m_values[nPos];
118  }
119  else if(nPos>=m_nSize)
120  {
121  nPos -= m_nSize;
122  if(nPos>=0)
123  return m_values[nPos];
124  }
125  else
126  {
127  return m_values[nPos];
128  }
129  // this should never be called
130  static T g_empty;
131  return g_empty;
132  }
139  bool GetValue(T& value, int nPos)
140  {
141  if(GetTime(nPos) == INVALID_TIME)
142  return false;
143  nPos = m_nCurrentPos+nPos;
144  if(nPos<0)
145  {
146  nPos += m_nSize;
147  if(nPos>=0)
148  {
149  value = m_values[nPos];
150  return true;
151  }
152  }
153  else if(nPos>=m_nSize)
154  {
155  nPos -= m_nSize;
156  if(nPos>=0)
157  {
158  value = m_values[nPos];
159  return true;
160  }
161  }
162  else
163  {
164  value = m_values[nPos];
165  return true;
166  }
167  // no value is found
168  return false;
169  }
170 
171 
178  BOOL CompareWith(const T& right)
179  {
180  if(m_times[m_nCurrentPos] == INVALID_TIME)
181  return false;
182  return (m_values[m_nCurrentPos] == right);
183  }
184 
189  bool IsConstant()
190  {
191  bool bFirstValue = true;
192  bool bConstant = false;
193  T lastValue;
194  for (int i=0;i<m_nSize;++i)
195  {
196  if( m_times[i] != INVALID_TIME)
197  {
198  if(bFirstValue)
199  {
200  lastValue = m_values[i];
201  bConstant = true;
202  bFirstValue = false;
203  }
204  else
205  {
206  if(lastValue != m_values[i])
207  return false;
208  }
209  }
210  else
211  return false;
212  }
213  return bConstant;
214  }
215 
216  private:
217  vector <int> m_times;
218  vector <T> m_values;
219  int m_nCurrentPos;
220  int m_nSize;
221  static const int INVALID_TIME = -1;
222  };
223 
227  class CNetByteArray: public std::vector<byte>
228  {
229  public:
231  bool operator == (const CNetByteArray& right)
232  {
233  return EqualsTo(&(right[0]), (int)right.size());
234  }
235 
237  void SetData(const byte* pData, int nSize)
238  {
239  this->resize(nSize);
240  if(nSize>0)
241  {
242  memcpy(&((*this)[0]), pData, nSize);
243  }
244  }
245 
247  CNetByteArray& operator = (const CNetByteArray& right)
248  {
249  SetData(&(right[0]), (int)right.size());
250  return *this;
251  }
252 
254  bool EqualsTo(const byte* pData, int nSize)
255  {
256  if((int)(this->size()) == nSize)
257  {
258  int i=0;
259  for (;i<nSize && ((*this)[i]==pData[i]);++i)
260  {
261  }
262  return (i==nSize);
263  }
264  else
265  return false;
266  }
267  };
268 
275 }
276 
bool GetValue(T &value, int nPos)
get the value in history.
Definition: ValueTracker.h:139
bool IsConstant()
return whether all values in the tracker are the same.
Definition: ValueTracker.h:189
void Push(int time, const T &value)
push a new item to the tracker.
Definition: ValueTracker.h:60
void SetSize(int nSize)
set how many items to be kept in the tracker
Definition: ValueTracker.h:44
different physics engine has different winding order.
Definition: EventBinding.h:32
const T & GetValue(int nPos=0)
get the value in history.
Definition: ValueTracker.h:110
int GetTime(int nPos=0)
get the time in history.
Definition: ValueTracker.h:84
void Update(int time, const T &value)
update the current (latest) value
Definition: ValueTracker.h:74
void SetData(const byte *pData, int nSize)
set data
Definition: ValueTracker.h:237
BOOL CompareWith(const T &right)
return true if the last value.
Definition: ValueTracker.h:178
Definition: enum_maker.hpp:46
void Reset()
make the tracker empty
Definition: ValueTracker.h:29
int GetSize()
Get how many items to be kept in the tracker.
Definition: ValueTracker.h:39
bool EqualsTo(const byte *pData, int nSize)
compare
Definition: ValueTracker.h:254
a class for tracking the history of values.
Definition: ValueTracker.h:14
for tracking binary data, such as custom model data.
Definition: ValueTracker.h:227