ChakraICU.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #pragma once
  6. #include "CommonDefines.h"
  7. #ifdef HAS_ICU
  8. #ifdef WINDOWS10_ICU
  9. // if WINDOWS10_ICU is defined, pretend like we are building for recent Redstone,
  10. // even if that isn't necessarily true
  11. #pragma push_macro("NTDDI_VERSION")
  12. #undef NTDDI_VERSION
  13. #define NTDDI_VERSION NTDDI_WIN10_RS5
  14. #include <icu.h>
  15. #pragma pop_macro("NTDDI_VERSION")
  16. #else
  17. #include "unicode/ucal.h"
  18. #include "unicode/uclean.h"
  19. #include "unicode/ucol.h"
  20. #include "unicode/udat.h"
  21. #include "unicode/udatpg.h"
  22. #include "unicode/uloc.h"
  23. #include "unicode/ulocdata.h"
  24. #include "unicode/unumsys.h"
  25. #include "unicode/ustring.h"
  26. #include "unicode/unorm2.h"
  27. #include "unicode/upluralrules.h"
  28. #endif
  29. // Different assertion code is used in ChakraFull that enforces that messages are char literals
  30. #ifdef _CHAKRACOREBUILD
  31. #define ICU_ERRORMESSAGE(e) u_errorName(e)
  32. #else
  33. #define ICU_ERRORMESSAGE(e) "Bad status returned from ICU"
  34. #endif
  35. #define ICU_FAILURE(e) (U_FAILURE(e) || e == U_STRING_NOT_TERMINATED_WARNING)
  36. #define ICU_BUFFER_FAILURE(e) (e == U_BUFFER_OVERFLOW_ERROR || e == U_STRING_NOT_TERMINATED_WARNING)
  37. namespace PlatformAgnostic
  38. {
  39. namespace ICUHelpers
  40. {
  41. template<typename TObject, void(__cdecl * CloseFunction)(TObject)>
  42. class ScopedICUObject
  43. {
  44. private:
  45. TObject object;
  46. public:
  47. explicit ScopedICUObject(TObject object) : object(object)
  48. {
  49. }
  50. ScopedICUObject(const ScopedICUObject &other) = delete;
  51. ScopedICUObject(ScopedICUObject &&other) = delete;
  52. ScopedICUObject &operator=(const ScopedICUObject &other) = delete;
  53. ScopedICUObject &operator=(ScopedICUObject &&other) = delete;
  54. ~ScopedICUObject()
  55. {
  56. if (object != nullptr)
  57. {
  58. CloseFunction(object);
  59. }
  60. }
  61. operator TObject()
  62. {
  63. return object;
  64. }
  65. };
  66. typedef ScopedICUObject<UEnumeration *, uenum_close> ScopedUEnumeration;
  67. typedef ScopedICUObject<UCollator *, ucol_close> ScopedUCollator;
  68. typedef ScopedICUObject<UDateFormat *, udat_close> ScopedUDateFormat;
  69. typedef ScopedICUObject<UNumberFormat *, unum_close> ScopedUNumberFormat;
  70. typedef ScopedICUObject<UNumberingSystem *, unumsys_close> ScopedUNumberingSystem;
  71. typedef ScopedICUObject<UDateTimePatternGenerator *, udatpg_close> ScopedUDateTimePatternGenerator;
  72. typedef ScopedICUObject<UFieldPositionIterator *, ufieldpositer_close> ScopedUFieldPositionIterator;
  73. inline int GetICUMajorVersion()
  74. {
  75. UVersionInfo version = { 0 };
  76. u_getVersion(version);
  77. return version[0];
  78. }
  79. }
  80. }
  81. #endif