AUI Framework  develop
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
AByteBufferView.h
    1/*
    2 * AUI Framework - Declarative UI toolkit for modern C++20
    3 * Copyright (C) 2020-2025 Alex2772 and Contributors
    4 *
    5 * SPDX-License-Identifier: MPL-2.0
    6 *
    7 * This Source Code Form is subject to the terms of the Mozilla Public
    8 * License, v. 2.0. If a copy of the MPL was not distributed with this
    9 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
   10 */
   11
   12#pragma once
   13
   14#include <AUI/Traits/serializable.h>
   15
   24class API_AUI_CORE AByteBufferView {
   25private:
   26    const char* mBuffer;
   27    size_t mSize;
   28
   29public:
   30    AByteBufferView() noexcept: mBuffer(nullptr), mSize(0) {}
   31    AByteBufferView(const char* buffer, size_t size) noexcept: mBuffer(buffer), mSize(size) {}
   32    explicit AByteBufferView(const std::string& string) noexcept: mBuffer(string.data()), mSize(string.size()) {}
   33    explicit AByteBufferView(std::string_view string) noexcept: mBuffer(string.data()), mSize(string.size()) {}
   34
   35    static AByteBufferView fromRange(const char* begin, const char* end) noexcept {
   36        return AByteBufferView(begin, std::distance(begin, end));
   37    }
   38
   45    template <typename T>
   46    const T& at(size_t byteIndex) const noexcept
   47    {
   48        return *reinterpret_cast<const T*>(mBuffer + byteIndex);
   49    }
   50
   51    [[nodiscard]]
   52    bool empty() const noexcept {
   53        return mSize == 0;
   54    }
   55
   56    [[nodiscard]]
   57    AByteBufferView slice(std::size_t offset /* to end */) const noexcept {
   58        return slice(offset, size() - offset);
   59    }
   60
   61    [[nodiscard]]
   62    AByteBufferView slice(std::size_t offset, std::size_t size) const noexcept {
   63        AUI_ASSERTX(offset + size <= mSize, "out of bounds");
   64        return { mBuffer + offset, size };
   65    }
   66
   67    [[nodiscard]]
   68    const char* data() const noexcept {
   69        return mBuffer;
   70    }
   71
   72    [[nodiscard]]
   73    size_t size() const noexcept {
   74        return mSize;
   75    }
   76
   77    [[nodiscard]]
   78    auto begin() const noexcept {
   79        return data();
   80    }
   81    [[nodiscard]]
   82    auto end() const noexcept {
   83        return data() + size();
   84    }
   85
   86    [[nodiscard]]
   87    AString toHexString() const;
   88
   89    [[nodiscard]]
   90    AString toBase64String() const;
   91
   92    template<typename T>
   93    [[nodiscard]]
   94    const T& as() const {
   95        if (mSize != sizeof(T)) {
   96            throw AException("as<T>(): invalid size");
   97        }
   98        return *reinterpret_cast<const T*>(mBuffer);
   99    }
  100
  101    template<typename T>
  102    [[nodiscard]]
  103    static AByteBufferView fromRaw(const T& data) noexcept {
  104        return { reinterpret_cast<const char*>(&data), sizeof(data) };
  105    }
  106
  107    [[nodiscard]]
  108    std::string_view toStdStringView() const noexcept {
  109        return { data(), size() };
  110    }
  111
  112
  113    _unique<IInputStream> toStream() const;
  114};
  115
  116inline std::ostream& operator<<(std::ostream& lhs, const AByteBufferView& rhs) {
  117    lhs << "[";
  118    for (const auto b : rhs) {
  119        char buf[8];
  120        lhs.write(buf, std::distance(std::begin(buf), fmt::format_to(buf, " {:02x}", b)));
  121    }
  122    lhs << " ]";
  123
  124    return lhs;
  125}
  126
  127template<>
  129    static void write(IOutputStream& os, AByteBufferView view) {
  130        os.write(view.data(), view.size());
  131    }
  132};
  133
  134template<>
  135struct std::hash<AByteBufferView>
  136{
  137    size_t operator()(AByteBufferView t) const noexcept
  138    {
  139        return std::hash<std::string_view>{}(t.toStdStringView());
  140    }
  141};
Acts like std::string_view but for AByteBuffer.
Definition AByteBufferView.h:24
const T & at(size_t byteIndex) const noexcept
Gets value of specified type by byte index relative to the beginning of internal buffer.
Definition AByteBufferView.h:46
Definition IOutputStream.h:18
virtual void write(const char *src, size_t size)=0
Writes exact size bytes to stream. Blocking (waiting for write all data) is allowed.
#define AUI_ASSERTX(condition, what)
Asserts that the passed condition evaluates to true. Adds extra message string.
Definition Assert.h:74
Definition serializable.h:26