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