proxygen
folly::TypedIOBuf< T > Class Template Reference

#include <TypedIOBuf.h>

Public Types

typedef T value_type
 
typedef value_typereference
 
typedef const value_typeconst_reference
 
typedef uint32_t size_type
 
typedef value_typeiterator
 
typedef const value_typeconst_iterator
 

Public Member Functions

 TypedIOBuf (IOBuf *buf)
 
IOBufioBuf ()
 
const IOBufioBuf () const
 
bool empty () const
 
const Tdata () const
 
TwritableData ()
 
const Ttail () const
 
TwritableTail ()
 
uint32_t length () const
 
uint32_t size () const
 
uint32_t headroom () const
 
uint32_t tailroom () const
 
const Tbuffer () const
 
TwritableBuffer ()
 
const TbufferEnd () const
 
uint32_t capacity () const
 
void advance (uint32_t n)
 
void retreat (uint32_t n)
 
void prepend (uint32_t n)
 
void append (uint32_t n)
 
void trimStart (uint32_t n)
 
void trimEnd (uint32_t n)
 
void clear ()
 
void reserve (uint32_t minHeadroom, uint32_t minTailroom)
 
void reserve (uint32_t minTailroom)
 
const Tcbegin () const
 
const Tcend () const
 
const Tbegin () const
 
const Tend () const
 
Tbegin ()
 
Tend ()
 
const Tfront () const
 
Tfront ()
 
const Tback () const
 
Tback ()
 
const Toperator[] (ssize_t idx) const
 
Toperator[] (ssize_t idx)
 
void push (const T &data)
 
void push_back (const T &data)
 
template<class IT >
void push (IT begin, IT end)
 
 TypedIOBuf (TypedIOBuf &&)=default
 
TypedIOBufoperator= (TypedIOBuf &&)=default
 

Private Member Functions

 TypedIOBuf (const TypedIOBuf &)=delete
 
TypedIOBufoperator= (const TypedIOBuf &)=delete
 

Static Private Member Functions

static Tcast (uint8_t *p)
 
static const Tcast (const uint8_t *p)
 
static uint32_t sdiv (uint32_t n)
 
static uint32_t smul (uint32_t n)
 

Private Attributes

IOBufbuf_
 

Detailed Description

template<class T>
class folly::TypedIOBuf< T >

Wrapper class to handle a IOBuf as a typed buffer (to a standard layout class).

This class punts on alignment, and assumes that you know what you're doing.

All methods are wrappers around the corresponding IOBuf methods. The TypedIOBuf object is stateless, so it's perfectly okay to access the underlying IOBuf in between TypedIOBuf method calls.

Definition at line 39 of file TypedIOBuf.h.

Member Typedef Documentation

template<class T>
typedef const value_type* folly::TypedIOBuf< T >::const_iterator

Definition at line 48 of file TypedIOBuf.h.

template<class T>
typedef const value_type& folly::TypedIOBuf< T >::const_reference

Definition at line 45 of file TypedIOBuf.h.

template<class T>
typedef value_type* folly::TypedIOBuf< T >::iterator

Definition at line 47 of file TypedIOBuf.h.

template<class T>
typedef value_type& folly::TypedIOBuf< T >::reference

Definition at line 44 of file TypedIOBuf.h.

template<class T>
typedef uint32_t folly::TypedIOBuf< T >::size_type

Definition at line 46 of file TypedIOBuf.h.

template<class T>
typedef T folly::TypedIOBuf< T >::value_type

Definition at line 40 of file TypedIOBuf.h.

Constructor & Destructor Documentation

template<class T>
folly::TypedIOBuf< T >::TypedIOBuf ( IOBuf buf)
inlineexplicit

Definition at line 50 of file TypedIOBuf.h.

Referenced by folly::TypedIOBuf< T >::push().

50 : buf_(buf) {}
template<class T>
folly::TypedIOBuf< T >::TypedIOBuf ( TypedIOBuf< T > &&  )
default
template<class T>
folly::TypedIOBuf< T >::TypedIOBuf ( const TypedIOBuf< T > &  )
privatedelete

Member Function Documentation

template<class T>
void folly::TypedIOBuf< T >::advance ( uint32_t  n)
inline

Definition at line 99 of file TypedIOBuf.h.

References folly::IOBuf::advance(), folly::TypedIOBuf< T >::buf_, and folly::TypedIOBuf< T >::smul().

99  {
100  buf_->advance(smul(n));
101  }
static uint32_t smul(uint32_t n)
Definition: TypedIOBuf.h:225
void advance(std::size_t amount)
Definition: IOBuf.h:632
template<class T>
void folly::TypedIOBuf< T >::append ( uint32_t  n)
inline

Definition at line 108 of file TypedIOBuf.h.

References folly::IOBuf::append(), folly::TypedIOBuf< T >::buf_, and folly::TypedIOBuf< T >::smul().

Referenced by folly::TypedIOBuf< T >::push().

108  {
109  buf_->append(smul(n));
110  }
static uint32_t smul(uint32_t n)
Definition: TypedIOBuf.h:225
void append(std::size_t amount)
Definition: IOBuf.h:689
template<class T>
const T& folly::TypedIOBuf< T >::back ( ) const
inline

Definition at line 154 of file TypedIOBuf.h.

References folly::TypedIOBuf< T >::empty(), and folly::TypedIOBuf< T >::end().

154  {
155  assert(!empty());
156  return end()[-1];
157  }
bool empty() const
Definition: TypedIOBuf.h:59
const T * end() const
Definition: TypedIOBuf.h:136
template<class T>
T& folly::TypedIOBuf< T >::back ( )
inline

Definition at line 158 of file TypedIOBuf.h.

References folly::TypedIOBuf< T >::empty(), and folly::TypedIOBuf< T >::end().

158  {
159  assert(!empty());
160  return end()[-1];
161  }
bool empty() const
Definition: TypedIOBuf.h:59
const T * end() const
Definition: TypedIOBuf.h:136
template<class T>
const T* folly::TypedIOBuf< T >::begin ( ) const
inline

Definition at line 133 of file TypedIOBuf.h.

References folly::TypedIOBuf< T >::cbegin().

Referenced by folly::TypedIOBuf< T >::front().

133  {
134  return cbegin();
135  }
const T * cbegin() const
Definition: TypedIOBuf.h:127
template<class T>
T* folly::TypedIOBuf< T >::begin ( )
inline

Definition at line 139 of file TypedIOBuf.h.

References folly::TypedIOBuf< T >::writableData().

139  {
140  return writableData();
141  }
template<class T>
const T* folly::TypedIOBuf< T >::buffer ( ) const
inline

Definition at line 87 of file TypedIOBuf.h.

References folly::TypedIOBuf< T >::buf_, folly::IOBuf::buffer(), and folly::TypedIOBuf< T >::cast().

87  {
88  return cast(buf_->buffer());
89  }
static T * cast(uint8_t *p)
Definition: TypedIOBuf.h:214
const uint8_t * buffer() const
Definition: IOBuf.h:562
template<class T>
const T* folly::TypedIOBuf< T >::bufferEnd ( ) const
inline

Definition at line 93 of file TypedIOBuf.h.

References folly::TypedIOBuf< T >::buf_, folly::IOBuf::bufferEnd(), and folly::TypedIOBuf< T >::cast().

93  {
94  return cast(buf_->bufferEnd());
95  }
static T * cast(uint8_t *p)
Definition: TypedIOBuf.h:214
const uint8_t * bufferEnd() const
Definition: IOBuf.h:583
template<class T>
uint32_t folly::TypedIOBuf< T >::capacity ( ) const
inline

Definition at line 96 of file TypedIOBuf.h.

References folly::TypedIOBuf< T >::buf_, folly::IOBuf::capacity(), and folly::TypedIOBuf< T >::sdiv().

Referenced by TEST().

96  {
97  return sdiv(buf_->capacity());
98  }
std::size_t capacity() const
Definition: IOBuf.h:593
static uint32_t sdiv(uint32_t n)
Definition: TypedIOBuf.h:221
template<class T>
static T* folly::TypedIOBuf< T >::cast ( uint8_t p)
inlinestaticprivate
template<class T>
static const T* folly::TypedIOBuf< T >::cast ( const uint8_t p)
inlinestaticprivate

Definition at line 217 of file TypedIOBuf.h.

