feisty meow concerns codebase  2.140
functions.h
Go to the documentation of this file.
1 #ifndef FUNCTIONS_GROUP
2 #define FUNCTIONS_GROUP
3 
4 /*****************************************************************************\
5 * *
6 * Name : functions *
7 * Author : Chris Koeritz *
8 * *
9 *******************************************************************************
10 * Copyright (c) 1991-$now By Author. This program is free software; you can *
11 * redistribute it and/or modify it under the terms of the GNU General Public *
12 * License as published by the Free Software Foundation; either version 2 of *
13 * the License or (at your option) any later version. This is online at: *
14 * http://www.fsf.org/copyleft/gpl.html *
15 * Please send any updates to: fred@gruntose.com *
16 \*****************************************************************************/
17 
22 #include "definitions.h"
23 
24 namespace basis {
25 
26 template <class type> type maximum(type a, type b)
27  { return (a > b)? a : b; }
29 template <class type> type minimum(type a, type b)
30  { return (a < b)? a : b; }
32 
33 template <class type> type absolute_value(type a)
34  { return (a >= 0)? a : -a; }
36 
38 
39 template <class type> bool positive(const type &a) { return a > 0; }
41 template <class type> bool non_positive(const type a) { return a <= 0; }
43 template <class type> bool negative(const type &a) { return a < 0; }
45 template <class type> bool non_negative(const type &a) { return a >= 0; }
47 
49 
50 // the following comparisons are borrowed from the STL. they provide the full set of comparison
51 // operators for any object that implements the equalizable and orderable base classes.
52 template <class T1, class T2>
53 bool operator != (const T1 &x, const T2 &y) { return !(x == y); }
54 
55 template <class T1, class T2>
56 bool operator > (const T1 &x, const T2 &y) { return y < x; }
57 
58 template <class T1, class T2>
59 bool operator <= (const T1 &x, const T2 &y) { return !(y < x); }
60 
61 template <class T1, class T2>
62 bool operator >= (const T1 &x, const T2 &y) { return !(x < y); }
63 
65 
67 template <class target_type, class source_type>
68 target_type *cast_or_throw(source_type &to_cast, const target_type &ignored)
69 {
70 // if (!&ignored) {} // do nothing.
71  target_type *cast = dynamic_cast<target_type *>(&to_cast);
72  if (!cast) throw "error: casting problem, unknown RTTI cast.";
73  return cast;
74 }
75 
77 template <class target_type, class source_type>
78 const target_type *cast_or_throw(const source_type &to_cast, const target_type &ignored)
79 {
80 // if (!&ignored) {} // do nothing.
81  const target_type *cast = dynamic_cast<const target_type *>(&to_cast);
82  if (!cast) throw "error: casting problem, unknown RTTI cast.";
83  return cast;
84 }
85 
87 
88 template <class type> bool range_check(const type &c, const type &low,
89  const type &high) { return (c >= low) && (c <= high); }
91 
92 template <class type> type square(const type &a) { return a * a; }
94 
95 template <class type> void flip_increasing(type &a, type &b)
96  { if (b < a) { type tmp = a; a = b; b = tmp; } }
98 
99 template <class type> void flip_decreasing(type &a, type &b)
100  { if (b > a) { type tmp = a; a = b; b = tmp; } }
102 
103 template <class type> void swap_values(type &a, type &b)
104  { type tmp = a; a = b; b = tmp; }
106 
107 template <class type> type sign(type a)
108  { if (a < 0) return -1; else if (a > 0) return 1; else return 0; }
110 
112 
113 // helpful coding / debugging macros:
114 
116 
120 template<class contents>
121 void WHACK(contents * &ptr) { if (ptr) { delete ptr; ptr = NULL_POINTER; } }
122 
124 
129 template <class type> type &bogonic() {
130  static type local_bogon;
131  return local_bogon;
132 }
133 
135 
136 template <class type>
137 type number_of_packets(type message_size, type packet_size)
138 { return message_size / packet_size + ((message_size % packet_size) != 0); }
140 
144 template <class type>
145 type last_packet_size(type message_size, type packet_size)
146 { return message_size % packet_size? message_size % packet_size : packet_size; }
148  /*< The companion call to number_of_packets; it returns the size of the last
149  packet in the sequence of packets, taking into account the special case
150  where the message_size divides evenly. */
151 
153 
154 } //namespace.
155 
156 #endif
157 
Constants and objects used throughout HOOPLE.
#define NULL_POINTER
The value representing a pointer to nothing.
Definition: definitions.h:32
The guards collection helps in testing preconditions and reporting errors.
Definition: array.h:30
bool operator!=(const T1 &x, const T2 &y)
Definition: functions.h:53
bool operator>=(const T1 &x, const T2 &y)
Definition: functions.h:62
bool range_check(const type &c, const type &low, const type &high)
Returns true if "c" is between "low" and "high" inclusive.
Definition: functions.h:88
type number_of_packets(type message_size, type packet_size)
Reports number of packets needed given a total size and the packet size.
Definition: functions.h:137
bool positive(const type &a)
positive returns true if "a" is greater than zero, or false otherwise.
Definition: functions.h:39
void WHACK(contents *&ptr)
deletion with clearing of the pointer.
Definition: functions.h:121
bool operator<=(const T1 &x, const T2 &y)
Definition: functions.h:59
void swap_values(type &a, type &b)
Exchanges the values held by "a" & "b".
Definition: functions.h:103
type maximum(type a, type b)
minimum returns the lesser of two values.
Definition: functions.h:26
bool non_positive(const type a)
non_positive returns true if "a" is less than or equal to zero.
Definition: functions.h:41
type square(const type &a)
Returns the square of the object (which is a * a).
Definition: functions.h:92
bool non_negative(const type &a)
non_negative returns true if "a" is greater than or equal to zero.
Definition: functions.h:45
bool operator>(const T1 &x, const T2 &y)
Definition: functions.h:56
void flip_decreasing(type &a, type &b)
Makes sure that two values are in decreasing order (a > b).
Definition: functions.h:99
target_type * cast_or_throw(source_type &to_cast, const target_type &ignored)
dynamically converts a type to a target type, or throws an exception if it cannot.
Definition: functions.h:68
type last_packet_size(type message_size, type packet_size)
Tells how many bytes are used within last packet.
Definition: functions.h:144
type sign(type a)
Returns the numerical sign of a number "a".
Definition: functions.h:107
void flip_increasing(type &a, type &b)
Makes sure that two values are in increasing order (a < b).
Definition: functions.h:95
type minimum(type a, type b)
maximum returns the greater of two values.
Definition: functions.h:29
bool negative(const type &a)
negative returns true if "a" is less than zero.
Definition: functions.h:43
type absolute_value(type a)
Returns a if a is non-negative, and returns -a otherwise.
Definition: functions.h:33
type & bogonic()
Returns an object that is defined statically.
Definition: functions.h:129