Boost logo

Boost :

From: Doug Gregor (gregod_at_[hidden])
Date: 2001-02-27 14:08:41


On Tuesday 27 February 2001 01:49, boost_at_[hidden] wrote:
> Actually, neither of these is what I'm looking for.

Why not supply a traits class to determine what to do when the buffer is
full? Then the decision is left to the user. Here's a rough outline
supporting the three possibilities (throw an exception, increase size, do
nothing):

template<typename T, typename Traits>
class circ_buffer {
// ...
  void push_back(const T& t) {
    if (size() == capacity()) {
      Traits::buffer_overflow(*this);
    }

    // Now go ahead and add the new element
  }
};

// Throw an exception on overflow
class buffer_overflow_error : public std::exception {};

struct exception_on_overflow {
  template<typename T>
  static void buffer_overflow(const circ_buffer<T, exception_on_overflow>&)
  {
    throw buffer_overflow_error();
  }
};

// Double the size on overflow
struct double_on_overflow {
  template<typename T>
  static void buffer_overflow(circ_buffer<T, double_on_overflow>& buf)
  {
    buf.reserve(buf.capacity()*2);
  }
};

// Do nothing on overflow
struct do_nothing_on_overflow {
  template<typename T>
  static void buffer_overflow(const circ_buffer<T, double_on_overflow>&) {}
};

        Doug


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk