Vanetza
 
Loading...
Searching...
No Matches
archives.hpp
1#ifndef ARCHIVES_HPP_TLVURDQK
2#define ARCHIVES_HPP_TLVURDQK
3
4#include <vanetza/common/byte_order.hpp>
5#include <exception>
6#include <istream>
7#include <ostream>
8#include <streambuf>
9
10namespace vanetza
11{
12
13/**
14 * This is a drop-in replacement for boost::archive::binary_iarchive
15 */
17{
18public:
19 using InputStream = std::basic_istream<char>;
20 using StreamBuffer = std::basic_streambuf<char>;
21 class Exception : public std::runtime_error {
22 using std::runtime_error::runtime_error;
23 };
24
25 InputArchive(InputStream& is);
26 InputArchive(StreamBuffer& buf);
27
28 template<typename T>
29 InputArchive& operator>>(T& t)
30 {
31 static_assert(std::is_integral<T>::value == true, "only integral types are supported");
32 char* ptr = reinterpret_cast<char*>(&t);
33 load_binary(ptr, sizeof(T));
34 return *this;
35 }
36
37 void load_binary(unsigned char* data, std::size_t len);
38 void load_binary(char* data, std::size_t len);
39 char peek_byte();
40 std::size_t remaining_bytes();
41
42private:
43 StreamBuffer* m_stream_buffer;
44};
45
46/**
47 * This is a drop-in replacement for boost::archive::binary_oarchive
48 */
50{
51public:
52 using OutputStream = std::basic_ostream<char>;
53 using StreamBuffer = std::basic_streambuf<char>;
54 class Exception : public std::runtime_error {
55 using std::runtime_error::runtime_error;
56 };
57
58 OutputArchive(OutputStream& os);
59 OutputArchive(StreamBuffer& buf);
60
61 template<typename T>
62 OutputArchive& operator<<(const T& t)
63 {
64 static_assert(std::is_integral<T>::value == true, "only integral types are supported");
65 const char* ptr = reinterpret_cast<const char*>(&t);
66 save_binary(ptr, sizeof(T));
67 return *this;
68 }
69
70 void save_binary(const unsigned char* data, std::size_t len);
71 void save_binary(const char* data, std::size_t len);
72
73private:
74 StreamBuffer* m_stream_buffer;
75};
76
77} // namespace vanetza
78
79#endif /* ARCHIVES_HPP_TLVURDQK */
80