summaryrefslogtreecommitdiff
path: root/Build/source/libs/icu/icu-4.6/test/intltest/fldset.h
blob: 0c9747b44c608cce1fd5d523b88deac326e4a849 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
************************************************************************
* Copyright (c) 2007-2010, International Business Machines
* Corporation and others.  All Rights Reserved.
************************************************************************
*/
#ifndef FLDSET_H_
#define FLDSET_H_

#include "unicode/utypes.h"

#if !UCONFIG_NO_FORMATTING
#include "unicode/calendar.h"
#include "unicode/ucal.h"
#include "unicode/udat.h"
#include "udbgutil.h"
#include "dbgutil.h"
#include "unicode/unistr.h"

#define U_FIELDS_SET_MAX  64

/**
 * This class represents a collection of integer values (fields), each designated by
 * one of a particular set of enum values.  Each integer value (int32_t) is optional and
 * may or may not be set. 
 * 
 * @internal ICU 3.8
 */
class FieldsSet {
    protected:
        /**
         * subclass interface - construct the FieldsSet to reference one of the standard
         * enumerations.
         * @param whichEnum which enumaration value goes with this set. Will be used to calculate string
         * values and also enum size.
         * @see UDebugEnumType
         */
        FieldsSet(UDebugEnumType whichEnum);
        
        /**
         * subclass interface - construct the FieldsSet without using a standard enum type.
         * @param fieldCount how many fields this object can hold.
         */
        FieldsSet(int32_t fieldsCount);

    public:
    
      /**
       * Compare two sets. In typical test usage, 'this' is the resul of 
       * a tested operation, and 'other' is the predefined expected value.
       * 
       * @param other the set to compare against.
       * @param status will return U_ILLEGAL_ARGUMENT_ERROR if sets are not the same size
       * @return a formatted string listing which fields are set in 
       *   this, with the comparison made agaainst those fields in other.
       */
      U_NAMESPACE_QUALIFIER UnicodeString diffFrom(const FieldsSet& other, UErrorCode &status) const;

    public:
      /**
       * Fill-in fields from a specified string, such as "NAME1=VALUE1,NAME2=VALUE2", etc. 
       * @param str string to parse
       * @param status status of parse
       * @return the number of valid parsed fields on success, or a negative number on failure.
       */
      int32_t parseFrom(const U_NAMESPACE_QUALIFIER UnicodeString& str, UErrorCode& status) {
          return parseFrom(str,NULL,status);
      }

      /**
       * Fill-in fields from a specified string, such as "NAME1=VALUE1,NAME2=VALUE2", etc. 
       * @param inheritFrom if a field's value is given as 0-length, such as NAME1 in "NAME1=,NAME2=VALUE2", 
       * the specified FieldsSet's value for NAME1 will be copied into this.
       * @param str string to parse
       * @param status status of parse
       * @return the number of valid parsed fields on success, or a negative number on failure.
       */
      int32_t parseFrom(const U_NAMESPACE_QUALIFIER UnicodeString& str,
                        const FieldsSet& inheritFrom,
                        UErrorCode& status) {
          return parseFrom(str, &inheritFrom, status);
      }

      /**
       * Fill-in fields from a specified string, such as "NAME1=VALUE1,NAME2=VALUE2", etc. 
       * @param inheritFrom if a field's value is given as 0-length, such as NAME1 in "NAME1=,NAME2=VALUE2", 
       * the specified FieldsSet's value for NAME1 will be copied into this.
       * @param str string to parse
       * @param status status of parse
       * @return the number of valid parsed fields on success, or a negative number on failure.
       */
      int32_t parseFrom(const U_NAMESPACE_QUALIFIER UnicodeString& str,
                        const FieldsSet* inheritFrom,
                        UErrorCode& status);

    protected:
      /**
       * Callback interface for subclass.
       * This function is called when parsing a field name, such as "MONTH"  in "MONTH=4".
       * Base implementation is to lookup the enum value using udbg_* utilities, or else as an integer if
       * enum is not available.
       * 
       * If there is a special directive, the implementer can catch it here and return -1 after special processing completes.
       * 
       * @param inheritFrom the set inheriting from - may be null.
       * @param name the field name (key side)
       * @param substr the string in question (value side)
       * @param status error status - set to error for failure.
       * @return field number, or negative if field should be skipped.
       */
      virtual int32_t handleParseName(const FieldsSet* inheritFrom,
                                      const U_NAMESPACE_QUALIFIER UnicodeString& name,
                                      const U_NAMESPACE_QUALIFIER UnicodeString& substr,
                                      UErrorCode& status);

      /**
       * Callback interface for subclass.
       * Base implementation is to call parseValueDefault(...)
       * @param inheritFrom the set inheriting from - may be null.
       * @param field which field is being parsed
       * @param substr the string in question (value side)
       * @param status error status - set to error for failure.
       * @see parseValueDefault
       */
      virtual void handleParseValue(const FieldsSet* inheritFrom,
                                    int32_t field,
                                    const U_NAMESPACE_QUALIFIER UnicodeString& substr,
                                    UErrorCode& status);

