diff options
author | Roland Reichwein <mail@reichwein.it> | 2023-01-05 18:19:44 +0100 |
---|---|---|
committer | Roland Reichwein <mail@reichwein.it> | 2023-01-05 18:19:44 +0100 |
commit | 15b6682b177dda1bc64384c7ff1a82e88917c2e5 (patch) | |
tree | 9d89f9729c1f815ddb46da9a1eca71dac76fcdbe | |
parent | 455fcf0bb13baa18eac12d265ebdb743cb8815f3 (diff) |
Moved archive.h to libreichwein
-rw-r--r-- | Makefile | 1 | ||||
-rw-r--r-- | archive.h | 227 | ||||
-rw-r--r-- | statistics.h | 2 | ||||
-rw-r--r-- | tests/Makefile | 1 | ||||
-rw-r--r-- | tests/test-archive.cpp | 28 | ||||
-rw-r--r-- | tests/test-https.cpp | 2 |
6 files changed, 2 insertions, 259 deletions
@@ -119,7 +119,6 @@ clean: -rm -rf result DISTFILES= \ - archive.h \ auth.cpp \ auth.h \ config.cpp \ diff --git a/archive.h b/archive.h deleted file mode 100644 index 9c9d256..0000000 --- a/archive.h +++ /dev/null @@ -1,227 +0,0 @@ -#pragma once - -#include <boost/coroutine2/coroutine.hpp> -#include <boost/endian/conversion.hpp> - -#include <cstdint> -#include <ostream> -#include <iostream> -#include <istream> -#include <sstream> -#include <string> -#include <vector> - -typedef boost::coroutines2::coroutine<void> coro_t; - -// Serialization, similar to Boost Serialization -// but for portable binary archive -// using big endian coding (network byte order) -namespace Serialization { - -class OArchive -{ -public: - OArchive(std::ostream& os): os(os) {} - ~OArchive() {} - - template<class T> - OArchive& operator &(T& v) { - v.serialize(*this); - - return *this; - }; - - template <class T> - OArchive& write_fundamental(T& v) - { - T value = boost::endian::native_to_big(v); - os.write((char*)&value, sizeof(value)); - return *this; - } - - OArchive& operator &(uint8_t& v) - { - return write_fundamental(v); - }; - - OArchive& operator &(uint16_t& v) - { - return write_fundamental(v); - }; - - OArchive& operator &(uint32_t& v) - { - return write_fundamental(v); - }; - - OArchive& operator &(uint64_t& v) - { - return write_fundamental(v); - }; - - OArchive& operator &(int64_t& v) - { - return write_fundamental(*reinterpret_cast<uint64_t*>(v)); - }; - - OArchive& operator &(std::vector<uint8_t>& v) - { - uint32_t size = static_cast<uint32_t>(v.size()); - *this & size; - os.write((char*)v.data(), size); - return *this; - }; - - OArchive& operator &(std::string& v) - { - uint32_t size = static_cast<uint32_t>(v.size()); - *this & size; - os.write((char*)v.data(), v.size()); - return *this; - }; - -private: - std::ostream &os; -}; - -class IArchive -{ -public: - IArchive(std::istream& is): is(is) {} - IArchive(std::stringstream& is, coro_t::pull_type& coro) : is(is), mStringStream(&is), mCoro(&coro) {} - ~IArchive() {} - - template<class T> - IArchive& operator &(T& v) - { - v.serialize(*this); - - return *this; - }; - - template <class T> - IArchive& read_fundamental(T& v) - { - // in coroutine case, wait for input, if necessary - if (mCoro && mStringStream) { - while (true) { - auto pindex {mStringStream->tellp()}; - auto gindex {mStringStream->tellg()}; - if (pindex != std::stringstream::pos_type(-1) && gindex != std::stringstream::pos_type(-1) && pindex > gindex) { - if (static_cast<size_t>(pindex - gindex) < sizeof(v)) - (*mCoro)(); - else - break; - } else { - std::cerr << "Error: read_fundamental: Bad stringstream indices: " << pindex << ", " << gindex << std::endl; - break; - } - } - } - - // now, we have enough bytes available - T value; - is.read((char*)&value, sizeof(value)); - v = boost::endian::big_to_native(value); - return *this; - } - - IArchive& operator &(uint8_t& v) - { - return read_fundamental(v); - }; - - IArchive& operator &(uint16_t& v) - { - return read_fundamental(v); - }; - - IArchive& operator &(uint32_t& v) - { - return read_fundamental(v); - }; - - IArchive& operator &(uint64_t& v) - { - return read_fundamental(v); - }; - - IArchive& operator &(int64_t& v) - { - uint64_t uv; - read_fundamental(uv); - v = *reinterpret_cast<int64_t*>(uv); - return *this; - }; - - template <class T> - IArchive& read_bytes_vector(T& v) - { - uint32_t size; - *this & size; - - v.resize(size); - - // in coroutine case, wait for input, if necessary - if (mCoro && mStringStream) { - while (true) { - auto pindex {mStringStream->tellp()}; - auto gindex {mStringStream->tellg()}; - if (pindex != std::stringstream::pos_type(-1) && gindex != std::stringstream::pos_type(-1) && pindex > gindex) { - if (static_cast<uint32_t>(pindex - gindex) < size) - (*mCoro)(); - else - break; - } else { - std::cerr << "Error: read_bytes_vector: Bad stringstream indices: " << pindex << ", " << gindex << std::endl; - break; - } - } - } - - // now, we have enough bytes available - is.read((char*)v.data(), size); - return *this; - } - - IArchive& operator &(std::vector<uint8_t>& v) - { - return read_bytes_vector(v); - }; - - IArchive& operator &(std::string& v) - { - return read_bytes_vector(v); - }; - -private: - std::istream &is; - std::stringstream* mStringStream{ }; // for i/o sizes access - coro_t::pull_type* mCoro{ }; // optional for coroutine -}; - -// - Free functions ---------------------------------------------------------- - -template<class Archive, class T> -void serialize(Archive& ar, T& v) -{ - ar & v; -} - -template<class T> -OArchive& operator <<(OArchive& ar, T& v) -{ - serialize(ar, v); - - return ar; -}; - -template<class T> -IArchive& operator >>(IArchive& ar, T& v) -{ - serialize(ar, v); - - return ar; -}; - -} diff --git a/statistics.h b/statistics.h index 7e4da7e..c38ef66 100644 --- a/statistics.h +++ b/statistics.h @@ -1,6 +1,6 @@ #pragma once -#include "archive.h" +#include "libreichwein/archive.h" #include <cstdint> #include <ctime> diff --git a/tests/Makefile b/tests/Makefile index 00c50a1..9ce3dad 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -44,7 +44,6 @@ UNITS=\ webserver.cpp TESTSRC=\ - test-archive.cpp \ test-auth.cpp \ test-config.cpp \ test-environment.cpp \ diff --git a/tests/test-archive.cpp b/tests/test-archive.cpp deleted file mode 100644 index e6b5894..0000000 --- a/tests/test-archive.cpp +++ /dev/null @@ -1,28 +0,0 @@ -#include <boost/test/unit_test.hpp> -#include <boost/test/data/dataset.hpp> -#include <boost/test/data/monomorphic.hpp> -#include <boost/test/data/test_case.hpp> - -#include <boost/property_tree/ptree.hpp> -#include <boost/property_tree/xml_parser.hpp> - -#include <sstream> -#include <string> - -#include "archive.h" - -using namespace std::string_literals; - -class ArchiveFixture -{ -public: - ArchiveFixture(){} - ~ArchiveFixture(){} - void setup(){} - void teardown(){} -}; - -BOOST_FIXTURE_TEST_CASE(archivetest, ArchiveFixture) -{ -} - diff --git a/tests/test-https.cpp b/tests/test-https.cpp index dcd33d7..cb917e9 100644 --- a/tests/test-https.cpp +++ b/tests/test-https.cpp @@ -9,7 +9,7 @@ #include <sstream> #include <string> -#include "archive.h" +#include "https.h" using namespace std::string_literals; |