fphdlimp.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. *******************************************************************************
  5. * Copyright (C) 2009-2015, International Business Machines Corporation and *
  6. * others. All Rights Reserved. *
  7. *******************************************************************************
  8. */
  9. #ifndef FPHDLIMP_H
  10. #define FPHDLIMP_H
  11. #include "unicode/utypes.h"
  12. #if !UCONFIG_NO_FORMATTING
  13. #include "unicode/fieldpos.h"
  14. #include "unicode/fpositer.h"
  15. #include "unicode/formattedvalue.h"
  16. U_NAMESPACE_BEGIN
  17. // utility FieldPositionHandler
  18. // base class, null implementation
  19. class U_I18N_API FieldPositionHandler: public UMemory {
  20. protected:
  21. int32_t fShift = 0;
  22. public:
  23. virtual ~FieldPositionHandler();
  24. virtual void addAttribute(int32_t id, int32_t start, int32_t limit) = 0;
  25. virtual void shiftLast(int32_t delta) = 0;
  26. virtual UBool isRecording(void) const = 0;
  27. void setShift(int32_t delta);
  28. };
  29. // utility subclass FieldPositionOnlyHandler
  30. class FieldPositionOnlyHandler : public FieldPositionHandler {
  31. FieldPosition& pos;
  32. UBool acceptFirstOnly = FALSE;
  33. UBool seenFirst = FALSE;
  34. public:
  35. FieldPositionOnlyHandler(FieldPosition& pos);
  36. virtual ~FieldPositionOnlyHandler();
  37. void addAttribute(int32_t id, int32_t start, int32_t limit) U_OVERRIDE;
  38. void shiftLast(int32_t delta) U_OVERRIDE;
  39. UBool isRecording(void) const U_OVERRIDE;
  40. /**
  41. * Enable this option to lock in the FieldPosition value after seeing the
  42. * first occurrence of the field. The default behavior is to take the last
  43. * occurrence.
  44. */
  45. void setAcceptFirstOnly(UBool acceptFirstOnly);
  46. };
  47. // utility subclass FieldPositionIteratorHandler
  48. // exported as U_I18N_API for tests
  49. class U_I18N_API FieldPositionIteratorHandler : public FieldPositionHandler {
  50. FieldPositionIterator* iter; // can be NULL
  51. UVector32* vec;
  52. UErrorCode status;
  53. UFieldCategory fCategory;
  54. // Note, we keep a reference to status, so if status is on the stack, we have
  55. // to be destroyed before status goes out of scope. Easiest thing is to
  56. // allocate us on the stack in the same (or narrower) scope as status has.
  57. // This attempts to encourage that by blocking heap allocation.
  58. static void* U_EXPORT2 operator new(size_t) U_NOEXCEPT = delete;
  59. static void* U_EXPORT2 operator new[](size_t) U_NOEXCEPT = delete;
  60. #if U_HAVE_PLACEMENT_NEW
  61. static void* U_EXPORT2 operator new(size_t, void*) U_NOEXCEPT = delete;
  62. #endif
  63. public:
  64. FieldPositionIteratorHandler(FieldPositionIterator* posIter, UErrorCode& status);
  65. /** If using this constructor, you must call getError() when done formatting! */
  66. FieldPositionIteratorHandler(UVector32* vec, UErrorCode& status);
  67. ~FieldPositionIteratorHandler();
  68. void addAttribute(int32_t id, int32_t start, int32_t limit) U_OVERRIDE;
  69. void shiftLast(int32_t delta) U_OVERRIDE;
  70. UBool isRecording(void) const U_OVERRIDE;
  71. /** Copies a failed error code into _status. */
  72. inline void getError(UErrorCode& _status) {
  73. if (U_SUCCESS(_status) && U_FAILURE(status)) {
  74. _status = status;
  75. }
  76. }
  77. inline void setCategory(UFieldCategory category) {
  78. fCategory = category;
  79. }
  80. };
  81. U_NAMESPACE_END
  82. #endif /* !UCONFIG_NO_FORMATTING */
  83. #endif /* FPHDLIMP_H */