Public Types | Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | List of all members
basic_memory_buffer< T, SIZE, Allocator > Class Template Reference

#include <format.h>

Inheritance diagram for basic_memory_buffer< T, SIZE, Allocator >:
internal::basic_buffer< T >

Public Types

typedef T value_type
 
typedef const T & const_reference
 
- Public Types inherited from internal::basic_buffer< T >
typedef T value_type
 
typedef const T & const_reference
 

Public Member Functions

 basic_memory_buffer (const Allocator &alloc=Allocator())
 
 ~basic_memory_buffer ()
 
 basic_memory_buffer (basic_memory_buffer &&other)
 
basic_memory_bufferoperator= (basic_memory_buffer &&other)
 
Allocator get_allocator () const
 
- Public Member Functions inherited from internal::basic_buffer< T >
virtual ~basic_buffer ()
 
T * begin () FMT_NOEXCEPT
 
T * end () FMT_NOEXCEPT
 
std::size_t size () const FMT_NOEXCEPT
 
std::size_t capacity () const FMT_NOEXCEPT
 
T * data () FMT_NOEXCEPT
 
const T * data () const FMT_NOEXCEPT
 
void resize (std::size_t new_size)
 
void clear ()
 
void reserve (std::size_t new_capacity)
 
void push_back (const T &value)
 
template<typename U >
void append (const U *begin, const U *end)
 
T & operator[] (std::size_t index)
 
const T & operator[] (std::size_t index) const
 

Protected Member Functions

void grow (std::size_t size) FMT_OVERRIDE
 
- Protected Member Functions inherited from internal::basic_buffer< T >
 basic_buffer (std::size_t sz) FMT_NOEXCEPT
 
 basic_buffer (T *p=FMT_NULL, std::size_t sz=0, std::size_t cap=0) FMT_NOEXCEPT
 
void set (T *buf_data, std::size_t buf_capacity) FMT_NOEXCEPT
 

Private Member Functions

void deallocate ()
 
void move (basic_memory_buffer &other)
 

Private Attributes

store_ [SIZE]
 

Detailed Description

template<typename T, std::size_t SIZE = inline_buffer_size, typename Allocator = std::allocator<T>>
class basic_memory_buffer< T, SIZE, Allocator >

A dynamically growing memory buffer for trivially copyable/constructible types with the first SIZE elements stored in the object itself.

You can use one of the following typedefs for common character types:

+-------------—+---------------------------—+ | Type | Definition | +================+==============================+ | memory_buffer | basic_memory_buffer<char> | +-------------—+---------------------------—+ | wmemory_buffer | basic_memory_buffer<wchar_t> | +-------------—+---------------------------—+

Example**::

fmt::memory_buffer out; format_to(out, "The answer is {}.", 42);

This will append the following output to the out object:

.. code-block:: none

The answer is 42.

The output can be converted to an std::string with to_string(out).

Definition at line 464 of file format.h.

Member Typedef Documentation

template<typename T, std::size_t SIZE = inline_buffer_size, typename Allocator = std::allocator<T>>
typedef const T& basic_memory_buffer< T, SIZE, Allocator >::const_reference

Definition at line 479 of file format.h.

template<typename T, std::size_t SIZE = inline_buffer_size, typename Allocator = std::allocator<T>>
typedef T basic_memory_buffer< T, SIZE, Allocator >::value_type

Definition at line 478 of file format.h.

Constructor & Destructor Documentation

template<typename T, std::size_t SIZE = inline_buffer_size, typename Allocator = std::allocator<T>>
basic_memory_buffer< T, SIZE, Allocator >::basic_memory_buffer ( const Allocator &  alloc = Allocator())
inlineexplicit

Definition at line 481 of file format.h.

482  : Allocator(alloc) {
483  this->set(store_, SIZE);
484  }
T store_[SIZE]
Definition: format.h:466
template<typename T, std::size_t SIZE = inline_buffer_size, typename Allocator = std::allocator<T>>
basic_memory_buffer< T, SIZE, Allocator >::~basic_memory_buffer ( )
inline

Definition at line 485 of file format.h.

485 { deallocate(); }
void deallocate()
Definition: format.h:469
template<typename T, std::size_t SIZE = inline_buffer_size, typename Allocator = std::allocator<T>>
basic_memory_buffer< T, SIZE, Allocator >::basic_memory_buffer ( basic_memory_buffer< T, SIZE, Allocator > &&  other)
inline

Constructs a :class:fmt::basic_memory_buffer object moving the content of the other object to it.

Definition at line 514 of file format.h.

514  {
515  move(other);
516  }
void move(basic_memory_buffer &other)
Definition: format.h:489

