AUI Framework  develop
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
serializable.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//
   13// Created by Alex2772 on 2/4/2022.
   14//
   15
   16#pragma once
   17
   18#include "values.h"
   19#include "types.h"
   20#include <AUI/Common/SharedPtr.h>
   21
   22class IInputStream;
   23class IOutputStream;
   24
   25template<typename T, typename T2 = void>
   27
   28namespace aui {
   29
   30    template<typename T>
   31    constexpr bool is_serializable = aui::is_complete<ASerializable<T>>;
   32
   33    template<typename T>
   34    inline void serialize(aui::no_escape<IOutputStream> dst, const T& t) {
   35        static_assert(is_serializable<T>, "T is not serializable");
   36        ASerializable<T>::write(*dst, t);
   37    }
   38
   39    template<typename T>
   40    inline void deserialize(aui::no_escape<IInputStream> from, T& t) {
   41        static_assert(is_serializable<T>, "T is not serializable");
   42        ASerializable<T>::read(*from, t);
   43    }
   44
   45    template<typename T>
   46    inline T deserialize(aui::no_escape<IInputStream> from) {
   47        T t;
   48        deserialize<T>(from, t);
   49        return t;
   50    }
   51}
   52
   53#include <AUI/IO/IInputStream.h>
   54#include <AUI/IO/IOutputStream.h>
   55
   56namespace aui {
   57    template<typename T>
   58    struct raw_serializable {
   59        static void write(IOutputStream& os, const T& value) {
   60            os.write(reinterpret_cast<const char*>(&value), sizeof(value));
   61        }
   62        static void read(IInputStream& is, T& t) {
   63            is.readExact(reinterpret_cast<char*>(&t), sizeof(T));
   64        }
   65    };
   66
   67    //NOLINTBEGIN(cppcoreguidelines-rvalue-reference-param-not-moved)
   68    template<typename T>
   69    struct serialize_sized {
   70        T* value;
   71
   72        serialize_sized(T& value): value(&value) {}
   73        serialize_sized(T&& value): value(&value) {}
   74    };
   75    template<typename T>
   76    struct serialize_raw {
   77        T* value;
   78
   79        serialize_raw(T& value): value(&value) {}
   80        serialize_raw(T&& value): value(&value) {}
   81    };
   82    //NOLINTEND(cppcoreguidelines-rvalue-reference-param-not-moved)
   83}
   84
   85// ints, floats, doubles, etc...
   86template<typename T>
   87struct ASerializable<T, std::enable_if_t<std::is_arithmetic_v<T>>>: aui::raw_serializable<T> {};
   88
   89// _<SerializableType>
   90template<typename T>
   91struct ASerializable<_<T>> {
   92    static void write(IOutputStream& os, const _<T>& value) {
   93        aui::serialize(os, *value);
   94    }
   95    static void read(IInputStream& is, _<T>& t) {
   96        t = _new<T>(std::move(aui::deserialize<T>(is)));
   97    }
   98};
   99
  100// std::string
  101template<>
  102struct ASerializable<std::string> {
  103    static void write(IOutputStream& os, const std::string& value) {
  104        os.write(value.data(), value.length());
  105    }
  106};
  107
  108template<>
  109struct ASerializable<std::string_view> {
  110    static void write(IOutputStream& os, std::string_view value) {
  111        os.write(value.data(), value.length());
  112    }
  113};
  114
  115// input stream
  116template<typename T>
  117struct ASerializable<T, std::enable_if_t<std::is_base_of_v<IInputStream, T>>> {
  118    static void write(IOutputStream& os, const T& value) {
  119        char buf[0x1000];
  120        for (size_t r; (r = const_cast<T&>(value).read(buf, sizeof(buf))) != 0;) {
  121            os.write(buf, r);
  122        }
  123    }
  124};
  125
  126// string literal
  127template<int L>
  128struct ASerializable<char[L]> {
  129    static void write(IOutputStream& os, const char* value) {
  130        os.write(value, L - 1);
  131    }
  132};
  133
  134// also string literal, but unknown blob size
  135template<>
  136struct ASerializable<const char*> {
  137    static void write(IOutputStream& os, const char* value) {
  138        os.write(value, std::strlen(value));
  139    }
  140};
  141
  142
  143template<>
  145    static void write(IOutputStream& os, const AString& value) {
  146        aui::serialize(os, value.toStdString());
  147    }
  148};
  149
  150
  151template<typename T>
  152struct ASerializable<aui::serialize_sized<T>> {
  153    static void write(IOutputStream& os, aui::serialize_sized<T> t) {
  154        os << std::uint32_t(t.value->size());
  155        os.write(reinterpret_cast<const char*>(t.value->data()), sizeof(*t.value->data()) * t.value->size());
  156    }
  157    static void read(IInputStream& is, aui::serialize_sized<T>& t) {
  158        std::uint32_t s;
  159        is >> s;
  160        t.value->resize(s);
  161        is.read(reinterpret_cast<char*>(t.value->data()), sizeof(*t.value->data()) * t.value->size());
  162    }
  163};
  164
  165template<typename T>
  166struct ASerializable<aui::serialize_raw<T>> {
  167    static void write(IOutputStream& os, aui::serialize_raw<T> t) {
  168        os.write(reinterpret_cast<const char*>(t.value), sizeof(T));
  169    }
  170    static void read(IInputStream& is, aui::serialize_raw<T>& t) {
  171        is.read(reinterpret_cast<char*>(t.value), sizeof(T));
  172    }
  173};
Represents a Unicode character string.
Definition AString.h:38
Represents an input stream.
Definition IInputStream.h:26
void readExact(char *dst, size_t size)
Reads exact size bytes from stream. Blocking (waiting for new data) is allowed.
Definition IInputStream.h:65
virtual size_t read(char *dst, size_t size)=0
Reads up to size bytes from stream. Blocking (waiting for new data) is allowed.
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.
An std::weak_ptr with AUI extensions.
Definition SharedPtrTypes.h:179
type_of< T > t
Selects views that are of the specified C++ types.
Definition type_of.h:71
constexpr bool is_complete
Determines whether T is complete or not.
Definition types.h:23
Definition serializable.h:26
Does not allow escaping, allowing to accept lvalue ref, rvalue ref, shared_ptr and etc without overhe...
Definition values.h:128
Definition serializable.h:58
Definition serializable.h:76
Definition serializable.h:69