/src/freeimage-svn/FreeImage/trunk/Source/OpenEXR/Imath/ImathLimits.h
Line  | Count  | Source  | 
1  |  | ///////////////////////////////////////////////////////////////////////////  | 
2  |  | //  | 
3  |  | // Copyright (c) 2002-2012, Industrial Light & Magic, a division of Lucas  | 
4  |  | // Digital Ltd. LLC  | 
5  |  | //   | 
6  |  | // All rights reserved.  | 
7  |  | //   | 
8  |  | // Redistribution and use in source and binary forms, with or without  | 
9  |  | // modification, are permitted provided that the following conditions are  | 
10  |  | // met:  | 
11  |  | // *       Redistributions of source code must retain the above copyright  | 
12  |  | // notice, this list of conditions and the following disclaimer.  | 
13  |  | // *       Redistributions in binary form must reproduce the above  | 
14  |  | // copyright notice, this list of conditions and the following disclaimer  | 
15  |  | // in the documentation and/or other materials provided with the  | 
16  |  | // distribution.  | 
17  |  | // *       Neither the name of Industrial Light & Magic nor the names of  | 
18  |  | // its contributors may be used to endorse or promote products derived  | 
19  |  | // from this software without specific prior written permission.   | 
20  |  | //   | 
21  |  | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS  | 
22  |  | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT  | 
23  |  | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR  | 
24  |  | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT  | 
25  |  | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,  | 
26  |  | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT  | 
27  |  | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,  | 
28  |  | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY  | 
29  |  | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT  | 
30  |  | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE  | 
31  |  | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  | 
32  |  | //  | 
33  |  | ///////////////////////////////////////////////////////////////////////////  | 
34  |  |  | 
35  |  |  | 
36  |  |  | 
37  |  | #ifndef INCLUDED_IMATHLIMITS_H  | 
38  |  | #define INCLUDED_IMATHLIMITS_H  | 
39  |  |  | 
40  |  | //----------------------------------------------------------------  | 
41  |  | //  | 
42  |  | //  Limitations of the basic C++ numerical data types  | 
43  |  | //  | 
44  |  | //----------------------------------------------------------------  | 
45  |  |  | 
46  |  | #include "ImathNamespace.h"  | 
47  |  | #include <float.h>  | 
48  |  | #include <limits.h>  | 
49  |  |  | 
50  |  | //------------------------------------------  | 
51  |  | // In Windows, min and max are macros.  Yay.  | 
52  |  | //------------------------------------------  | 
53  |  |  | 
54  |  | #if defined _WIN32 || defined _WIN64  | 
55  |  |     #ifdef min  | 
56  |  |         #undef min  | 
57  |  |     #endif  | 
58  |  |     #ifdef max  | 
59  |  |         #undef max  | 
60  |  |     #endif  | 
61  |  | #endif  | 
62  |  |  | 
63  |  | IMATH_INTERNAL_NAMESPACE_HEADER_ENTER  | 
64  |  |  | 
65  |  |  | 
66  |  | //-----------------------------------------------------------------  | 
67  |  | //  | 
68  |  | // Template class limits<T> returns information about the limits  | 
69  |  | // of numerical data type T:  | 
70  |  | //  | 
71  |  | //  min()   largest possible negative value of type T  | 
72  |  | //  | 
73  |  | //  max()   largest possible positive value of type T  | 
74  |  | //  | 
75  |  | //  smallest()  smallest possible positive value of type T  | 
76  |  | //      (for float and double: smallest normalized  | 
77  |  | //      positive value)  | 
78  |  | //  | 
79  |  | //  epsilon() smallest possible e of type T, for which  | 
80  |  | //      1 + e != 1  | 
81  |  | //  | 
82  |  | //  isIntegral()  returns true if T is an integral type  | 
83  |  | //  | 
84  |  | //  isSigned()  returns true if T is signed  | 
85  |  | //  | 
86  |  | // Class limits<T> is useful to implement template classes or  | 
87  |  | // functions which depend on the limits of a numerical type  | 
88  |  | // which is not known in advance; for example:  | 
89  |  | //  | 
90  |  | //  template <class T> max (T x[], int n)  | 
91  |  | //  { | 
92  |  | //      T m = limits<T>::min();  | 
93  |  | //  | 
94  |  | //      for (int i = 0; i < n; i++)  | 
95  |  | //    if (m < x[i])  | 
96  |  | //        m = x[i];  | 
97  |  | //  | 
98  |  | //      return m;  | 
99  |  | //  }  | 
100  |  | //  | 
101  |  | // Class limits<T> has been implemented for the following types:  | 
102  |  | //  | 
103  |  | //  char, signed char, unsigned char  | 
104  |  | //  short, unsigned short  | 
105  |  | //  int, unsigned int  | 
106  |  | //  long, unsigned long  | 
107  |  | //  float  | 
108  |  | //  double  | 
109  |  | //  long double  | 
110  |  | //  | 
111  |  | // Class limits<T> has only static member functions, all of which  | 
112  |  | // are implemented as inlines.  No objects of type limits<T> are  | 
113  |  | // ever created.  | 
114  |  | //  | 
115  |  | //-----------------------------------------------------------------  | 
116  |  |  | 
117  |  |  | 
118  |  | template <class T> struct limits  | 
119  |  | { | 
120  |  |     static T  min();  | 
121  |  |     static T  max();  | 
122  |  |     static T  smallest();  | 
123  |  |     static T  epsilon();  | 
124  |  |     static bool isIntegral();  | 
125  |  |     static bool isSigned();  | 
126  |  | };  | 
127  |  |  | 
128  |  |  | 
129  |  | //---------------  | 
130  |  | // Implementation  | 
131  |  | //---------------  | 
132  |  |  | 
133  |  | template <>  | 
134  |  | struct limits <char>  | 
135  |  | { | 
136  | 0  |     static char     min()   {return CHAR_MIN;} | 
137  | 0  |     static char     max()   {return CHAR_MAX;} | 
138  | 0  |     static char     smallest()  {return 1;} | 
139  | 0  |     static char     epsilon() {return 1;} | 
140  | 0  |     static bool     isIntegral()  {return true;} | 
141  | 0  |     static bool     isSigned()  {return (char) ~0 < 0;} | 
142  |  | };  | 
143  |  |  | 
144  |  | template <>  | 
145  |  | struct limits <signed char>  | 
146  |  | { | 
147  | 0  |     static signed char    min()   {return SCHAR_MIN;} | 
148  | 0  |     static signed char    max()   {return SCHAR_MAX;} | 
149  | 0  |     static signed char    smallest()  {return 1;} | 
150  | 0  |     static signed char    epsilon() {return 1;} | 
151  | 0  |     static bool     isIntegral()  {return true;} | 
152  | 0  |     static bool     isSigned()  {return true;} | 
153  |  | };  | 
154  |  |  | 
155  |  | template <>  | 
156  |  | struct limits <unsigned char>  | 
157  |  | { | 
158  | 0  |     static unsigned char  min()   {return 0;} | 
159  | 0  |     static unsigned char  max()   {return UCHAR_MAX;} | 
160  | 0  |     static unsigned char  smallest()  {return 1;} | 
161  | 0  |     static unsigned char  epsilon() {return 1;} | 
162  | 0  |     static bool     isIntegral()  {return true;} | 
163  | 0  |     static bool     isSigned()  {return false;} | 
164  |  | };  | 
165  |  |  | 
166  |  | template <>  | 
167  |  | struct limits <short>  | 
168  |  | { | 
169  | 0  |     static short    min()   {return SHRT_MIN;} | 
170  | 0  |     static short    max()   {return SHRT_MAX;} | 
171  | 0  |     static short    smallest()  {return 1;} | 
172  | 0  |     static short    epsilon() {return 1;} | 
173  | 0  |     static bool     isIntegral()  {return true;} | 
174  | 0  |     static bool     isSigned()  {return true;} | 
175  |  | };  | 
176  |  |  | 
177  |  | template <>  | 
178  |  | struct limits <unsigned short>  | 
179  |  | { | 
180  | 0  |     static unsigned short min()   {return 0;} | 
181  | 0  |     static unsigned short max()   {return USHRT_MAX;} | 
182  | 0  |     static unsigned short smallest()  {return 1;} | 
183  | 0  |     static unsigned short epsilon() {return 1;} | 
184  | 0  |     static bool     isIntegral()  {return true;} | 
185  | 0  |     static bool     isSigned()  {return false;} | 
186  |  | };  | 
187  |  |  | 
188  |  | template <>  | 
189  |  | struct limits <int>  | 
190  |  | { | 
191  | 0  |     static int      min()   {return INT_MIN;} | 
192  | 0  |     static int      max()   {return INT_MAX;} | 
193  | 0  |     static int      smallest()  {return 1;} | 
194  | 0  |     static int      epsilon() {return 1;} | 
195  | 0  |     static bool     isIntegral()  {return true;} | 
196  | 0  |     static bool     isSigned()  {return true;} | 
197  |  | };  | 
198  |  |  | 
199  |  | template <>  | 
200  |  | struct limits <unsigned int>  | 
201  |  | { | 
202  | 0  |     static unsigned int   min()   {return 0;} | 
203  | 0  |     static unsigned int   max()   {return UINT_MAX;} | 
204  | 0  |     static unsigned int   smallest()  {return 1;} | 
205  | 0  |     static unsigned int   epsilon() {return 1;} | 
206  | 0  |     static bool     isIntegral()  {return true;} | 
207  | 0  |     static bool     isSigned()  {return false;} | 
208  |  | };  | 
209  |  |  | 
210  |  | template <>  | 
211  |  | struct limits <long>  | 
212  |  | { | 
213  | 0  |     static long     min()   {return LONG_MIN;} | 
214  | 0  |     static long     max()   {return LONG_MAX;} | 
215  | 0  |     static long     smallest()  {return 1;} | 
216  | 0  |     static long     epsilon() {return 1;} | 
217  | 0  |     static bool     isIntegral()  {return true;} | 
218  | 0  |     static bool     isSigned()  {return true;} | 
219  |  | };  | 
220  |  |  | 
221  |  | template <>  | 
222  |  | struct limits <unsigned long>  | 
223  |  | { | 
224  | 0  |     static unsigned long  min()   {return 0;} | 
225  | 0  |     static unsigned long  max()   {return ULONG_MAX;} | 
226  | 0  |     static unsigned long  smallest()  {return 1;} | 
227  | 0  |     static unsigned long  epsilon() {return 1;} | 
228  | 0  |     static bool     isIntegral()  {return true;} | 
229  | 0  |     static bool     isSigned()  {return false;} | 
230  |  | };  | 
231  |  |  | 
232  |  | template <>  | 
233  |  | struct limits <float>  | 
234  |  | { | 
235  | 0  |     static float    min()   {return -FLT_MAX;} | 
236  | 0  |     static float    max()   {return FLT_MAX;} | 
237  | 0  |     static float    smallest()  {return FLT_MIN;} | 
238  | 0  |     static float    epsilon() {return FLT_EPSILON;} | 
239  | 0  |     static bool     isIntegral()  {return false;} | 
240  | 0  |     static bool     isSigned()  {return true;} | 
241  |  | };  | 
242  |  |  | 
243  |  | template <>  | 
244  |  | struct limits <double>  | 
245  |  | { | 
246  | 0  |     static double   min()   {return -DBL_MAX;} | 
247  | 0  |     static double   max()   {return DBL_MAX;} | 
248  | 0  |     static double   smallest()  {return DBL_MIN;} | 
249  | 0  |     static double   epsilon() {return DBL_EPSILON;} | 
250  | 0  |     static bool     isIntegral()  {return false;} | 
251  | 0  |     static bool     isSigned()  {return true;} | 
252  |  | };  | 
253  |  |  | 
254  |  | template <>  | 
255  |  | struct limits <long double>  | 
256  |  | { | 
257  | 0  |     static long double    min()   {return -LDBL_MAX;} | 
258  | 0  |     static long double    max()   {return LDBL_MAX;} | 
259  | 0  |     static long double    smallest()  {return LDBL_MIN;} | 
260  | 0  |     static long double    epsilon() {return LDBL_EPSILON;} | 
261  | 0  |     static bool     isIntegral()  {return false;} | 
262  | 0  |     static bool     isSigned()  {return true;} | 
263  |  | };  | 
264  |  |  | 
265  |  |  | 
266  |  | IMATH_INTERNAL_NAMESPACE_HEADER_EXIT  | 
267  |  |  | 
268  |  | #endif // INCLUDED_IMATHLIMITS_H  |