cpp-mate  0.7
Helpful library for C++.
WriteStream.hpp
1 /*
2  * Copyright (C) 2020 Alexander Kornilov (akornilov.82@gmail.com)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef CPP_MATE_WRITE_STREAM_HPP
18 #define CPP_MATE_WRITE_STREAM_HPP
19 
20 #include <CppMate/BinarySink.hpp>
21 #include <CppMate/ByteOrder.hpp>
22 #include <CppMate/Checkers.hpp>
23 
24 #include <cstring>
25 
26 #include <memory>
27 #include <string>
28 
29 namespace CppMate {
30 
34 class WriteStream final
35 {
36 public:
37 
44  WriteStream(std::shared_ptr<BinarySink> sink, ByteOrder byteOrder = ByteOrder::Unknown):
45  _sharedSink(std::move(sink)),
46  _sink(_sharedSink.get()),
47  _byteOrder(byteOrder != ByteOrder::Unknown ? byteOrder : getNativeByteOrder()),
48  _isNativeByteOrder(_byteOrder == getNativeByteOrder())
49  {
50  checkArg(_sink);
51  }
52 
60  _sink(sink),
61  _byteOrder(byteOrder != ByteOrder::Unknown ? byteOrder : getNativeByteOrder()),
62  _isNativeByteOrder(_byteOrder == getNativeByteOrder())
63  {
64  checkArg(_sink);
65  }
66 
71  ByteOrder getByteOrder() const { return _byteOrder; }
72 
78  template<typename T>
79  inline void write(T value) {
80  if (_isNativeByteOrder || sizeof(T) < 2) {
81  _sink->write(&value, sizeof(value));
82  } else {
83  T reverse;
84  auto const srcPtr = reinterpret_cast<uint8_t *>(&value);
85  auto const dstPtr = reinterpret_cast<uint8_t *>(&reverse);
86  switch (sizeof(T)) {
87  case 2:
88  dstPtr[0] = srcPtr[1];
89  dstPtr[1] = srcPtr[0];
90  break;
91  case 4:
92  dstPtr[0] = srcPtr[3];
93  dstPtr[1] = srcPtr[2];
94  dstPtr[2] = srcPtr[1];
95  dstPtr[3] = srcPtr[0];
96  break;
97  case 8:
98  dstPtr[0] = srcPtr[7];
99  dstPtr[1] = srcPtr[6];
100  dstPtr[2] = srcPtr[5];
101  dstPtr[3] = srcPtr[4];
102  dstPtr[4] = srcPtr[3];
103  dstPtr[5] = srcPtr[2];
104  dstPtr[6] = srcPtr[1];
105  dstPtr[7] = srcPtr[0];
106  break;
107  default:
108  for (auto i = 0u; i < sizeof(T); i++) {
109  dstPtr[i] = srcPtr[sizeof(T) - i - 1];
110  }
111  break;
112  }
113  _sink->write(&reverse, sizeof(reverse));
114  }
115  }
116 
122  template<typename T>
123  inline WriteStream& operator<<(T value) {
124  write(value);
125  return *this;
126  }
127 
133  void writeString(const char* str) {
134  checkArg(str);
135  _sink->write(str, strlen(str));
136  }
137 
143  WriteStream& operator<<(const char* value) {
144  writeString(value);
145  return *this;
146  }
147 
153  void writeString(const std::string& str) {
154  _sink->write(str.c_str(), str.length());
155  }
156 
162  WriteStream& operator<<(const std::string& value) {
163  writeString(value);
164  return *this;
165  }
166 
173  void writeBytes(const void* data, uint64_t size) {
174  _sink->write(data, size);
175  }
176 
177 private:
178  std::shared_ptr<BinarySink> const _sharedSink;
179  BinarySink* const _sink;
180  const ByteOrder _byteOrder;
181  const bool _isNativeByteOrder;
182 };
183 
184 } // namespace CppMate
185 
186 #endif // CPP_MATE_WRITE_STREAM_HPP
Represents low-level interface of abstract binary sink.
Definition: BinarySink.hpp:36
virtual void write(const void *data, uint64_t size)=0
Writes data into the sink.
Represents the byte stream reader with custom byte order.
Definition: WriteStream.hpp:35
void writeBytes(const void *data, uint64_t size)
Writes raw bytes to the stream.
Definition: WriteStream.hpp:173
void write(T value)
Writes item to stream.
Definition: WriteStream.hpp:79
void writeString(const std::string &str)
Writes string into the stream.
Definition: WriteStream.hpp:153
WriteStream & operator<<(T value)
operator <<
Definition: WriteStream.hpp:123
WriteStream(BinarySink *sink, ByteOrder byteOrder=ByteOrder::Unknown)
Constructor.
Definition: WriteStream.hpp:59
WriteStream & operator<<(const std::string &value)
operator <<
Definition: WriteStream.hpp:162
ByteOrder getByteOrder() const
Returns byte order of the stream.
Definition: WriteStream.hpp:71
void writeString(const char *str)
Writes string into the stream.
Definition: WriteStream.hpp:133
WriteStream(std::shared_ptr< BinarySink > sink, ByteOrder byteOrder=ByteOrder::Unknown)
Constructor.
Definition: WriteStream.hpp:44
WriteStream & operator<<(const char *value)
operator <<
Definition: WriteStream.hpp:143
Definition: BinaryData.hpp:28
ByteOrder CPP_MATE_LIB_API getNativeByteOrder()
Returns platform byte order.
ByteOrder
The byte order type.
Definition: ByteOrder.hpp:29
@ Unknown
Unknown byte order.