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 "callstack_tracker.h"
00023 #include "chaos.h"
00024
00025 #include <string.h>
00026
00027 template <class type> inline type maximum(type a, type b)
00028 { return (a > b)? a : b; }
00030 template <class type> inline type minimum(type a, type b)
00031 { return (a < b)? a : b; }
00033
00034 template <class type> inline type absolute_value(type a)
00035 { return (a >= 0)? a : -a; }
00037
00038 template <class type> inline bool positive(type a) { return a > 0; }
00040 template <class type> inline bool non_positive(type a) { return a <= 0; }
00042 template <class type> inline bool negative(type a) { return a < 0; }
00044 template <class type> inline bool non_negative(type a) { return a >= 0; }
00046
00047 namespace basis {
00048
00049
00050
00051 template <class T1, class T2>
00052 inline bool operator != (const T1 &x, const T2 &y) { return !(x == y); }
00053 template <class T1, class T2>
00054 inline bool operator > (const T1 &x, const T2 &y) { return y < x; }
00055 template <class T1, class T2>
00056 inline bool operator <= (const T1 &x, const T2 &y) { return !(y < x); }
00057 template <class T1, class T2>
00058 inline bool operator >= (const T1 &x, const T2 &y) { return !(x < y); }
00059 }
00060
00061 template <class type> inline bool range_check(const type &c, const type &low,
00062 const type &high) { return (c >= low) && (c <= high); }
00064
00065 template <class type> inline type square(const type &a) { return a * a; }
00067
00068 template <class type> inline void flip_increasing(type &a, type &b)
00069 { if (b < a) { type tmp = a; a = b; b = tmp; } }
00071
00072 template <class type> inline void flip_decreasing(type &a, type &b)
00073 { if (b > a) { type tmp = a; a = b; b = tmp; } }
00075
00076 template <class type> inline void swap_values(type &a, type &b)
00077 { type tmp = a; a = b; b = tmp; }
00079
00080 template <class type> inline type sign(type a)
00081 { if (a < 0) return -1; else if (a > 0) return 1; else return 0; }
00083
00085 template <class type> inline void shuffle_list(type *list, int elems) {
00086 const chaos rando;
00087 for (int i = 0; i < elems; i++) {
00088 int new_posn = rando.inclusive(0, elems - 1);
00089 if (new_posn == i) continue;
00090
00091 type hold = list[i];
00092 list[i] = list[new_posn];
00093 list[new_posn] = hold;
00094 }
00095 }
00096
00098
00099 template <class type>
00100 inline type number_of_packets(type message_size, type packet_size)
00101 { return message_size / packet_size + ((message_size % packet_size) != 0); }
00103
00107 template <class type>
00108 inline type last_packet_size(type message_size, type packet_size)
00109 { return message_size % packet_size? message_size % packet_size : packet_size; }
00111
00112
00113
00114
00116
00117
00118
00120
00124 template<class contents>
00125 inline void WHACK(contents * &ptr) { if (ptr) { delete ptr; ptr = NIL; } }
00126
00128 #define GC_WHACK(ptr) \
00129 delete ptr; \
00130 ptr = NIL
00131
00133 #ifndef low_byte
00134 #define low_byte(w) ((byte)(w))
00135 #endif
00137 #ifndef high_byte
00138 #define high_byte(w) ((byte)(uint16(w) >> 8))
00139 #endif
00140
00142 #ifndef low_short
00143 #define low_short(l) ((u_short)(l))
00144 #endif
00146 #ifndef high_short
00147 #define high_short(l) ((u_short)(u_int(l) >> 16))
00148 #endif
00149
00151
00154 #define __WHERE__ isprintf("%s [line %d]", __FILE__, __LINE__)
00155
00157
00162 #define FUNCDEF(func_in) \
00163 const char *func = (const char *)func_in; \
00164 frame_tracking_instance __trail_of_function(static_class_name(), func, \
00165 __FILE__, __LINE__, true)
00166
00168 #define BASE_FUNCTION(func) istring just_function = istring(func); \
00169 istring function_name = static_class_name(); \
00170 function_name += istring("::") + just_function
00172
00175 #define FUNCTION(func) BASE_FUNCTION(func); \
00176 function_name += ": "; \
00177 update_current_stack_frame_line_number(__LINE__)
00178
00180 #define BASE_INSTANCE_FUNCTION(func) istring just_function = istring(func); \
00181 istring function_name = instance_name(); \
00182 function_name += istring("::") + just_function
00184
00187 #define INSTANCE_FUNCTION(func) BASE_INSTANCE_FUNCTION(func); \
00188 function_name += ": "; \
00189 update_current_stack_frame_line_number(__LINE__)
00190
00192
00195 template <class contents>
00196 void memory_assign(contents *destination, const contents *source, int length) {
00197 #ifdef SYSV
00198 ::memcpy((void *)source, (void *)destination, length);
00199 #else
00200 ::memcpy((void *)destination, (void *)source, length);
00201 #endif
00202 }
00203
00205
00207 template <class contents>
00208 void object_assign(contents *destination, const contents *source) {
00209 if (!destination || !source) return;
00210 memory_assign(destination, source, sizeof(contents));
00211 }
00212
00213 template <class type> type &bogonic() { static type _bogon; return _bogon; }
00215
00221
00222
00223 #endif
00224