References folly::T.

217  {
218  return reinterpret_cast<const T*>(p);
219  }
folly::std T
template<class T>
const T* folly::TypedIOBuf< T >::cbegin ( ) const
inline

Definition at line 127 of file TypedIOBuf.h.

References folly::TypedIOBuf< T >::data().

Referenced by folly::TypedIOBuf< T >::begin().

127  {
128  return data();
129  }
const T * data() const
Definition: TypedIOBuf.h:62
template<class T>
const T* folly::TypedIOBuf< T >::cend ( ) const
inline

Definition at line 130 of file TypedIOBuf.h.

References folly::TypedIOBuf< T >::tail().

Referenced by folly::TypedIOBuf< T >::end().

130  {
131  return tail();
132  }
const T * tail() const
Definition: TypedIOBuf.h:68
template<class T>
void folly::TypedIOBuf< T >::clear ( )
inline

Definition at line 117 of file TypedIOBuf.h.

References folly::TypedIOBuf< T >::buf_, and folly::IOBuf::clear().

117  {
118  buf_->clear();
119  }
void clear()
Definition: IOBuf.h:728
template<class T>
const T* folly::TypedIOBuf< T >::data ( ) const
inline

Definition at line 62 of file TypedIOBuf.h.

References folly::TypedIOBuf< T >::buf_, folly::TypedIOBuf< T >::cast(), and folly::IOBuf::data().

Referenced by folly::TypedIOBuf< T >::cbegin(), and folly::TypedIOBuf< T >::operator[]().

62  {
63  return cast(buf_->data());
64  }
static T * cast(uint8_t *p)
Definition: TypedIOBuf.h:214
const uint8_t * data() const
Definition: IOBuf.h:499
template<class T>
bool folly::TypedIOBuf< T >::empty ( ) const
inline

Definition at line 59 of file TypedIOBuf.h.

References folly::TypedIOBuf< T >::buf_, and folly::IOBuf::empty().

Referenced by folly::TypedIOBuf< T >::back(), and folly::TypedIOBuf< T >::front().

59  {
60  return buf_->empty();
61  }
bool empty() const
Definition: IOBuf.cpp:482
template<class T>
const T* folly::TypedIOBuf< T >::end ( ) const
inline

Definition at line 136 of file TypedIOBuf.h.

References folly::TypedIOBuf< T >::cend().

Referenced by folly::TypedIOBuf< T >::back().

136  {
137  return cend();
138  }
const T * cend() const
Definition: TypedIOBuf.h:130
template<class T>
T* folly::TypedIOBuf< T >::end ( )
inline

Definition at line 142 of file TypedIOBuf.h.

References folly::TypedIOBuf< T >::writableTail().

142  {
143  return writableTail();
144  }
template<class T>
const T& folly::TypedIOBuf< T >::front ( ) const
inline

Definition at line 146 of file TypedIOBuf.h.

References folly::TypedIOBuf< T >::begin(), and folly::TypedIOBuf< T >::empty().

146  {
147  assert(!empty());
148  return *begin();
149  }
bool empty() const
Definition: TypedIOBuf.h:59
const T * begin() const
Definition: TypedIOBuf.h:133
template<class T>
T& folly::TypedIOBuf< T >::front ( )
inline

Definition at line 150 of file TypedIOBuf.h.

References folly::TypedIOBuf< T >::begin(), and folly::TypedIOBuf< T >::empty().

150  {
151  assert(!empty());
152  return *begin();
153  }
bool empty() const
Definition: TypedIOBuf.h:59
const T * begin() const
Definition: TypedIOBuf.h:133
template<class T>
uint32_t folly::TypedIOBuf< T >::headroom ( ) const
inline

Definition at line 81 of file TypedIOBuf.h.

References folly::TypedIOBuf< T >::buf_, folly::IOBuf::headroom(), and folly::TypedIOBuf< T >::sdiv().

Referenced by folly::TypedIOBuf< T >::push().

81  {
82  return sdiv(buf_->headroom());
83  }
std::size_t headroom() const
Definition: IOBuf.h:542
static uint32_t sdiv(uint32_t n)
Definition: TypedIOBuf.h:221
template<class T>
IOBuf* folly::TypedIOBuf< T >::ioBuf ( )
inline

Definition at line 52 of file TypedIOBuf.h.

References folly::TypedIOBuf< T >::buf_.

52  {
53  return buf_;
54  }
template<class T>
const IOBuf* folly::TypedIOBuf< T >::ioBuf ( ) const
inline

Definition at line 55 of file TypedIOBuf.h.

References folly::TypedIOBuf< T >::buf_.

55  {
56  return buf_;
57  }
template<class T>
uint32_t folly::TypedIOBuf< T >::length ( ) const
inline

Definition at line 74 of file TypedIOBuf.h.

References folly::TypedIOBuf< T >::buf_, folly::IOBuf::length(), and folly::TypedIOBuf< T >::sdiv().

Referenced by folly::TypedIOBuf< T >::operator[](), and folly::TypedIOBuf< T >::size().

74  {
75  return sdiv(buf_->length());
76  }
std::size_t length() const
Definition: IOBuf.h:533
static uint32_t sdiv(uint32_t n)
Definition: TypedIOBuf.h:221
template<class T>
TypedIOBuf& folly::TypedIOBuf< T >::operator= ( TypedIOBuf< T > &&  )
default
template<class T>
TypedIOBuf& folly::TypedIOBuf< T >::operator= ( const TypedIOBuf< T > &  )
privatedelete
template<class T>
const T& folly::TypedIOBuf< T >::operator[] ( ssize_t  idx) const
inline

Simple wrapper to make it easier to treat this TypedIOBuf as an array of T.

Definition at line 167 of file TypedIOBuf.h.

References folly::TypedIOBuf< T >::data(), and folly::TypedIOBuf< T >::length().

167  {
168  assert(idx >= 0 && idx < length());
169  return data()[idx];
170  }
uint32_t length() const
Definition: TypedIOBuf.h:74
const T * data() const
Definition: TypedIOBuf.h:62
template<class T>
T& folly::TypedIOBuf< T >::operator[] ( ssize_t  idx)
inline

Definition at line 172 of file TypedIOBuf.h.

References folly::TypedIOBuf< T >::length(), and folly::TypedIOBuf< T >::writableData().

172  {
173  assert(idx >= 0 && idx < length());
174  return writableData()[idx];
175  }
uint32_t length() const
Definition: TypedIOBuf.h:74
template<class T>
void folly::TypedIOBuf< T >::prepend ( uint32_t  n)
inline

Definition at line 105 of file TypedIOBuf.h.

References folly::TypedIOBuf< T >::buf_, folly::IOBuf::prepend(), and folly::TypedIOBuf< T >::smul().

105  {
106  buf_->prepend(smul(n));
107  }
static uint32_t smul(uint32_t n)
Definition: TypedIOBuf.h:225
void prepend(std::size_t amount)
Definition: IOBuf.h:673
template<class T>
void folly::TypedIOBuf< T >::push ( const T data)
inline

Append one element.

Definition at line 180 of file TypedIOBuf.h.

Referenced by folly::TypedIOBuf< T >::push_back().

180  {
181  push(&data, &data + 1);
182  }
void push(const T &data)
Definition: TypedIOBuf.h:180
const T * data() const
Definition: TypedIOBuf.h:62
template<class T>
template<class IT >
void folly::TypedIOBuf< T >::push ( IT  begin,
IT  end 
)
inline

Append multiple elements in a sequence; will call distance().

Definition at line 191 of file TypedIOBuf.h.

References folly::TypedIOBuf< T >::append(), folly::copy(), folly::TypedIOBuf< T >::headroom(), max, folly::TypedIOBuf< T >::operator=(), folly::TypedIOBuf< T >::reserve(), folly::TypedIOBuf< T >::size(), folly::TypedIOBuf< T >::tailroom(), folly::TypedIOBuf< T >::TypedIOBuf(), uint32_t, folly::usingJEMalloc(), and folly::TypedIOBuf< T >::writableTail().

191  {
192  uint32_t n = std::distance(begin, end);
193  if (usingJEMalloc()) {
194  // Rely on xallocx() and avoid exponential growth to limit
195  // amount of memory wasted.
196  reserve(headroom(), n);
197  } else if (tailroom() < n) {
198  reserve(headroom(), std::max(n, 3 + size() / 2));
199  }
201  append(n);
202  }
LogLevel max
Definition: LogLevel.cpp:31
void append(uint32_t n)
Definition: TypedIOBuf.h:108
bool usingJEMalloc() noexcept
Definition: Malloc.h:147
uint32_t tailroom() const
Definition: TypedIOBuf.h:84
uint32_t size() const
Definition: TypedIOBuf.h:77
constexpr std::decay< T >::type copy(T &&value) noexcept(noexcept(typename std::decay< T >::type(std::forward< T >(value))))
Definition: Utility.h:72
const T * end() const
Definition: TypedIOBuf.h:136
const T * begin() const
Definition: TypedIOBuf.h:133
void reserve(uint32_t minHeadroom, uint32_t minTailroom)
Definition: TypedIOBuf.h:120
uint32_t headroom() const
Definition: TypedIOBuf.h:81
template<class T>
void folly::TypedIOBuf< T >::push_back ( const T data)
inline

Definition at line 183 of file TypedIOBuf.h.

References folly::TypedIOBuf< T >::push().

183  {
184  push(data);
185  }
void push(const T &data)
Definition: TypedIOBuf.h:180
const T * data() const
Definition: TypedIOBuf.h:62
template<class T>
void folly::TypedIOBuf< T >::reserve ( uint32_t  minHeadroom,
uint32_t  minTailroom 
)
inline

Definition at line 120 of file TypedIOBuf.h.

References folly::TypedIOBuf< T >::buf_, folly::IOBuf::reserve(), and folly::TypedIOBuf< T >::smul().

Referenced by folly::TypedIOBuf< T >::push(), folly::TypedIOBuf< T >::reserve(), and TEST().

120  {
121  buf_->reserve(smul(minHeadroom), smul(minTailroom));
122  }
static uint32_t smul(uint32_t n)
Definition: TypedIOBuf.h:225
void reserve(std::size_t minHeadroom, std::size_t minTailroom)
Definition: IOBuf.h:741
template<class T>
void folly::TypedIOBuf< T >::reserve ( uint32_t  minTailroom)
inline

Definition at line 123 of file TypedIOBuf.h.

References folly::TypedIOBuf< T >::reserve().

123  {
124  reserve(0, minTailroom);
125  }
void reserve(uint32_t minHeadroom, uint32_t minTailroom)
Definition: TypedIOBuf.h:120
template<class T>
void folly::TypedIOBuf< T >::retreat ( uint32_t  n)
inline

Definition at line 102 of file TypedIOBuf.h.

References folly::TypedIOBuf< T >::buf_, folly::IOBuf::retreat(), and folly::TypedIOBuf< T >::smul().

102  {
103  buf_->retreat(smul(n));
104  }
static uint32_t smul(uint32_t n)
Definition: TypedIOBuf.h:225
void retreat(std::size_t amount)
Definition: IOBuf.h:653
template<class T>
static uint32_t folly::TypedIOBuf< T >::sdiv ( uint32_t  n)
inlinestaticprivate

Definition at line 221 of file TypedIOBuf.h.

References folly::T.

Referenced by folly::TypedIOBuf< T >::capacity(), folly::TypedIOBuf< T >::headroom(), folly::TypedIOBuf< T >::length(), and folly::TypedIOBuf< T >::tailroom().

221  {
222  return n / sizeof(T);
223  }
folly::std T
template<class T>
uint32_t folly::TypedIOBuf< T >::size ( ) const
inline

Definition at line 77 of file TypedIOBuf.h.

References folly::TypedIOBuf< T >::length().

Referenced by folly::TypedIOBuf< T >::push().

77  {
78  return length();
79  }
uint32_t length() const
Definition: TypedIOBuf.h:74
template<class T>
static uint32_t folly::TypedIOBuf< T >::smul ( uint32_t  n)
inlinestaticprivate

Definition at line 225 of file TypedIOBuf.h.

References folly::T, and uint64_t.

Referenced by folly::TypedIOBuf< T >::advance(), folly::TypedIOBuf< T >::append(), folly::TypedIOBuf< T >::prepend(), folly::TypedIOBuf< T >::reserve(), folly::TypedIOBuf< T >::retreat(), folly::TypedIOBuf< T >::trimEnd(), and folly::TypedIOBuf< T >::trimStart().

225  {
226  // In debug mode, check for overflow
227  assert((uint64_t(n) * sizeof(T)) < (uint64_t(1) << 32));
228  return n * sizeof(T);
229  }
folly::std T
template<class T>
const T* folly::TypedIOBuf< T >::tail ( ) const
inline

Definition at line 68 of file TypedIOBuf.h.

References folly::TypedIOBuf< T >::buf_, folly::TypedIOBuf< T >::cast(), and folly::IOBuf::tail().

Referenced by folly::TypedIOBuf< T >::cend().

68  {
69  return cast(buf_->tail());
70  }
static T * cast(uint8_t *p)
Definition: TypedIOBuf.h:214
const uint8_t * tail() const
Definition: IOBuf.h:516
template<class T>
uint32_t folly::TypedIOBuf< T >::tailroom ( ) const
inline

Definition at line 84 of file TypedIOBuf.h.

References folly::TypedIOBuf< T >::buf_, folly::TypedIOBuf< T >::sdiv(), and folly::IOBuf::tailroom().

Referenced by folly::TypedIOBuf< T >::push().

84  {
85  return sdiv(buf_->tailroom());
86  }
std::size_t tailroom() const
Definition: IOBuf.h:551
static uint32_t sdiv(uint32_t n)
Definition: TypedIOBuf.h:221
template<class T>
void folly::TypedIOBuf< T >::trimEnd ( uint32_t  n)
inline

Definition at line 114 of file TypedIOBuf.h.

References folly::TypedIOBuf< T >::buf_, folly::TypedIOBuf< T >::smul(), and folly::IOBuf::trimEnd().

114  {
115  buf_->trimEnd(smul(n));
116  }
static uint32_t smul(uint32_t n)
Definition: TypedIOBuf.h:225
void trimEnd(std::size_t amount)
Definition: IOBuf.h:718
template<class T>
void folly::TypedIOBuf< T >::trimStart ( uint32_t  n)
inline

Definition at line 111 of file TypedIOBuf.h.

References folly::TypedIOBuf< T >::buf_, folly::TypedIOBuf< T >::smul(), and folly::IOBuf::trimStart().

111  {
112  buf_->trimStart(smul(n));
113  }
static uint32_t smul(uint32_t n)
Definition: TypedIOBuf.h:225
void trimStart(std::size_t amount)
Definition: IOBuf.h:703
template<class T>
T* folly::TypedIOBuf< T >::writableBuffer ( )
inline

Definition at line 90 of file TypedIOBuf.h.

References folly::TypedIOBuf< T >::buf_, folly::TypedIOBuf< T >::cast(), and folly::IOBuf::writableBuffer().

90  {
91  return cast(buf_->writableBuffer());
92  }
static T * cast(uint8_t *p)
Definition: TypedIOBuf.h:214
uint8_t * writableBuffer()
Definition: IOBuf.h:572
template<class T>
T* folly::TypedIOBuf< T >::writableData ( )
inline

Definition at line 65 of file TypedIOBuf.h.

References folly::TypedIOBuf< T >::buf_, folly::TypedIOBuf< T >::cast(), and folly::IOBuf::writableData().

Referenced by folly::TypedIOBuf< T >::begin(), and folly::TypedIOBuf< T >::operator[]().

65  {
66  return cast(buf_->writableData());
67  }
static T * cast(uint8_t *p)
Definition: TypedIOBuf.h:214
uint8_t * writableData()
Definition: IOBuf.h:509
template<class T>
T* folly::TypedIOBuf< T >::writableTail ( )
inline

Definition at line 71 of file TypedIOBuf.h.

References folly::TypedIOBuf< T >::buf_, folly::TypedIOBuf< T >::cast(), and folly::IOBuf::writableTail().

Referenced by folly::TypedIOBuf< T >::end(), and folly::TypedIOBuf< T >::push().

71  {
72  return cast(buf_->writableTail());
73  }
static T * cast(uint8_t *p)
Definition: TypedIOBuf.h:214
uint8_t * writableTail()
Definition: IOBuf.h:526

Member Data Documentation


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