From c44379cc7d9c7aa113989237ab0f56db12aa5219 Mon Sep 17 00:00:00 2001 From: "William R. Otte" Date: Mon, 24 Jul 2006 15:50:30 +0000 Subject: Repo restructuring --- ACE/ace/Truncate.h | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 ACE/ace/Truncate.h (limited to 'ACE/ace/Truncate.h') diff --git a/ACE/ace/Truncate.h b/ACE/ace/Truncate.h new file mode 100644 index 00000000000..de347d1dc06 --- /dev/null +++ b/ACE/ace/Truncate.h @@ -0,0 +1,84 @@ +// -*- C++ -*- +//============================================================================= +/** + * @file Truncate.h + * + * $Id$ + * + * @author Steve Huston + */ +//============================================================================= +#ifndef ACE_TRUNCATE_H +#define ACE_TRUNCATE_H +#include /**/ "ace/pre.h" + +#include "ace/config-all.h" + +#if !defined (ACE_LACKS_PRAGMA_ONCE) +# pragma once +#endif /* ACE_LACKS_PRAGMA_ONCE */ + +#include "ace/Global_Macros.h" + +#if !defined(ACE_LACKS_NUMERIC_LIMITS) +// some platforms pollute the namespace by defining max() and min() macros +# ifdef max +# undef max +# endif +# ifdef min +# undef min +# endif +# include +#else +# include "ace/os_include/os_limits.h" +#endif /* ACE_LACKS_NUMERIC_LIMITS */ + +ACE_BEGIN_VERSIONED_NAMESPACE_DECL + +namespace ACE_Utils +{ +/** + * @class Truncate + * + * @brief Helper function to truncate an integral value to an int. + * + * Very useful since ACE methods return int very often and the value's + * source is often a different-size integral type, such as size_t. + * This function hides the truncation logic and resolves compiler + * diagnostics. + * + * @internal Internal use only. + */ +template +inline int Truncate (const X& val) +{ +#if !defined (ACE_LACKS_NUMERIC_LIMITS) + if (val > static_cast (std::numeric_limits::max ())) + return std::numeric_limits::max (); +#else + if (val > static_cast (INT_MAX)) + return INT_MAX; +#endif /* ACE_LACKS_NUMERIC_LIMITS */ + return static_cast (val); +} + +// Specialize one for size_t to alleviate the explicit instantiation pain. +template<> +inline int Truncate (const size_t& val) +{ +#if !defined (ACE_LACKS_NUMERIC_LIMITS) + if (val > static_cast (std::numeric_limits::max ())) + return std::numeric_limits::max (); +#else + if (val > static_cast (INT_MAX)) + return INT_MAX; +#endif /* ACE_LACKS_NUMERIC_LIMITS */ + return static_cast (val); +} + +} // namespace ACE_Utils + +ACE_END_VERSIONED_NAMESPACE_DECL + +#include /**/ "ace/post.h" +#endif /* ACE_TRUNCATE_H*/ -- cgit v1.2.1