Skip to content

AByteBuffer#

std::vector-like growing array for byte storage.

Header:#include <AUI/Common/AByteBuffer.h>
CMake:aui_link(my_target PUBLIC aui::core)

Public Methods#

at#


template<typename T >
T& AByteBuffer::at(size_t byteIndex)

Gets value of specified type by byte index relative to the beginning of internal buffer.

Arguments
byteIndex
byte offset realtive to the beginning of internal buffer
Returns
data

Examples#

examples/7guis/cells/src/AST.cpp

7GUIs Cells - Spreadsheet processor (Excel).

                });
            }
        };
        auto out = std::make_unique<FunctionCall>();
        out->function = functions::predefined().at(expect<token::Identifier>().name.uppercase());
        mIterator++;
        expect<token::LPar>();
        mIterator++;

        for (;;) {

template<typename T >
const T& AByteBuffer::at(size_t byteIndex)

Gets value of specified type by byte index relative to the beginning of internal buffer.

Arguments
byteIndex
byte offset realtive to the beginning of internal buffer
Returns
data

Examples#

examples/7guis/cells/src/AST.cpp

7GUIs Cells - Spreadsheet processor (Excel).

                });
            }
        };
        auto out = std::make_unique<FunctionCall>();
        out->function = functions::predefined().at(expect<token::Identifier>().name.uppercase());
        mIterator++;
        expect<token::LPar>();
        mIterator++;

        for (;;) {

capacity#


size_t AByteBuffer::capacity()
Returns
size of whole buffer (including possibly invalid data)

data#


char* AByteBuffer::data()
Returns
Internal buffer.

empty#


bool AByteBuffer::empty()
Returns
true if size == 0

Examples#

examples/ui/contacts/src/view/ContactDetailsView.cpp

AUI Contacts - Usage of AUI_DECLARATIVE_FOR to make a contacts-like application.

namespace {
_<AView> profilePhoto(const _<Contact>& contact) {
    return Centered {
        Label {} & contact->displayName.readProjected([](const AString& s) {
            return s.empty() ? "?" : AString(1, s.first()).uppercase();
        }) AUI_WITH_STYLE { Opacity(0.5f), FontSize { 32_dp } },
    } AUI_WITH_STYLE {
        FixedSize { 64_dp },
        BorderRadius { 32_dp },
        BackgroundGradient { AColor::GRAY.lighter(0.5f), AColor::GRAY, 163_deg },
examples/7guis/cells/src/Formula.cpp

7GUIs Cells - Spreadsheet processor (Excel).

    return precompile(expression)(spreadsheet);
}

formula::Precompiled formula::precompile(const AString& expression) {
    if (expression.empty()) {
        return [](const Spreadsheet&) { return std::nullopt; };
    }
    if (auto d = expression.toDouble()) {
        return [d = *d](const Spreadsheet&) { return d; };
    }
examples/7guis/cells/src/AST.cpp

7GUIs Cells - Spreadsheet processor (Excel).

        auto putValue = [&](_unique<INode> node) {
            if (temporaryValue) {
                throw AException("SYNTAX");
            }
            if (!binaryOperators.empty()) {
                if (binaryOperators.last().op->right) {
                    throw AException {};
                }
                binaryOperators.last().op->right = std::move(node);
                return;

getAvailableToWrite#


size_t AByteBuffer::getAvailableToWrite()
Returns
delta between internal buffer size and payload size.

getReserved#


size_t AByteBuffer::getReserved()
Returns
size of internal buffer. Must be greater that getSize()

getSize#


size_t AByteBuffer::getSize()
Returns
size of payload (valid data)

Examples#

examples/app/fractal/src/FractalView.cpp

Fractal Example - Fractal viewer application demonstrating usage of custom shaders.

    AView::render(context);

    mShader.use();
    mTexture->bind();
    context.render.rectangle(ACustomShaderBrush {}, { 0, 0 }, getSize());
}

void FractalView::setSize(glm::ivec2 size) {
    AView::setSize(size);
    mShader.use();
examples/app/game_of_life/src/main.cpp

Game of Life - Game of Life implementation that uses advanced large dynamic data rendering techniques such as ITexture, AImage to be GPU friendly. The computation is performed in AThreadPool.

        }
        auto drawGrid = [&] {
            ASmallVector<std::pair<glm::vec2, glm::vec2>, 128 * 2> points;
            for (int i = 1; i < mCells->size().x; ++i) {
                points << std::make_pair(glm::vec2(i * SCALE, 0.f), glm::vec2(i * SCALE, getSize().y));
            }
            for (int i = 1; i < mCells->size().y; ++i) {
                points << std::make_pair(glm::vec2(0.f, i * SCALE), glm::vec2(getSize().x, i * SCALE));
            }
            ctx.render.lines(ASolidBrush { AColor::GRAY }, points);

grow#


void AByteBuffer::grow(size_t size)

If getReserved() - getSize() is less than size increases internal buffer size enough to store size bytes.

increaseInternalBuffer#


void AByteBuffer::increaseInternalBuffer(size_t size)

Increases internal buffer.

increaseSize#


void AByteBuffer::increaseSize(size_t s)
Arguments
s
new size of the payload

Forces new size of the buffer.

Sneaky assert:
Assert fails when new size is greater that reserved buffer size. Use AByteBuffer::resize to avoid this.

reallocate#


void AByteBuffer::reallocate(size_t s)
Arguments
s
new size of the payload

Resizes the buffer WITHOUT keeping it's contents. When reserved buffer size is differs from the new size, buffer is reallocated with new size.

reserve#


void AByteBuffer::reserve(size_t size)

Resizes internal buffer.

Examples#

examples/ui/views/src/DemoGraphView.cpp

Views Example - All-in-one views building example.

{
    setFixedSize({ 5_dp * POINT_COUNT, 100_dp }); // set fixed size


    mPoints.reserve(POINT_COUNT);
    for (std::size_t i = 0; i < POINT_COUNT; ++i) {
        // map a sinusoid to view
        mPoints << glm::vec2{ 5_dp * float(i), 100_dp * ((glm::sin(i / 10.f) + 1.f) / 2.f) };
    }
}

resize#


void AByteBuffer::resize(size_t s)
Arguments
s
new size of the payload

Resizes the buffer keeping it's contents. When reserved buffer size is less than the new size, buffer is reallocated with new size.

Examples#

examples/app/game_of_life/src/main.cpp

Game of Life - Game of Life implementation that uses advanced large dynamic data rendering techniques such as ITexture, AImage to be GPU friendly. The computation is performed in AThreadPool.

public:
    Cells(glm::ivec2 size) {
        mSize = size;
        for (auto s : { &mStorage, &mNextPopulation }) {
            s->resize(size.x * size.y);
        }

        connect(mTimer->fired, me::frame);
        connect(isRunning.changed, AUI_SLOT(mTimer)::setRunning);
    }
examples/app/minesweeper/src/MinesweeperWindow.cpp

Minesweeper Game - Minesweeper game implementation driven by ass.

    mReveal = false;
    mBombsPlanted = false;
    mGrid->setLayout(std::make_unique<AGridLayout>(columns, rows));
    mField.clear();
    mField.resize(columns * rows);

    for (int i = 0; i < columns * rows; ++i) {
        int x = i % columns;
        int y = i / columns;
        auto cell = _new<CellView>(fieldAt(x, y));
examples/7guis/cells/src/Spreadsheet.h

7GUIs Cells - Spreadsheet processor (Excel).

class Spreadsheet {
public:
    explicit Spreadsheet(glm::uvec2 size) : mSize(size) {
        mCells.resize(size.x * size.y);
        for (auto& v : mCells) {
            v = std::make_unique<Cell>();
            v->spreadsheet = this;
        }
    }
examples/7guis/cells/src/main.cpp

7GUIs Cells - Spreadsheet processor (Excel).

            AGridSplitter::Builder()
                    .noDefaultSpacers()
                    .withItems([&] {
                        AVector<AVector<_<AView>>> views;
                        views.resize(mState->spreadsheet.size().y + 1);
                        for (auto& c : views) {
                            c.resize(mState->spreadsheet.size().x + 1);
                        }

                        views[0][0] = _new<AView>();   // blank

setSize#


void AByteBuffer::setSize(size_t s)
Arguments
s
new size of the payload

Forces new size of the buffer.

Sneaky assert:
Assert fails when new size is greater that reserved buffer size. Use AByteBuffer::resize to avoid this.

Examples#

examples/app/fractal/src/FractalView.cpp

Fractal Example - Fractal viewer application demonstrating usage of custom shaders.

    mTexture->bind();
    context.render.rectangle(ACustomShaderBrush {}, { 0, 0 }, getSize());
}

void FractalView::setSize(glm::ivec2 size) {
    AView::setSize(size);
    mShader.use();
    mShader.set(UNIFORM_RATIO, mAspectRatio = float(size.x) / float(size.y));
}
examples/app/fractal/src/FractalView.h

Fractal Example - Fractal viewer application demonstrating usage of custom shaders.

    void onKeyRepeat(AInput::Key key) override;

    void onScroll(const AScrollEvent& event) override;

    void setSize(glm::ivec2 size) override;

    gl::Program& getShader() { return mShader; }

    const _<gl::Texture2D>& getTexture() const { return mTexture; }

size#


size_t AByteBuffer::size()
Returns
size of payload (valid data)

Examples#

examples/app/fractal/src/FractalView.cpp

Fractal Example - Fractal viewer application demonstrating usage of custom shaders.

    mTexture->bind();
    context.render.rectangle(ACustomShaderBrush {}, { 0, 0 }, getSize());
}

void FractalView::setSize(glm::ivec2 size) {
    AView::setSize(size);
    mShader.use();
    mShader.set(UNIFORM_RATIO, mAspectRatio = float(size.x) / float(size.y));
}
examples/app/game_of_life/src/main.cpp

Game of Life - Game of Life implementation that uses advanced large dynamic data rendering techniques such as ITexture, AImage to be GPU friendly. The computation is performed in AThreadPool.

using CellsImage = AFormattedImage<APixelFormat::RGBA_BYTE>;

class Cells : public AObject {
public:
    Cells(glm::ivec2 size) {
        mSize = size;
        for (auto s : { &mStorage, &mNextPopulation }) {
            s->resize(size.x * size.y);
        }
examples/7guis/cells/src/Spreadsheet.h

7GUIs Cells - Spreadsheet processor (Excel).

#include "AUI/Common/AException.h"

class Spreadsheet {
public:
    explicit Spreadsheet(glm::uvec2 size) : mSize(size) {
        mCells.resize(size.x * size.y);
        for (auto& v : mCells) {
            v = std::make_unique<Cell>();
            v->spreadsheet = this;
        }
examples/7guis/cells/src/main.cpp

7GUIs Cells - Spreadsheet processor (Excel).

            AGridSplitter::Builder()
                    .noDefaultSpacers()
                    .withItems([&] {
                        AVector<AVector<_<AView>>> views;
                        views.resize(mState->spreadsheet.size().y + 1);
                        for (auto& c : views) {
                            c.resize(mState->spreadsheet.size().x + 1);
                        }

                        views[0][0] = _new<AView>();   // blank
examples/7guis/cells/src/Functions.cpp

7GUIs Cells - Spreadsheet processor (Excel).

              return double(accumulator);
            } },
        { "IF",
            [](Ctx ctx) {
              if (ctx.args.size() != 3) {
                  throw AException("ARG");
              }
              auto condition = std::get_if<double>(&ctx.args[0]);
              if (condition == nullptr) {
                  throw AException("ARG0");

write#


void AByteBuffer::write(IInputStream& stream, size_t size)
Arguments
stream
size

Reads exact size bytes from stream.