diff options
author | Sven Gothel <[email protected]> | 2020-11-10 03:54:25 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2020-11-10 03:54:25 +0100 |
commit | 1371255266daf92cb29dabb16b86a559e1786db6 (patch) | |
tree | 7961c73e9069d78c2a7c1db63092dd9492709146 /include | |
parent | 43ad530af98168289e3538c89d3cbb20a4d016de (diff) |
Add jau/packed_attribute.hpp: Macro __pack(...) for MSC and GCC compatiblw, macro __packed for GCC compatible only
__packed is a simple short for '__attribute__ ((packed))'
__pack(...) wraps the given __VA_ARGS__ to add 'packed' attributes (GCC compatible) or pragmas (MSVC)
Diffstat (limited to 'include')
-rw-r--r-- | include/jau/basic_types.hpp | 18 | ||||
-rw-r--r-- | include/jau/packed_attribute.hpp | 41 |
2 files changed, 51 insertions, 8 deletions
diff --git a/include/jau/basic_types.hpp b/include/jau/basic_types.hpp index ffb7109..b1b0d87 100644 --- a/include/jau/basic_types.hpp +++ b/include/jau/basic_types.hpp @@ -37,6 +37,8 @@ extern "C" { #include <byteswap.h> } +#include <jau/packed_attribute.hpp> + namespace jau { /** @@ -157,7 +159,7 @@ namespace jau { // ************************************************* */ - struct __attribute__((packed)) uint128_t { + __pack( struct uint128_t { uint8_t data[16]; constexpr uint128_t() noexcept : data{0} {} @@ -174,7 +176,7 @@ namespace jau { } constexpr bool operator!=(uint128_t const &o) const noexcept { return !(*this == o); } - }; + } ) ; constexpr uint128_t bswap(uint128_t const & source) noexcept { uint128_t dest; @@ -186,7 +188,7 @@ namespace jau { return dest; } - struct __attribute__((packed)) uint192_t { + __pack( struct uint192_t { uint8_t data[24]; constexpr uint192_t() noexcept : data{0} {} @@ -203,7 +205,7 @@ namespace jau { } constexpr bool operator!=(uint192_t const &o) const noexcept { return !(*this == o); } - }; + } ); constexpr uint192_t bswap(uint192_t const & source) noexcept { uint192_t dest; @@ -215,7 +217,7 @@ namespace jau { return dest; } - struct __attribute__((packed)) uint256_t { + __pack( struct uint256_t { uint8_t data[32]; constexpr uint256_t() noexcept : data{0} {} @@ -232,7 +234,7 @@ namespace jau { } constexpr bool operator!=(uint256_t const &o) const noexcept { return !(*this == o); } - }; + } ); constexpr uint256_t bswap(uint256_t const & source) noexcept { uint256_t dest; @@ -441,10 +443,10 @@ namespace jau { * This template shall cause no costs, the cast data pointer is identical to 'T & p = &store'. * </p> */ - template<typename T> struct __attribute__((__packed__)) packed_t { + template<typename T> __pack ( struct packed_t { T store; constexpr T get(const bool littleEndian) const noexcept { return littleEndian ? le_to_cpu(store) : be_to_cpu(store); } - }; + } ) ; inline void put_uint16(uint8_t * buffer, nsize_t const byte_offset, const uint16_t v) noexcept { diff --git a/include/jau/packed_attribute.hpp b/include/jau/packed_attribute.hpp new file mode 100644 index 0000000..deb29ff --- /dev/null +++ b/include/jau/packed_attribute.hpp @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2020 Gothel Software e.K. + * Copyright (c) 2020 ZAFENA AB + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + */ + +#ifndef PACKED_ATTRIBUTE_HPP_ +#define PACKED_ATTRIBUTE_HPP_ + +#ifndef __packed + #define __packed __attribute__ ((packed)) +#endif + +#ifndef __pack + #ifdef _MSC_VER + #define __pack(...) __pragma( pack(push, 1) ) __VA_ARGS__ __pragma( pack(pop)) + #else + #define __pack(...) __VA_ARGS__ __attribute__ ((packed)) + #endif +#endif + +#endif /* PACKED_ATTRIBUTE_HPP_ */ |