From 469bbcf0701e1eb8a6670c23145b0da87357e178 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Sun, 25 Mar 2012 20:00:08 +0000 Subject: Code is all reorganized. We're about ready to release. I should write up a little explenation of the arrangement. --- src/stable/util.h | 187 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 187 insertions(+) create mode 100644 src/stable/util.h (limited to 'src/stable/util.h') 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 @@ +/* + * Copyright (C) 2007-2011 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 + +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 + 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 + const item &min( const item &a, const item &b ) + { + return a + item &min( item &a, item &b ) + { + return a + const item &max( const item &a, const item &b ) + { + return b + item &max( item &a, item &b ) + { + return b + 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 + 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 + 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 + struct __basicGTCmp + { + bool operator()( const item &a, const item &b ) + { + return a > b; + } + }; + + /** + * As __basicLTCmp but dereferences the passed in pointers before comparing. + */ + template + struct __basicPtrLTCmp + { + bool operator()( const item &a, const item &b ) + { + return *a < *b; + } + }; + + /** + * As __basicGTCmp but dereferences the passed in pointers before comparing. + */ + template + 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 -- cgit v1.2.3