OpenLB 1.7
Loading...
Searching...
No Matches
serializerIO.hh
Go to the documentation of this file.
1/* This file is part of the OpenLB library
2 *
3 * Copyright (C) 2006-2016 Jonas Latt, Mathias J. Krause
4 * E-mail contact: info@openlb.net
5 * The most recent release of OpenLB can be downloaded at
6 * <http://www.openlb.net/>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public
19 * License along with this program; if not, write to the Free
20 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22*/
23
24#ifndef SERIALIZER_IO_HH
25#define SERIALIZER_IO_HH
26
27#include "serializerIO.h"
28#include "base64.h"
29#include "core/olbDebug.h"
30
31#include <limits>
32#include <istream>
33#include <ostream>
34#include <fstream>
35#include <sstream>
36#include <cstring>
37
38namespace olb {
39
40void serializer2ostr(Serializer& serializer, std::ostream& ostr, bool enforceUint)
41{
42 serializer.resetCounter();
43 // write binary size into first integer of stream
44 std::size_t binarySize = serializer.getSize();
45 if (enforceUint) {
46 Base64Encoder<unsigned int> sizeEncoder(ostr, 1);
47 OLB_PRECONDITION(binarySize <= std::numeric_limits<unsigned int>::max());
48 unsigned int uintBinarySize = (unsigned int)binarySize;
49 sizeEncoder.encode(&uintBinarySize, 1);
50 }
51 else {
52 Base64Encoder<std::size_t> sizeEncoder(ostr, 1);
53 sizeEncoder.encode(&binarySize, 1);
54 }
55
56 Base64Encoder<bool> dataEncoder (ostr, binarySize);
57
58 std::size_t blockSize;
59 const bool* dataBuffer = nullptr;
60 while (dataBuffer = serializer.getNextBlock(blockSize, false), dataBuffer != nullptr) {
61 dataEncoder.encode(dataBuffer, blockSize);
62 }
63 serializer.resetCounter();
64}
65
66void istr2serializer(Serializer& serializer, std::istream& istr, bool enforceUint)
67{
68 //std::size_t binarySize = serializer.getSize();
69 serializer.resetCounter();
70
71 // read binary size from first integer of stream
72 std::size_t binarySize;
73 if (enforceUint) {
74 unsigned int uintBinarySize;
75 Base64Decoder<unsigned int> sizeDecoder(istr, 1);
76 sizeDecoder.decode(&uintBinarySize, 1);
77 binarySize = uintBinarySize;
78 }
79 else {
80 Base64Decoder<std::size_t> sizeDecoder(istr, 1);
81 sizeDecoder.decode(&binarySize, 1);
82 }
83 //OLB_PRECONDITION(binarySize == serializer.getSize());
84
85
86 Base64Decoder<bool> dataDecoder(istr, binarySize);
87
88 std::size_t blockSize;
89 bool* dataBuffer = nullptr;
90 while (dataBuffer = serializer.getNextBlock(blockSize, true), dataBuffer != nullptr) {
91 dataDecoder.decode(dataBuffer, blockSize);
92 }
93 serializer.resetCounter();
94}
95
96void serializer2buffer(Serializer& serializer, std::uint8_t* buffer)
97{
98 serializer.resetCounter();
99 std::size_t blockSize;
100 const bool* dataBuffer = nullptr;
101 while (dataBuffer = serializer.getNextBlock(blockSize, false), dataBuffer != nullptr) {
102 std::memcpy(buffer, dataBuffer, blockSize);
103 buffer += blockSize;
104 }
105 serializer.resetCounter();
106}
107
108void buffer2serializer(Serializer& serializer, const std::uint8_t* buffer)
109{
110 serializer.resetCounter();
111 std::size_t blockSize;
112 bool* dataBuffer = nullptr;
113 while (dataBuffer = serializer.getNextBlock(blockSize, true), dataBuffer != nullptr) {
114 std::memcpy(dataBuffer, buffer, blockSize);
115 buffer += blockSize;
116 }
117 serializer.resetCounter();
118}
119
120} // namespace olb
121
122#endif
void decode(T *data, size_t length)
Definition base64.hh:145
void encode(const T *data, size_t length)
Definition base64.hh:59
Class for writing, reading, sending and receiving Serializable objects.
Definition serializer.h:41
void resetCounter()
Resets the _iBlock counter.
Definition serializer.hh:49
std::size_t getSize() const
Returns the total memory size in bits.
Definition serializer.hh:54
bool * getNextBlock(std::size_t &sizeBlock, const bool loadingMode)
Returns pointer to the memory of the current block and increments iBlock
Definition serializer.hh:59
Top level namespace for all of OpenLB.
void serializer2buffer(Serializer &serializer, std::uint8_t *buffer)
processes data from a serializer to a given buffer
void serializer2ostr(Serializer &serializer, std::ostream &ostr, bool enforceUint=false)
processes data from a serializer to a given ostr, always in parallel
void buffer2serializer(Serializer &serializer, const std::uint8_t *buffer)
processes a buffer to a serializer
void istr2serializer(Serializer &serializer, std::istream &istr, bool enforceUint=false)
processes an istr to a serializer, always in parallel
#define OLB_PRECONDITION(COND)
Definition olbDebug.h:46