1 //-----------------------------------------------------------------------------
2 // Project : SDK Core
3 //
4 // Category : SDK Core Interfaces
5 // Filename : pluginterfaces/base/futils.h
6 // Created by : Steinberg, 01/2004
7 // Description : Basic utilities
8 //
9 //-----------------------------------------------------------------------------
10 // This file is part of a Steinberg SDK. It is subject to the license terms
11 // in the LICENSE file found in the top-level directory of this distribution
12 // and at www.steinberg.net/sdklicenses.
13 // No part of the SDK, including this file, may be copied, modified, propagated,
14 // or distributed except according to the terms contained in the LICENSE file.
15 //-----------------------------------------------------------------------------
16
17 #pragma once
18
19 #include "pluginterfaces/base/ftypes.h"
20
21 namespace Steinberg {
22 //----------------------------------------------------------------------------
23 // min/max/etc. template functions
24 template <class T>
Min(const T & a,const T & b)25 inline const T& Min (const T& a, const T& b)
26 {
27 return b < a ? b : a;
28 }
29
30 template <class T>
Max(const T & a,const T & b)31 inline const T& Max (const T& a, const T& b)
32 {
33 return a < b ? b : a;
34 }
35
36 template <class T>
Abs(const T & value)37 inline T Abs (const T& value)
38 {
39 return (value >= (T)0) ? value : -value;
40 }
41
42 template <class T>
Sign(const T & value)43 inline T Sign (const T& value)
44 {
45 return (value == (T)0) ? 0 : ((value >= (T)0) ? 1 : -1);
46 }
47
48 template <class T>
Bound(T minval,T maxval,T x)49 inline T Bound (T minval, T maxval, T x)
50 {
51 if (x < minval)
52 return minval;
53 else if (x > maxval)
54 return maxval;
55 return x;
56 }
57
58 template <class T>
Swap(T & t1,T & t2)59 void Swap (T& t1, T& t2)
60 {
61 T tmp = t1;
62 t1 = t2;
63 t2 = tmp;
64 }
65
66 template <class T>
IsApproximateEqual(T t1,T t2,T epsilon)67 bool IsApproximateEqual (T t1, T t2, T epsilon)
68 {
69 if (t1 == t2)
70 return true;
71 T diff = t1 - t2;
72 if (diff < 0.0)
73 diff = -diff;
74 if (diff < epsilon)
75 return true;
76 return false;
77 }
78
79 template <class T>
ToNormalized(const T & value,const int32 numSteps)80 inline T ToNormalized (const T& value, const int32 numSteps)
81 {
82 return value / T (numSteps);
83 }
84
85 template <class T>
FromNormalized(const T & norm,const int32 numSteps)86 inline int32 FromNormalized (const T& norm, const int32 numSteps)
87 {
88 return Min<int32> (numSteps, int32 (norm * (numSteps + 1)));
89 }
90
91 // Four character constant
92 #ifndef CCONST
93 #define CCONST(a, b, c, d) \
94 ((((int32) (a)) << 24) | (((int32) (b)) << 16) | (((int32) (c)) << 8) | (((int32) (d)) << 0))
95 #endif
96
97 //------------------------------------------------------------------------
98 } // namespace Steinberg
99