LunaSysMgr
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
CircularBuffer.h
Go to the documentation of this file.
1 /* @@@LICENSE
2 *
3 * Copyright (c) 2010-2012 Hewlett-Packard Development Company, L.P.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 * LICENSE@@@ */
18 
19 
20 
21 
22 #ifndef CIRCULARBUFFER_H_
23 #define CIRCULARBUFFER_H_
24 
25 
26 template<typename T>
28 {
30  Buffer(NULL)
31  {
32  }
33 
35  Buffer(NULL)
36  {
37  Reset(BufferSize);
38  }
39 
41  {
42  if (Buffer) {
43  delete [] Buffer;
44  }
45  }
46 
47  void Reset(int BufferSize = 0)
48  {
49  if (BufferSize) {
50  if (Buffer && BufferSize != this->BufferSize) {
51  delete [] Buffer;
52  Buffer = NULL;
53  }
54  this->BufferSize = BufferSize;
55  if (!Buffer) {
56  Buffer = new Sample[BufferSize];
57  }
58  }
59  CurrentIndex = 0;
60  SampleCount = 0;
61  }
62 
63  void AddSample(const T& Item, long long int Time)
64  {
65  if(Buffer) {
66  if (++CurrentIndex >= BufferSize) {
67  CurrentIndex = 0;
68  }
69  Buffer[CurrentIndex].Data = Item;
70  Buffer[CurrentIndex].Time = Time;
71  SampleCount++;
72  }
73  }
74 
75  const T& LastSample()
76  {
77  if (!Buffer)
78  return NULL;
79 
80  return Buffer[CurrentIndex].Data;
81  }
82 
83 protected:
84  struct Sample {
85  T Data;
86  long long int Time;
87  };
88 
89  Sample* Buffer;
90  int* TimeStamp;
94 };
95 
96 #endif /*CIRCULARBUFFER_H_*/