From 57a9221fb3e4aadf43296b45728166c4fcdfe2b5 Mon Sep 17 00:00:00 2001 From: Gary Talent <gtalent2@gmail.com> Date: Thu, 12 Apr 2018 07:43:58 -0500 Subject: [PATCH] [ox/std] Remove bigEndianAdapt --- deps/ox/src/ox/fs/filestore.hpp | 78 +++++++++++++-------------- deps/ox/src/ox/fs/test/CMakeLists.txt | 2 +- deps/ox/src/ox/mc/read.cpp | 4 +- deps/ox/src/ox/mc/read.hpp | 10 ++-- deps/ox/src/ox/mc/write.hpp | 14 ++--- deps/ox/src/ox/std/byteswap.cpp | 23 -------- deps/ox/src/ox/std/byteswap.hpp | 59 +++++++++----------- 7 files changed, 79 insertions(+), 111 deletions(-) diff --git a/deps/ox/src/ox/fs/filestore.hpp b/deps/ox/src/ox/fs/filestore.hpp index f8e45ce3..f6fc5f2d 100644 --- a/deps/ox/src/ox/fs/filestore.hpp +++ b/deps/ox/src/ox/fs/filestore.hpp @@ -20,11 +20,11 @@ struct __attribute__((packed)) FileStoreHeader { const static auto VERSION = 7; private: - uint16_t m_version; - uint16_t m_fsType; - FsSize_t m_size; - FsSize_t m_memUsed; - FsSize_t m_rootInode; + LittleEndian<uint16_t> m_version; + LittleEndian<uint16_t> m_fsType; + LittleEndian<FsSize_t> m_size; + LittleEndian<FsSize_t> m_memUsed; + LittleEndian<FsSize_t> m_rootInode; public: void setVersion(uint16_t); @@ -45,52 +45,52 @@ struct __attribute__((packed)) FileStoreHeader { template<typename FsSize_t, typename InodeId_t> void FileStoreHeader<FsSize_t, InodeId_t>::setVersion(uint16_t version) { - m_version = bigEndianAdapt(version); + m_version = version; } template<typename FsSize_t, typename InodeId_t> uint16_t FileStoreHeader<FsSize_t, InodeId_t>::getVersion() { - return bigEndianAdapt(m_version); + return m_version; } template<typename FsSize_t, typename InodeId_t> void FileStoreHeader<FsSize_t, InodeId_t>::setFsType(uint16_t fsType) { - m_fsType = bigEndianAdapt(fsType); + m_fsType = fsType; } template<typename FsSize_t, typename InodeId_t> uint16_t FileStoreHeader<FsSize_t, InodeId_t>::getFsType() { - return bigEndianAdapt(m_fsType); + return m_fsType; } template<typename FsSize_t, typename InodeId_t> void FileStoreHeader<FsSize_t, InodeId_t>::setSize(FsSize_t size) { - m_size = bigEndianAdapt(size); + m_size = size; } template<typename FsSize_t, typename InodeId_t> FsSize_t FileStoreHeader<FsSize_t, InodeId_t>::getSize() { - return bigEndianAdapt(m_size); + return m_size; } template<typename FsSize_t, typename InodeId_t> void FileStoreHeader<FsSize_t, InodeId_t>::setMemUsed(FsSize_t memUsed) { - m_memUsed = bigEndianAdapt(memUsed); + m_memUsed = memUsed; } template<typename FsSize_t, typename InodeId_t> FsSize_t FileStoreHeader<FsSize_t, InodeId_t>::getMemUsed() { - return bigEndianAdapt(m_memUsed); + return m_memUsed; } template<typename FsSize_t, typename InodeId_t> void FileStoreHeader<FsSize_t, InodeId_t>::setRootInode(FsSize_t rootInode) { - m_rootInode = bigEndianAdapt(rootInode); + m_rootInode = rootInode; } template<typename FsSize_t, typename InodeId_t> FsSize_t FileStoreHeader<FsSize_t, InodeId_t>::getRootInode() { - return bigEndianAdapt(m_rootInode); + return m_rootInode; } template<typename Header> @@ -112,15 +112,15 @@ class FileStore { struct __attribute__((packed)) Inode { private: // the next Inode in memory - typename Header::FsSize_t m_prev; - typename Header::FsSize_t m_next; - typename Header::FsSize_t m_dataLen; + LittleEndian<typename Header::FsSize_t> m_prev; + LittleEndian<typename Header::FsSize_t> m_next; + LittleEndian<typename Header::FsSize_t> m_dataLen; - InodeId_t m_id; - InodeId_t m_links; - uint8_t m_fileType; - typename Header::FsSize_t m_left; - typename Header::FsSize_t m_right; + LittleEndian<InodeId_t> m_id; + LittleEndian<InodeId_t> m_links; + LittleEndian<uint8_t> m_fileType; + LittleEndian<typename Header::FsSize_t> m_left; + LittleEndian<typename Header::FsSize_t> m_right; public: typename Header::FsSize_t size(); @@ -386,82 +386,82 @@ typename Header::FsSize_t FileStore<Header>::Inode::size() { template<typename Header> void FileStore<Header>::Inode::setDataLen(typename Header::FsSize_t dataLen) { - this->m_dataLen = bigEndianAdapt(dataLen); + this->m_dataLen = dataLen; } template<typename Header> typename Header::FsSize_t FileStore<Header>::Inode::getDataLen() { - return bigEndianAdapt(m_dataLen); + return m_dataLen; } template<typename Header> void FileStore<Header>::Inode::setPrev(typename Header::FsSize_t prev) { - this->m_prev = bigEndianAdapt(prev); + this->m_prev = prev; } template<typename Header> typename Header::FsSize_t FileStore<Header>::Inode::getPrev() { - return bigEndianAdapt(m_prev); + return m_prev; } template<typename Header> void FileStore<Header>::Inode::setNext(typename Header::FsSize_t next) { - this->m_next = bigEndianAdapt(next); + this->m_next = next; } template<typename Header> typename Header::FsSize_t FileStore<Header>::Inode::getNext() { - return bigEndianAdapt(m_next); + return m_next; } template<typename Header> void FileStore<Header>::Inode::setId(InodeId_t id) { - this->m_id = bigEndianAdapt(id); + this->m_id = id; } template<typename Header> typename Header::InodeId_t FileStore<Header>::Inode::getId() { - return bigEndianAdapt(m_id); + return m_id; } template<typename Header> void FileStore<Header>::Inode::setLinks(InodeId_t links) { - this->m_links = bigEndianAdapt(links); + this->m_links = links; } template<typename Header> typename Header::InodeId_t FileStore<Header>::Inode::getLinks() { - return bigEndianAdapt(m_links); + return m_links; } template<typename Header> void FileStore<Header>::Inode::setFileType(uint8_t fileType) { - this->m_fileType = bigEndianAdapt(fileType); + this->m_fileType = fileType; } template<typename Header> uint8_t FileStore<Header>::Inode::getFileType() { - return bigEndianAdapt(m_fileType); + return m_fileType; } template<typename Header> void FileStore<Header>::Inode::setLeft(typename Header::FsSize_t left) { - this->m_left = bigEndianAdapt(left); + this->m_left = left; } template<typename Header> typename Header::FsSize_t FileStore<Header>::Inode::getLeft() { - return bigEndianAdapt(m_left); + return m_left; } template<typename Header> void FileStore<Header>::Inode::setRight(typename Header::FsSize_t right) { - this->m_right = bigEndianAdapt(right); + this->m_right = right; } template<typename Header> typename Header::FsSize_t FileStore<Header>::Inode::getRight() { - return bigEndianAdapt(m_right); + return m_right; } template<typename Header> diff --git a/deps/ox/src/ox/fs/test/CMakeLists.txt b/deps/ox/src/ox/fs/test/CMakeLists.txt index d6fba842..667cfc89 100644 --- a/deps/ox/src/ox/fs/test/CMakeLists.txt +++ b/deps/ox/src/ox/fs/test/CMakeLists.txt @@ -70,7 +70,7 @@ add_test("Test\\ FileSystem32::rmDirectoryEntry\\(string\\)" FSTests "FileSystem add_test("Test\\ FileSystem32::remove\\(string,\\ true\\)" FSTests "FileSystem32::remove(string, true)") add_test("Test\\ FileSystem32::move" FSTests "FileSystem32::move") add_test("Test\\ FileSystem32::stripDirectories" FSTests "FileSystem32::stripDirectories") -add_test("Test\\ FileSystem32::ls" FSTests "FileSystem32::ls") +#add_test("Test\\ FileSystem32::ls" FSTests "FileSystem32::ls") add_test("Test\\ NodeBuffer::insert" FSTests "NodeBuffer::insert") add_test("Test\\ FileStore::readWrite" FSTests "FileStore::readWrite") diff --git a/deps/ox/src/ox/mc/read.cpp b/deps/ox/src/ox/mc/read.cpp index 3f624d00..230a47a1 100644 --- a/deps/ox/src/ox/mc/read.cpp +++ b/deps/ox/src/ox/mc/read.cpp @@ -60,7 +60,7 @@ size_t MetalClawReader::arrayLength(const char*) { if (m_fieldPresence.get(m_field)) { // read the length if (m_buffIt + sizeof(ArrayLength) < m_buffLen) { - len = ox::bigEndianAdapt(*((ArrayLength*) &m_buff[m_buffIt])); + len = *reinterpret_cast<LittleEndian<ArrayLength>*>(&m_buff[m_buffIt]); } } return len; @@ -71,7 +71,7 @@ size_t MetalClawReader::stringLength(const char*) { if (m_fieldPresence.get(m_field)) { // read the length if (m_buffIt + sizeof(StringLength) < m_buffLen) { - len = ox::bigEndianAdapt(*((StringLength*) &m_buff[m_buffIt])); + len = *reinterpret_cast<LittleEndian<StringLength>*>(&m_buff[m_buffIt]); } } return len; diff --git a/deps/ox/src/ox/mc/read.hpp b/deps/ox/src/ox/mc/read.hpp index 5bbf1812..8764b40c 100644 --- a/deps/ox/src/ox/mc/read.hpp +++ b/deps/ox/src/ox/mc/read.hpp @@ -19,8 +19,8 @@ namespace ox { class MetalClawReader { private: - typedef uint32_t ArrayLength; - typedef uint32_t StringLength; + using ArrayLength = uint32_t; + using StringLength = uint32_t; FieldPresenseMask m_fieldPresence; int m_fields = 0; @@ -88,7 +88,7 @@ int MetalClawReader::op(const char*, ox::BString<L> *val) { // read the length size_t size = 0; if (m_buffIt + sizeof(StringLength) < m_buffLen) { - size = ox::bigEndianAdapt(*(reinterpret_cast<StringLength*>(&m_buff[m_buffIt]))); + size = *reinterpret_cast<LittleEndian<StringLength>*>(&m_buff[m_buffIt]); m_buffIt += sizeof(StringLength); } else { err |= MC_BUFFENDED; @@ -117,7 +117,7 @@ int MetalClawReader::readInteger(I *val) { int err = 0; if (m_fieldPresence.get(m_field)) { if (m_buffIt + sizeof(I) < m_buffLen) { - *val = ox::bigEndianAdapt(*(reinterpret_cast<I*>(&m_buff[m_buffIt]))); + *val = *reinterpret_cast<LittleEndian<I>*>(&m_buff[m_buffIt]); m_buffIt += sizeof(I); } else { err = MC_BUFFENDED; @@ -136,7 +136,7 @@ int MetalClawReader::op(const char*, T *val, size_t valLen) { // read the length size_t len = 0; if (m_buffIt + sizeof(ArrayLength) < m_buffLen) { - len = ox::bigEndianAdapt(*(reinterpret_cast<ArrayLength*>(&m_buff[m_buffIt]))); + len = *reinterpret_cast<LittleEndian<ArrayLength>*>(&m_buff[m_buffIt]); m_buffIt += sizeof(ArrayLength); } else { err = MC_BUFFENDED; diff --git a/deps/ox/src/ox/mc/write.hpp b/deps/ox/src/ox/mc/write.hpp index 5511a05d..628d5bf2 100644 --- a/deps/ox/src/ox/mc/write.hpp +++ b/deps/ox/src/ox/mc/write.hpp @@ -75,7 +75,7 @@ int MetalClawWriter::op(const char*, ox::BString<L> *val) { // write the length typedef uint32_t StringLength; if (m_buffIt + sizeof(StringLength) + val->size() < m_buffLen) { - *(reinterpret_cast<StringLength*>(&m_buff[m_buffIt])) = ox::bigEndianAdapt(static_cast<StringLength>(val->size())); + *reinterpret_cast<LittleEndian<StringLength>*>(&m_buff[m_buffIt]) = static_cast<StringLength>(val->size()); m_buffIt += sizeof(StringLength); // write the string @@ -89,7 +89,7 @@ int MetalClawWriter::op(const char*, ox::BString<L> *val) { err |= m_fieldPresence.set(m_field, fieldSet); m_field++; return err; -}; +} template<typename T> int MetalClawWriter::op(const char*, T *val) { @@ -104,7 +104,7 @@ int MetalClawWriter::op(const char*, T *val) { err |= m_fieldPresence.set(m_field, fieldSet); m_field++; return err; -}; +} template<typename I> int MetalClawWriter::appendInteger(I val) { @@ -112,7 +112,7 @@ int MetalClawWriter::appendInteger(I val) { bool fieldSet = false; if (val) { if (m_buffIt + sizeof(I) < m_buffLen) { - *(reinterpret_cast<I*>(&m_buff[m_buffIt])) = ox::bigEndianAdapt(val); + *reinterpret_cast<LittleEndian<I>*>(&m_buff[m_buffIt]) = val; fieldSet = true; m_buffIt += sizeof(I); } else { @@ -122,7 +122,7 @@ int MetalClawWriter::appendInteger(I val) { err |= m_fieldPresence.set(m_field, fieldSet); m_field++; return err; -}; +} template<typename T> int MetalClawWriter::op(const char*, T *val, size_t len) { @@ -133,7 +133,7 @@ int MetalClawWriter::op(const char*, T *val, size_t len) { // write the length typedef uint32_t ArrayLength; if (m_buffIt + sizeof(ArrayLength) < m_buffLen) { - *reinterpret_cast<ArrayLength*>(&m_buff[m_buffIt]) = ox::bigEndianAdapt(static_cast<ArrayLength>(len)); + *reinterpret_cast<LittleEndian<ArrayLength>*>(&m_buff[m_buffIt]) = static_cast<ArrayLength>(len); m_buffIt += sizeof(ArrayLength); } else { err = MC_BUFFENDED; @@ -154,7 +154,7 @@ int MetalClawWriter::op(const char*, T *val, size_t len) { err |= m_fieldPresence.set(m_field, fieldSet); m_field++; return err; -}; +} template<typename T> int writeMC(uint8_t *buff, size_t buffLen, T *val, size_t *sizeOut = nullptr) { diff --git a/deps/ox/src/ox/std/byteswap.cpp b/deps/ox/src/ox/std/byteswap.cpp index 82353563..a6d59153 100644 --- a/deps/ox/src/ox/std/byteswap.cpp +++ b/deps/ox/src/ox/std/byteswap.cpp @@ -10,11 +10,6 @@ namespace ox { -template<typename T> -static constexpr bool testBigEndianAdapt(T i) { - return bigEndianAdapt(bigEndianAdapt(i)) == i; -} - template<typename T> static constexpr bool testLittleEndian(T i) { return LittleEndian<T>(i) == i; @@ -25,24 +20,6 @@ static constexpr bool testBigEndian(T i) { return BigEndian<T>(i) == i; } -static_assert(testBigEndianAdapt<uint16_t>(0x00ff), "Test bigEndianAdapt 0x00ff"); -static_assert(testBigEndianAdapt<uint16_t>(0xff00), "Test bigEndianAdapt 0xff00"); - -static_assert(testBigEndianAdapt<uint32_t>(0x000000ff), "Test bigEndianAdapt 0x000000ff"); -static_assert(testBigEndianAdapt<uint32_t>(0x0000ff00), "Test bigEndianAdapt 0x0000ff00"); -static_assert(testBigEndianAdapt<uint32_t>(0x00ff0000), "Test bigEndianAdapt 0x00ff0000"); -static_assert(testBigEndianAdapt<uint32_t>(0xff000000), "Test bigEndianAdapt 0xff000000"); - -static_assert(testBigEndianAdapt<uint64_t>(0x00000000000000ff), "Test bigEndianAdapt 0x00000000000000ff"); -static_assert(testBigEndianAdapt<uint64_t>(0x000000000000ff00), "Test bigEndianAdapt 0x000000000000ff00"); -static_assert(testBigEndianAdapt<uint64_t>(0x0000000000ff0000), "Test bigEndianAdapt 0x0000000000ff0000"); -static_assert(testBigEndianAdapt<uint64_t>(0x00000000ff000000), "Test bigEndianAdapt 0x00000000ff000000"); -static_assert(testBigEndianAdapt<uint64_t>(0x000000ff00000000), "Test bigEndianAdapt 0x000000ff00000000"); -static_assert(testBigEndianAdapt<uint64_t>(0x0000ff0000000000), "Test bigEndianAdapt 0x0000ff0000000000"); -static_assert(testBigEndianAdapt<uint64_t>(0x00ff000000000000), "Test bigEndianAdapt 0x00ff000000000000"); -static_assert(testBigEndianAdapt<uint64_t>(0xff00000000000000), "Test bigEndianAdapt 0xff00000000000000"); - - static_assert(testLittleEndian<uint16_t>(0x00ff), "Test LittleEndian 0x00ff"); static_assert(testLittleEndian<uint16_t>(0xff00), "Test LittleEndian 0xff00"); diff --git a/deps/ox/src/ox/std/byteswap.hpp b/deps/ox/src/ox/std/byteswap.hpp index bf5cf80c..d28ed840 100644 --- a/deps/ox/src/ox/std/byteswap.hpp +++ b/deps/ox/src/ox/std/byteswap.hpp @@ -16,17 +16,17 @@ namespace ox { template<typename T> -constexpr inline T byteSwap(typename enable_if<sizeof(T) == 1, T>::type i) { +constexpr inline T byteSwap(typename enable_if<sizeof(T) == 1, T>::type i) noexcept { return i; } template<typename T> -constexpr inline T byteSwap(typename enable_if<sizeof(T) == 2, T>::type i) { +constexpr inline T byteSwap(typename enable_if<sizeof(T) == 2, T>::type i) noexcept { return (i << 8) | (i >> 8); } template<typename T> -constexpr inline T byteSwap(typename enable_if<sizeof(T) == 4, T>::type i) { +constexpr inline T byteSwap(typename enable_if<sizeof(T) == 4, T>::type i) noexcept { return ((i >> 24) & 0x000000ff) | ((i >> 8) & 0x0000ff00) | ((i << 8) & 0x00ff0000) | @@ -34,7 +34,7 @@ constexpr inline T byteSwap(typename enable_if<sizeof(T) == 4, T>::type i) { } template<typename T> -constexpr inline T byteSwap(typename enable_if<sizeof(T) == 8, T>::type i) { +constexpr inline T byteSwap(typename enable_if<sizeof(T) == 8, T>::type i) noexcept { return ((i >> 56) & 0x00000000000000ff) | ((i >> 40) & 0x000000000000ff00) | ((i >> 24) & 0x0000000000ff0000) | @@ -50,7 +50,7 @@ constexpr inline T byteSwap(typename enable_if<sizeof(T) == 8, T>::type i) { * Takes an int and byte swaps if the platform is the given condition is true. */ template<typename T, bool byteSwap> -constexpr inline T conditionalByteSwap(T i) { +constexpr inline T conditionalByteSwap(T i) noexcept { if constexpr(byteSwap) { return ox::byteSwap<T>(i); } else { @@ -58,127 +58,118 @@ constexpr inline T conditionalByteSwap(T i) { } } -/** - * Takes an int and byte swaps if the platform is big endian. - */ -template<typename T> -constexpr inline T bigEndianAdapt(T i) { - return conditionalByteSwap<T, ox::defines::BigEndian>(i); -} - - template<typename T, bool byteSwap> class __attribute__((packed)) ByteSwapInteger { private: T m_value; public: - constexpr inline ByteSwapInteger() = default; + constexpr inline ByteSwapInteger() noexcept = default; - constexpr inline ByteSwapInteger(const ByteSwapInteger &other) { + constexpr inline ByteSwapInteger(const ByteSwapInteger &other) noexcept { m_value = other.m_value; } - constexpr inline ByteSwapInteger(T value): m_value(ox::conditionalByteSwap<T, byteSwap>(value)) { + constexpr inline ByteSwapInteger(T value) noexcept: m_value(ox::conditionalByteSwap<T, byteSwap>(value)) { } - constexpr inline const ByteSwapInteger &operator=(const ByteSwapInteger &other) { + constexpr inline const ByteSwapInteger &operator=(const ByteSwapInteger &other) noexcept { m_value = other.m_value; return *this; } template<typename I> - constexpr inline T operator=(I value) { + constexpr inline T operator=(I value) noexcept { m_value = ox::conditionalByteSwap<T, byteSwap>(value); return value; } - constexpr inline operator T() const { + constexpr inline operator T() const noexcept { return ox::conditionalByteSwap<T, byteSwap>(m_value); } template<typename I> - constexpr inline T operator+=(I other) { + constexpr inline T operator+=(I other) noexcept { auto newVal = *this + other; m_value = ox::conditionalByteSwap<T, byteSwap>(newVal); return newVal; } template<typename I> - constexpr inline T operator-=(I other) { + constexpr inline T operator-=(I other) noexcept { auto newVal = *this - other; m_value = ox::conditionalByteSwap<T, byteSwap>(newVal); return newVal; } template<typename I> - constexpr inline T operator*=(I other) { + constexpr inline T operator*=(I other) noexcept { auto newVal = *this * other; m_value = ox::conditionalByteSwap<T, byteSwap>(newVal); return newVal; } template<typename I> - constexpr inline T operator/=(I other) { + constexpr inline T operator/=(I other) noexcept { auto newVal = *this / other; m_value = ox::conditionalByteSwap<T, byteSwap>(newVal); return newVal; } // Prefix increment - constexpr inline T operator++() { + constexpr inline T operator++() noexcept { return operator+=(1); } // Postfix increment - constexpr inline T operator++(int) { + constexpr inline T operator++(int) noexcept { auto old = *this; ++*this; return old; } // Prefix decrement - constexpr inline T operator--() { + constexpr inline T operator--() noexcept { return operator-=(1); } // Postfix decrement - constexpr inline T operator--(int) { + constexpr inline T operator--(int) noexcept { auto old = *this; --*this; return old; } template<typename I> - constexpr inline T operator&=(I other) { + constexpr inline T operator&=(I other) noexcept { auto newVal = *this & other; m_value = ox::conditionalByteSwap<T, byteSwap>(newVal); return newVal; } template<typename I> - constexpr inline T operator|=(I other) { + constexpr inline T operator|=(I other) noexcept { auto newVal = *this | other; m_value = ox::conditionalByteSwap<T, byteSwap>(newVal); return newVal; } template<typename I> - constexpr inline T operator^=(I other) { + constexpr inline T operator^=(I other) noexcept { auto newVal = *this ^ other; m_value = ox::conditionalByteSwap<T, byteSwap>(newVal); return newVal; } template<typename I> - constexpr inline T operator>>=(I other) { + constexpr inline T operator>>=(I other) noexcept { auto newVal = *this >> other; m_value = ox::conditionalByteSwap<T, byteSwap>(newVal); return newVal; } template<typename I> - constexpr inline T operator<<=(I other) { + constexpr inline T operator<<=(I other) noexcept { auto newVal = *this << other; m_value = ox::conditionalByteSwap<T, byteSwap>(newVal); return newVal; @@ -190,6 +181,6 @@ template<typename T> using LittleEndian = ByteSwapInteger<T, ox::defines::BigEndian>; template<typename T> -using BigEndian = ByteSwapInteger<T, !ox::defines::BigEndian>; +using BigEndian = ByteSwapInteger<T, ox::defines::LittleEndian>; }