diff options
Diffstat (limited to 'src/stable/util.h')
-rw-r--r-- | src/stable/util.h | 187 |
1 files changed, 187 insertions, 0 deletions
diff --git a/src/stable/util.h b/src/stable/util.h new file mode 100644 index 0000000..691184d --- /dev/null +++ b/src/stable/util.h | |||
@@ -0,0 +1,187 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2007-2011 Xagasoft, All rights reserved. | ||
3 | * | ||
4 | * This file is part of the libbu++ library and is released under the | ||
5 | * terms of the license contained in the file LICENSE. | ||
6 | */ | ||
7 | |||
8 | #ifndef BU_UTIL_H | ||
9 | #define BU_UTIL_H | ||
10 | |||
11 | #ifndef NULL | ||
12 | # define NULL 0 | ||
13 | #endif | ||
14 | |||
15 | /* I borrowed this from someone who borrowed it from glib who borrowed it | ||
16 | * from... | ||
17 | */ | ||
18 | #if (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)) | ||
19 | # define DEPRECATED __attribute__((__deprecated__)) | ||
20 | #else | ||
21 | # define DEPRECATED | ||
22 | #endif /* __GNUC__ */ | ||
23 | |||
24 | #include <string.h> | ||
25 | |||
26 | namespace Bu | ||
27 | { | ||
28 | /** | ||
29 | * Swap the value of two variables, uses references, so it's pretty safe. | ||
30 | * Objects passed in must support a basic assignemnt operator (=); | ||
31 | *@param a Variable to recieve the value of parameter b | ||
32 | *@param b Variable to recieve the value of parameter a | ||
33 | */ | ||
34 | template<typename item> | ||
35 | void swap( item &a, item &b ) | ||
36 | { | ||
37 | item tmp = a; | ||
38 | a = b; | ||
39 | b = tmp; | ||
40 | } | ||
41 | |||
42 | #ifdef WIN32 | ||
43 | #warning: removing min and max win32 macros because of compile conflict | ||
44 | #undef min | ||
45 | #undef max | ||
46 | #endif | ||
47 | /** | ||
48 | * Finds the lesser of the two objects, objects passed in must be | ||
49 | * less-than-comparable. | ||
50 | *@param a A value to test. | ||
51 | *@param b Another value to test. | ||
52 | *@returns A reference to the lesser of a or b. | ||
53 | */ | ||
54 | template<typename item> | ||
55 | const item &min( const item &a, const item &b ) | ||
56 | { | ||
57 | return a<b?a:b; | ||
58 | } | ||
59 | |||
60 | /** | ||
61 | * Finds the lesser of the two objects, objects passed in must be | ||
62 | * less-than-comparable. | ||
63 | *@param a A value to test. | ||
64 | *@param b Another value to test. | ||
65 | *@returns A reference to the lesser of a or b. | ||
66 | */ | ||
67 | template<typename item> | ||
68 | item &min( item &a, item &b ) | ||
69 | { | ||
70 | return a<b?a:b; | ||
71 | } | ||
72 | |||
73 | /** | ||
74 | * Finds the greater of the two objects, objects passed in must be | ||
75 | * less-than-comparable. | ||
76 | *@param a A value to test. | ||
77 | *@param b Another value to test. | ||
78 | *@returns A reference to the greater of a or b. | ||
79 | */ | ||
80 | template<typename item> | ||
81 | const item &max( const item &a, const item &b ) | ||
82 | { | ||
83 | return b<a?a:b; | ||
84 | } | ||
85 | |||
86 | /** | ||
87 | * Finds the greater of the two objects, objects passed in must be | ||
88 | * less-than-comparable. | ||
89 | *@param a A value to test. | ||
90 | *@param b Another value to test. | ||
91 | *@returns A reference to the greater of a or b. | ||
92 | */ | ||
93 | template<typename item> | ||
94 | item &max( item &a, item &b ) | ||
95 | { | ||
96 | return b<a?a:b; | ||
97 | } | ||
98 | |||
99 | /** | ||
100 | * Given three objects this finds the one between the other two. | ||
101 | *@param a A value to test. | ||
102 | *@param b Another value to test. | ||
103 | *@param c Yet another value to test. | ||
104 | *@returns A reference to the mid-value of a, b, and c. | ||
105 | */ | ||
106 | template<typename item> | ||
107 | const item &mid( const item &a, const item &b, const item &c ) | ||
108 | { | ||
109 | return min( max( a, b ), c ); | ||
110 | } | ||
111 | |||
112 | /** | ||
113 | * Given three objects this finds the one between the other two. | ||
114 | *@param a A value to test. | ||
115 | *@param b Another value to test. | ||
116 | *@param c Yet another value to test. | ||
117 | *@returns A reference to the mid-value of a, b, and c. | ||
118 | */ | ||
119 | template<typename item> | ||
120 | item &mid( item &a, item &b, item &c ) | ||
121 | { | ||
122 | return min( max( a, b ), c ); | ||
123 | } | ||
124 | |||
125 | // | ||
126 | // Basic comparison functors | ||
127 | // | ||
128 | /** | ||
129 | * Simple less-than comparison functor. Objects being used should be | ||
130 | * less-than-comparable. | ||
131 | */ | ||
132 | template<typename item> | ||
133 | struct __basicLTCmp | ||
134 | { | ||
135 | bool operator()( const item &a, const item &b ) | ||
136 | { | ||
137 | return a < b; | ||
138 | } | ||
139 | }; | ||
140 | |||
141 | /** | ||
142 | * Simple greater-than comparison functor. Objects being used should be | ||
143 | * greater-than-comparable. | ||
144 | */ | ||
145 | template<typename item> | ||
146 | struct __basicGTCmp | ||
147 | { | ||
148 | bool operator()( const item &a, const item &b ) | ||
149 | { | ||
150 | return a > b; | ||
151 | } | ||
152 | }; | ||
153 | |||
154 | /** | ||
155 | * As __basicLTCmp but dereferences the passed in pointers before comparing. | ||
156 | */ | ||
157 | template<typename item> | ||
158 | struct __basicPtrLTCmp | ||
159 | { | ||
160 | bool operator()( const item &a, const item &b ) | ||
161 | { | ||
162 | return *a < *b; | ||
163 | } | ||
164 | }; | ||
165 | |||
166 | /** | ||
167 | * As __basicGTCmp but dereferences the passed in pointers before comparing. | ||
168 | */ | ||
169 | template<typename item> | ||
170 | struct __basicPtrGTCmp | ||
171 | { | ||
172 | bool operator()( const item &a, const item &b ) | ||
173 | { | ||
174 | return *a > *b; | ||
175 | } | ||
176 | }; | ||
177 | |||
178 | /** | ||
179 | * Get the number of days in the month in the gregorian calendar, taking | ||
180 | * leap years into account. | ||
181 | */ | ||
182 | int getDaysInMonth( int iMonth, int iYear ); | ||
183 | |||
184 | void memcpy( void *pDest, const void *pSrc, size_t iBytes ); | ||
185 | }; | ||
186 | |||
187 | #endif | ||