Truncate.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //=============================================================================
00003 /**
00004  * @file Truncate.h
00005  *
00006  * Truncate.h,v 4.3 2006/02/24 17:33:32 shuston Exp
00007  *
00008  * @author Steve Huston <shuston@riverace.com>
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 /* ACE_LACKS_PRAGMA_ONCE */
00020 
00021 #include "ace/Global_Macros.h"
00022 
00023 #if !defined(ACE_LACKS_NUMERIC_LIMITS)
00024 // some platforms pollute the namespace by defining max() and min() macros
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 /* ACE_LACKS_NUMERIC_LIMITS */
00035 
00036 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00037 
00038 namespace ACE_Utils
00039 {
00040 /**
00041  * @class Truncate
00042  *
00043  * @brief Helper function to truncate an integral value to an int.
00044  *
00045  *        Very useful since ACE methods return int very often and the value's
00046  *        source is often a different-size integral type, such as size_t.
00047  *        This function hides the truncation logic and resolves compiler
00048  *        diagnostics.
00049  *
00050  * @internal Internal use only.
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 /* ACE_LACKS_NUMERIC_LIMITS */
00062   return static_cast<int> (val);
00063 }
00064 
00065 // Specialize one for size_t to alleviate the explicit instantiation pain.
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 /* ACE_LACKS_NUMERIC_LIMITS */
00076   return static_cast<int> (val);
00077 }
00078 
00079 } // namespace ACE_Utils
00080 
00081 ACE_END_VERSIONED_NAMESPACE_DECL
00082 
00083 #include /**/ "ace/post.h"
00084 #endif /* ACE_TRUNCATE_H*/

Generated on Thu Nov 9 09:42:08 2006 for ACE by doxygen 1.3.6