1 /*
2  * Copyright (C) 2007-2020 by the Widelands Development Team
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  *
18  */
19 
20 #ifndef WL_IO_STREAMWRITE_H
21 #define WL_IO_STREAMWRITE_H
22 
23 #include <cstring>
24 #include <string>
25 
26 #include "base/macros.h"
27 #include "io/machdep.h"
28 
29 /**
30  * Abstract base class for stream-like data sinks.
31  * It is intended for serializing network packets and for writing log-type
32  * data to disk.
33  *
34  * All implementations need to implement \ref data . Some implementations
35  * may need to implement \ref flush .
36  *
37  * Convenience functions are provided for many data types.
38  */
39 class StreamWrite {
40 public:
StreamWrite()41 	explicit StreamWrite() {
42 	}
43 	virtual ~StreamWrite();
44 
45 	/**
46 	 * Write a number of bytes to the stream.
47 	 */
48 	virtual void data(const void* const write_data, const size_t size) = 0;
49 
50 	/**
51 	 * Make sure all data submitted so far is written to disk.
52 	 *
53 	 * The default implementation is a no-op. Implementations may want
54 	 * to override this if they're buffered internally.
55 	 */
56 	virtual void flush();
57 
58 	// TODO(unknown): implement an overloaded method that accepts fmt as std::string
59 	void print_f(char const*, ...) __attribute__((format(printf, 2, 3)));
60 
signed_8(int8_t const x)61 	void signed_8(int8_t const x) {
62 		data(&x, 1);
63 	}
unsigned_8(uint8_t const x)64 	void unsigned_8(uint8_t const x) {
65 		data(&x, 1);
66 	}
signed_16(int16_t const x)67 	void signed_16(int16_t const x) {
68 		int16_t const y = little_16(x);
69 		data(&y, 2);
70 	}
unsigned_16(uint16_t const x)71 	void unsigned_16(uint16_t const x) {
72 		uint16_t const y = little_16(x);
73 		data(&y, 2);
74 	}
signed_32(int32_t const x)75 	void signed_32(int32_t const x) {
76 		uint32_t const y = little_32(x);
77 		data(&y, 4);
78 	}
unsigned_32(uint32_t const x)79 	void unsigned_32(uint32_t const x) {
80 		uint32_t const y = little_32(x);
81 		data(&y, 4);
82 	}
float_32(const float x)83 	void float_32(const float x) {
84 		uint32_t y;
85 		memcpy(&y, &x, 4);
86 		y = little_32(y);
87 		data(&y, 4);
88 	}
string(const std::string & str)89 	void string(const std::string& str) {
90 		data(str.c_str(), str.size() + 1);
91 	}
92 
93 	//  Write strings with    null terminator.
c_string(char const * const x)94 	void c_string(char const* const x) {
95 		data(x, strlen(x) + 1);
96 	}
c_string(const std::string & x)97 	void c_string(const std::string& x) {
98 		data(x.c_str(), x.size() + 1);
99 	}
100 
101 	//  Write strings without null terminator.
text(char const * const x)102 	void text(char const* const x) {
103 		data(x, strlen(x));
104 	}
text(const std::string & x)105 	void text(const std::string& x) {
106 		data(x.c_str(), x.size());
107 	}
108 
109 private:
110 	DISALLOW_COPY_AND_ASSIGN(StreamWrite);
111 };
112 
113 #endif  // end of include guard: WL_IO_STREAMWRITE_H
114