Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:CARLA simulator Carla BufferView

From Leeroopedia
Revision as of 12:12, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/CARLA_simulator_Carla_BufferView.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Knowledge Sources
Domains Memory, Networking
Last Updated 2026-02-15 05:00 GMT

Overview

BufferView provides a read-only, shared-ownership view over a Buffer, enabling safe zero-copy sharing of buffer data across multiple consumers.

Description

The BufferView class in the carla namespace creates an immutable view over a Buffer by taking ownership of the buffer via move semantics. It inherits from std::enable_shared_from_this<BufferView> and is designed to be used exclusively through shared_ptr (the default constructor and copy constructor are deleted).

Construction:

The only public factory method is CreateFrom(Buffer&&), which move-constructs a BufferView and wraps it in a shared_ptr. The private constructor stores the buffer as a const Buffer, ensuring immutability.

Data access:

  • operator[](size_t i): Returns a const reference to the byte at position i
  • data(): Returns a const pointer to the underlying memory, or nullptr if empty
  • cbuffer() / buffer(): Creates a boost::asio::const_buffer from the data, suitable for Boost.Asio async operations. The caller must ensure the BufferView remains alive while the asio buffer is in use.

Capacity:

  • empty(): Returns true if the buffer has zero size
  • size(): Returns the buffer size as uint32_t
  • max_size(): Returns the maximum possible size (uint32_t max)
  • capacity(): Returns the allocated capacity of the underlying buffer

Iterators:

Provides begin(), cbegin(), end(), cend() const iterators for range-based iteration over the raw bytes.

The type alias SharedBufferView (std::shared_ptr<BufferView>) is defined for convenience. It also conditionally includes UE4 container headers when compiled within Unreal Engine.

Usage

BufferView is used extensively in the multi-GPU and streaming subsystems to share buffer data across multiple TCP write operations without copying. Primary::MakeMessage and Secondary::MakeMessage accept SharedBufferView arguments to construct network messages.

Code Reference

Source Location

  • Repository: CARLA
  • File: LibCarla/source/carla/BufferView.h

Signature

class BufferView : public std::enable_shared_from_this<BufferView> {
public:
    using value_type = unsigned char;
    using size_type = uint32_t;
    using const_iterator = const value_type *;

    BufferView() = delete;
    BufferView(const BufferView &) = delete;

    static std::shared_ptr<BufferView> CreateFrom(Buffer &&buffer);

    const value_type &operator[](size_t i) const;
    const value_type *data() const noexcept;
    boost::asio::const_buffer cbuffer() const noexcept;
    boost::asio::const_buffer buffer() const noexcept;

    bool empty() const noexcept;
    size_type size() const noexcept;
    static constexpr size_type max_size() noexcept;
    size_type capacity() const noexcept;

    const_iterator cbegin() const noexcept;
    const_iterator begin() const noexcept;
    const_iterator cend() const noexcept;
    const_iterator end() const noexcept;
};

using SharedBufferView = std::shared_ptr<BufferView>;

Import

#include "carla/BufferView.h"

I/O Contract

Inputs

Name Type Required Description
buffer Buffer&& Yes (CreateFrom) Source buffer to take ownership of (moved into the view)

Outputs

Name Type Description
CreateFrom() shared_ptr<BufferView> Shared pointer to an immutable view over the buffer data
data() const unsigned char* Raw pointer to the underlying data
buffer()/cbuffer() boost::asio::const_buffer Asio-compatible const buffer for network I/O
size() uint32_t Number of bytes in the buffer
begin()/end() const_iterator Iterators for byte-level traversal

Usage Examples

// Create a BufferView from a Buffer
carla::Buffer buf(data, size);
auto view = carla::BufferView::CreateFrom(std::move(buf));

// Access data read-only
const unsigned char *ptr = view->data();
uint32_t len = view->size();

// Use as boost::asio buffer for network send
boost::asio::async_write(socket, view->buffer(), handler);

// Iterate over bytes
for (auto it = view->begin(); it != view->end(); ++it) {
    // process byte *it
}

// Share across multiple message consumers (zero-copy)
auto msg = Primary::MakeMessage(view, anotherView);

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment