Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
Endian.h
Go to the documentation of this file.
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#ifndef _Stroika_Foundation_Common_Endian_h_
5#define _Stroika_Foundation_Common_Endian_h_ 1
6
7#include "Stroika/Foundation/StroikaPreComp.h"
8
9#include <concepts>
10#include <cstdint>
11
12#include "Stroika/Foundation/Common/Common.h"
13
14/**
15 * \file
16 *
17 * \note Code-Status: <a href="Code-Status.md#Beta">Beta</a>
18 */
19
21
22 /**
23 * \brief in principle complicated question of correspondence between bit and byte and word numbering, but often fairly simple - similar to but subtler version of std::endian
24 *
25 * @see http://en.wikipedia.org/wiki/Endianness
26 * @see https://en.cppreference.com/w/cpp/types/endian
27 *
28 * \note cannot use values from std::endian for matching cases (eBig/eLittle) because these values are impl defined
29 */
30 enum class Endian {
31 eBigByte, // byte-swapped big-endian
32 eBigWord, // word-swapped big-endian
33
34 eLittleByte, // byte-swapped little-endian (e.g. x86)
35 eLittleWord, // word-swapped little-endian
36
37 eBig = eBigByte, // (std::endian::big)
38 eLittle = eLittleByte, // e.g. x86 (std::endian::little)
39 ePDP = eLittleWord,
40
41 eX86 = eLittle, // so common, worth an alias
42 eARM = eLittle // ""
43 };
44
45 /**
46 * \brief returns native (std::endian::native) Endianness flag. Can be complicated (mixed, etc). But often very simple (e.g. Endian::eLittle) and for the
47 * simple cases, based on std::endian::native.
48 *
49 * if (endian::native == endian::little) {
50 * return Endian::eLittle;
51 * }
52 * else if (endian::native == endian::big) {
53 * return Endian::eBig;
54 * }
55 * else complicated...
56 */
57 constexpr Endian GetEndianness ();
58
59 /**
60 * Utility to convert endianness. Logically this can be defined on any numeric
61 * integer type, but for now is restricted to uint16_t, uint32_t;
62 *
63 * Its common, for example in networking, when you know the endianness of a source, or target data structure,
64 * and you can find YOUR machines endianness via GetEndianness (), this lets you automate the mapping (portably).
65 *
66 * \par Example Usage
67 * \code
68 * EXPECT_EQ (EndianConverter<uint16_t> (0xAABB, Endian::eBig, Endian::eLittle), 0xBBAA);
69 * uint16_t useThisNumber = EndianConverter<uint16_t> (0xAABB, Endian::eBig, GetEndianness ());
70 * \endcode
71 *
72 * @see https://en.cppreference.com/w/cpp/numeric/byteswap
73 * @see GetEndianness
74 */
75 template <integral T>
76 constexpr T EndianConverter (T value, Endian from, Endian to);
77
78}
79
80/*
81 ********************************************************************************
82 ***************************** Implementation Details ***************************
83 ********************************************************************************
84 */
85#include "Endian.inl"
86
87#endif /*_Stroika_Foundation_Common_Endian_h_*/
Endian
in principle complicated question of correspondence between bit and byte and word numbering,...
Definition Endian.h:30
constexpr Endian GetEndianness()
returns native (std::endian::native) Endianness flag. Can be complicated (mixed, etc)....
Definition Endian.inl:25
constexpr T EndianConverter(T value, Endian from, Endian to)
Definition Endian.inl:60