]> git.mxchange.org Git - simgear.git/blob - simgear/sg_inlines.h
Update the SoundSample api so we can request that a copy of the sample be
[simgear.git] / simgear / sg_inlines.h
1 /** 
2  * \file sg_inlines.h
3  * Various inline template definitions.
4  */
5
6 // Written by Norman Vine, started June 2000.
7 //
8 // Copyright (C) 2000  Norman Vine  - nhv@cape.com
9 //
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Library General Public
12 // License as published by the Free Software Foundation; either
13 // version 2 of the License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 // Library General Public License for more details.
19 //
20 // You should have received a copy of the GNU Library General Public
21 // License along with this library; if not, write to the
22 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 // Boston, MA  02111-1307, USA.
24 //
25 // $Id$
26
27
28 #ifndef _SG_INLINES_H
29 #define _SG_INLINES_H
30
31 // return the sign of a value
32 template <class T>
33 inline int SG_SIGN(const T x) {
34     return x < T(0) ? -1 : 1;
35 }
36
37 // return the minimum of two values
38 template <class T>
39 inline T SG_MIN2(const T a, const T b) {
40     return a < b ? a : b;
41 }
42
43 // return the minimum of three values
44 template <class T>
45 inline T SG_MIN3( const T a, const T b, const T c) {
46     return (a < b ? SG_MIN2 (a, c) : SG_MIN2 (b, c));
47 }
48
49 // return the maximum of two values
50 template <class T>
51 inline T SG_MAX2(const T a, const T b) {
52     return  a > b ? a : b;
53 }
54
55 // return the maximum of three values
56 template <class T>
57 inline T SG_MAX3 (const T a, const T b, const T c) {
58     return (a > b ? SG_MAX2 (a, c) : SG_MAX2 (b, c));
59 }
60
61 // return the minimum and maximum of three values
62 template <class T>
63 inline void SG_MIN_MAX3 ( T &min, T &max, const T a, const T b, const T c) {
64     if( a > b ) {
65         if( a > c ) {
66             max = a;
67             min = SG_MIN2 (b, c);
68         } else {
69             max = c;
70             min = SG_MIN2 (a, b);
71         }
72     } else {
73         if( b > c ) {
74             max = b;
75             min = SG_MIN2 (a, c);
76         } else {
77             max = c;
78             min = SG_MIN2 (a, b);
79         }
80     }
81 }
82
83 // swap two values
84 template <class T>
85 inline void SG_SWAP( T &a, T &b) {
86     T c = a;  a = b;  b = c;
87 }
88
89 // clamp a value to lie between min and max
90 template <class T>
91 inline void SG_CLAMP_RANGE(T &x, const T min, const T max ) {
92     if ( x < min ) { x = min; }
93     if ( x > max ) { x = max; }
94 }
95
96 // normalize a value to lie between min and max
97 template <class T>
98 inline void SG_NORMALIZE_RANGE( T &val, const T min, const T max ) {
99     T step = max - min;
100     while( val >= max )  val -= step;
101     while( val < min ) val += step;
102 };
103
104 #endif // _SG_INLINES_H