From 906432161be312f2f6c51031b164c5855f816363 Mon Sep 17 00:00:00 2001 From: vitaut Date: Fri, 20 Mar 2015 06:31:24 -0700 Subject: [PATCH] Make Buffer part of the public API --- format.h | 33 ++++++++++++++++++++------------- test/util-test.cc | 2 +- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/format.h b/format.h index 576f46f4..6d41d179 100644 --- a/format.h +++ b/format.h @@ -283,7 +283,6 @@ class FormatError : public std::runtime_error { }; namespace internal { - // The number of characters to store in the MemoryBuffer object itself // to avoid dynamic memory allocation. enum { INLINE_BUFFER_SIZE = 500 }; @@ -298,8 +297,9 @@ inline stdext::checked_array_iterator make_ptr(T *ptr, std::size_t size) { template inline T *make_ptr(T *ptr, std::size_t) { return ptr; } #endif +} // namespace internal -// A buffer for POD types. It supports a subset of std::vector's operations. +/** A buffer supporting a subset of ``std::vector``'s operations. */ template class Buffer { private: @@ -313,25 +313,31 @@ class Buffer { Buffer(T *ptr = 0, std::size_t capacity = 0) : ptr_(ptr), size_(0), capacity_(capacity) {} + /** + Increases the buffer capacity to hold at least *size* elements updating + ``ptr_`` and ``capacity_``. + */ virtual void grow(std::size_t size) = 0; public: virtual ~Buffer() {} - // Returns the size of this buffer. + /** Returns the size of this buffer. */ std::size_t size() const { return size_; } - // Returns the capacity of this buffer. + /** Returns the capacity of this buffer. */ std::size_t capacity() const { return capacity_; } - // Resizes the buffer. If T is a POD type new elements are not initialized. + /** + Resizes the buffer. If T is a POD type new elements may not be initialized. + */ void resize(std::size_t new_size) { if (new_size > capacity_) grow(new_size); size_ = new_size; } - // Reserves space to store at least capacity elements. + /** Reserves space to store at least *capacity* elements. */ void reserve(std::size_t capacity) { if (capacity > capacity_) grow(capacity); @@ -345,7 +351,7 @@ class Buffer { ptr_[size_++] = value; } - // Appends data to the end of the buffer. + /** Appends data to the end of the buffer. */ void append(const T *begin, const T *end); T &operator[](std::size_t index) { return ptr_[index]; } @@ -357,10 +363,12 @@ void Buffer::append(const T *begin, const T *end) { std::ptrdiff_t num_elements = end - begin; if (size_ + num_elements > capacity_) grow(size_ + num_elements); - std::copy(begin, end, make_ptr(ptr_, capacity_) + size_); + std::copy(begin, end, internal::make_ptr(ptr_, capacity_) + size_); size_ += num_elements; } +namespace internal { + // A memory buffer for POD types with the first SIZE elements stored in // the object itself. template > @@ -439,10 +447,9 @@ void MemoryBuffer::grow(std::size_t size) { // A fixed-size buffer. template -class FixedBuffer : public fmt::internal::Buffer { +class FixedBuffer : public fmt::Buffer { public: - FixedBuffer(Char *array, std::size_t size) - : fmt::internal::Buffer(array, size) {} + FixedBuffer(Char *array, std::size_t size) : fmt::Buffer(array, size) {} protected: void grow(std::size_t size); @@ -1605,7 +1612,7 @@ template class BasicWriter { private: // Output buffer. - internal::Buffer &buffer_; + Buffer &buffer_; FMT_DISALLOW_COPY_AND_ASSIGN(BasicWriter); @@ -1685,7 +1692,7 @@ class BasicWriter { /** Constructs a ``BasicWriter`` object. */ - explicit BasicWriter(internal::Buffer &b) : buffer_(b) {} + explicit BasicWriter(Buffer &b) : buffer_(b) {} public: /** diff --git a/test/util-test.cc b/test/util-test.cc index 63925287..03fc4272 100644 --- a/test/util-test.cc +++ b/test/util-test.cc @@ -51,7 +51,7 @@ using fmt::StringRef; using fmt::internal::Arg; using fmt::internal::Value; -using fmt::internal::Buffer; +using fmt::Buffer; using fmt::internal::MemoryBuffer; using testing::Return;