File: memory_rw.h

package info (click to toggle)
simutrans 111.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 13,504 kB
  • ctags: 12,645
  • sloc: cpp: 101,849; ansic: 3,466; makefile: 694; sh: 44
file content (70 lines) | stat: -rw-r--r-- 1,515 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#ifndef _MEMORY_RW_H
#define _MEMORY_RW_H

/* This is a class to write data to memory in intel byte order
 * on every architecture
 */


#include "../simtypes.h"

class plainstring;

class memory_rw_t {
private:
	// pointer to the buffer
	char *ptr;
	// actual read/write position
	uint32 index;
	// maximal buffer size
	uint32 max_size;

	bool saving:1;
	bool overflow:1;
public:
	memory_rw_t( void *ptr, uint32 max, bool saving );

protected:
	void set_max_size( uint32 new_max_size ) { max_size = new_max_size; }

	void set_index(uint32 new_index) { index = new_index; }

public:
	uint32 get_current_index() const { return index; }

	bool is_saving() const { return saving; }
	bool is_loading() const { return !saving; }

	bool is_overflow() const { return overflow; }

	void rdwr_byte(sint8 &c);
	void rdwr_byte(uint8 &c);
	void rdwr_short(sint16 &i);
	void rdwr_short(uint16 &i);
	void rdwr_long(sint32 &i);
	void rdwr_long(uint32 &i);
	void rdwr_longlong(sint64 &i);
	void rdwr_bool(bool &i);
	void rdwr_double(double &dbl);
	// s: pointer to a string allocated with malloc!
	void rdwr_str(char *&s);

	void rdwr_str(plainstring& s);

	/**
	 * appends the contents of the other buffer from [0 .. index-1]
	 * (only if saving)
	 */
	void append(const memory_rw_t &mem);

	/**
	 * appends the contents of the other buffer from [index .. max_size-1]
	 * (only if reading)
	 */
	void append_tail(const memory_rw_t &mem);

private:
	// Low-Level Read / Write function
	void rdwr(void *data, uint32 len);
};
#endif