Initial commit
This commit is contained in:
commit
da7c0ab3aa
|
@ -0,0 +1,8 @@
|
|||
---
|
||||
Checks: 'boost-*,cppcoreguidelines-*,clang-analyzer-*,clang-diagnostic-*,misc-*,modernize-*,performance-*,readability-*,-readability-magic-numbers,-cppcoreguidelines-avoid-magic-numbers,-modernize-use-trailing-return-type,-readability-uppercase-literal-suffix'
|
||||
CheckOptions:
|
||||
- key: readability-implicit-bool-conversion.AllowIntegerConditions
|
||||
value: 1
|
||||
- key: readability-implicit-bool-conversion.AllowPointerConditions
|
||||
value: 1
|
||||
...
|
|
@ -0,0 +1 @@
|
|||
build*/
|
|
@ -0,0 +1,46 @@
|
|||
cmake_minimum_required(VERSION 3.6)
|
||||
project(e6l.serial VERSION 1.0.0
|
||||
LANGUAGES CXX
|
||||
DESCRIPTION "Header only serialization and deserialization of integers to and from span<byte>"
|
||||
HOMEPAGE_URL "https://git.enimihil.net/enimihil/e6l.serial")
|
||||
|
||||
set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)
|
||||
|
||||
include(GNUInstallDirs)
|
||||
|
||||
find_package(ms-gsl REQUIRED)
|
||||
|
||||
add_library(serial INTERFACE)
|
||||
target_include_directories(serial INTERFACE
|
||||
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include/>
|
||||
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
|
||||
target_compile_features(serial INTERFACE cxx_std_17)
|
||||
target_link_libraries(serial INTERFACE ms-gsl)
|
||||
|
||||
add_library(e6l::serial ALIAS serial)
|
||||
|
||||
install(TARGETS serial
|
||||
EXPORT serial-config
|
||||
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDDIR})
|
||||
|
||||
install(EXPORT serial-config
|
||||
FILE "${PROJECT_NAME}-config.cmake"
|
||||
NAMESPACE e6l::
|
||||
DESTINATION "${CMAKE_INSTALL_LIBDIR}/${PROJECT_NAME}-${PROJECT_VERSION}/cmake")
|
||||
|
||||
install(FILES include/e6l/serial.hpp DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/e6l/)
|
||||
|
||||
include(CMakePackageConfigHelpers)
|
||||
write_basic_package_version_file("${PROJECT_NAME}-config-version.cmake"
|
||||
VERSION ${PROJECT_VERSION}
|
||||
COMPATIBILITY SameMajorVersion)
|
||||
install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}-config-version.cmake"
|
||||
DESTINATION ${CMAKE_INSTALL_LIBDIR}/${PROJECT_NAME}-${PROJECT_VERSION}/cmake)
|
||||
|
||||
|
||||
option("ENABLE_TEST" "Enable unit tests for ${PROJECT_NAME}" OFF)
|
||||
if( ${ENABLE_TEST} )
|
||||
include(CTest)
|
||||
add_subdirectory(test)
|
||||
endif()
|
||||
|
|
@ -0,0 +1,202 @@
|
|||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
|
@ -0,0 +1,19 @@
|
|||
E6L Serial
|
||||
==========
|
||||
|
||||
A C++17 library depending on ``gsl::span`` for serialization and
|
||||
deserialization of ``gsl::span<std::byte>`` view to and from integers.
|
||||
|
||||
To use, install using your normal mechanism (cmake; make; make install, git
|
||||
submodule, etc.) and add::
|
||||
|
||||
target_link_libraries(<your target> e6l::serial)
|
||||
|
||||
To each target that wants to use the library.
|
||||
|
||||
If installed outside your project::
|
||||
|
||||
find_package(e6l.serial)
|
||||
|
||||
Read the comment(s) in the header (``e6l/serial.hpp``) for usage, or see the
|
||||
unit tests.
|
|
@ -0,0 +1,24 @@
|
|||
find_path(MS_GSL_INCLUDE_DIR gsl/gsl)
|
||||
if(MS_GSL_INCLUDE_DIR STREQUAL "GSL_INCLUDE_DIR-NOTFOUND")
|
||||
message(FATAL_ERROR "Could not find Guidelines Support Library.")
|
||||
set(MS_GSL_FOUND FALSE)
|
||||
endif()
|
||||
|
||||
set(MS_GSL_FOUND TRUE)
|
||||
|
||||
if(MS_GSL_FOUND)
|
||||
include(FindPackageHandleStandardArgs)
|
||||
find_package_handle_standard_args(ms-gsl
|
||||
REQUIRED_VARS MS_GSL_INCLUDE_DIR
|
||||
FOUND_VAR ms-gsl_FOUND
|
||||
FAIL_MESSAGE "Failed to find ms-gsl")
|
||||
|
||||
if(NOT TARGET ms-gsl)
|
||||
add_library(ms-gsl INTERFACE IMPORTED)
|
||||
set_target_properties(ms-gsl PROPERTIES
|
||||
INTERFACE_INCLUDE_DIRECTORIES ${MS_GSL_INCLUDE_DIR})
|
||||
endif(NOT TARGET ms-gsl)
|
||||
|
||||
mark_as_advanced(MS_GSL_INCLUDE_DIR)
|
||||
|
||||
endif(MS_GSL_FOUND)
|
|
@ -0,0 +1,173 @@
|
|||
#ifndef E6L_SERIAL_HPP_INCLUDED
|
||||
#define E6L_SERIAL_HPP_INCLUDED
|
||||
/*
|
||||
* e6l/serial.hpp
|
||||
*
|
||||
* Copyright 2020 Greg S.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <cstdint>
|
||||
#include <gsl/span>
|
||||
|
||||
/*
|
||||
|
||||
size_t serialize(T const&, gsl::span<std::byte, sizeof(T)>)
|
||||
Accepts an integral type T and produces a bytewise representation of it in
|
||||
the span provided, returning the number of bytes written.
|
||||
|
||||
The byte-wise representation written is a big-endian representation.
|
||||
(Regardless of host architecture)
|
||||
|
||||
size_t deserialize(gsl::span<std::byte, sizeof(T)>, T&)
|
||||
Accepts a span of the bytewise representation of (integral) T and populates
|
||||
the passed T& accordingly.
|
||||
|
||||
This reads the bytes as a big-endian integral value. (Regardless of host
|
||||
architecture)
|
||||
|
||||
The overload set accepts:
|
||||
std::uint8_t (1 byte)
|
||||
std::uint16_t (2 bytes)
|
||||
std::uint32_t (as 3-bytes, ignoring the MSB)
|
||||
std::uint32_t (4 bytes)
|
||||
std::uint64_t (8 bytes)
|
||||
uint128_t (16 bytes, compiler supporting)
|
||||
*/
|
||||
|
||||
namespace e6l {
|
||||
|
||||
constexpr inline std::size_t serialize(std::uint8_t n, gsl::span<std::byte, sizeof(std::uint8_t)> buf) {
|
||||
buf[0] = static_cast<std::byte>(n);
|
||||
return sizeof(std::uint8_t);
|
||||
}
|
||||
|
||||
constexpr inline std::size_t serialize(std::uint16_t n, gsl::span<std::byte, sizeof(std::uint16_t)> buf) {
|
||||
buf[0] = static_cast<std::byte>(n>>8 & 0xff);
|
||||
buf[1] = static_cast<std::byte>(n & 0xff);
|
||||
return sizeof(std::uint16_t);
|
||||
}
|
||||
|
||||
constexpr inline std::size_t serialize(std::uint32_t n, gsl::span<std::byte, sizeof(std::uint32_t)> buf) {
|
||||
buf[0] = static_cast<std::byte>(n>>24 & 0xff);
|
||||
buf[1] = static_cast<std::byte>(n>>16 & 0xff);
|
||||
buf[2] = static_cast<std::byte>(n>>8 & 0xff);
|
||||
buf[3] = static_cast<std::byte>(n & 0xff);
|
||||
return sizeof(std::uint32_t);
|
||||
}
|
||||
|
||||
constexpr inline std::size_t serialize(std::uint32_t n, gsl::span<std::byte, 3> buf) {
|
||||
buf[0] = static_cast<std::byte>(n>>16 & 0xff);
|
||||
buf[1] = static_cast<std::byte>(n>>8 & 0xff);
|
||||
buf[2] = static_cast<std::byte>(n & 0xff);
|
||||
return 3;
|
||||
}
|
||||
|
||||
constexpr inline std::size_t serialize(std::uint64_t n, gsl::span<std::byte, sizeof(std::uint64_t)> buf) {
|
||||
buf[0] = static_cast<std::byte>(n>>56 & 0xff);
|
||||
buf[1] = static_cast<std::byte>(n>>48 & 0xff);
|
||||
buf[2] = static_cast<std::byte>(n>>40 & 0xff);
|
||||
buf[3] = static_cast<std::byte>(n>>32 & 0xff);
|
||||
buf[4] = static_cast<std::byte>(n>>24 & 0xff);
|
||||
buf[5] = static_cast<std::byte>(n>>16 & 0xff);
|
||||
buf[6] = static_cast<std::byte>(n>>8 & 0xff);
|
||||
buf[7] = static_cast<std::byte>(n & 0xff);
|
||||
return sizeof(std::uint64_t);
|
||||
}
|
||||
|
||||
#ifdef __SIZEOF_INT128__
|
||||
using uint128_t = unsigned __int128;
|
||||
constexpr inline std::size_t serialize(uint128_t n, gsl::span<std::byte, sizeof(uint128_t)> buf) {
|
||||
buf[ 0] = static_cast<std::byte>(n>>120 & 0xff);
|
||||
buf[ 1] = static_cast<std::byte>(n>>112 & 0xff);
|
||||
buf[ 2] = static_cast<std::byte>(n>>104 & 0xff);
|
||||
buf[ 3] = static_cast<std::byte>(n>>96 & 0xff);
|
||||
buf[ 4] = static_cast<std::byte>(n>>88 & 0xff);
|
||||
buf[ 5] = static_cast<std::byte>(n>>80 & 0xff);
|
||||
buf[ 6] = static_cast<std::byte>(n>>72 & 0xff);
|
||||
buf[ 7] = static_cast<std::byte>(n>>64 & 0xff);
|
||||
buf[ 8] = static_cast<std::byte>(n>>56 & 0xff);
|
||||
buf[ 9] = static_cast<std::byte>(n>>48 & 0xff);
|
||||
buf[10] = static_cast<std::byte>(n>>40 & 0xff);
|
||||
buf[11] = static_cast<std::byte>(n>>32 & 0xff);
|
||||
buf[12] = static_cast<std::byte>(n>>24 & 0xff);
|
||||
buf[13] = static_cast<std::byte>(n>>16 & 0xff);
|
||||
buf[14] = static_cast<std::byte>(n>>8 & 0xff);
|
||||
buf[15] = static_cast<std::byte>(n & 0xff);
|
||||
return sizeof(uint128_t);
|
||||
}
|
||||
#endif
|
||||
|
||||
constexpr inline std::size_t deserialize(gsl::span<std::byte const, sizeof(std::uint8_t)> buf, std::uint8_t& n) {
|
||||
n = static_cast<uint8_t>(buf[0]);
|
||||
return sizeof(std::uint8_t);
|
||||
}
|
||||
|
||||
constexpr inline std::size_t deserialize(gsl::span<std::byte const, sizeof(std::uint16_t)> buf, std::uint16_t& n) {
|
||||
n = static_cast<uint16_t>(buf[0])<<8
|
||||
| static_cast<uint16_t>(buf[1]);
|
||||
return sizeof(std::uint16_t);
|
||||
}
|
||||
|
||||
constexpr inline std::size_t deserialize(gsl::span<std::byte const, sizeof(std::uint32_t)> buf, std::uint32_t& n) {
|
||||
n = static_cast<uint32_t>(buf[0])<<24
|
||||
| static_cast<uint32_t>(buf[1])<<16
|
||||
| static_cast<uint32_t>(buf[2])<<8
|
||||
| static_cast<uint32_t>(buf[3]);
|
||||
return sizeof(std::uint32_t);
|
||||
}
|
||||
|
||||
constexpr inline std::size_t deserialize(gsl::span<std::byte const, 3> buf, std::uint32_t& n) {
|
||||
n = (static_cast<uint32_t>(buf[0])<<16)
|
||||
| (static_cast<uint32_t>(buf[1])<<8)
|
||||
| static_cast<uint32_t>(buf[2]);
|
||||
return 3;
|
||||
}
|
||||
|
||||
constexpr inline std::size_t deserialize(gsl::span<std::byte const, sizeof(std::uint64_t)> buf, std::uint64_t& n) {
|
||||
n = (static_cast<std::uint64_t>(buf[0])<<56)
|
||||
| (static_cast<std::uint64_t>(buf[1])<<48)
|
||||
| (static_cast<std::uint64_t>(buf[2])<<40)
|
||||
| (static_cast<std::uint64_t>(buf[3])<<32)
|
||||
| (static_cast<std::uint64_t>(buf[4])<<24)
|
||||
| (static_cast<std::uint64_t>(buf[5])<<16)
|
||||
| (static_cast<std::uint64_t>(buf[6])<<8)
|
||||
| (static_cast<std::uint64_t>(buf[7]));
|
||||
return sizeof(std::uint64_t);
|
||||
}
|
||||
|
||||
#ifdef __SIZEOF_INT128__
|
||||
constexpr inline std::size_t deserialize(gsl::span<std::byte const, sizeof(uint128_t)> buf, uint128_t& n) {
|
||||
n= (static_cast<uint128_t>(buf[ 0])<<120)
|
||||
| (static_cast<uint128_t>(buf[ 1])<<112)
|
||||
| (static_cast<uint128_t>(buf[ 2])<<104)
|
||||
| (static_cast<uint128_t>(buf[ 3])<<96)
|
||||
| (static_cast<uint128_t>(buf[ 4])<<88)
|
||||
| (static_cast<uint128_t>(buf[ 5])<<80)
|
||||
| (static_cast<uint128_t>(buf[ 6])<<72)
|
||||
| (static_cast<uint128_t>(buf[ 7])<<64)
|
||||
| (static_cast<uint128_t>(buf[ 8])<<56)
|
||||
| (static_cast<uint128_t>(buf[ 9])<<48)
|
||||
| (static_cast<uint128_t>(buf[10])<<40)
|
||||
| (static_cast<uint128_t>(buf[11])<<32)
|
||||
| (static_cast<uint128_t>(buf[12])<<24)
|
||||
| (static_cast<uint128_t>(buf[13])<<16)
|
||||
| (static_cast<uint128_t>(buf[14])<<8)
|
||||
| (static_cast<uint128_t>(buf[15]));
|
||||
return sizeof(uint128_t);
|
||||
}
|
||||
#endif
|
||||
|
||||
} // namespace e6l
|
||||
#endif
|
|
@ -0,0 +1,10 @@
|
|||
find_package(Catch2 REQUIRED)
|
||||
|
||||
add_executable(serialtest
|
||||
serialtest.cpp)
|
||||
target_link_libraries(serialtest
|
||||
e6l::serial
|
||||
Catch2::Catch2)
|
||||
|
||||
include(ParseAndAddCatchTests)
|
||||
ParseAndAddCatchTests(serialtest)
|
|
@ -0,0 +1,12 @@
|
|||
#ifndef BYTESUTIL_HPP_INCLUDED
|
||||
#define BYTESUTIL_HPP_INCLUDED
|
||||
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
|
||||
template<typename... Ts>
|
||||
std::array<std::byte, sizeof...(Ts)> make_bytes(Ts&&... args) noexcept {
|
||||
return {std::byte(std::forward<Ts>(args))...};
|
||||
}
|
||||
|
||||
#endif
|
|
@ -0,0 +1,154 @@
|
|||
#include <e6l/serial.hpp>
|
||||
|
||||
#define CATCH_CONFIG_MAIN
|
||||
#include <catch2/catch.hpp>
|
||||
|
||||
#include "bytesutil.hpp"
|
||||
|
||||
TEST_CASE("deserialize 8-byte to uint64_t", "[integers]") {
|
||||
auto raw = make_bytes(0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08);
|
||||
|
||||
uint64_t p = 0;
|
||||
REQUIRE( e6l::deserialize(raw, p) == 8 );
|
||||
|
||||
REQUIRE( p == 0x0102030405060708 );
|
||||
}
|
||||
|
||||
TEST_CASE("deserialize 4-byte to uint32_t", "[integers]") {
|
||||
auto raw = make_bytes(0x01, 0x02, 0x03, 0x04);
|
||||
|
||||
uint32_t p = 0;
|
||||
REQUIRE( e6l::deserialize(gsl::span<std::byte, 4>(raw), p) == 4 );
|
||||
|
||||
REQUIRE( p == 0x01020304 );
|
||||
}
|
||||
|
||||
TEST_CASE("deserialize 3-byte to uint32_t", "[integers]") {
|
||||
auto raw = make_bytes(0x01, 0x02, 0x03);
|
||||
|
||||
uint32_t p = 0;
|
||||
REQUIRE( e6l::deserialize(gsl::span<std::byte, 3>(raw), p) == 3 );
|
||||
|
||||
REQUIRE( p == 0x010203 );
|
||||
}
|
||||
|
||||
TEST_CASE("deserialize 2-bytes to uint16_t", "[integers]") {
|
||||
auto raw = make_bytes(0x18, 0x01);
|
||||
|
||||
uint16_t p = 0;
|
||||
REQUIRE( e6l::deserialize(raw, p) == 2 );
|
||||
|
||||
REQUIRE( p == 0x1801 );
|
||||
}
|
||||
|
||||
TEST_CASE("deserialize 1-byte to uint8_t", "[integers]") {
|
||||
auto raw = make_bytes(0x23);
|
||||
|
||||
uint8_t p = 0;
|
||||
REQUIRE( e6l::deserialize(raw, p) == 1 );
|
||||
|
||||
REQUIRE( p == 0x23 );
|
||||
}
|
||||
|
||||
TEST_CASE("serialize uint64_t to 8 bytes", "[integers]") {
|
||||
uint64_t p = 0x0102030405060708;
|
||||
auto out = make_bytes(0, 0, 0, 0, 0, 0, 0, 0);
|
||||
|
||||
REQUIRE( e6l::serialize(p, out) == 8 );
|
||||
|
||||
REQUIRE( out[0] == std::byte{0x01} );
|
||||
REQUIRE( out[1] == std::byte{0x02} );
|
||||
REQUIRE( out[2] == std::byte{0x03} );
|
||||
REQUIRE( out[3] == std::byte{0x04} );
|
||||
REQUIRE( out[4] == std::byte{0x05} );
|
||||
REQUIRE( out[5] == std::byte{0x06} );
|
||||
REQUIRE( out[6] == std::byte{0x07} );
|
||||
REQUIRE( out[7] == std::byte{0x08} );
|
||||
}
|
||||
|
||||
|
||||
TEST_CASE("serialize uint32_t to 4 bytes", "[integers]") {
|
||||
uint32_t p = 0x01020304;
|
||||
auto out = make_bytes(0, 0, 0, 0);
|
||||
|
||||
REQUIRE( e6l::serialize(p, gsl::span<std::byte, 4>(out)) == 4 );
|
||||
|
||||
REQUIRE( out[0] == std::byte{0x01} );
|
||||
REQUIRE( out[1] == std::byte{0x02} );
|
||||
REQUIRE( out[2] == std::byte{0x03} );
|
||||
REQUIRE( out[3] == std::byte{0x04} );
|
||||
}
|
||||
|
||||
TEST_CASE("serialize uint32_t to 3 bytes", "[integers]") {
|
||||
uint32_t p = 0x01020304;
|
||||
auto out = make_bytes(0, 0, 0);
|
||||
|
||||
REQUIRE( e6l::serialize(p, gsl::span<std::byte, 3>(out)) == 3 );
|
||||
|
||||
REQUIRE( out[0] == std::byte{0x02} );
|
||||
REQUIRE( out[1] == std::byte{0x03} );
|
||||
REQUIRE( out[2] == std::byte{0x04} );
|
||||
}
|
||||
|
||||
TEST_CASE("serialize uint16_t to 2 bytes", "[integers]") {
|
||||
uint16_t p = 0x1801;
|
||||
auto out = make_bytes(0, 0);
|
||||
|
||||
REQUIRE( e6l::serialize(p, out) == 2 );
|
||||
|
||||
REQUIRE( out[0] == std::byte{0x18} );
|
||||
REQUIRE( out[1] == std::byte{0x01} );
|
||||
}
|
||||
|
||||
TEST_CASE("serialize uint8_t to 1 byte", "[integers]") {
|
||||
uint8_t p = 0x23;
|
||||
auto out = make_bytes(0);
|
||||
|
||||
REQUIRE( e6l::serialize(p, out) == 1 );
|
||||
|
||||
REQUIRE( out[0] == std::byte{0x23} );
|
||||
}
|
||||
|
||||
#ifdef __SIZEOF_INT128__
|
||||
TEST_CASE("serialize uint128_t to 16 bytes", "[integers]") {
|
||||
e6l::uint128_t p = static_cast<e6l::uint128_t>(0x0102030405060708ul)<<64;;
|
||||
p |= static_cast<e6l::uint128_t>(0x090a0b0c0d0e0f00ul);
|
||||
|
||||
auto out = make_bytes(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
|
||||
|
||||
REQUIRE( e6l::serialize( p, out) == 16 );
|
||||
|
||||
REQUIRE( out[ 0] == std::byte{0x01} );
|
||||
REQUIRE( out[ 1] == std::byte{0x02} );
|
||||
REQUIRE( out[ 2] == std::byte{0x03} );
|
||||
REQUIRE( out[ 3] == std::byte{0x04} );
|
||||
REQUIRE( out[ 4] == std::byte{0x05} );
|
||||
REQUIRE( out[ 5] == std::byte{0x06} );
|
||||
REQUIRE( out[ 6] == std::byte{0x07} );
|
||||
REQUIRE( out[ 7] == std::byte{0x08} );
|
||||
REQUIRE( out[ 8] == std::byte{0x09} );
|
||||
REQUIRE( out[ 9] == std::byte{0x0a} );
|
||||
REQUIRE( out[10] == std::byte{0x0b} );
|
||||
REQUIRE( out[11] == std::byte{0x0c} );
|
||||
REQUIRE( out[12] == std::byte{0x0d} );
|
||||
REQUIRE( out[13] == std::byte{0x0e} );
|
||||
REQUIRE( out[14] == std::byte{0x0f} );
|
||||
REQUIRE( out[15] == std::byte{0x00} );
|
||||
}
|
||||
|
||||
TEST_CASE("deserialize uint128_t from 16 bytes", "[integers]") {
|
||||
e6l::uint128_t p = static_cast<e6l::uint128_t>(0x0102030405060708ul)<<64;;
|
||||
p |= static_cast<e6l::uint128_t>(0x090a0b0c0d0e0f00ul);
|
||||
|
||||
auto raw = make_bytes(0x01, 0x02, 0x03, 0x04,
|
||||
0x05, 0x06, 0x07, 0x08,
|
||||
0x09, 0x0a, 0x0b, 0x0c,
|
||||
0x0d, 0x0e, 0x0f, 0x00);
|
||||
|
||||
e6l::uint128_t p1 = 0;
|
||||
REQUIRE( e6l::deserialize( raw, p1 ) == 16 );
|
||||
|
||||
REQUIRE( static_cast<std::uint64_t>(p) == static_cast<std::uint64_t>(p1) );
|
||||
REQUIRE( static_cast<std::uint64_t>(p>>64) == static_cast<std::uint64_t>(p1>>64) );
|
||||
}
|
||||
#endif
|
Loading…
Reference in New Issue