#ifndef __pxx_buffer_ih__ #define __pxx_buffer_ih__ #include "pxx_buffer.hh" #include "pxx_default_allocator.ih" namespace pxx { inline Buffer::Buffer (size_t _size /* = default_size */) : capacity (_size), length (0), unget_capacity (initial_unget_capacity), unget_length (0), associated (null) { cursor = buf = static_cast(default_allocator.allocate(capacity)); unget_buf = static_cast(default_allocator.allocate(unget_capacity)); } inline Buffer::~Buffer () { if (associated != null) associated->associated = null; default_allocator.deallocate(unget_buf); default_allocator.deallocate(buf); } inline void Buffer::put (char _c) { if (cursor >= buf + capacity) write(); *cursor++ = _c; length++; } inline bool Buffer::get (char& _c) { if (unget_length > 0) { _c = unget_buf[--unget_length]; return true; } if (cursor >= buf + length) { size_t n = read(); if (n == 0) { return false; } } _c = *cursor++; return true; } inline void Buffer::unget (char _c) { if (unget_length >= unget_capacity) { unget_capacity <<= 1; unget_buf = static_cast( default_allocator.reallocate(unget_buf, unget_capacity) ); } unget_buf[unget_length++] = _c; } inline void Buffer::group (Buffer& _b1, Buffer& _b2) { _b1.associated = &_b2; _b2.associated = &_b1; } } #endif // __pxx_buffer_ih__