AUI Framework  develop
Cross-platform base for C++ UI apps
Loading...
Searching...
No Matches
AUuid.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
   15#include <array>
   16#include "AString.h"
   17#include "AException.h"
   18#include <AUI/Traits/serializable.h>
   19#include <AUI/Traits/memory.h>
   20
   21
   26class API_AUI_CORE AUuid {
   27private:
   28    std::array<uint8_t, 16> mData;
   29
   30    static uint8_t fromHex(char c);
   31
   32public:
   33    AUuid() {
   34        aui::zero(mData);
   35    }
   36    explicit AUuid(const std::array<uint8_t, 16>& data) : mData(data) {}
   37
   38
   39    [[nodiscard]]
   40    const std::array<uint8_t, 16>& data() const noexcept {
   41        return mData;
   42    }
   43
   44    [[nodiscard]]
   45    std::array<uint8_t, 16>& data() noexcept {
   46        return mData;
   47    }
   48
   57    AUuid(const AString& s);
   58
   65    static AUuid fromString(const AString& string);
   66
   70    [[nodiscard]] AString toString() const;
   71
   75    [[nodiscard]] AString toRawString() const;
   76
   77    operator AString() const {
   78        return toString();
   79    }
   80
   81
   82    bool operator<(const AUuid& o) const {
   83        const auto* self = reinterpret_cast<const unsigned*>(mData.data());
   84        const auto* other = reinterpret_cast<const unsigned*>(o.mData.data());
   85
   86        for (unsigned i = 0; i < sizeof(mData) / sizeof(unsigned); ++i) {
   87            if (self[i] != other[i]) {
   88                return self[i] < other[i];
   89            }
   90        };
   91        return false;
   92    }
   93
   94    bool operator==(const AUuid& other) const {
   95        return mData == other.mData;
   96    }
   97
   98    bool operator!=(const AUuid& other) const {
   99        return mData != other.mData;
  100    }
  101
  102};
  103
  104inline std::ostream& operator<<(std::ostream& o, const AUuid& u) {
  105    o << "urn:uuid:" << u.toString();
  106    return o;
  107}
  108
  109
  110class AUuidException: public AException {
  111private:
  112    friend class AUuid;
  113
  114    using AException::AException;
  115public:
  116};
  117
  118template<>
Represents a Unicode character string.
Definition AString.h:38
Definition AUuid.h:110
Implements universally unique identifier (UUID)
Definition AUuid.h:26
AString toRawString() const
AString toString() const
static AUuid fromString(const AString &string)
AUuid(const AString &s)
Definition serializable.h:26
Definition serializable.h:58