Template parameters
Having seen the example about a Stack with a fixed size I thought to
apply the same approach to my RingBuffer implementation, which became
#ifndef RINGBUFFER_H
#define RINGBUFFER_H
#include <ostream>
#include <vector>
#include <deque>
template<typename T, size_t Size>
class RingBuffer
{
private:
size_t max_size;
protected:
std::deque<T> buffer;
bool isFull() {
return (buffer.size() == Size);
}
public:
void push(T el) {
if (isFull()) {
buffer.pop_front();
}
buffer.push_back(el);
}
T operator[](int index) { return buffer[index]; }
// usual short circuiting algorithm to check for membership
bool hasMember(T el) {
typename std::deque<T>::iterator it;
for (it=buffer.begin(); it!=buffer.end(); ++it) {
if ((*it) == el)
return true;
}
return true;
}
friend std::ostream& operator<<(std::ostream& s, const RingBuffer& t) {
typename std::deque<T>::const_iterator it;
for (it=t.buffer.begin(); it!=t.buffer.end(); ++it)
s << (*it) << " ";
return s;
}
};
#endif /* RINGBUFFER_H */
Everything works fine if the size is a "magic number" but what I wanted
to do is that the size is computed at run-time.
But something like this below doesn't work...
#include <iostream>
#include "myring.hpp"
int fun() {
return 10;
}
const int size = fun();
class BufInt : public RingBuffer<int, size>
{
};
int main() {
BufInt b;
b.push(10);
std::cout << b << std::endl;
return 0;
}
I guess that the value is not known at compile time so it's not
accepted...
Is there a way to make it work or I can just drop the idea and continue
to use in the "older" way?