00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef ACE_TRUNCATE_H
00012 #define ACE_TRUNCATE_H
00013 #include "ace/pre.h"
00014
00015 #include "ace/config-all.h"
00016
00017 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00018 # pragma once
00019 #endif
00020
00021 #include "ace/Global_Macros.h"
00022
00023 #if !defined(ACE_LACKS_NUMERIC_LIMITS)
00024
00025 # ifdef max
00026 # undef max
00027 # endif
00028 # ifdef min
00029 # undef min
00030 # endif
00031 # include <limits>
00032 #else
00033 # include "ace/os_include/os_limits.h"
00034 #endif
00035
00036 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00037
00038 namespace ACE_Utils
00039 {
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052 template<typename X>
00053 inline int Truncate (const X& val)
00054 {
00055 #if !defined (ACE_LACKS_NUMERIC_LIMITS)
00056 if (val > static_cast<X> (std::numeric_limits<int>::max ()))
00057 return std::numeric_limits<int>::max ();
00058 #else
00059 if (val > static_cast<X> (INT_MAX))
00060 return INT_MAX;
00061 #endif
00062 return static_cast<int> (val);
00063 }
00064
00065
00066 template<>
00067 inline int Truncate<size_t> (const size_t& val)
00068 {
00069 #if !defined (ACE_LACKS_NUMERIC_LIMITS)
00070 if (val > static_cast<size_t> (std::numeric_limits<int>::max ()))
00071 return std::numeric_limits<int>::max ();
00072 #else
00073 if (val > static_cast<size_t> (INT_MAX))
00074 return INT_MAX;
00075 #endif
00076 return static_cast<int> (val);
00077 }
00078
00079 }
00080
00081 ACE_END_VERSIONED_NAMESPACE_DECL
00082
00083 #include "ace/post.h"
00084 #endif