File: Size.h

package info (click to toggle)
storm-lang 0.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,028 kB
  • sloc: ansic: 261,471; cpp: 140,432; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (181 lines) | stat: -rw-r--r-- 4,190 bytes parent folder | download | duplicates (3)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#pragma once
#include "ISize.h"

/**
 * Machine-independent size description. This class allows composition
 * of primitive types to composite types. For composite types, the
 * class keeps track of the needed alignment for the type as well.
 *
 * This is currently implemented as two separate sizes, one assuming
 * 32-bit pointers and the other assuming 64-bit pointers. This will
 * suffice for a while, and may be extended in the future.
 */
class Size {
	friend class Offset;
public:
	// Initialize to zero.
	Size();

	// Initialize to a known size (platform independent).
	explicit Size(nat s);

	// Initialize to previously obtained values.
	Size(nat size32, nat align32, nat size64, nat align64);

	// Get the size for the current platform, the size being properly aligned.
	nat current() const;

	// Get a Size of zero, only with the align of this Size.
	Size align() const;

	// Pointer size.
	static Size sPtr;

	// Integer sizes.
	static Size sChar;
	static Size sByte;
	static Size sInt;
	static Size sNat;
	static Size sLong;
	static Size sWord;

	// Float sizes.
	static Size sFloat;
	static Size sDouble;

	// Addition (note that we do not have subtraction).
	Size &operator +=(const Size &o);
	Size operator +(const Size &o) const;

	// Multiplication with positive. Equal to repeated addition.
	Size &operator *=(nat o);
	Size operator *(nat o) const;

	// Equality check.
	bool operator ==(const Size &o) const;
	bool operator !=(const Size &o) const;

	// Greater/lesser?
	bool operator <(const Size &o) const;
	bool operator >(const Size &o) const;
	bool operator >=(const Size &o) const;
	bool operator <=(const Size &o) const;

	// Find out the 32- and 64-bit sizes (for storage).
	inline nat size32() const { return s32.size; }
	inline nat size64() const { return s64.size; }
	inline nat align32() const { return s32.align; }
	inline nat align64() const { return s64.align; }

	// Output.
	friend wostream &operator <<(wostream &to, const Size &s);
private:
	// Initialize to specific values.
	Size(nat s32, nat s64);

	// 32-bit ptr size. Suprisingly, alignments of 8 occur when working with long long ints.
	ISize<8> s32;

	// 64-bit ptr size.
	ISize<8> s64;
};

/**
 * Output.
 */
wostream &operator <<(wostream &to, const Size &s);


/**
 * Offset. Differently from size, an offset does not keep alignment!
 */
class Offset {
	friend class Size;
public:
	// Initialize to zero.
	Offset();

	// Initialize to a known size (platform independent).
	explicit Offset(int s);

	// Convert from Size.
	explicit Offset(Size s);

	// Initialize to specific values.
	Offset(int s32, int s64);

	// Get the size for the current platform.
	int current() const;

	// Pointer size.
	static Offset sPtr;

	// Integer sizes.
	static Offset sChar;
	static Offset sByte;
	static Offset sInt;
	static Offset sNat;
	static Offset sLong;
	static Offset sWord;

	// Addition and subtraction
	template <class O>
	Offset operator +(const O &o) const {
		Offset t = *this;
		t += o;
		return t;
	}
	template <class O>
	Offset operator -(const O &o) const {
		Offset t = *this;
		t -= o;
		return t;
	}

	Offset &operator +=(const Offset &o);
	Offset &operator -=(const Offset &o);

	// Addition/subtraction with Size
	Offset &operator +=(const Size &o);
	Offset &operator -=(const Size &o);

	// Multiplication.
	Offset &operator *=(int o);
	Offset operator *(int o) const;

	// Negation. 'add' will still move further from zero.
	Offset operator -() const;

	// Equality check.
	bool operator ==(const Offset &o) const;
	bool operator !=(const Offset &o) const;

	// Greater/lesser?
	bool operator <(const Offset &o) const;
	bool operator >(const Offset &o) const;
	bool operator >=(const Offset &o) const;
	bool operator <=(const Offset &o) const;

	// Formatted string output.
	String format(bool sign) const;

	// Find out the 32- and 64-bit sizes (for storage).
	inline nat v32() const { return o32; }
	inline nat v64() const { return o64; }

	// Output.
	friend wostream &operator <<(wostream &to, const Offset &s);

	Offset abs() const;
private:
	// 32-bit offset.
	int o32;

	// 64-bit offset.
	int o64;
};

/**
 * Output.
 */
wostream &operator <<(wostream &to, const Size &s);