Member Function Documentation

template<typename T, std::size_t SIZE = inline_buffer_size, typename Allocator = std::allocator<T>>
void basic_memory_buffer< T, SIZE, Allocator >::deallocate ( )
inlineprivate

Definition at line 469 of file format.h.

469  {
470  T* data = this->data();
471  if (data != store_) Allocator::deallocate(data, this->capacity());
472  }
T * data() FMT_NOEXCEPT
Definition: core.h:256
std::size_t capacity() const FMT_NOEXCEPT
Definition: core.h:253
T store_[SIZE]
Definition: format.h:466
template<typename T, std::size_t SIZE = inline_buffer_size, typename Allocator = std::allocator<T>>
Allocator basic_memory_buffer< T, SIZE, Allocator >::get_allocator ( ) const
inline

Definition at line 531 of file format.h.

531 { return *this; }
template<typename T , std::size_t SIZE, typename Allocator >
void basic_memory_buffer< T, SIZE, Allocator >::grow ( std::size_t  capacity)
protectedvirtual

Increases the buffer capacity to hold at least capacity elements.

Implements internal::basic_buffer< T >.

Definition at line 535 of file format.h.

535  {
536  std::size_t old_capacity = this->capacity();
537  std::size_t new_capacity = old_capacity + old_capacity / 2;
538  if (size > new_capacity)
539  new_capacity = size;
540  T *old_data = this->data();
541  T *new_data = internal::allocate<Allocator>(*this, new_capacity);
542  // The following code doesn't throw, so the raw pointer above doesn't leak.
543  std::uninitialized_copy(old_data, old_data + this->size(),
544  internal::make_checked(new_data, new_capacity));
545  this->set(new_data, new_capacity);
546  // deallocate must not throw according to the standard, but even if it does,
547  // the buffer already uses the new storage and will deallocate it in
548  // destructor.
549  if (old_data != store_)
550  Allocator::deallocate(old_data, old_capacity);
551 }
T * data() FMT_NOEXCEPT
Definition: core.h:256
std::size_t size() const FMT_NOEXCEPT
Definition: core.h:250
std::size_t capacity() const FMT_NOEXCEPT
Definition: core.h:253
T * make_checked(T *p, std::size_t)
Definition: format.h:389
T store_[SIZE]
Definition: format.h:466
template<typename T, std::size_t SIZE = inline_buffer_size, typename Allocator = std::allocator<T>>
void basic_memory_buffer< T, SIZE, Allocator >::move ( basic_memory_buffer< T, SIZE, Allocator > &  other)
inlineprivate

Definition at line 489 of file format.h.

489  {
490  Allocator &this_alloc = *this, &other_alloc = other;
491  this_alloc = std::move(other_alloc);
492  T* data = other.data();
493  std::size_t size = other.size(), capacity = other.capacity();
494  if (data == other.store_) {
495  this->set(store_, capacity);
496  std::uninitialized_copy(other.store_, other.store_ + size,
498  } else {
499  this->set(data, capacity);
500  // Set pointer to the inline array so that delete is not called
501  // when deallocating.
502  other.set(other.store_, 0);
503  }
504  this->resize(size);
505  }
void resize(std::size_t new_size)
Definition: core.h:264
T * data() FMT_NOEXCEPT
Definition: core.h:256
def move(depos, offset)
Definition: depos.py:107
std::size_t size() const FMT_NOEXCEPT
Definition: core.h:250
std::size_t capacity() const FMT_NOEXCEPT
Definition: core.h:253
T * make_checked(T *p, std::size_t)
Definition: format.h:389
T store_[SIZE]
Definition: format.h:466
void set(T *buf_data, std::size_t buf_capacity) FMT_NOEXCEPT
Definition: core.h:232
template<typename T, std::size_t SIZE = inline_buffer_size, typename Allocator = std::allocator<T>>
basic_memory_buffer& basic_memory_buffer< T, SIZE, Allocator >::operator= ( basic_memory_buffer< T, SIZE, Allocator > &&  other)
inline

Moves the content of the other basic_memory_buffer object to this one.

Definition at line 523 of file format.h.

523  {
524  assert(this != &other);
525  deallocate();
526  move(other);
527  return *this;
528  }
void deallocate()
Definition: format.h:469
void move(basic_memory_buffer &other)
Definition: format.h:489

Member Data Documentation

template<typename T, std::size_t SIZE = inline_buffer_size, typename Allocator = std::allocator<T>>
T basic_memory_buffer< T, SIZE, Allocator >::store_[SIZE]
private

Definition at line 466 of file format.h.


The documentation for this class was generated from the following file: