ark::core::ByteBuffer

Defined in header “ark/core/byte_buffer.hh”.


A byte buffer is a buffer that is oriented around efficient storage of byte arrays. It has provisions for wrapping arrays, intelligent allocations, growing on demand, and zero-copy support.

If you copy this structure, you will get a shallow copy by default. Modifying the buffers (via write, or accessing data in a non-const fashion), incurs a copy penalty.

Methods

  • ByteBuffer()
    Constructor. Creates an empty byte buffer.

  • ByteBuffer(void * data, std::size_t size)
    Constructor. Wraps an existing byte array. The byte array that this is wrapping must stay in scope; the byte buffer does not take ownership of it.

    In this mode, you cannot adjust the capacity of the byte buffer.

  • ByteBuffer(const ByteBuffer & other)
    Copy constructor. This produces a shallow copy.

  • ByteBuffer(ByteBuffer && other)
    Move constructor.

  • ~ByteBuffer()
    Destructor. Cleans up internal resources.

  • size_t ref_count()
    Returns the reference count of this buffer. If zero the underlying data is not owned by the buffer (it is either empty or a wrapping external data).

  • ByteBuffer clone()
    Create a new buffer containing a deep copy of the byte buffer data.

  • const uint8_t * data()
    Returns a reference to the raw bytes stored within this buffer.

  • uint8_t * data()
    Returns a reference to the raw bytes stored within this buffer.

  • const uint8_t * begin()
    Returns a ‘starting’ iterator to the byte buffer.

  • const uint8_t * end()
    Returns an ’ending’ iterator to the data in the byte buffer.

  • core::ByteBuffer subview(size_t offset, size_t count)
    Returns a sub-view of this byte buffer. This will just be a ‘view’ into the buffer over the given range you must keep the original buffer in scope for this to remain valid.

  • std::string_view str()
    Returns a string-view wrapped version of the data stored within this buffer.

  • std::size_t capacity()
    Returns the current capacity of this byte buffer. This is the number of bytes available in the internal buffer. If those are all consumed, the buffer would need resized.

  • std::size_t size()
    Returns the total number of bytes actually consumed within this buffer. This number is always equal to or less than the capacity.

  • bool empty()
    Returns if this buffer is empty or not (size is zero).

  • void write(const std::string_view & bytes)
    Convenience method for writing the given string of bytes to this buffer. The buffer is automatically resized, and the content is placed at the end of the buffer.

  • void write(const void *data __attribute__, size_t size)
    Convenience method for writing the given array of bytes to this buffer. The buffer is automatically resized, and the content is placed at the end of the buffer.

  • void resize(std::size_t new_size)
    Resizes this byte byffer. This will adjust the size parameter to match the passed in value. If the size is greater than the current capacity, the buffer will be resized appropriately.

     core::ByteBuffer buffer;
    
     buffer.write("Hello world!");
     EXPECT_EQ(buffer.str(), "Hello world!");
    
     buffer.resize(5);
     EXPECT_EQ(buffer.str(), "Hello");
    
     buffer.resize(0);
     EXPECT_TRUE(buffer.empty());
    
  • void reserve(std::size_t additional_capacity)
    Set the overall capacity of the buffer to the given number of bytes. This cannot be used to shrink the allocation of the byte buffer, only expand it.

    This will reserve ’new_capacity’ additional bytes, on top of the current ‘size’ of the buffer (not the capacity).

     core::ByteBuffer buffer;
    
     buffer.write("This is a test.");
    
     auto original_size = buffer.size();
    
     buffer.reserve(16384);
     buffer.reserve(0);
    
     EXPECT_GE(buffer.capacity(), (original_size + 16384));
    
  • void remove_front(std::size_t count)
    Removes the first N bytes from the front of the byte buffer. May be more efficient then copying the buffer manually.

     core::ByteBuffer primary;
    
     primary.write("12345678");
    
     auto copy = primary;
    
     primary.remove_front(4);
    
     EXPECT_EQ(primary.str(), "5678");
     EXPECT_EQ(primary.size(), 4);
     EXPECT_EQ(copy.str(), "12345678");
     EXPECT_EQ(copy.size(), 8);
    
     primary.remove_front(4);
    
     EXPECT_EQ(primary.str(), "");
     EXPECT_EQ(primary.size(), 0);
     EXPECT_EQ(copy.str(), "12345678");
     EXPECT_EQ(copy.size(), 8);
    
  • ByteBuffer & operator=(const ByteBuffer & other)
    Copy operator.

  • ByteBuffer & operator=(ByteBuffer && other)
    Move operator.

  • bool operator==(const ByteBuffer & other)
    Equality operator. Returns true if the data is identical, ignoring capacity and ownership.

  • bool operator!=(const ByteBuffer & other)
    Inequality operator. Opposite of the equality operator.

  • std::strong_ordering operator(const ByteBuffer & rhs)
    Spaceship operator, for other comparisons.

  • uint8_t & operator[](size_t index)
    An overload that allows direct access into the byte buffer contents.

  • constexpr const uint8_t & operator[](size_t index)
    An overload that allows direct access into the byte buffer contents.