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