]> git.saurik.com Git - apple/icu.git/blob - icuSources/common/unicode/parsepos.h
e3d7d778ac1382b70c07a483438e76d1f9f4c03e
[apple/icu.git] / icuSources / common / unicode / parsepos.h
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 * Copyright (C) 1997-2005, International Business Machines Corporation and others. All Rights Reserved.
5 *******************************************************************************
6 *
7 * File PARSEPOS.H
8 *
9 * Modification History:
10 *
11 * Date Name Description
12 * 07/09/97 helena Converted from java.
13 * 07/17/98 stephen Added errorIndex support.
14 * 05/11/99 stephen Cleaned up.
15 *******************************************************************************
16 */
17
18 #ifndef PARSEPOS_H
19 #define PARSEPOS_H
20
21 #include "unicode/utypes.h"
22 #include "unicode/uobject.h"
23
24
25 #if U_SHOW_CPLUSPLUS_API
26 U_NAMESPACE_BEGIN
27
28 /**
29 * \file
30 * \brief C++ API: Canonical Iterator
31 */
32 /**
33 * <code>ParsePosition</code> is a simple class used by <code>Format</code>
34 * and its subclasses to keep track of the current position during parsing.
35 * The <code>parseObject</code> method in the various <code>Format</code>
36 * classes requires a <code>ParsePosition</code> object as an argument.
37 *
38 * <p>
39 * By design, as you parse through a string with different formats,
40 * you can use the same <code>ParsePosition</code>, since the index parameter
41 * records the current position.
42 *
43 * The ParsePosition class is not suitable for subclassing.
44 *
45 * @version 1.3 10/30/97
46 * @author Mark Davis, Helena Shih
47 * @see java.text.Format
48 */
49
50 class U_COMMON_API ParsePosition : public UObject {
51 public:
52 /**
53 * Default constructor, the index starts with 0 as default.
54 * @stable ICU 2.0
55 */
56 ParsePosition()
57 : UObject(),
58 index(0),
59 errorIndex(-1)
60 {}
61
62 /**
63 * Create a new ParsePosition with the given initial index.
64 * @param newIndex the new text offset.
65 * @stable ICU 2.0
66 */
67 ParsePosition(int32_t newIndex)
68 : UObject(),
69 index(newIndex),
70 errorIndex(-1)
71 {}
72
73 /**
74 * Copy constructor
75 * @param copy the object to be copied from.
76 * @stable ICU 2.0
77 */
78 ParsePosition(const ParsePosition& copy)
79 : UObject(copy),
80 index(copy.index),
81 errorIndex(copy.errorIndex)
82 {}
83
84 /**
85 * Destructor
86 * @stable ICU 2.0
87 */
88 virtual ~ParsePosition();
89
90 /**
91 * Assignment operator
92 * @stable ICU 2.0
93 */
94 inline ParsePosition& operator=(const ParsePosition& copy);
95
96 /**
97 * Equality operator.
98 * @return TRUE if the two parse positions are equal, FALSE otherwise.
99 * @stable ICU 2.0
100 */
101 inline UBool operator==(const ParsePosition& that) const;
102
103 /**
104 * Equality operator.
105 * @return TRUE if the two parse positions are not equal, FALSE otherwise.
106 * @stable ICU 2.0
107 */
108 inline UBool operator!=(const ParsePosition& that) const;
109
110 /**
111 * Clone this object.
112 * Clones can be used concurrently in multiple threads.
113 * If an error occurs, then NULL is returned.
114 * The caller must delete the clone.
115 *
116 * @return a clone of this object
117 *
118 * @see getDynamicClassID
119 * @stable ICU 2.8
120 */
121 ParsePosition *clone() const;
122
123 /**
124 * Retrieve the current parse position. On input to a parse method, this
125 * is the index of the character at which parsing will begin; on output, it
126 * is the index of the character following the last character parsed.
127 * @return the current index.
128 * @stable ICU 2.0
129 */
130 inline int32_t getIndex(void) const;
131
132 /**
133 * Set the current parse position.
134 * @param index the new index.
135 * @stable ICU 2.0
136 */
137 inline void setIndex(int32_t index);
138
139 /**
140 * Set the index at which a parse error occurred. Formatters
141 * should set this before returning an error code from their
142 * parseObject method. The default value is -1 if this is not
143 * set.
144 * @stable ICU 2.0
145 */
146 inline void setErrorIndex(int32_t ei);
147
148 /**
149 * Retrieve the index at which an error occurred, or -1 if the
150 * error index has not been set.
151 * @stable ICU 2.0
152 */
153 inline int32_t getErrorIndex(void) const;
154
155 /**
156 * ICU "poor man's RTTI", returns a UClassID for this class.
157 *
158 * @stable ICU 2.2
159 */
160 static UClassID U_EXPORT2 getStaticClassID();
161
162 /**
163 * ICU "poor man's RTTI", returns a UClassID for the actual class.
164 *
165 * @stable ICU 2.2
166 */
167 virtual UClassID getDynamicClassID() const;
168
169 private:
170 /**
171 * Input: the place you start parsing.
172 * <br>Output: position where the parse stopped.
173 * This is designed to be used serially,
174 * with each call setting index up for the next one.
175 */
176 int32_t index;
177
178 /**
179 * The index at which a parse error occurred.
180 */
181 int32_t errorIndex;
182
183 };
184
185 inline ParsePosition&
186 ParsePosition::operator=(const ParsePosition& copy)
187 {
188 index = copy.index;
189 errorIndex = copy.errorIndex;
190 return *this;
191 }
192
193 inline UBool
194 ParsePosition::operator==(const ParsePosition& copy) const
195 {
196 if(index != copy.index || errorIndex != copy.errorIndex)
197 return FALSE;
198 else
199 return TRUE;
200 }
201
202 inline UBool
203 ParsePosition::operator!=(const ParsePosition& copy) const
204 {
205 return !operator==(copy);
206 }
207
208 inline int32_t
209 ParsePosition::getIndex() const
210 {
211 return index;
212 }
213
214 inline void
215 ParsePosition::setIndex(int32_t offset)
216 {
217 this->index = offset;
218 }
219
220 inline int32_t
221 ParsePosition::getErrorIndex() const
222 {
223 return errorIndex;
224 }
225
226 inline void
227 ParsePosition::setErrorIndex(int32_t ei)
228 {
229 this->errorIndex = ei;
230 }
231 U_NAMESPACE_END
232 #endif // U_SHOW_CPLUSPLUS_API
233
234 #endif