Library of Assembled Shared Sources
binary_o_stream.cpp
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2011 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43#include "lass_common.h"
44#include "binary_o_stream.h"
45#include "../num/num_cast.h"
46#include <string.h>
47
48// static_cast from pointer to TintPtr gives warning on MSVC, yet both are identical in size [Bramz]
49//
50#if LASS_COMPILER_TYPE == LASS_COMPILER_TYPE_MSVC
51# pragma warning(disable: 4311) // 'reinterpret_cast' : 'reinterpret_cast' : pointer truncation from 'const void *' to 'lass::num::TintPtr'
52#endif
53
54namespace lass
55{
56namespace io
57{
58
59// --- public --------------------------------------------------------------------------------------
60
61BinaryOStream::BinaryOStream():
63{
64}
65
66
67
68BinaryOStream::~BinaryOStream()
69{
70}
71
72
73
74BinaryOStream::pos_type BinaryOStream::tellp() const
75{
76 return doTellp();
77}
78
79
80
81BinaryOStream& BinaryOStream::seekp(pos_type position)
82{
83 doSeekp(position);
84 return *this;
85}
86
87
88
89BinaryOStream& BinaryOStream::seekp(off_type offset, std::ios_base::seekdir direction)
90{
91 doSeekp(offset, direction);
92 return *this;
93}
94
95
96
97void BinaryOStream::flush()
98{
99 doFlush();
100}
101
102
103#if !defined(LASS_HAVE_STDINT_H_INT8_T_IS_CHAR)
104
105BinaryOStream& BinaryOStream::operator<<( char x )
106{
107 return writeValue(x);
108}
109
110#endif
111
112
113BinaryOStream& BinaryOStream::operator<<( num::Tint8 x )
114{
115 return writeValue(x);
116}
117
118
119
120BinaryOStream& BinaryOStream::operator<<( num::Tuint8 x )
121{
122 return writeValue(x);
123}
124
125
126
127BinaryOStream& BinaryOStream::operator<<( num::Tint16 x )
128{
129 return writeValue(x);
130}
131
132
133
134BinaryOStream& BinaryOStream::operator<<( num::Tuint16 x )
135{
136 return writeValue(x);
137}
138
139
140
141BinaryOStream& BinaryOStream::operator<<( num::Tint32 x )
142{
143 return writeValue(x);
144}
145
146
147
148BinaryOStream& BinaryOStream::operator<<( num::Tuint32 x )
149{
150 return writeValue(x);
151}
152
153
154
155BinaryOStream& BinaryOStream::operator<<( num::Tint64 x )
156{
157 return writeValue(x);
158}
159
160
161
162BinaryOStream& BinaryOStream::operator<<( num::Tuint64 x )
163{
164 return writeValue(x);
165}
166
167
168
169BinaryOStream& BinaryOStream::operator<<( num::Tfloat32 x )
170{
171 return writeValue(x);
172}
173
174
175
176BinaryOStream& BinaryOStream::operator<<( num::Tfloat64 x )
177{
178 return writeValue(x);
179}
180
181
182
183BinaryOStream& BinaryOStream::operator<<(bool x)
184{
185 return *this << (x ? num::Tuint8(1) : num::Tuint8(0));
186}
187
188
189
190BinaryOStream& BinaryOStream::operator<<(const void* x)
191{
192 static_assert(sizeof(num::TintPtr) == sizeof(const void*), "TintPtr must have same size as pointers");
193 static_assert(sizeof(num::TintPtr) <= sizeof(num::Tint64), "TintPtr must not be wider than 64-bits");
194 return *this << static_cast<num::Tint64>(reinterpret_cast<num::TintPtr>(x));
195}
196
197
198
199BinaryOStream& BinaryOStream::operator<<(const char* x)
200{
201 return writeString(x, ::strlen(x));
202}
203
204
205
206BinaryOStream& BinaryOStream::operator<<(const std::string& x)
207{
208 return writeString(x.data(), x.length());
209}
210
211
212
213#if LASS_HAVE_WCHAR_SUPPORT
214
215BinaryOStream& BinaryOStream::operator<<(const wchar_t* x)
216{
217 std::string utf8;
218 util::wcharToUtf8(x, ::wcslen(x), utf8);
219 return writeString(utf8.data(), utf8.length());
220}
221
222
223
224BinaryOStream& BinaryOStream::operator<<(const std::wstring& x)
225{
226 std::string utf8;
227 util::wcharToUtf8(x.data(), x.length(), utf8);
228 return writeString(utf8.data(), utf8.length());
229}
230
231#endif
232
233
234
235/** write a buffer of bytes to the stream
236 * @param bytes pointer to buffer.
237 * @param numBytes length of buffer in bytes.
238 */
239size_t BinaryOStream::write(const void* bytes, size_t numBytes)
240{
241 return doWrite(bytes, numBytes);
242}
243
244// --- private -------------------------------------------------------------------------------------
245
246template <typename T>
247BinaryOStream& BinaryOStream::writeValue(T x)
248{
249 const T temp = num::fixEndianness(x, endianness());
250 doWrite(&temp, sizeof(T));
251 return *this;
252}
253
254
255
256BinaryOStream& BinaryOStream::writeString(const char* string, size_t length)
257{
258 static_assert(sizeof(size_t) <= sizeof(num::Tuint64), "size_t must not be wider than 64-bit");
259 *this << static_cast<num::Tuint64>(length);
260 doWrite(string, length);
261 return *this;
262}
263
264}
265
266}
267
base class of binary output streams.
size_t write(const void *buffer, size_t byteLength)
write a buffer of bytes to the stream
Base class for LASS binary streams with byte-order househoulding.
streams, binary streams, vrmlstreams, ...
Library for Assembled Shared Sources.
Definition config.h:53