Template parameters

A

Andrea Crotti

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::eek:stream& operator<<(std::eek:stream& 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?
 
A

Andrea Crotti

Andrea Crotti said:
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::eek:stream& operator<<(std::eek:stream& 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?

Otherwise I would have solved adding something like:

RingBuffer() : max_size(0) {};
RingBuffer(size_t _max_size) : max_size(_max_size) {};

void setSize(size_t _max_size) { max_size = _max_size; }

bool initialized() {
return (max_size == 0);
}

And then I have to do something like
if (! buf.initialized()) {
buf.setSize(10);
}

for example, which is not that great but it should work...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top