aboutsummaryrefslogtreecommitdiffstats
path: root/util.h
blob: d98a278378f75f8e553b289393938f5c21ed1d67 (plain) (blame)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#ifndef INCLUDE_UTIL_H
#define INCLUDE_UTIL_H

// util.h
// Revision 24-apr-2009

#include <string>
#include <sstream>
#include <stdexcept>
#include <iostream>

#include <stdlib.h>

#ifdef __BORLANDC__
#pragma warn -8027
#endif

namespace util {

inline std::string stringlset (const std::string & value,
	std::string::size_type l)
{
	const std::string::size_type lvalue= value.size ();
	if (lvalue < l)
		return value + std::string (l - lvalue, ' ');
	else if (lvalue > l)
		return value.substr (0, l);
	else
		return value;
}

inline std::string stringrset (const std::string & value,
	std::string::size_type l)
{
	const std::string::size_type lvalue= value.size ();
	if (lvalue < l)
		return std::string (l - lvalue, ' ') + value;
	else if (lvalue > l)
		return value.substr (0, l);
	else
		return value;
}

template <class DEST, class ORG, class EX>
DEST checked_cast (ORG org, EX ex)
{
	DEST dest= static_cast <DEST> (org);
	if (static_cast <ORG> (dest) != org)
		throw ex;
	return dest;
}

template <class C, size_t N>
size_t dim_array (C (&) [N])
{ return N; }

template <class C, size_t N>
size_t dim_array (const C (&) [N])
{ return N; }

template <class C>
class auto_buffer
{
public:
	auto_buffer (size_t ns) :
		p (new C [ns]),
		s (ns)
	{ }
	auto_buffer () : p (0) { }
	~auto_buffer ()
	{ delete [] p; }
	void release () { p= 0; }
	void alloc (size_t ns)
	{
		delete [] p;
		p= new C [ns];
		s= ns;
	}
	operator C * () { return p; }
	typedef C * iterator;
	iterator begin () { return p; }
	iterator end () { return p + s; }
	// We define data for commodity, and better legibility
	// than begin where not used as iterator.
	C * data () { return p; }
private:
	auto_buffer (const auto_buffer &); // Forbidden
	auto_buffer & operator = (const auto_buffer &); // Forbidden
	C * p;
	size_t s;
};

template <class C>
class auto_alloc
{
public:
	auto_alloc (size_t size) :
		p (static_cast <C *> (malloc (size * sizeof (C) ) ) )
	{
		if (p == 0)
			throw std::bad_alloc ();
	}
	~auto_alloc ()
	{ free (p); }
	void release () { p= 0; }
	operator C * () { return p; }
	C * data () { return p; } // Commodity when cast is needed
private:
	auto_alloc (const auto_alloc &); // Forbidden
	auto_alloc & operator = (const auto_alloc &); // Forbidden
	C * p;
};

// This template is intended for use as a pointer to a
// private implementation that translates the constness of
// the operations in the main class to the implementation.

template <class C>
class pimpl_ptr
{
public:
	pimpl_ptr (C * ptr) :
		p (ptr)
	{ }
	~pimpl_ptr ()
	{
		delete p;
	}

	// Bypass the acces control
	C * get () const
	{ return p; }

	// The dereference operator that preserves constness.
	C & operator * ()
	{ return * p; }
	const C & operator * () const
	{ return * p; }
	C * operator -> ()
	{ return p; }
	const C * operator -> () const
	{
		return p;
	}
private:
	pimpl_ptr (const pimpl_ptr &); // Forbidden
	pimpl_ptr & operator = (const pimpl_ptr &); // Forbidden
	C * const p;
};

template <class C>
std::string to_string (const C & c)
{
	std::ostringstream oss;
	oss << c;
	return oss.str ();
}

// Functions used to avoid warnings about unused parameters.

template <class T>
inline void touch (const T & t)
{
	(void) t;
}
template <class T1, class T2>
inline void touch (const T1 & t1, const T2 & t2)
{
	(void) t1; (void) t2;
}
template <class T1, class T2, class T3>
inline void touch (const T1 & t1, const T2 & t2, const T3 & t3)
{
	(void) t1; (void) t2; (void) t3;
}
template <class T1, class T2, class T3, class T4>
inline void touch (const T1 & t1, const T2 & t2, const T3 & t3,
	const T4 & t4)
{
	(void) t1; (void) t2; (void) t3;
	(void) t4;
}
template <class T1, class T2, class T3, class T4, class T5>
inline void touch (const T1 & t1, const T2 & t2, const T3 & t3,
	const T4 & t4, const T5 & t5)
{
	(void) t1; (void) t2; (void) t3;
	(void) t4; (void) t5;
}
template <class T1, class T2, class T3, class T4, class T5, class T6>
inline void touch (const T1 & t1, const T2 & t2, const T3 & t3,
	const T4 & t4, const T5 & t5, const T6 & t6)
{
	(void) t1; (void) t2; (void) t3;
	(void) t4; (void) t5; (void) t6;
}

} // namespace

#endif

// End of util.h
Un proyecto texto-plano.xyz