cpu: Timebuf const accessors

Add const accessors for timebuf elements.
This commit is contained in:
Andrew Bardsley 2014-05-09 18:58:47 -04:00
parent f7d80348fa
commit 8087d2622d

View file

@ -49,7 +49,7 @@ class TimeBuffer
std::vector<char *> index; std::vector<char *> index;
unsigned base; unsigned base;
void valid(int idx) void valid(int idx) const
{ {
assert (idx >= -past && idx <= future); assert (idx >= -past && idx <= future);
} }
@ -189,7 +189,10 @@ class TimeBuffer
new (index[ptr]) T; new (index[ptr]) T;
} }
T *access(int idx) protected:
//Calculate the index into this->index for element at position idx
//relative to now
inline int calculateVectorIndex(int idx) const
{ {
//Need more complex math here to calculate index. //Need more complex math here to calculate index.
valid(idx); valid(idx);
@ -201,24 +204,31 @@ class TimeBuffer
vector_index += size; vector_index += size;
} }
return vector_index;
}
public:
T *access(int idx)
{
int vector_index = calculateVectorIndex(idx);
return reinterpret_cast<T *>(index[vector_index]); return reinterpret_cast<T *>(index[vector_index]);
} }
T &operator[](int idx) T &operator[](int idx)
{ {
//Need more complex math here to calculate index. int vector_index = calculateVectorIndex(idx);
valid(idx);
int vector_index = idx + base;
if (vector_index >= (int)size) {
vector_index -= size;
} else if (vector_index < 0) {
vector_index += size;
}
return reinterpret_cast<T &>(*index[vector_index]); return reinterpret_cast<T &>(*index[vector_index]);
} }
const T &operator[] (int idx) const
{
int vector_index = calculateVectorIndex(idx);
return reinterpret_cast<const T &>(*index[vector_index]);
}
wire getWire(int idx) wire getWire(int idx)
{ {
valid(idx); valid(idx);