[7297] Fixed profession spells sorting in trainer spell list at client.
[getmangos.git] / dep / ACE_wrappers / ace / Truncate.h
blob5b9a0e1187af1d425b5d1c137021771cad2c6825
1 // -*- C++ -*-
3 //=============================================================================
4 /**
5 * @file Truncate.h
7 * $Id: Truncate.h 82623 2008-08-13 14:41:09Z johnnyw $
9 * @author Steve Huston <shuston@riverace.com>
10 * @author Ossama Othman <ossama_othman@symantec.com>
11 * @author Russell Mora <russell_mora@symantec.com>
13 //=============================================================================
15 #ifndef ACE_TRUNCATE_H
16 #define ACE_TRUNCATE_H
18 #include /**/ "ace/pre.h"
20 #include "ace/config-all.h"
22 #if !defined (ACE_LACKS_PRAGMA_ONCE)
23 # pragma once
24 #endif /* ACE_LACKS_PRAGMA_ONCE */
26 #include "ace/Global_Macros.h"
27 #include "ace/If_Then_Else.h"
28 #include "ace/Numeric_Limits.h"
30 #if defined (ACE_LACKS_LONGLONG_T) \
31 || defined (__BORLANDC__) && __BORLANDC__ < 0x590
32 # include "ace/Basic_Types.h"
33 #endif /* ACE_LACKS_LONGLONG_T || __BORLANDC__ < 0x590 */
35 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
37 namespace ACE_Utils
40 #if !defined (__BORLANDC__) || __BORLANDC__ >= 0x590
42 template<typename T> struct Sign_Check;
44 // Specialize the unsigned signed cases.
45 template<> struct Sign_Check<unsigned char> { ACE_STATIC_CONSTANT (bool, is_signed = 0); };
46 template<> struct Sign_Check<unsigned short> { ACE_STATIC_CONSTANT (bool, is_signed = 0); };
47 template<> struct Sign_Check<unsigned int> { ACE_STATIC_CONSTANT (bool, is_signed = 0); };
48 template<> struct Sign_Check<unsigned long> { ACE_STATIC_CONSTANT (bool, is_signed = 0); };
49 #if !(defined(ACE_LACKS_LONGLONG_T) || defined(ACE_LACKS_UNSIGNEDLONGLONG_T))
50 # ifdef __GNUC__
51 // Silence g++ "-pedantic" warnings regarding use of "long long"
52 // type.
53 __extension__
54 # endif /* __GNUC__ */
55 template<> struct Sign_Check<unsigned long long> { ACE_STATIC_CONSTANT (bool, is_signed = 0); };
56 #else
57 template<> struct Sign_Check<ACE_U_LongLong> { ACE_STATIC_CONSTANT (bool, is_signed = 0); };
58 #endif /* !ACE_LACKS_LONGLONG_T */
60 // Specialize the signed cases.
61 template<> struct Sign_Check<signed char> { ACE_STATIC_CONSTANT (bool, is_signed = 1); };
62 template<> struct Sign_Check<signed short> { ACE_STATIC_CONSTANT (bool, is_signed = 1); };
63 template<> struct Sign_Check<signed int> { ACE_STATIC_CONSTANT (bool, is_signed = 1); };
64 template<> struct Sign_Check<signed long> { ACE_STATIC_CONSTANT (bool, is_signed = 1); };
65 #ifndef ACE_LACKS_LONGLONG_T
66 # ifdef __GNUC__
67 // Silence g++ "-pedantic" warnings regarding use of "long long"
68 // type.
69 __extension__
70 # endif /* __GNUC__ */
71 template<> struct Sign_Check<signed long long> { ACE_STATIC_CONSTANT (bool, is_signed = 1); };
72 #endif /* !ACE_LACKS_LONGLONG_T */
74 // -----------------------------------------------------
76 /**
77 * @struct To_Unsigned
79 * @brief Retrieve unsigned counterpart to given type or value.
81 * Retrieve unsigned counterpart to given type or value.
83 template<typename T> struct To_Unsigned;
85 template<>
86 struct To_Unsigned<unsigned char>
88 typedef unsigned char unsigned_type;
90 unsigned_type operator() (unsigned_type x) { return x; }
93 template<>
94 struct To_Unsigned<unsigned short>
96 typedef unsigned short unsigned_type;
98 unsigned_type operator() (unsigned_type x) { return x; }
101 template<>
102 struct To_Unsigned<unsigned int>
104 typedef unsigned int unsigned_type;
106 unsigned_type operator() (unsigned_type x) { return x; }
109 template<>
110 struct To_Unsigned<unsigned long>
112 typedef unsigned long unsigned_type;
114 unsigned_type operator() (unsigned_type x) { return x; }
117 #if !(defined(ACE_LACKS_LONGLONG_T) || defined(ACE_LACKS_UNSIGNEDLONGLONG_T))
118 # ifdef __GNUC__
119 // Silence g++ "-pedantic" warnings regarding use of "long long"
120 // type.
121 __extension__
122 # endif /* __GNUC__ */
123 template<>
124 struct To_Unsigned<unsigned long long>
126 typedef unsigned long long unsigned_type;
128 unsigned_type operator() (unsigned_type x) { return x; }
130 #else
131 template<>
132 struct To_Unsigned<ACE_U_LongLong>
134 typedef ACE_U_LongLong unsigned_type;
136 unsigned_type operator() (unsigned_type x) { return x; }
138 #endif /* !ACE_LACKS_LONGLONG_T */
140 // ----------------
142 template<>
143 struct To_Unsigned<signed char>
145 typedef signed char signed_type;
146 typedef unsigned char unsigned_type;
148 unsigned_type operator() (signed_type x)
150 return static_cast<unsigned_type> (x);
154 template<>
155 struct To_Unsigned<signed short>
157 typedef signed short signed_type;
158 typedef unsigned short unsigned_type;
160 unsigned_type operator() (signed_type x)
162 return static_cast<unsigned_type> (x);
166 template<>
167 struct To_Unsigned<signed int>
169 typedef signed int signed_type;
170 typedef unsigned int unsigned_type;
172 unsigned_type operator() (signed_type x)
174 return static_cast<unsigned_type> (x);
178 template<>
179 struct To_Unsigned<signed long>
181 typedef signed long signed_type;
182 typedef unsigned long unsigned_type;
184 unsigned_type operator() (signed_type x)
186 return static_cast<unsigned_type> (x);
190 #if !(defined(ACE_LACKS_LONGLONG_T) || defined(ACE_LACKS_UNSIGNEDLONGLONG_T))
191 # ifdef __GNUC__
192 // Silence g++ "-pedantic" warnings regarding use of "long long"
193 // type.
194 __extension__
195 # endif /* __GNUC__ */
196 template<>
197 struct To_Unsigned<signed long long>
199 typedef signed long long signed_type;
200 typedef unsigned long long unsigned_type;
202 unsigned_type operator() (signed_type x)
204 return static_cast<unsigned_type> (x);
207 #endif /* !ACE_LACKS_LONGLONG_T */
209 // -----------------------------------------------------
212 * @struct Safe_Comparator
214 * @brief Conservative comparison of types that may not be safely
215 * promoted and/or converted to each other.
217 * The comparison operations provided by this structure perform
218 * negative value checking when necessary to prevent wrap-around
219 * when explicitly casting to an unsigned type.
221 * @internal This structure is not meant for general use.
223 template<typename LEFT,
224 typename RIGHT,
225 bool IS_LEFT_SIGNED,
226 bool IS_RIGHT_SIGNED> struct Safe_Comparator;
228 // LEFT: signed, RIGHT: unsigned
229 template<typename LEFT, typename RIGHT>
230 struct Safe_Comparator<LEFT, RIGHT, true, false>
232 static bool greater_than (LEFT lhs, RIGHT rhs)
234 // Prevent wrap-around when casting to unsigned.
235 if (lhs < 0)
236 return false; // since rhs is always positive
237 else
239 // Implicit promotion of unsigned LEFT and RIGHT types here.
240 return To_Unsigned<LEFT>() (lhs) > rhs;
245 // LEFT: unsigned, RIGHT: signed
246 template<typename LEFT, typename RIGHT>
247 struct Safe_Comparator<LEFT, RIGHT, false, true>
249 static bool greater_than (LEFT lhs, RIGHT rhs)
251 // Prevent wrap-around when casting to unsigned.
252 if (rhs < 0)
253 return true; // since lhs is always positive
254 else
256 // Implicit promotion of unsigned LEFT and RIGHT types here.
257 return lhs > To_Unsigned<RIGHT>() (rhs);
262 // LEFT: unsigned, RIGHT: unsigned
263 template<typename LEFT, typename RIGHT>
264 struct Safe_Comparator<LEFT, RIGHT, false, false>
266 static bool greater_than (LEFT lhs, RIGHT rhs)
268 // Implicit promotion of unsigned LEFT and RIGHT types here.
269 return lhs > rhs;
273 // LEFT: signed, RIGHT: signed
274 template<typename LEFT, typename RIGHT>
275 struct Safe_Comparator<LEFT, RIGHT, true, true>
277 static bool greater_than (LEFT lhs, RIGHT rhs)
279 // Implicit promotion of signed LEFT and RIGHT types here.
280 return lhs > rhs;
284 // -----------------------------------------------------
287 * @struct Fast_Comparator
289 * @brief Quick comparison of types that can be safely promoted
290 * and/or converted to each other.
292 * The comparison operations provided by this structure perform no
293 * negative value checking, meaning it is not applicable to all
294 * types. Check the value of the @c USABLE enumerator to determine
295 * if it applies to the types in question.
297 * @internal This structure is not meant for general use.
299 template<typename LEFT, typename RIGHT>
300 struct Fast_Comparator
302 ACE_STATIC_CONSTANT (
303 bool,
304 USE_LEFT = ((sizeof (LEFT) > sizeof (RIGHT)
305 && (Sign_Check<LEFT>::is_signed == 1
306 || Sign_Check<RIGHT>::is_signed == 0))
308 // The following is basically the case where LEFT
309 // and RIGHT are the same integral type.
310 || (sizeof (LEFT) == sizeof (RIGHT)
311 // Can't portably do
312 // Sign_Check<LEFT>::is_signed ==
313 // Sign_Check<RIGHT>::is_signed,
314 // i.e. comparison of anonymous enumerations,
315 // without triggering a compiler diagnostic
316 // so expand the comparison.
317 && ((Sign_Check<LEFT>::is_signed == 1
318 && Sign_Check<RIGHT>::is_signed == 1)
319 || (Sign_Check<LEFT>::is_signed == 0
320 && Sign_Check<RIGHT>::is_signed == 0)))));
322 ACE_STATIC_CONSTANT (
323 bool,
324 USE_RIGHT = (sizeof (RIGHT) > sizeof (LEFT)
325 && (Sign_Check<RIGHT>::is_signed == 1
326 || Sign_Check<LEFT>::is_signed == 0)));
328 ACE_STATIC_CONSTANT (bool, USABLE = (USE_LEFT || USE_RIGHT));
330 typedef typename ACE::If_Then_Else<
331 USE_LEFT,
332 LEFT,
333 typename ACE::If_Then_Else<
334 USE_RIGHT,
335 RIGHT,
336 void>::result_type>::result_type promote_type;
338 static bool greater_than (LEFT lhs, RIGHT rhs)
340 // The explicit cast is assumed to change the type of rhs without
341 // changing its value.
342 return
343 (static_cast<promote_type> (lhs) > static_cast<promote_type> (rhs));
348 // -----------------------------------------------------
351 * @struct Comparator
353 * @brief Structure that provides optimal comparison operation for
354 * given types.
356 * The comparison operations provided by this structure are chosen
357 * at compile time based on the signs and sizes of types being
358 * compared.
359 * @par
360 * Comparisons of values with the same sign or those with types that
361 * can be promoted safely are done quickly, without any range
362 * checking.
363 * @par
364 * Comparisons of values of different types that cannot be safely
365 * promoted incur an additional check for a negative value to allow
366 * the compiler to perform the appropriate implicit unsigned type
367 * promotion.
369 * @note In general, the operations found in this structure should
370 * not be used to work around compiler diagnostics regarding
371 * comparison of signed and unsigned types. Verify that your
372 * types are correct before relying on those operations.
374 * @internal This structure is not meant for general use.
376 template<typename LEFT, typename RIGHT>
377 struct Comparator
379 typedef typename ACE::If_Then_Else<
380 Fast_Comparator<LEFT, RIGHT>::USABLE,
381 Fast_Comparator<LEFT, RIGHT>,
382 Safe_Comparator<LEFT,
383 RIGHT,
384 Sign_Check<LEFT>::is_signed,
385 Sign_Check<RIGHT>::is_signed> >::result_type comp_type;
388 // -----------------------------------------------------
391 * @struct Truncator
393 * @brief Truncate value of type @c FROM to value of type @c TO.
395 * Truncate a value of type @c FROM to value of type @c TO, if the
396 * value is larger than the maximum of value of type @c TO.
398 template<typename FROM, typename TO>
399 struct Truncator
401 ACE_STATIC_CONSTANT (
402 bool,
403 // max FROM always greater than max TO
404 MAX_FROM_GT_MAX_TO = (sizeof(FROM) > sizeof (TO)
405 || (sizeof(FROM) == sizeof (TO)
406 && Sign_Check<FROM>::is_signed == 0)));
408 typedef typename ACE::If_Then_Else<
409 MAX_FROM_GT_MAX_TO,
410 FROM,
411 TO>::result_type comp_to_type;
413 // Take advantage of knowledge that we're casting a positive value
414 // to a type large enough to hold it so that we can bypass
415 // negative value checks at compile-time. Otherwise fallback on
416 // the safer comparison.
417 typedef typename ACE::If_Then_Else<
418 MAX_FROM_GT_MAX_TO,
419 Fast_Comparator<FROM, comp_to_type>,
420 typename Comparator<FROM, comp_to_type>::comp_type>::result_type comparator;
422 /// Truncate a value of type @c FROM to value of type @c TO, if
423 /// the value is larger than the maximum of value of type @c TO.
424 TO operator() (FROM val)
426 return
427 (comparator::greater_than (val, ACE_Numeric_Limits<TO>::max ())
428 ? ACE_Numeric_Limits<TO>::max ()
429 : static_cast<TO> (val));
434 // Partial specialization for the case where the types are the same.
435 // No truncation is necessary.
436 template<typename T>
437 struct Truncator<T, T>
439 T operator() (T val)
441 return val;
446 #if defined (ACE_LACKS_LONGLONG_T) || defined (ACE_LACKS_UNSIGNEDLONGLONG_T)
447 // Partial specialization for the case where we're casting from
448 // ACE_U_LongLong to a smaller integer. We assume that we're always
449 // truncating from ACE_U_LongLong to a smaller type. The partial
450 // specialization above handles the case where both the FROM and TO
451 // types are ACE_U_LongLong.
452 template<typename TO>
453 struct Truncator<ACE_U_LongLong, TO>
455 TO operator() (ACE_U_LongLong const & val)
457 // If val less than or equal to ACE_Numeric_Limits<TO>::max(),
458 // val.lo() must be less than or equal to
459 // ACE_Numeric_Limits<TO>::max (), as well.
460 return
461 (val > ACE_Numeric_Limits<TO>::max ()
462 ? ACE_Numeric_Limits<TO>::max ()
463 : static_cast<TO> (val.lo ()));
466 #endif /* ACE_LACKS_LONGLONG_T || ACE_LACKS_UNSIGNEDLONGLONG_T */
468 // -----------------------------------------------------
470 * @struct Noop_Truncator
472 * @brief No-op truncation.
474 * This structure/functor performs no truncation since it assumes
475 * that @c sizeof(FROM) @c < @c sizeof(TO), meaning that
476 * @c numeric_limits<FROM>::max() @c < @c numeric_limits<TO>::max().
478 template<typename FROM, typename TO>
479 struct Noop_Truncator
481 TO operator() (FROM val)
483 return static_cast<TO> (val);
486 // -----------------------------------------------------
489 * @class truncate_cast
491 * @brief Helper function to truncate an integral value to the
492 * maximum value of the given type.
494 * Very useful since ACE methods return @c int very often and
495 * the value's source is often a different-size integral
496 * type, such as @c size_t. This function hides the
497 * truncation logic and resolves compiler diagnostics.
499 * @internal Internal use only.
501 template<typename TO, typename FROM>
502 inline TO truncate_cast (FROM val)
504 // If the size of FROM is less than the size of TO, "val" will
505 // never be greater than the maximum "TO" value, so there is no
506 // need to attempt to truncate.
507 typedef typename ACE::If_Then_Else<
508 (sizeof (FROM) < sizeof (TO)),
509 Noop_Truncator<FROM, TO>,
510 Truncator<FROM, TO> >::result_type truncator;
512 return truncator() (val);
515 #else
517 // Borland can't handle the template meta-programming above so
518 // provide explicit specializations for a few types. More will be
519 // added if necessary.
522 * @deprecated Borland ACE_Utils::Truncator<> specializations should
523 * be removed.
526 template<typename FROM, typename TO> struct Truncator;
528 //----------------------------------------------------------
529 // sizeof(FROM) > sizeof(TO)
530 //----------------------------------------------------------
532 template<>
533 struct Truncator<ACE_INT32, ACE_INT8>
535 ACE_INT8 operator() (ACE_INT32 val)
537 return
538 (val > ACE_Numeric_Limits<ACE_INT8>::max ()
539 ? ACE_Numeric_Limits<ACE_INT8>::max ()
540 : static_cast<ACE_INT8> (val));
544 template<>
545 struct Truncator<ACE_UINT32, ACE_UINT8>
547 ACE_UINT8 operator() (ACE_UINT32 val)
549 return
550 (val > static_cast<ACE_UINT32> (ACE_Numeric_Limits<ACE_UINT8>::max ())
551 ? ACE_Numeric_Limits<ACE_UINT8>::max ()
552 : static_cast<ACE_UINT8> (val));
556 template<>
557 struct Truncator<ACE_INT32, ACE_UINT8>
559 ACE_UINT8 operator() (ACE_INT32 val)
561 return
562 (val > static_cast<ACE_INT32> (ACE_Numeric_Limits<ACE_UINT8>::max ())
563 ? ACE_Numeric_Limits<ACE_UINT8>::max ()
564 : static_cast<ACE_UINT8> (val));
568 template<>
569 struct Truncator<ACE_UINT32, ACE_INT8>
571 ACE_INT8 operator() (ACE_UINT32 val)
573 return
574 (val > static_cast<ACE_UINT32> (ACE_Numeric_Limits<ACE_INT8>::max ())
575 ? ACE_Numeric_Limits<ACE_INT8>::max ()
576 : static_cast<ACE_INT8> (val));
580 #if defined (ACE_SIZEOF_LONG) && ACE_SIZEOF_LONG < 8
581 template<>
582 struct Truncator<ACE_INT64, signed long>
584 signed long operator() (ACE_INT64 val)
586 return
587 (val > ACE_Numeric_Limits<signed long>::max ()
588 ? ACE_Numeric_Limits<signed long>::max ()
589 : static_cast<signed long> (val));
593 template<>
594 struct Truncator<ACE_INT64, unsigned long>
596 unsigned long operator() (ACE_INT64 val)
598 return
599 (val > static_cast<ACE_INT64> (ACE_Numeric_Limits<unsigned long>::max ())
600 ? ACE_Numeric_Limits<unsigned long>::max ()
601 : static_cast<unsigned long> (val));
605 template<>
606 struct Truncator<ACE_UINT64, unsigned long>
608 unsigned long operator() (ACE_UINT64 val)
610 return
611 (val > static_cast<ACE_UINT64> (ACE_Numeric_Limits<unsigned long>::max ())
612 ? ACE_Numeric_Limits<unsigned long>::max ()
613 : static_cast<unsigned long> (val));
617 template<>
618 struct Truncator<ACE_UINT64, signed long>
620 signed long operator() (ACE_UINT64 val)
622 return
623 (val > static_cast<ACE_UINT64> (ACE_Numeric_Limits<signed long>::max ())
624 ? ACE_Numeric_Limits<signed long>::max ()
625 : static_cast<signed long> (val));
629 template<>
630 struct Truncator<const ACE_UINT64, signed long>
632 signed long operator() (const ACE_UINT64 val)
634 return
635 (val > static_cast<ACE_UINT64> (ACE_Numeric_Limits<signed long>::max ())
636 ? ACE_Numeric_Limits<signed long>::max ()
637 : static_cast<signed long> (val));
641 #endif /* ACE_SIZEOF_LONG < 8 */
643 #if defined (ACE_SIZEOF_INT) && ACE_SIZEOF_INT < 8
644 template<>
645 struct Truncator<ACE_INT64, signed int>
647 ACE_INT32 operator() (ACE_INT64 val)
649 return
650 (val > ACE_Numeric_Limits<signed int>::max ()
651 ? ACE_Numeric_Limits<signed int>::max ()
652 : static_cast<signed int> (val));
656 template<>
657 struct Truncator<ACE_INT64, unsigned int>
659 ACE_UINT32 operator() (ACE_INT64 val)
661 return
662 (val > static_cast<ACE_INT64> (ACE_Numeric_Limits<unsigned int>::max ())
663 ? ACE_Numeric_Limits<unsigned int>::max ()
664 : static_cast<unsigned int> (val));
668 template<>
669 struct Truncator<ACE_UINT64, unsigned int>
671 ACE_UINT32 operator() (ACE_UINT64 val)
673 return
674 (val > static_cast<ACE_UINT64> (ACE_Numeric_Limits<unsigned int>::max ())
675 ? ACE_Numeric_Limits<unsigned int>::max ()
676 : static_cast<unsigned int> (val));
680 template<>
681 struct Truncator<ACE_UINT64, signed int>
683 signed int operator() (ACE_UINT64 val)
685 return
686 (val > static_cast<ACE_UINT64> (ACE_Numeric_Limits<signed int>::max ())
687 ? ACE_Numeric_Limits<signed int>::max ()
688 : static_cast<signed int> (val));
692 #endif /* ACE_SIZEOF_INT < 8 */
694 //----------------------------------------------------------
695 // sizeof(FROM) == sizeof(TO)
696 //----------------------------------------------------------
698 template<>
699 struct Truncator<unsigned int, char>
701 char operator() (unsigned int val)
703 return static_cast<char> (val);
707 template<>
708 struct Truncator<unsigned int, wchar_t>
710 wchar_t operator() (unsigned int val)
712 return static_cast<wchar_t> (val);
716 template<>
717 struct Truncator<signed int, unsigned int>
719 unsigned int operator() (signed int val)
721 return static_cast<unsigned int> (val);
725 template<>
726 struct Truncator<unsigned int, signed int>
728 signed int operator() (unsigned int val)
730 return
731 (val > static_cast<unsigned int> (ACE_Numeric_Limits<signed int>::max ())
732 ? ACE_Numeric_Limits<signed int>::max ()
733 : static_cast<signed int> (val));
737 template<>
738 struct Truncator<const unsigned int, signed int>
740 signed int operator() (const unsigned int val)
742 return
743 (val > static_cast<unsigned int> (ACE_Numeric_Limits<signed int>::max ())
744 ? ACE_Numeric_Limits<signed int>::max ()
745 : static_cast<signed int> (val));
749 template<>
750 struct Truncator<signed long, unsigned long>
752 unsigned long operator() (signed long val)
754 return static_cast<unsigned long> (val);
758 template<>
759 struct Truncator<unsigned long, signed long>
761 signed long operator() (unsigned long val)
763 return
764 (val > static_cast<unsigned long> (ACE_Numeric_Limits<signed long>::max ())
765 ? ACE_Numeric_Limits<signed long>::max ()
766 : static_cast<signed long> (val));
770 #if defined (ACE_SIZEOF_INT) && defined (ACE_SIZEOF_LONG) \
771 && ACE_SIZEOF_INT == ACE_SIZEOF_LONG
773 template<>
774 struct Truncator<signed int, unsigned long>
776 unsigned long operator() (signed int val)
778 return static_cast<unsigned long> (val);
782 template<>
783 struct Truncator<signed int, signed long>
785 signed long operator() (signed int val)
787 return static_cast<signed long> (val);
791 template<>
792 struct Truncator<unsigned long, signed int>
794 signed int operator() (unsigned long val)
796 return
797 (val > static_cast<unsigned long> (ACE_Numeric_Limits<signed int>::max ())
798 ? ACE_Numeric_Limits<signed int>::max ()
799 : static_cast<signed int> (val));
803 template<>
804 struct Truncator<signed long, signed int>
806 signed int operator() (signed long val)
808 return static_cast<signed int> (val);
809 // This code causes asserts and compiler crashes with BCB6 Static and
810 // BCB2007 Static
811 // return
812 // (val > static_cast<signed long> (ACE_Numeric_Limits<signed int>::max ())
813 // ? ACE_Numeric_Limits<signed int>::max ()
814 // : static_cast<signed int> (val));
818 template<>
819 struct Truncator<signed long, unsigned int>
821 unsigned int operator() (signed long val)
823 return static_cast<unsigned int> (val);
827 template<>
828 struct Truncator<const signed long, unsigned int>
830 unsigned int operator() (const signed long val)
832 return static_cast<unsigned int> (val);
837 template<>
838 struct Truncator<unsigned int, signed long>
840 signed long operator() (unsigned int val)
842 return
843 (val > static_cast<unsigned int> (ACE_Numeric_Limits<signed long>::max ())
844 ? ACE_Numeric_Limits<signed long>::max ()
845 : static_cast<signed long> (val));
849 #endif /* ACE_SIZEOF_INT == ACE_SIZEOF_LONG */
851 template<>
852 struct Truncator<ACE_INT64, ACE_UINT64>
854 ACE_UINT64 operator() (ACE_INT64 val)
856 return static_cast<ACE_UINT64> (val);
860 template<>
861 struct Truncator<ACE_UINT64, ACE_INT64>
863 ACE_INT64 operator() (ACE_UINT64 val)
865 return
866 (val > static_cast<ACE_UINT64> (ACE_Numeric_Limits<ACE_INT64>::max ())
867 ? ACE_Numeric_Limits<ACE_INT64>::max ()
868 : static_cast<ACE_INT64> (val));
872 //----------------------------------------------------------
873 // sizeof(FROM) < sizeof(TO)
874 //----------------------------------------------------------
876 template<>
877 struct Truncator<ACE_INT8, ACE_INT32>
879 ACE_INT32 operator() (ACE_INT8 val)
881 return static_cast<ACE_INT32> (val);
885 template<>
886 struct Truncator<ACE_UINT8, ACE_UINT32>
888 ACE_UINT32 operator() (ACE_UINT8 val)
890 return static_cast<ACE_UINT32> (val);
894 template<>
895 struct Truncator<ACE_UINT8, ACE_INT32>
897 ACE_INT32 operator() (ACE_UINT8 val)
899 return static_cast<ACE_INT32> (val);
903 template<>
904 struct Truncator<ACE_INT8, ACE_UINT32>
906 ACE_UINT32 operator() (ACE_INT8 val)
908 return static_cast<ACE_UINT32> (val);
912 #if defined (ACE_SIZEOF_LONG) && ACE_SIZEOF_LONG < 8
913 template<>
914 struct Truncator<signed long, ACE_INT64>
916 ACE_INT64 operator() (signed long val)
918 return static_cast<ACE_INT64> (val);
922 template<>
923 struct Truncator<signed long, ACE_UINT64>
925 ACE_UINT64 operator() (signed long val)
927 return static_cast<ACE_UINT64> (val);
931 template<>
932 struct Truncator<const signed long, ACE_UINT64>
934 ACE_UINT64 operator() (const signed long val)
936 return
937 (val > static_cast<ACE_UINT64> (ACE_Numeric_Limits<signed long>::max ())
938 ? ACE_Numeric_Limits<signed long>::max ()
939 : static_cast<signed long> (val));
943 template<>
944 struct Truncator<unsigned long, ACE_UINT64>
946 ACE_UINT64 operator() (unsigned long val)
948 return static_cast<ACE_UINT64> (val);
952 template<>
953 struct Truncator<unsigned long, ACE_INT64>
955 ACE_INT64 operator() (unsigned long val)
957 return static_cast<ACE_INT64> (val);
960 #endif /* ACE_SIZEOF_LONG < 8 */
962 #if defined (ACE_SIZEOF_INT) && ACE_SIZEOF_INT < 8
963 template<>
964 struct Truncator<signed int, ACE_INT64>
966 ACE_INT64 operator() (signed int val)
968 return static_cast<ACE_INT64> (val);
972 template<>
973 struct Truncator<signed int, ACE_UINT64>
975 ACE_UINT64 operator() (signed int val)
977 return static_cast<ACE_UINT64> (val);
981 template<>
982 struct Truncator<unsigned int, ACE_UINT64>
984 ACE_UINT64 operator() (unsigned int val)
986 return static_cast<ACE_UINT64> (val);
990 template<>
991 struct Truncator<unsigned int, ACE_INT64>
993 ACE_INT64 operator() (unsigned int val)
995 return static_cast<ACE_INT64> (val);
998 #endif /* ACE_SIZEOF_INT < 8 */
1000 template<>
1001 struct Truncator<size_t, unsigned long>
1003 unsigned long operator() (size_t val)
1005 return
1006 (val > static_cast<unsigned long> (ACE_Numeric_Limits<size_t>::max ())
1007 ? ACE_Numeric_Limits<size_t>::max ()
1008 : static_cast<size_t> (val));
1012 // Partial specialization for the case where the types are the same.
1013 // No truncation is necessary.
1014 template<typename T>
1015 struct Truncator<T, T>
1017 T operator() (T val)
1019 return val;
1023 // Partial specialization for the case where the types are the same,
1024 // but the from type is const. No truncation is necessary.
1026 // This is only necessary to workaround a problem with the BCB6
1027 // compiler.
1028 template<typename T>
1029 struct Truncator<T const, T>
1031 T operator() (T val)
1033 return val;
1037 // -------------------------------------
1039 template<typename TO, typename FROM>
1040 inline TO truncate_cast (FROM val)
1042 typedef Truncator<FROM, TO> truncator;
1044 return truncator() (val);
1047 #endif /* !__BORLANDC__ || __BORLANDC__ >= 0x590 */
1049 } // namespace ACE_Utils
1051 ACE_END_VERSIONED_NAMESPACE_DECL
1053 #include /**/ "ace/post.h"
1055 #endif /* ACE_TRUNCATE_H*/