summaryrefslogtreecommitdiff
path: root/ACE/ace/Truncate.h
diff options
context:
space:
mode:
Diffstat (limited to 'ACE/ace/Truncate.h')
-rw-r--r--ACE/ace/Truncate.h81
1 files changed, 76 insertions, 5 deletions
diff --git a/ACE/ace/Truncate.h b/ACE/ace/Truncate.h
index efae24177d6..28bc27dabf6 100644
--- a/ACE/ace/Truncate.h
+++ b/ACE/ace/Truncate.h
@@ -27,9 +27,10 @@
#include "ace/If_Then_Else.h"
#include "ace/Numeric_Limits.h"
-#if defined (__BORLANDC__) && __BORLANDC__ <= 0x590
+#if defined (ACE_LACKS_LONGLONG_T) \
+ || defined (__BORLANDC__) && __BORLANDC__ <= 0x590
# include "ace/Basic_Types.h"
-#endif /* __BORLANDC__ <= 0x590 */
+#endif /* ACE_LACKS_LONGLONG_T || __BORLANDC__ <= 0x590 */
ACE_BEGIN_VERSIONED_NAMESPACE_DECL
@@ -52,6 +53,8 @@ namespace ACE_Utils
__extension__
# endif /* __GNUC__ */
template<> struct Sign_Check<unsigned long long> { ACE_STATIC_CONSTANT (bool, is_signed = 0); };
+#else
+ template<> struct Sign_Check<ACE_U_LongLong> { ACE_STATIC_CONSTANT (bool, is_signed = 0); };
#endif /* !ACE_LACKS_LONGLONG_T */
// Specialize the signed cases.
@@ -124,6 +127,14 @@ namespace ACE_Utils
unsigned_type operator() (unsigned_type x) { return x; }
};
+#else
+ template<>
+ struct To_Unsigned<ACE_U_LongLong>
+ {
+ typedef ACE_U_LongLong unsigned_type;
+
+ unsigned_type operator() (unsigned_type x) { return x; }
+ };
#endif /* !ACE_LACKS_LONGLONG_T */
// ----------------
@@ -431,6 +442,29 @@ namespace ACE_Utils
}
};
+
+#if defined (ACE_LACKS_LONGLONG_T) || defined (ACE_LACKS_UNSIGNEDLONGLONG_T)
+ // Partial specialization for the case where we're casting from
+ // ACE_U_LongLong to a smaller integer. We assume that we're always
+ // truncating from ACE_U_LongLong to a smaller type. The partial
+ // specialization above handles the case where both the FROM and TO
+ // types are ACE_U_LongLong.
+ template<typename TO>
+ struct Truncator<ACE_U_LongLong, TO>
+ {
+ TO operator() (ACE_U_LongLong const & val)
+ {
+ // If val less than or equal to ACE_Numeric_Limits<TO>::max(),
+ // val.lo() must be less than or equal to
+ // ACE_Numeric_Limits<TO>::max (), as well.
+ return
+ (val > ACE_Numeric_Limits<TO>::max ()
+ ? ACE_Numeric_Limits<TO>::max ()
+ : static_cast<TO> (val.lo ()));
+ }
+ };
+#endif /* ACE_LACKS_LONGLONG_T || ACE_LACKS_UNSIGNEDLONGLONG_T */
+
// -----------------------------------------------------
/**
* @struct Noop_Truncator
@@ -452,7 +486,7 @@ namespace ACE_Utils
// -----------------------------------------------------
/**
- * @class Truncate
+ * @class truncate_cast
*
* @brief Helper function to truncate an integral value to the
* maximum value of the given type.
@@ -465,7 +499,7 @@ namespace ACE_Utils
* @internal Internal use only.
*/
template<typename TO, typename FROM>
- inline TO Truncate (FROM val)
+ inline TO truncate_cast (FROM val)
{
// If the size of FROM is less than the size of TO, "val" will
// never be greater than the maximum "TO" value, so there is no
@@ -592,6 +626,18 @@ namespace ACE_Utils
}
};
+ template<>
+ struct Truncator<const ACE_UINT64, signed long>
+ {
+ signed long operator() (const ACE_UINT64 val)
+ {
+ return
+ (val > static_cast<ACE_UINT64> (ACE_Numeric_Limits<signed long>::max ())
+ ? ACE_Numeric_Limits<signed long>::max ()
+ : static_cast<signed long> (val));
+ }
+ };
+
#endif /* ACE_SIZEOF_LONG < 8 */
#if defined (ACE_SIZEOF_INT) && ACE_SIZEOF_INT < 8
@@ -725,6 +771,16 @@ namespace ACE_Utils
};
template<>
+ struct Truncator<const signed long, unsigned int>
+ {
+ unsigned int operator() (const signed long val)
+ {
+ return static_cast<unsigned int> (val);
+ }
+ };
+
+
+ template<>
struct Truncator<unsigned int, signed long>
{
signed long operator() (unsigned int val)
@@ -886,9 +942,24 @@ namespace ACE_Utils
}
};
+ // Partial specialization for the case where the types are the same,
+ // but the from type is const. No truncation is necessary.
+ //
+ // This is only necessary to workaround a problem with the BCB6
+ // compiler.
+ template<typename T>
+ struct Truncator<T const, T>
+ {
+ T operator() (T val)
+ {
+ return val;
+ }
+ };
+
// -------------------------------------
+
template<typename TO, typename FROM>
- inline TO Truncate (FROM val)
+ inline TO truncate_cast (FROM val)
{
typedef Truncator<FROM, TO> truncator;