Stefan Scholz / ETL
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers integral_limits.h Source File

integral_limits.h

Go to the documentation of this file.
00001 ///\file
00002 
00003 /******************************************************************************
00004 The MIT License(MIT)
00005 
00006 Embedded Template Library.
00007 https://github.com/ETLCPP/etl
00008 http://www.etlcpp.com
00009 
00010 Copyright(c) 2014 jwellbelove
00011 
00012 Permission is hereby granted, free of charge, to any person obtaining a copy
00013 of this software and associated documentation files(the "Software"), to deal
00014 in the Software without restriction, including without limitation the rights
00015 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
00016 copies of the Software, and to permit persons to whom the Software is
00017 furnished to do so, subject to the following conditions :
00018 
00019 The above copyright notice and this permission notice shall be included in all
00020 copies or substantial portions of the Software.
00021 
00022 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00023 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00024 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
00025 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00026 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00027 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00028 SOFTWARE.
00029 ******************************************************************************/
00030 
00031 #ifndef __ETL_INTEGRAL_LIMITS__
00032 #define __ETL_INTEGRAL_LIMITS__
00033 
00034 #include <limits.h>
00035 #include <stddef.h>
00036 
00037 #include "platform.h "
00038 #include "type_traits.h "
00039 
00040 #ifdef ETL_COMPILER_MICROSOFT
00041 #undef min
00042 #undef max
00043 #endif
00044 
00045 //*****************************************************************************
00046 ///\defgroup integral_limits integral_limits
00047 /// A set of templated compile time constants that mirror some of std::numeric_limits funtionality.
00048 ///\ingroup utilities
00049 //*****************************************************************************
00050 
00051 namespace etl
00052 {
00053   //***************************************************************************
00054   ///\ingroup integral_limits
00055   //***************************************************************************
00056   template <typename T>
00057   struct integral_limits;
00058 
00059   //***************************************************************************
00060   ///\ingroup integral_limits
00061   //***************************************************************************
00062   template <>
00063   struct integral_limits<void>
00064   {
00065     static const int min        = 0;
00066     static const int max        = 0;
00067     static const int bits       = 0;
00068     static const bool is_signed = false;
00069   };
00070 
00071   //***************************************************************************
00072   ///\ingroup integral_limits
00073   //***************************************************************************
00074   template <>
00075   struct integral_limits<signed char>
00076   {
00077     static const signed char min = SCHAR_MIN;
00078     static const signed char max = SCHAR_MAX;
00079     static const int bits        = CHAR_BIT;
00080     static const bool is_signed  = etl::is_signed<signed char>::value;
00081   };
00082 
00083   //***************************************************************************
00084   ///\ingroup integral_limits
00085   //***************************************************************************
00086   template <>
00087   struct integral_limits<unsigned char>
00088   {
00089     static const unsigned char min = 0;
00090     static const unsigned char max = UCHAR_MAX;
00091     static const int bits          = CHAR_BIT;
00092     static const bool is_signed    = etl::is_signed<unsigned char>::value;
00093   };
00094 
00095   //***************************************************************************
00096   ///\ingroup integral_limits
00097   //***************************************************************************
00098   template <>
00099   struct integral_limits<char>
00100   {
00101     static const char min       = (etl::is_signed<char>::value) ? SCHAR_MIN : 0;
00102     static const char max       = (etl::is_signed<char>::value) ? SCHAR_MAX : char(UCHAR_MAX);
00103     static const int bits       = CHAR_BIT;
00104     static const bool is_signed = etl::is_signed<char>::value;
00105   };
00106 
00107   //***************************************************************************
00108   ///\ingroup integral_limits
00109   //***************************************************************************
00110   template <>
00111   struct integral_limits<short>
00112   {
00113     static const short min      = SHRT_MIN;
00114     static const short max      = SHRT_MAX;
00115     static const int bits       = CHAR_BIT * (sizeof(short) / sizeof(char));
00116     static const bool is_signed = etl::is_signed<short>::value;
00117   };
00118 
00119   //***************************************************************************
00120   ///\ingroup integral_limits
00121   //***************************************************************************
00122   template <>
00123   struct integral_limits<unsigned short>
00124   {
00125     static const unsigned short min = 0;
00126     static const unsigned short max = USHRT_MAX;
00127     static const int bits           = CHAR_BIT * (sizeof(unsigned short) / sizeof(char));
00128     static const bool is_signed     = etl::is_signed<unsigned short>::value;
00129   };
00130 
00131   //***************************************************************************
00132   ///\ingroup integral_limits
00133   //***************************************************************************
00134   template <>
00135   struct integral_limits<int>
00136   {
00137     static const int min        = INT_MIN;
00138     static const int max        = INT_MAX;
00139     static const int bits       = CHAR_BIT * (sizeof(int) / sizeof(char));
00140     static const bool is_signed = etl::is_signed<int>::value;
00141   };
00142 
00143   //***************************************************************************
00144   ///\ingroup integral_limits
00145   //***************************************************************************
00146   template <>
00147   struct integral_limits<unsigned int>
00148   {
00149     static const unsigned int min = 0;
00150     static const unsigned int max = UINT_MAX;
00151     static const int bits         = CHAR_BIT * (sizeof(unsigned int) / sizeof(char));
00152     static const bool is_signed   = etl::is_signed<unsigned int>::value;
00153   };
00154 
00155   //***************************************************************************
00156   ///\ingroup integral_limits
00157   //***************************************************************************
00158   template <>
00159   struct integral_limits<long>
00160   {
00161     static const long min       = LONG_MIN;
00162     static const long max       = LONG_MAX;
00163     static const int bits       = CHAR_BIT * (sizeof(long) / sizeof(char));
00164     static const bool is_signed = etl::is_signed<long>::value;
00165   };
00166 
00167   //***************************************************************************
00168   ///\ingroup integral_limits
00169   //***************************************************************************
00170   template <>
00171   struct integral_limits<unsigned long>
00172   {
00173     static const unsigned long min = 0;
00174     static const unsigned long max = ULONG_MAX;
00175     static const int bits          = CHAR_BIT * (sizeof(unsigned long) / sizeof(char));
00176     static const bool is_signed    = etl::is_signed<unsigned long>::value;
00177   };
00178 
00179 #ifndef LLONG_MAX
00180 #define LLONG_MAX   9223372036854775807LL
00181 #endif
00182 
00183 #ifndef LLONG_MIN
00184 #define LLONG_MIN   (-LLONG_MAX - 1LL)
00185 #endif
00186 
00187 #ifndef ULLONG_MAX
00188 #define ULLONG_MAX 18446744073709551615ULL
00189 #endif
00190 
00191   //***************************************************************************
00192   ///\ingroup integral_limits
00193   //***************************************************************************
00194   template <>
00195   struct integral_limits<long long>
00196   {
00197     static const long long min  = LLONG_MIN;
00198     static const long long max  = LLONG_MAX;
00199     static const int bits       = CHAR_BIT * (sizeof(long long) / sizeof(char));
00200     static const bool is_signed = etl::is_signed<long long>::value;
00201   };
00202 
00203   //***************************************************************************
00204   ///\ingroup integral_limits
00205   //***************************************************************************
00206   template <>
00207   struct integral_limits<unsigned long long>
00208   {
00209     static const unsigned long long min = 0;
00210     static const unsigned long long max = ULLONG_MAX;
00211     static const int bits               = CHAR_BIT * (sizeof(unsigned long long) / sizeof(char));
00212     static const bool is_signed         = etl::is_signed<unsigned long long>::value;
00213   };
00214 }
00215 
00216 #ifdef ETL_COMPILER_MICROSOFT
00217 #define min(a,b) (((a) < (b)) ? (a) : (b))
00218 #define max(a,b) (((a) > (b)) ? (a) : (b))
00219 #endif
00220 
00221 #endif
00222