Skip to content

Commit

Permalink
Use std::byte for Bytes type
Browse files Browse the repository at this point in the history
  • Loading branch information
dancazarin committed Feb 11, 2025
1 parent 26fe064 commit ce42fa9
Show file tree
Hide file tree
Showing 33 changed files with 236 additions and 208 deletions.
2 changes: 1 addition & 1 deletion examples/showcase/src/ShowcaseComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ void ShowcaseComponent::configureWindow(RC<GUIWindow> window) {
}

void ShowcaseComponent::saveScreenshot(RC<Image> image) {
std::vector<uint8_t> bytes = pngEncode(image);
Bytes bytes = pngEncode(image);
if (auto file = showSaveDialog({ FileDialogFilter{ "*.png", "PNG image"_tr } },
defaultFolder(DefaultFolder::Pictures))) {
if (auto s = writeBytes(*file, bytes)) {
Expand Down
31 changes: 15 additions & 16 deletions include/brisk/core/BasicTypes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ namespace Brisk {
using namespace std::literals::string_view_literals;
using namespace std::literals::string_literals;

constexpr std::byte operator""_b(unsigned long long n) {
return static_cast<std::byte>(n);
}

/**
* @brief Creates a `std::span` representing a view over a single mutable element.
*
Expand Down Expand Up @@ -73,25 +77,20 @@ std::span<const T> one(const T& value) noexcept {
return std::span<const T>{ &value, 1 };
}

/**
* @brief Type alias for a single byte (8-bit unsigned integer).
*/
using byte = uint8_t;

/**
* @brief Type alias for a vector of bytes.
*/
using Bytes = std::vector<byte>;
using Bytes = std::vector<std::byte>;

/**
* @brief Type alias for a non-modifiable view of a sequence of bytes.
*/
using BytesView = std::span<const byte>;
using BytesView = std::span<const std::byte>;

/**
* @brief Type alias for a modifiable view of a sequence of bytes.
*/
using BytesMutableView = std::span<byte>;
using BytesMutableView = std::span<std::byte>;

#ifdef BRISK_USE_CHAR8_T
/**
Expand Down Expand Up @@ -233,7 +232,7 @@ template <typename Type>
inline BytesView asBytesView(const Type& value)
requires simpleMemoryRepresentation<Type>
{
return BytesView(reinterpret_cast<const byte*>(std::addressof(value)), sizeof(Type));
return BytesView(reinterpret_cast<const std::byte*>(std::addressof(value)), sizeof(Type));
}

/**
Expand All @@ -249,7 +248,7 @@ inline BytesView toBytesView(Container&& cont)
requires std::is_constructible_v<std::span<const T>, Container&>
{
std::span<const T> sp(cont);
return BytesView(reinterpret_cast<const byte*>(sp.data()), sp.size_bytes());
return BytesView(reinterpret_cast<const std::byte*>(sp.data()), sp.size_bytes());
}

/**
Expand All @@ -260,7 +259,7 @@ inline BytesView toBytesView(Container&& cont)
*/
template <size_t N>
inline BytesView toBytesView(const char (&str)[N]) {
return BytesView(reinterpret_cast<const byte*>(str), N - 1);
return BytesView(reinterpret_cast<const std::byte*>(str), N - 1);
}

/**
Expand All @@ -271,7 +270,7 @@ inline BytesView toBytesView(const char (&str)[N]) {
*/
template <size_t N>
inline BytesView toBytesView(const char8_t (&str)[N]) {
return BytesView(reinterpret_cast<const byte*>(str), N - 1);
return BytesView(reinterpret_cast<const std::byte*>(str), N - 1);
}

/**
Expand All @@ -282,7 +281,7 @@ inline BytesView toBytesView(const char8_t (&str)[N]) {
*/
template <size_t N>
inline BytesView toBytesView(const char16_t (&str)[N]) {
return BytesView(reinterpret_cast<const byte*>(str), (N - 1) * sizeof(char16_t));
return BytesView(reinterpret_cast<const std::byte*>(str), (N - 1) * sizeof(char16_t));
}

/**
Expand All @@ -293,7 +292,7 @@ inline BytesView toBytesView(const char16_t (&str)[N]) {
*/
template <size_t N>
inline BytesView toBytesView(const char32_t (&str)[N]) {
return BytesView(reinterpret_cast<const byte*>(str), (N - 1) * sizeof(char32_t));
return BytesView(reinterpret_cast<const std::byte*>(str), (N - 1) * sizeof(char32_t));
}

/**
Expand All @@ -304,7 +303,7 @@ inline BytesView toBytesView(const char32_t (&str)[N]) {
*/
template <size_t N>
inline BytesView toBytesView(const wchar_t (&str)[N]) {
return BytesView(reinterpret_cast<const byte*>(str), (N - 1) * sizeof(wchar_t));
return BytesView(reinterpret_cast<const std::byte*>(str), (N - 1) * sizeof(wchar_t));
}

/**
Expand All @@ -320,7 +319,7 @@ inline BytesMutableView toBytesMutableView(Container&& cont)
requires requires { std::span{ cont }; }
{
std::span sp(cont);
return BytesMutableView(reinterpret_cast<byte*>(sp.data()), sp.size_bytes());
return BytesMutableView(reinterpret_cast<std::byte*>(sp.data()), sp.size_bytes());
}

/**
Expand Down
9 changes: 3 additions & 6 deletions include/brisk/core/Bytes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@

namespace Brisk {

using Bytes = std::vector<uint8_t>;

/// @brief Converts string in Hex format to span of bytes
/// @returns number of bytes written or SIZE_MAX in case of error
[[nodiscard]] size_t fromHex(BytesMutableView data, std::string_view encoded);
Expand All @@ -49,8 +47,7 @@ using Bytes = std::vector<uint8_t>;

/// @brief Converts span of bytes to string in Base64 format
/// @returns number of characters written or SIZE_MAX in case of error
[[nodiscard]] size_t toBase64(std::span<char> encoded, BytesView data, bool urlSafe = false,
bool pad = true);
[[nodiscard]] size_t toBase64(std::span<char> encoded, BytesView data, bool urlSafe = false, bool pad = true);

[[nodiscard]] optional<Bytes> fromBase64(std::string_view encoded, bool urlSafe = false, bool strict = false);
[[nodiscard]] std::string toBase64(BytesView data, bool urlSafe = false, bool pad = true);
Expand Down Expand Up @@ -236,7 +233,7 @@ struct FixedBytes {
return Brisk::toBase64({ data(), size() }, urlSafe, pad);
}

using value_type = uint8_t;
using value_type = std::byte;
using size_type = size_t;
using pointer = value_type*;
using const_pointer = const value_type*;
Expand Down Expand Up @@ -279,7 +276,7 @@ struct FixedBytes {
return data() + size();
}

uint8_t m_data[Size];
value_type m_data[Size];
};

template <size_t Size>
Expand Down
6 changes: 5 additions & 1 deletion include/brisk/core/Cryptography.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,14 @@ struct Hasher {
* @param size The number of bytes to write.
* @return True if the write operation was successful, otherwise false.
*/
bool write(const uint8_t* data, size_t size) {
bool write(const std::byte* data, size_t size) {
return write({ data, size });
}

bool write(const uint8_t* data, size_t size) {
return write(reinterpret_cast<const std::byte*>(data), size);
}

/** The hashing method used by this hasher. */
HashMethod method;

Expand Down
4 changes: 2 additions & 2 deletions include/brisk/core/Json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ struct Json : protected JsonVariant {
* @brief Converts the current Json object to a MessagePack byte array.
* @return A vector of bytes representing the MessagePack format.
*/
std::vector<uint8_t> toMsgPack() const;
Bytes toMsgPack() const;

/**
* @brief Parses a MessagePack byte array and returns a Json object.
Expand Down Expand Up @@ -933,7 +933,7 @@ inline bool fromJson(const Json& b, std::set<K, C, Alloc>& v) {
template <typename T, typename Alloc>
inline bool toJson(Json& b, const std::vector<T, Alloc>& v) {
if constexpr (std::is_same_v<T, uint8_t> || std::is_same_v<T, int8_t> || std::is_same_v<T, std::byte>) {
b = toHex(v);
b = toHex(toBytesView(v));
return true;
} else {
JsonArray a(v.size());
Expand Down
22 changes: 12 additions & 10 deletions include/brisk/core/Resources.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,22 @@ inline bool resourceExists(std::string_view name) {
return rsrc && *rsrc->size > 0;
}

inline std::vector<uint8_t> loadResource(std::string_view name, bool emptyOk = false) {
inline Bytes loadResource(std::string_view name, bool emptyOk = false) {
const Internal::ResourceEntry* rsrc = Internal::lookupResource(name);
if ((!rsrc || *rsrc->size == 0) && emptyOk) {
return {};
}
BRISK_ASSERT(rsrc);
if (rsrc->compression == Internal::ResourceCompression::None) {
return std::vector<uint8_t>(rsrc->data, rsrc->data + *rsrc->size);
return Bytes((const std::byte*)rsrc->data, (const std::byte*)rsrc->data + *rsrc->size);
}
return compressionDecode(static_cast<CompressionMethod>(rsrc->compression),
std::span<const uint8_t>(rsrc->data, rsrc->data + *rsrc->size));
return compressionDecode(
static_cast<CompressionMethod>(rsrc->compression),
BytesView((const std::byte*)rsrc->data, (const std::byte*)rsrc->data + *rsrc->size));
}

inline const std::vector<uint8_t>& loadResourceCached(std::string name, bool emptyOk = false) {
static std::map<std::string, std::vector<uint8_t>> cache;
inline const Bytes& loadResourceCached(std::string name, bool emptyOk = false) {
static std::map<std::string, Bytes> cache;
auto data = loadResource(name, emptyOk);
auto it = cache.insert_or_assign(std::move(name), std::move(data));
return it.first->second;
Expand All @@ -73,11 +74,12 @@ inline std::string loadResourceText(std::string_view name, bool emptyOk = false)
}
BRISK_ASSERT(rsrc);
if (rsrc->compression == Internal::ResourceCompression::None) {
return std::string(rsrc->data, rsrc->data + *rsrc->size);
return std::string((const char*)rsrc->data, (const char*)rsrc->data + *rsrc->size);
}
auto bytes = compressionDecode(static_cast<CompressionMethod>(rsrc->compression),
std::span<const uint8_t>(rsrc->data, rsrc->data + *rsrc->size));
return std::string(bytes.data(), bytes.data() + bytes.size());
auto bytes = compressionDecode(
static_cast<CompressionMethod>(rsrc->compression),
BytesView((const std::byte*)rsrc->data, (const std::byte*)rsrc->data + *rsrc->size));
return std::string((const char*)bytes.data(), (const char*)bytes.data() + bytes.size());
}

inline const std::string& loadResourceTextCached(std::string name, bool emptyOk = false) {
Expand Down
Loading

0 comments on commit ce42fa9

Please sign in to comment.