      /**
       * the default implementation for handleParseValue.
       * Base implementation is to parse a decimal integer value, or inherit from inheritFrom if the string is 0-length.
       * Implementations of this function should call set(field,...) on successful parse.
       * @see handleParseValue
       */
      void parseValueDefault(const FieldsSet* inheritFrom,
                             int32_t field,
                             const U_NAMESPACE_QUALIFIER UnicodeString& substr,
                             UErrorCode& status);      


      /**
       * convenience implementation for handleParseValue
       * attempt to load a value from an enum value using udbg_enumByString()
       * if fails, will call parseValueDefault()
       * @see handleParseValue
       */
      void parseValueEnum(UDebugEnumType type,
                          const FieldsSet* inheritFrom,
                          int32_t field,
                          const U_NAMESPACE_QUALIFIER UnicodeString& substr,
                          UErrorCode& status);

    private:
      /** 
       * Not callable - construct a default FieldsSet
       * @internal
       */
      FieldsSet();
      
      /**
       * construct the object.
       * @internal
       */
      void construct(UDebugEnumType whichEnum, int32_t fieldCount);

    public:
    /**
     * destructor
     */
     virtual ~FieldsSet();

    /**
     * Mark all fields as unset
     */
    void clear();
    
    /**
     * Mark a specific field as unset
     * @param field the field to unset
     */
    void clear(int32_t field);
    
    /**
     * Set a specific field
     * @param field the field to set (i.e. enum value)
     * @param value the field's value
     */
    void set(int32_t field, int32_t value);
    
    UBool isSet(int32_t field) const;

    /**
     * Return the field's value
     * @param field which field
     * @return field's value, or -1 if unset.
     */
    int32_t get(int32_t field) const;
    
    /**
     * Return true if both FieldsSet objects either are based on the same enum, or have the same number of fields.
     */
    UBool isSameType(const FieldsSet& other) const;

    /**
     * @return the number of fields 
     */
    int32_t fieldCount() const;
    
    protected:
       int32_t fValue[U_FIELDS_SET_MAX];
       UBool fIsSet[U_FIELDS_SET_MAX];
    protected:
       int32_t fFieldCount;
       UDebugEnumType fEnum;
};

/**
 * A subclass of FieldsSet representing the fields in a Calendar
 * @see Calendar
 */
class CalendarFieldsSet : public FieldsSet {
public:
    CalendarFieldsSet();
    virtual ~CalendarFieldsSet();

//        void clear(UCalendarDateFields field) { clear((int32_t)field); }
//        void set(UCalendarDateFields field, int32_t amount) { set ((int32_t)field, amount); }

//        UBool isSet(UCalendarDateFields field) const { return isSet((int32_t)field); }
//        int32_t get(UCalendarDateFields field) const { return get((int32_t)field); }

    /**
     * @param matches fillin to hold any fields different. Will have the calendar's value set on them.
     * @return true if the calendar matches in these fields.
     */
    UBool matches(U_NAMESPACE_QUALIFIER Calendar *cal,
                  CalendarFieldsSet &diffSet,
                  UErrorCode& status) const;

    /**
     * For each set field, set the same field on this Calendar.
     * Doesn't clear the Calendar first.
     * @param cal Calendar to modify
     * @param status Contains any errors propagated by the Calendar.
     */
    void setOnCalendar(U_NAMESPACE_QUALIFIER Calendar *cal, UErrorCode& status) const;
        
protected:
    /**
     * subclass override 
     */
    void handleParseValue(const FieldsSet* inheritFrom,
                          int32_t field,
                          const U_NAMESPACE_QUALIFIER UnicodeString& substr,
                          UErrorCode& status);
};

/**
 * This class simply implements a set of date and time styles
 * such as DATE=SHORT  or TIME=SHORT,DATE=LONG, such as would be passed
 * to DateFormat::createInstance()
 * @see DateFormat
 */
class DateTimeStyleSet : public FieldsSet {
    public:
        DateTimeStyleSet();
        virtual ~DateTimeStyleSet();

        /** 
         * @return the date style, or UDAT_NONE if not set
         */
        UDateFormatStyle getDateStyle() const;
        
        /** 
         * @return the time style, or UDAT_NONE if not set
         */
        UDateFormatStyle getTimeStyle() const;
    protected:
        void handleParseValue(const FieldsSet* inheritFrom,
                              int32_t field,
                              const U_NAMESPACE_QUALIFIER UnicodeString& substr,
                              UErrorCode& status);
        int32_t handleParseName(const FieldsSet* inheritFrom,
                                const U_NAMESPACE_QUALIFIER UnicodeString& name,
                                const U_NAMESPACE_QUALIFIER UnicodeString& substr,
                                UErrorCode& status);
};


#endif /*!UCONFIG_NO_FORMAT*/
#endif /*FLDSET_H_*/