Loading...
Searching...
No Matches
binary_stream.hpp
1/*---------------------------------------------------------------------------*\
2 *
3 * bitpit
4 *
5 * Copyright (C) 2015-2021 OPTIMAD engineering Srl
6 *
7 * -------------------------------------------------------------------------
8 * License
9 * This file is part of bitpit.
10 *
11 * bitpit is free software: you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License v3 (LGPL)
13 * as published by the Free Software Foundation.
14 *
15 * bitpit is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18 * License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with bitpit. If not, see <http://www.gnu.org/licenses/>.
22 *
23\*---------------------------------------------------------------------------*/
24
25#ifndef __BITPIT_BINARY_STREAM_HPP__
26#define __BITPIT_BINARY_STREAM_HPP__
27
28#include <array>
29#include <iostream>
30#include <map>
31#include <string>
32#include <unordered_map>
33#include <unordered_set>
34#include <vector>
35
36namespace bitpit {
37
38// Stream operators
39class IBinaryStream;
40class OBinaryStream;
41
42template<typename T, std::size_t d>
43IBinaryStream &operator>>(IBinaryStream &stream, std::array<T, d> &data);
44template<typename T, std::size_t d>
45OBinaryStream &operator<<(OBinaryStream &stream, const std::array<T, d> &data);
46
47template<typename T>
48IBinaryStream& operator>>(IBinaryStream &stream, std::vector<T> &data);
49template<typename T>
50OBinaryStream& operator<<(OBinaryStream &stream, const std::vector<T> &data);
51
52template<typename K, typename T>
53IBinaryStream &operator>>(IBinaryStream &stream, std::pair<K, T> &data);
54template<typename K, typename T>
55OBinaryStream &operator<<(OBinaryStream &stream, const std::pair<K, T> &data);
56
57template<typename K, typename T>
58IBinaryStream &operator>>(IBinaryStream &stream, std::map<K, T> &data);
59template<typename K, typename T>
60OBinaryStream &operator<<(OBinaryStream &stream, const std::map<K, T> &data);
61
62template<typename K, typename T>
63IBinaryStream &operator>>(IBinaryStream &stream, std::unordered_map<K, T> &data);
64template<typename K, typename T>
65OBinaryStream &operator<<(OBinaryStream &stream, const std::unordered_map<K, T> &data);
66
67template<typename T>
68IBinaryStream &operator>>(IBinaryStream &stream, std::unordered_set<T> &data);
69template<typename T>
70OBinaryStream &operator<<(OBinaryStream &stream, const std::unordered_set<T> &data);
71
72template<typename T>
73IBinaryStream & operator>>(IBinaryStream &stream, T &data);
74template<typename T>
75OBinaryStream & operator<<(OBinaryStream &stream, const T &data);
76
77template<>
78IBinaryStream & operator>>(IBinaryStream &stream, std::string &data);
79template<>
80OBinaryStream & operator<<(OBinaryStream &stream, const std::string &data);
81
82// Binary stream
84
85public:
86 virtual ~BinaryStream() = default;
87
88 bool eof() const;
89
90 std::streampos tellg() const;
91 bool seekg(std::size_t pos);
92 bool seekg(std::streamoff offset, std::ios_base::seekdir way);
93
94 char * data();
95 const char * data() const;
96
97 virtual void setSize(std::size_t size);
98 std::size_t getSize() const;
99
100 std::size_t getCapacity() const;
101
102 int getChunkSize();
103 int getChunkCount();
104
105protected:
106 std::vector<char> m_buffer;
107 std::size_t m_size;
108 std::size_t m_pos;
109
110 BinaryStream();
111 BinaryStream(std::size_t capacity);
112 BinaryStream(const char *buffer, std::size_t capacity);
113 BinaryStream(const std::vector<char> &buffer);
114
115 void open(const char *buffer, std::size_t capacity);
116 void open(std::size_t capacity);
117
118 void setCapacity(std::size_t capacity);
119
120private:
121 int m_chunkSize;
122
123};
124
126
127template<typename T>
128friend IBinaryStream & (operator>>)(IBinaryStream &stream, T &data);
129
130public:
131 IBinaryStream(void);
132 IBinaryStream(std::size_t size);
133 IBinaryStream(const char *buffer, std::size_t size);
134 IBinaryStream(const std::vector<char> &buffer);
135
136 void open(const char *buffer, std::size_t size);
137 void open(std::size_t size);
138
139 void read(char *data, std::size_t size);
140
141};
142
144
145template<typename T>
146friend OBinaryStream & (operator<<)(OBinaryStream &stream, const T &data);
147
148public:
150 OBinaryStream(std::size_t size);
151
152 void open(std::size_t size);
153
154 void setSize(std::size_t size) override;
155 void squeeze();
156
157 void write(const char *data, std::size_t size);
158
159private:
160 bool m_expandable;
161
162};
163
164}
165
166// Template implementation
167#include "binary_stream.tpp"
168
169#endif
Base class for defining input and output binary streams.
std::streampos tellg() const
std::size_t getCapacity() const
void open(const char *buffer, std::size_t capacity)
bool seekg(std::size_t pos)
void setCapacity(std::size_t capacity)
virtual void setSize(std::size_t size)
std::size_t getSize() const
Output binary stream.
void read(char *data, std::size_t size)
void open(const char *buffer, std::size_t size)
Output binary stream.
void write(const char *data, std::size_t size)
void open(std::size_t size)
void setSize(std::size_t size) override
std::ostream & operator<<(std::ostream &, const std::vector< T > &)
--- layout: doxygen_footer ---