functions.h

Go to the documentation of this file.
00001 #ifndef FUNCTIONS_GROUP
00002 #define FUNCTIONS_GROUP
00003 
00004 /*****************************************************************************\
00005 *                                                                             *
00006 *  Name   : functions                                                         *
00007 *  Author : Chris Koeritz                                                     *
00008 *                                                                             *
00009 *******************************************************************************
00010 * Copyright (c) 1991-$now By Author.  This program is free software; you can  *
00011 * redistribute it and/or modify it under the terms of the GNU General Public  *
00012 * License as published by the Free Software Foundation; either version 2 of   *
00013 * the License or (at your option) any later version.  This is online at:      *
00014 *     http://www.fsf.org/copyleft/gpl.html                                    *
00015 * Please send any updates to: fred@gruntose.com                               *
00016 \*****************************************************************************/
00017 
00022 #include "definitions.h"
00023 
00024 namespace basis {
00025 
00026 template <class type> type maximum(type a, type b)
00027         { return (a > b)? a : b; }
00029 template <class type> type minimum(type a, type b)
00030         { return (a < b)? a : b; }
00032 
00033 template <class type> type absolute_value(type a)
00034         { return (a >= 0)? a : -a; }
00036 
00038 
00039 template <class type> bool positive(const type &a) { return a > 0; }
00041 template <class type> bool non_positive(const type a) { return a <= 0; }
00043 template <class type> bool negative(const type &a) { return a < 0; }
00045 template <class type> bool non_negative(const type &a) { return a >= 0; }
00047 
00049 
00050 // the following comparisons are borrowed from the STL.  they provide the full set of comparison
00051 // operators for any object that implements the equalizable and orderable base classes.
00052 template <class T1, class T2>
00053 bool operator != (const T1 &x, const T2 &y) { return !(x == y); }
00054 
00055 template <class T1, class T2>
00056 bool operator > (const T1 &x, const T2 &y) { return y < x; }
00057 
00058 template <class T1, class T2>
00059 bool operator <= (const T1 &x, const T2 &y) { return !(y < x); }
00060 
00061 template <class T1, class T2>
00062 bool operator >= (const T1 &x, const T2 &y) { return !(x < y); }
00063 
00065 
00067 template <class target_type, class source_type>
00068 target_type *cast_or_throw(source_type &to_cast, const target_type &ignored)
00069 {
00070   if (!&ignored) {}  // do nothing.
00071   target_type *cast = dynamic_cast<target_type *>(&to_cast);
00072   if (!cast) throw "error: casting problem, unknown RTTI cast.";
00073   return cast;
00074 }
00075 
00077 template <class target_type, class source_type>
00078 const target_type *cast_or_throw(const source_type &to_cast, const target_type &ignored)
00079 {
00080   if (!&ignored) {}  // do nothing.
00081   const target_type *cast = dynamic_cast<const target_type *>(&to_cast);
00082   if (!cast) throw "error: casting problem, unknown RTTI cast.";
00083   return cast;
00084 }
00085 
00087 
00088 template <class type> bool range_check(const type &c, const type &low,
00089     const type &high) { return (c >= low) && (c <= high); }
00091 
00092 template <class type> type square(const type &a) { return a * a; }
00094 
00095 template <class type> void flip_increasing(type &a, type &b)
00096       { if (b < a) { type tmp = a; a = b; b = tmp; } }
00098 
00099 template <class type> void flip_decreasing(type &a, type &b)
00100       { if (b > a) { type tmp = a; a = b; b = tmp; } }
00102 
00103 template <class type> void swap_values(type &a, type &b)
00104       { type tmp = a; a = b; b = tmp; }
00106 
00107 template <class type> type sign(type a)
00108       { if (a < 0) return -1; else if (a > 0) return 1; else return 0; }
00110 
00112 
00113 // helpful coding / debugging macros:
00114 
00116 
00120 template<class contents>
00121 void WHACK(contents * &ptr) { if (ptr) { delete ptr; ptr = NIL; } }
00122 
00124 
00129 template <class type> type &bogonic() {
00130    static type local_bogon;
00131    return local_bogon;
00132 }
00133 
00135 
00136 template <class type>
00137 type number_of_packets(type message_size, type packet_size)
00138 { return message_size / packet_size + ((message_size % packet_size) != 0); }
00140 
00144 template <class type>
00145 type last_packet_size(type message_size, type packet_size)
00146 { return message_size % packet_size? message_size % packet_size : packet_size; }
00148   /*< The companion call to number_of_packets; it returns the size of the last
00149   packet in the sequence of packets, taking into account the special case
00150   where the message_size divides evenly. */
00151 
00153 
00154 } //namespace.
00155 
00156 #endif
00157 
Generated on Sat Jan 28 04:22:17 2012 for hoople2 project by  doxygen 1.6.3