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