TrinityCore
Loading...
Searching...
No Matches
FourCC.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef TRINITYCORE_FOURCC_H
19#define TRINITYCORE_FOURCC_H
20
21#include "Define.h"
22#include "StringFormatFwd.h"
23#include <string_view>
24
25template <typename Tag>
26struct FourCC
27{
29
30 constexpr FourCC() : Value(0) { }
31 explicit constexpr FourCC(uint32 value) : Value(value) { }
32
33 inline static constexpr FourCC FromString(std::string_view text)
34 {
35 uint32 uintValue = 0;
36 for (uint8 c : text)
37 {
38 uintValue <<= 8;
39 uintValue |= c;
40 }
41 return FourCC(uintValue);
42 }
43
44 constexpr bool operator==(FourCC const& right) const = default;
45
46 constexpr std::strong_ordering operator<=>(FourCC const& right) const = default;
47
48 constexpr operator uint32() const { return Value; }
49
50 template <typename Other>
51 constexpr bool operator==(FourCC<Other> const& right) const = delete;
52
53 template <typename Other>
54 constexpr std::strong_ordering operator<=>(FourCC<Other> const& right) const = delete;
55};
56
58{
59consteval uint32 operator""_fourcc(char const* chars, std::size_t length)
60{
61 if (length > sizeof(uint32))
62 throw "Text can only be max 4 characters long";
63
64 return FourCC<void>::FromString({ chars, length });
65}
66}
67
68template <>
70{
71 template <typename FormatContext>
72 auto format(FourCC<void> fcc, FormatContext& ctx) const -> decltype(ctx.out());
73};
74
75template <typename Tag>
76struct fmt::formatter<FourCC<Tag>, char, void> : fmt::formatter<FourCC<void>, char, void>
77{
78 template <typename FormatContext>
79 auto format(FourCC<Tag> fcc, FormatContext& ctx) const -> decltype(ctx.out())
80 {
81 return fmt::formatter<FourCC<void>, char, void>::format(FourCC<void>(fcc.Value), ctx);
82 }
83};
84
85#endif // TRINITYCORE_FOURCC_H
uint8_t uint8
Definition Define.h:156
uint32_t uint32
Definition Define.h:154
constexpr FourCC(uint32 value)
Definition FourCC.h:31
uint32 Value
Definition FourCC.h:28
constexpr bool operator==(FourCC< Other > const &right) const =delete
constexpr std::strong_ordering operator<=>(FourCC< Other > const &right) const =delete
constexpr FourCC()
Definition FourCC.h:30
constexpr std::strong_ordering operator<=>(FourCC const &right) const =default
constexpr bool operator==(FourCC const &right) const =default
static constexpr FourCC FromString(std::string_view text)
Definition FourCC.h:33
auto format(FourCC< Tag > fcc, FormatContext &ctx) const -> decltype(ctx.out())
Definition FourCC.h:79