| 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 |
|
| 10 |
#ifndef FPHDLIMP_H |
| 11 |
#define FPHDLIMP_H |
| 12 |
|
| 13 |
#include "unicode/utypes.h" |
| 14 |
|
| 15 |
#if !UCONFIG_NO_FORMATTING |
| 16 |
|
| 17 |
#include "unicode/fieldpos.h" |
| 18 |
#include "unicode/fpositer.h" |
| 19 |
#include "unicode/formattedvalue.h" |
| 20 |
|
| 21 |
U_NAMESPACE_BEGIN |
| 22 |
|
| 23 |
// utility FieldPositionHandler |
| 24 |
// base class, null implementation |
| 25 |
|
| 26 |
class U_I18N_API FieldPositionHandler: public UMemory { |
| 27 |
protected: |
| 28 |
int32_t fShift = 0; |
| 29 |
|
| 30 |
public: |
| 31 |
virtual ~FieldPositionHandler(); |
| 32 |
virtual void addAttribute(int32_t id, int32_t start, int32_t limit) = 0; |
| 33 |
virtual void shiftLast(int32_t delta) = 0; |
| 34 |
virtual UBool isRecording() const = 0; |
| 35 |
|
| 36 |
void setShift(int32_t delta); |
| 37 |
}; |
| 38 |
|
| 39 |
|
| 40 |
// utility subclass FieldPositionOnlyHandler |
| 41 |
|
| 42 |
class FieldPositionOnlyHandler : public FieldPositionHandler { |
| 43 |
FieldPosition& pos; |
| 44 |
UBool acceptFirstOnly = false; |
| 45 |
UBool seenFirst = false; |
| 46 |
|
| 47 |
public: |
| 48 |
FieldPositionOnlyHandler(FieldPosition& pos); |
| 49 |
virtual ~FieldPositionOnlyHandler(); |
| 50 |
|
| 51 |
void addAttribute(int32_t id, int32_t start, int32_t limit) override; |
| 52 |
void shiftLast(int32_t delta) override; |
| 53 |
UBool isRecording() const override; |
| 54 |
|
| 55 |
/** |
| 56 |
* Enable this option to lock in the FieldPosition value after seeing the |
| 57 |
* first occurrence of the field. The default behavior is to take the last |
| 58 |
* occurrence. |
| 59 |
*/ |
| 60 |
void setAcceptFirstOnly(UBool acceptFirstOnly); |
| 61 |
}; |
| 62 |
|
| 63 |
|
| 64 |
// utility subclass FieldPositionIteratorHandler |
| 65 |
// exported as U_I18N_API for tests |
| 66 |
|
| 67 |
class U_I18N_API FieldPositionIteratorHandler : public FieldPositionHandler { |
| 68 |
FieldPositionIterator* iter; // can be nullptr |
| 69 |
UVector32* vec; |
| 70 |
UErrorCode status; |
| 71 |
UFieldCategory fCategory; |
| 72 |
|
| 73 |
// Note, we keep a reference to status, so if status is on the stack, we have |
| 74 |
// to be destroyed before status goes out of scope. Easiest thing is to |
| 75 |
// allocate us on the stack in the same (or narrower) scope as status has. |
| 76 |
// This attempts to encourage that by blocking heap allocation. |
| 77 |
static void* U_EXPORT2 operator new(size_t) noexcept = delete; |
| 78 |
static void* U_EXPORT2 operator new[](size_t) noexcept = delete; |
| 79 |
#if U_HAVE_PLACEMENT_NEW |
| 80 |
static void* U_EXPORT2 operator new(size_t, void*) noexcept = delete; |
| 81 |
#endif |
| 82 |
|
| 83 |
public: |
| 84 |
FieldPositionIteratorHandler(FieldPositionIterator* posIter, UErrorCode& status); |
| 85 |
/** If using this constructor, you must call getError() when done formatting! */ |
| 86 |
FieldPositionIteratorHandler(UVector32* vec, UErrorCode& status); |
| 87 |
~FieldPositionIteratorHandler(); |
| 88 |
|
| 89 |
void addAttribute(int32_t id, int32_t start, int32_t limit) override; |
| 90 |
void shiftLast(int32_t delta) override; |
| 91 |
UBool isRecording() const override; |
| 92 |
|
| 93 |
/** Copies a failed error code into _status. */ |
| 94 |
inline void getError(UErrorCode& _status) { |
| 95 |
if (U_SUCCESS(_status) && U_FAILURE(status)) { |
| 96 |
_status = status; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
inline void setCategory(UFieldCategory category) { |
| 101 |
fCategory = category; |
| 102 |
} |
| 103 |
}; |
| 104 |
|
| 105 |
U_NAMESPACE_END |
| 106 |
|
| 107 |
#endif /* !UCONFIG_NO_FORMATTING */ |
| 108 |
|
| 109 |
#endif /* FPHDLIMP_H */ |