source: NonGTP/OpenEXR/include/Imath/ImathLimits.h @ 855

Revision 855, 7.6 KB checked in by igarcia, 19 years ago (diff)
RevLine 
[855]1///////////////////////////////////////////////////////////////////////////
2//
3// Copyright (c) 2002, 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 <float.h>
47#include <limits.h>
48
49//------------------------------------------
50// In Windows, min and max are macros.  Yay.
51//------------------------------------------
52
53#ifdef PLATFORM_WINDOWS
54    #ifdef min
55        #undef min
56    #endif
57    #ifdef max
58        #undef max
59    #endif
60#endif
61
62namespace Imath {
63
64
65//-----------------------------------------------------------------
66//
67// Template class limits<T> returns information about the limits
68// of numerical data type T:
69//
70//      min()           largest possible negative value of type T
71//
72//      max()           largest possible positive value of type T
73//
74//      smallest()      smallest possible positive value of type T
75//
76//      epsilon()       smallest possible e of type T, for which
77//                      1 + e != 1
78//
79//      isIntegral()    returns true if T is an integral type
80//
81//      isSigned()      returns true if T is signed
82//
83// Class limits<T> is useful to implement template classes or
84// functions which depend on the limits of a numerical type
85// which is not known in advance; for example:
86//
87//      template <class T> max (T x[], int n)
88//      {
89//          T m = limits<T>::min();
90//
91//          for (int i = 0; i < n; i++)
92//              if (m < x[i])
93//                  m = x[i];
94//
95//          return m;
96//      }
97//
98// Class limits<T> has been implemented for the following types:
99//
100//      char, signed char, unsigned char
101//      short, unsigned short
102//      int, unsigned int
103//      long, unsigned long
104//      float
105//      double
106//      long double
107//
108// Class limits<T> has only static member functions, all of which
109// are implemented as inlines.  No objects of type limits<T> are
110// ever created.
111//
112//-----------------------------------------------------------------
113
114
115template <class T> struct limits
116{
117    static T    min();
118    static T    max();
119    static T    smallest();
120    static T    epsilon();
121    static bool isIntegral();
122    static bool isSigned();
123};
124
125
126//---------------
127// Implementation
128//---------------
129
130template <>
131struct limits <char>
132{
133    static char                 min()           {return CHAR_MIN;}
134    static char                 max()           {return CHAR_MAX;}
135    static char                 smallest()      {return 1;}
136    static char                 epsilon()       {return 1;}
137    static bool                 isIntegral()    {return true;}
138    static bool                 isSigned()      {return (char) ~0 < 0;}
139};
140
141template <>
142struct limits <signed char>
143{
144    static signed char          min()           {return SCHAR_MIN;}
145    static signed char          max()           {return SCHAR_MAX;}
146    static signed char          smallest()      {return 1;}
147    static signed char          epsilon()       {return 1;}
148    static bool                 isIntegral()    {return true;}
149    static bool                 isSigned()      {return true;}
150};
151
152template <>
153struct limits <unsigned char>
154{
155    static unsigned char        min()           {return 0;}
156    static unsigned char        max()           {return UCHAR_MAX;}
157    static unsigned char        smallest()      {return 1;}
158    static unsigned char        epsilon()       {return 1;}
159    static bool                 isIntegral()    {return true;}
160    static bool                 isSigned()      {return false;}
161};
162
163template <>
164struct limits <short>
165{
166    static short                min()           {return SHRT_MIN;}
167    static short                max()           {return SHRT_MAX;}
168    static short                smallest()      {return 1;}
169    static short                epsilon()       {return 1;}
170    static bool                 isIntegral()    {return true;}
171    static bool                 isSigned()      {return true;}
172};
173
174template <>
175struct limits <unsigned short>
176{
177    static unsigned short       min()           {return 0;}
178    static unsigned short       max()           {return USHRT_MAX;}
179    static unsigned short       smallest()      {return 1;}
180    static unsigned short       epsilon()       {return 1;}
181    static bool                 isIntegral()    {return true;}
182    static bool                 isSigned()      {return false;}
183};
184
185template <>
186struct limits <int>
187{
188    static int                  min()           {return INT_MIN;}
189    static int                  max()           {return INT_MAX;}
190    static int                  smallest()      {return 1;}
191    static int                  epsilon()       {return 1;}
192    static bool                 isIntegral()    {return true;}
193    static bool                 isSigned()      {return true;}
194};
195
196template <>
197struct limits <unsigned int>
198{
199    static unsigned int         min()           {return 0;}
200    static unsigned int         max()           {return UINT_MAX;}
201    static unsigned int         smallest()      {return 1;}
202    static unsigned int         epsilon()       {return 1;}
203    static bool                 isIntegral()    {return true;}
204    static bool                 isSigned()      {return false;}
205};
206
207template <>
208struct limits <long>
209{
210    static long                 min()           {return LONG_MIN;}
211    static long                 max()           {return LONG_MAX;}
212    static long                 smallest()      {return 1;}
213    static long                 epsilon()       {return 1;}
214    static bool                 isIntegral()    {return true;}
215    static bool                 isSigned()      {return true;}
216};
217
218template <>
219struct limits <unsigned long>
220{
221    static unsigned long        min()           {return 0;}
222    static unsigned long        max()           {return ULONG_MAX;}
223    static unsigned long        smallest()      {return 1;}
224    static unsigned long        epsilon()       {return 1;}
225    static bool                 isIntegral()    {return true;}
226    static bool                 isSigned()      {return false;}
227};
228
229template <>
230struct limits <float>
231{
232    static float                min()           {return -FLT_MAX;}
233    static float                max()           {return FLT_MAX;}
234    static float                smallest()      {return FLT_MIN;}
235    static float                epsilon()       {return FLT_EPSILON;}
236    static bool                 isIntegral()    {return false;}
237    static bool                 isSigned()      {return true;}
238};
239
240template <>
241struct limits <double>
242{
243    static double               min()           {return -DBL_MAX;}
244    static double               max()           {return DBL_MAX;}
245    static double               smallest()      {return DBL_MIN;}
246    static double               epsilon()       {return DBL_EPSILON;}
247    static bool                 isIntegral()    {return false;}
248    static bool                 isSigned()      {return true;}
249};
250
251template <>
252struct limits <long double>
253{
254    static long double          min()           {return -LDBL_MAX;}
255    static long double          max()           {return LDBL_MAX;}
256    static long double          smallest()      {return LDBL_MIN;}
257    static long double          epsilon()       {return LDBL_EPSILON;}
258    static bool                 isIntegral()    {return false;}
259    static bool                 isSigned()      {return true;}
260};
261
262
263} // namespace Imath
264
265#endif
Note: See TracBrowser for help on using the repository browser.