functions.h
Go to the documentation of this file.00001 #ifndef FUNCTIONS_GROUP
00002 #define FUNCTIONS_GROUP
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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
00051
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) {}
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) {}
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
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
00149
00150
00151
00153
00154 }
00155
00156 #endif
00157