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
  108// input stream
  109template<typename T>
  110struct ASerializable<T, std::enable_if_t<std::is_base_of_v<IInputStream, T>>> {
  111    static void write(IOutputStream& os, const T& value) {
  112        char buf[0x1000];
  113        for (size_t r; (r = const_cast<T&>(value).read(buf, sizeof(buf))) != 0;) {
  114            os.write(buf, r);
  115        }
  116    }
  117};
  118
  119// string literal
  120template<int L>
  121struct ASerializable<char[L]> {
  122    static void write(IOutputStream& os, const char* value) {
  123        os.write(value, L - 1);
  124    }
  125};
  126
  127// also string literal, but unknown blob size
  128template<>
  129struct ASerializable<const char*> {
  130    static void write(IOutputStream& os, const char* value) {
  131        os.write(value, std::strlen(value));
  132    }
  133};
  134
  135
  136template<>
  138    static void write(IOutputStream& os, const AString& value) {
  139        aui::serialize(os, value.toStdString());
  140    }
  141};
  142
  143
  144template<typename T>
  145struct ASerializable<aui::serialize_sized<T>> {
  146    static void write(IOutputStream& os, aui::serialize_sized<T> t) {
  147        os << std::uint32_t(t.value->size());
  148        os.write(reinterpret_cast<const char*>(t.value->data()), sizeof(*t.value->data()) * t.value->size());
  149    }
  150    static void read(IInputStream& is, aui::serialize_sized<T>& t) {
  151        std::uint32_t s;
  152        is >> s;
  153        t.value->resize(s);
  154        is.read(reinterpret_cast<char*>(t.value->data()), sizeof(*t.value->data()) * t.value->size());
  155    }
  156};
  157
  158template<typename T>
  159struct ASerializable<aui::serialize_raw<T>> {
  160    static void write(IOutputStream& os, aui::serialize_raw<T> t) {
  161        os.write(reinterpret_cast<const char*>(t.value), sizeof(T));
  162    }
  163    static void read(IInputStream& is, aui::serialize_raw<T>& t) {
  164        is.read(reinterpret_cast<char*>(t.value), sizeof(T));
  165    }
  166};
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