Skip to content

Commit

Permalink
Changed endianness to implicitly resolve system byte order.
Browse files Browse the repository at this point in the history
  • Loading branch information
crowbar27 committed Oct 10, 2023
1 parent c125d1e commit b8a4816
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 24 deletions.
15 changes: 11 additions & 4 deletions datraw/datraw/endianness.h
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
// <copyright file="endianness.h" company="Visualisierungsinstitut der Universität Stuttgart">
// Copyright © 2017 Visualisierungsinstitut der Universität Stuttgart. Alle Rechte vorbehalten.
// Copyright © 2017 - 2023 Visualisierungsinstitut der Universität Stuttgart. Alle Rechte vorbehalten.
// </copyright>
// <author>Christoph Müller</author>

#pragma once

#include <cinttypes>


namespace datraw {

/// <summary>
/// Represents endianness of a data set or system.
/// </summary>
enum class endianness {
enum class endianness : std::uint32_t {

/// <summary>
/// The least significant byte is stored first, like on Intel x86 or
/// AMD64 architectures.
/// </summary>
little = 1,
little = 0x03020100,

/// <summary>
/// The most significant byte is stored first, like on Sun Spark.
/// </summary>
big
big = 0x00010203,

/// <summary>
/// The native endian of the system the code is running on.
/// </summary>
system = '\0\1\2\3'
};

}
8 changes: 7 additions & 1 deletion datraw/datraw/info.h
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,13 @@ namespace datraw {
/// <summary>
/// Answer the endianness of the system we are running on.
/// </summary>
static datraw::endianness sys_endianness(void);
/// <remarks>
/// This method is provided for backwards compatibility. New code should
/// use <see cref="datraw::endianness::system" />.
/// </remarks>
static constexpr datraw::endianness sys_endianness(void) noexcept {
return endianness::system;
}

/// <summmary>
/// Split a range of characters at positions designated by
Expand Down
19 changes: 0 additions & 19 deletions datraw/datraw/info.inl
Original file line number Diff line number Diff line change
Expand Up @@ -832,25 +832,6 @@ template<class I> I datraw::info<C>::skip_spaces(I it, I end) {
}


/*
* datraw::info<C>::sys_endianness
*/
template<class C>
datraw::endianness datraw::info<C>::sys_endianness(void) {
static const union {
std::uint32_t u;
std::uint8_t b[4];
} BYTES = { 0x01020304 };

switch (BYTES.b[0]) {
case 0x01: return endianness::big;
case 0x04: return endianness::little;
default: throw std::runtime_error("Unknown endianness. This should "
"never happen!");
}
}


/*
* datraw::info<C>::tokenise
*/
Expand Down

0 comments on commit b8a4816

Please sign in to comment.