aboutsummaryrefslogtreecommitdiff
path: root/src/stable/util.h
blob: 087af6e774aad7f94e0a700bf33501ab1eb03651 (plain)
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
/*
 * Copyright (C) 2007-2012 Xagasoft, All rights reserved.
 *
 * This file is part of the libbu++ library and is released under the
 * terms of the license contained in the file LICENSE.
 */

#ifndef BU_UTIL_H
#define BU_UTIL_H

#ifndef NULL
# define NULL 0
#endif

/* I borrowed this from someone who borrowed it from glib who borrowed it
 * from...
 */
#if (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
# define DEPRECATED  __attribute__((__deprecated__))
#else
# define DEPRECATED
#endif /* __GNUC__ */

#include <string.h>

namespace Bu
{
	/**
	 * Swap the value of two variables, uses references, so it's pretty safe.
	 * Objects passed in must support a basic assignemnt operator (=);
	 *@param a Variable to recieve the value of parameter b
	 *@param b Variable to recieve the value of parameter a
	 */
	template<typename item>
	void swap( item &a, item &b )
	{
		item tmp = a;
		a = b;
		b = tmp;
	}

#ifdef WIN32
	#warning: removing min and max win32 macros because of compile conflict
	#undef min
	#undef max
#endif
	/**
	 * Finds the lesser of the two objects, objects passed in must be
	 * less-than-comparable.
	 *@param a A value to test.
	 *@param b Another value to test.
	 *@returns A reference to the lesser of a or b.
	 */
	template<typename item>
	const item &min( const item &a, const item &b )
	{
		return a<b?a:b;
	}
	
	/**
	 * Finds the lesser of the two objects, objects passed in must be
	 * less-than-comparable.
	 *@param a A value to test.
	 *@param b Another value to test.
	 *@returns A reference to the lesser of a or b.
	 */
	template<typename item>
	item &min( item &a, item &b )
	{
		return a<b?a:b;
	}
	
	/**
	 * Finds the greater of the two objects, objects passed in must be
	 * less-than-comparable.
	 *@param a A value to test.
	 *@param b Another value to test.
	 *@returns A reference to the greater of a or b.
	 */
	template<typename item>
	const item &max( const item &a, const item &b )
	{
		return b<a?a:b;
	}
	
	/**
	 * Finds the greater of the two objects, objects passed in must be
	 * less-than-comparable.
	 *@param a A value to test.
	 *@param b Another value to test.
	 *@returns A reference to the greater of a or b.
	 */
	template<typename item>
	item &max( item &a, item &b )
	{
		return b<a?a:b;
	}

	/**
	 * Given three objects this finds the one between the other two.
	 *@param a A value to test.
	 *@param b Another value to test.
	 *@param c Yet another value to test.
	 *@returns A reference to the mid-value of a, b, and c.
	 */
	template<typename item>
	const item &mid( const item &a, const item &b, const item &c )
	{
		return min( max( a, b ), c );
	}
		
	/**
	 * Given three objects this finds the one between the other two.
	 *@param a A value to test.
	 *@param b Another value to test.
	 *@param c Yet another value to test.
	 *@returns A reference to the mid-value of a, b, and c.
	 */
	template<typename item>
	item &mid( item &a, item &b, item &c )
	{
		return min( max( a, b ), c );
	}
	
	//
	//  Basic comparison functors
	//
	/**
	 * Simple less-than comparison functor.  Objects being used should be
	 * less-than-comparable.
	 */
	template<typename item>
	struct __basicLTCmp
	{
		bool operator()( const item &a, const item &b )
		{
			return a < b;
		}
	};
	
	/**
	 * Simple greater-than comparison functor.  Objects being used should be
	 * greater-than-comparable.
	 */
	template<typename item>
	struct __basicGTCmp
	{
		bool operator()( const item &a, const item &b )
		{
			return a > b;
		}
	};

	/**
	 * As __basicLTCmp but dereferences the passed in pointers before comparing.
	 */
	template<typename item>
	struct __basicPtrLTCmp
	{
		bool operator()( const item &a, const item &b )
		{
			return *a < *b;
		}
	};
	
	/**
	 * As __basicGTCmp but dereferences the passed in pointers before comparing.
	 */
	template<typename item>
	struct __basicPtrGTCmp
	{
		bool operator()( const item &a, const item &b )
		{
			return *a > *b;
		}
	};

	/**
	 * Get the number of days in the month in the gregorian calendar, taking
	 * leap years into account.
	 */
	int getDaysInMonth( int iMonth, int iYear );

	void memcpy( void *pDest, const void *pSrc, size_t iBytes );
};

#endif