LESwaps.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. *
  3. * (C) Copyright IBM Corp. 1998-2014 - All Rights Reserved
  4. *
  5. */
  6. #ifndef __LESWAPS_H
  7. #define __LESWAPS_H
  8. #include "LETypes.h"
  9. /**
  10. * \file
  11. * \brief C++ API: Endian independent access to data for LayoutEngine
  12. */
  13. U_NAMESPACE_BEGIN
  14. /**
  15. * A convenience macro which invokes the swapWord member function
  16. * from a concise call.
  17. *
  18. * @deprecated ICU 54. See {@link icu::LayoutEngine}
  19. */
  20. #define SWAPW(value) LESwaps::swapWord((le_uint16)(value))
  21. /**
  22. * A convenience macro which invokes the swapLong member function
  23. * from a concise call.
  24. *
  25. * @deprecated ICU 54. See {@link icu::LayoutEngine}
  26. */
  27. #define SWAPL(value) LESwaps::swapLong((le_uint32)(value))
  28. /**
  29. * This class is used to access data which stored in big endian order
  30. * regardless of the conventions of the platform.
  31. *
  32. * All methods are static and inline in an attempt to induce the compiler
  33. * to do most of the calculations at compile time.
  34. *
  35. * @deprecated ICU 54. See {@link icu::LayoutEngine}
  36. */
  37. class U_LAYOUT_API LESwaps /* not : public UObject because all methods are static */ {
  38. public:
  39. /**
  40. * Reads a big-endian 16-bit word and returns a native-endian value.
  41. * No-op on a big-endian platform, byte-swaps on a little-endian platform.
  42. *
  43. * @param value - the word to be byte swapped
  44. *
  45. * @return the byte swapped word
  46. *
  47. * @deprecated ICU 54. See {@link icu::LayoutEngine}
  48. */
  49. static le_uint16 swapWord(le_uint16 value)
  50. {
  51. #if (defined(U_IS_BIG_ENDIAN) && U_IS_BIG_ENDIAN) || \
  52. (defined(BYTE_ORDER) && defined(BIG_ENDIAN) && (BYTE_ORDER == BIG_ENDIAN)) || \
  53. defined(__BIG_ENDIAN__)
  54. // Fastpath when we know that the platform is big-endian.
  55. return value;
  56. #else
  57. // Reads a big-endian value on any platform.
  58. const le_uint8 *p = reinterpret_cast<const le_uint8 *>(&value);
  59. return (le_uint16)((p[0] << 8) | p[1]);
  60. #endif
  61. };
  62. /**
  63. * Reads a big-endian 32-bit word and returns a native-endian value.
  64. * No-op on a big-endian platform, byte-swaps on a little-endian platform.
  65. *
  66. * @param value - the long to be byte swapped
  67. *
  68. * @return the byte swapped long
  69. *
  70. * @deprecated ICU 54. See {@link icu::LayoutEngine}
  71. */
  72. static le_uint32 swapLong(le_uint32 value)
  73. {
  74. #if (defined(U_IS_BIG_ENDIAN) && U_IS_BIG_ENDIAN) || \
  75. (defined(BYTE_ORDER) && defined(BIG_ENDIAN) && (BYTE_ORDER == BIG_ENDIAN)) || \
  76. defined(__BIG_ENDIAN__)
  77. // Fastpath when we know that the platform is big-endian.
  78. return value;
  79. #else
  80. // Reads a big-endian value on any platform.
  81. const le_uint8 *p = reinterpret_cast<const le_uint8 *>(&value);
  82. return (le_uint32)((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
  83. #endif
  84. };
  85. private:
  86. LESwaps() {} // private - forbid instantiation
  87. };
  88. U_NAMESPACE_END
  89. #endif