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