measunit.h revision c73f511526464f8e56c242df80552e9b0d94ae3d
1/*
2**********************************************************************
3* Copyright (c) 2004-2014, International Business Machines
4* Corporation and others.  All Rights Reserved.
5**********************************************************************
6* Author: Alan Liu
7* Created: April 26, 2004
8* Since: ICU 3.0
9**********************************************************************
10*/
11#ifndef __MEASUREUNIT_H__
12#define __MEASUREUNIT_H__
13
14#include "unicode/utypes.h"
15
16#if !UCONFIG_NO_FORMATTING
17
18#include "unicode/unistr.h"
19
20/**
21 * \file
22 * \brief C++ API: A unit for measuring a quantity.
23 */
24
25U_NAMESPACE_BEGIN
26
27class StringEnumeration;
28
29/**
30 * A unit such as length, mass, volume, currency, etc.  A unit is
31 * coupled with a numeric amount to produce a Measure.
32 *
33 * @author Alan Liu
34 * @stable ICU 3.0
35 */
36class U_I18N_API MeasureUnit: public UObject {
37 public:
38
39    /**
40     * Default constructor.
41     * @stable ICU 3.0
42     */
43    MeasureUnit() : fTypeId(0), fSubTypeId(0) {
44        fCurrency[0] = 0;
45    }
46
47    /**
48     * Copy constructor.
49     * @draft ICU 53
50     */
51    MeasureUnit(const MeasureUnit &other);
52
53    /**
54     * Assignment operator.
55     * @draft ICU 53
56     */
57    MeasureUnit &operator=(const MeasureUnit &other);
58
59    /**
60     * Returns a polymorphic clone of this object.  The result will
61     * have the same class as returned by getDynamicClassID().
62     * @stable ICU 3.0
63     */
64    virtual UObject* clone() const;
65
66    /**
67     * Destructor
68     * @stable ICU 3.0
69     */
70    virtual ~MeasureUnit();
71
72    /**
73     * Equality operator.  Return true if this object is equal
74     * to the given object.
75     * @stable ICU 3.0
76     */
77    virtual UBool operator==(const UObject& other) const;
78
79#ifndef U_HIDE_DRAFT_API
80    /**
81     * Inequality operator.  Return true if this object is not equal
82     * to the given object.
83     * @draft ICU 53
84     */
85    UBool operator!=(const UObject& other) const {
86        return !(*this == other);
87    }
88
89    /**
90     * Get the type.
91     * @draft ICU 53
92     */
93    const char *getType() const;
94
95    /**
96     * Get the sub type.
97     * @draft ICU 53
98     */
99    const char *getSubtype() const;
100
101    /**
102     * getAvailable gets all of the available units.
103     * If there are too many units to fit into destCapacity then the
104     * error code is set to U_BUFFER_OVERFLOW_ERROR.
105     *
106     * @param destArray destination buffer.
107     * @param destCapacity number of MeasureUnit instances available at dest.
108     * @param errorCode ICU error code.
109     * @return number of available units.
110     * @draft ICU 53
111     */
112    static int32_t getAvailable(
113            MeasureUnit *destArray,
114            int32_t destCapacity,
115            UErrorCode &errorCode);
116
117    /**
118     * getAvailable gets all of the available units for a specific type.
119     * If there are too many units to fit into destCapacity then the
120     * error code is set to U_BUFFER_OVERFLOW_ERROR.
121     *
122     * @param type the type
123     * @param destArray destination buffer.
124     * @param destCapacity number of MeasureUnit instances available at dest.
125     * @param errorCode ICU error code.
126     * @return number of available units for type.
127     * @draft ICU 53
128     */
129    static int32_t getAvailable(
130            const char *type,
131            MeasureUnit *destArray,
132            int32_t destCapacity,
133            UErrorCode &errorCode);
134
135    /**
136     * getAvailableTypes gets all of the available types. Caller owns the
137     * returned StringEnumeration and must delete it when finished using it.
138     *
139     * @param errorCode ICU error code.
140     * @return the types.
141     * @draft ICU 53
142     */
143    static StringEnumeration* getAvailableTypes(UErrorCode &errorCode);
144#endif /* U_HIDE_DRAFT_API */
145
146    /**
147     * Return the class ID for this class. This is useful only for comparing to
148     * a return value from getDynamicClassID(). For example:
149     * <pre>
150     * .   Base* polymorphic_pointer = createPolymorphicObject();
151     * .   if (polymorphic_pointer->getDynamicClassID() ==
152     * .       erived::getStaticClassID()) ...
153     * </pre>
154     * @return          The class ID for all objects of this class.
155     * @draft ICU 53
156     */
157    static UClassID U_EXPORT2 getStaticClassID(void);
158
159    /**
160     * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This
161     * method is to implement a simple version of RTTI, since not all C++
162     * compilers support genuine RTTI. Polymorphic operator==() and clone()
163     * methods call this method.
164     *
165     * @return          The class ID for this object. All objects of a
166     *                  given class have the same class ID.  Objects of
167     *                  other classes have different class IDs.
168     * @draft ICU 53
169     */
170    virtual UClassID getDynamicClassID(void) const;
171
172#ifndef U_HIDE_INTERNAL_API
173    /**
174     * ICU use only.
175     * Returns associated array index for this measure unit. Only valid for
176     * non-currency measure units.
177     * @internal
178     */
179    int32_t getIndex() const;
180
181    /**
182     * ICU use only.
183     * Returns maximum value from getIndex plus 1.
184     * @internal
185     */
186    static int32_t getIndexCount();
187#endif /* U_HIDE_INTERNAL_API */
188
189// Start generated createXXX methods
190
191#ifndef U_HIDE_DRAFT_API
192    /**
193     * Creates a unit of acceleration: g-force.
194     * Caller owns returned value and must free it.
195     * @param status ICU error code.
196     * @draft ICU 53
197     */
198    static MeasureUnit *createGForce(UErrorCode &status);
199
200    /**
201     * Creates a unit of angle: arc-minute.
202     * Caller owns returned value and must free it.
203     * @param status ICU error code.
204     * @draft ICU 53
205     */
206    static MeasureUnit *createArcMinute(UErrorCode &status);
207
208    /**
209     * Creates a unit of angle: arc-second.
210     * Caller owns returned value and must free it.
211     * @param status ICU error code.
212     * @draft ICU 53
213     */
214    static MeasureUnit *createArcSecond(UErrorCode &status);
215
216    /**
217     * Creates a unit of angle: degree.
218     * Caller owns returned value and must free it.
219     * @param status ICU error code.
220     * @draft ICU 53
221     */
222    static MeasureUnit *createDegree(UErrorCode &status);
223
224    /**
225     * Creates a unit of area: acre.
226     * Caller owns returned value and must free it.
227     * @param status ICU error code.
228     * @draft ICU 53
229     */
230    static MeasureUnit *createAcre(UErrorCode &status);
231
232    /**
233     * Creates a unit of area: hectare.
234     * Caller owns returned value and must free it.
235     * @param status ICU error code.
236     * @draft ICU 53
237     */
238    static MeasureUnit *createHectare(UErrorCode &status);
239
240    /**
241     * Creates a unit of area: square-foot.
242     * Caller owns returned value and must free it.
243     * @param status ICU error code.
244     * @draft ICU 53
245     */
246    static MeasureUnit *createSquareFoot(UErrorCode &status);
247
248    /**
249     * Creates a unit of area: square-kilometer.
250     * Caller owns returned value and must free it.
251     * @param status ICU error code.
252     * @draft ICU 53
253     */
254    static MeasureUnit *createSquareKilometer(UErrorCode &status);
255
256    /**
257     * Creates a unit of area: square-meter.
258     * Caller owns returned value and must free it.
259     * @param status ICU error code.
260     * @draft ICU 53
261     */
262    static MeasureUnit *createSquareMeter(UErrorCode &status);
263
264    /**
265     * Creates a unit of area: square-mile.
266     * Caller owns returned value and must free it.
267     * @param status ICU error code.
268     * @draft ICU 53
269     */
270    static MeasureUnit *createSquareMile(UErrorCode &status);
271
272    /**
273     * Creates a unit of duration: day.
274     * Caller owns returned value and must free it.
275     * @param status ICU error code.
276     * @draft ICU 53
277     */
278    static MeasureUnit *createDay(UErrorCode &status);
279
280    /**
281     * Creates a unit of duration: hour.
282     * Caller owns returned value and must free it.
283     * @param status ICU error code.
284     * @draft ICU 53
285     */
286    static MeasureUnit *createHour(UErrorCode &status);
287
288    /**
289     * Creates a unit of duration: millisecond.
290     * Caller owns returned value and must free it.
291     * @param status ICU error code.
292     * @draft ICU 53
293     */
294    static MeasureUnit *createMillisecond(UErrorCode &status);
295
296    /**
297     * Creates a unit of duration: minute.
298     * Caller owns returned value and must free it.
299     * @param status ICU error code.
300     * @draft ICU 53
301     */
302    static MeasureUnit *createMinute(UErrorCode &status);
303
304    /**
305     * Creates a unit of duration: month.
306     * Caller owns returned value and must free it.
307     * @param status ICU error code.
308     * @draft ICU 53
309     */
310    static MeasureUnit *createMonth(UErrorCode &status);
311
312    /**
313     * Creates a unit of duration: second.
314     * Caller owns returned value and must free it.
315     * @param status ICU error code.
316     * @draft ICU 53
317     */
318    static MeasureUnit *createSecond(UErrorCode &status);
319
320    /**
321     * Creates a unit of duration: week.
322     * Caller owns returned value and must free it.
323     * @param status ICU error code.
324     * @draft ICU 53
325     */
326    static MeasureUnit *createWeek(UErrorCode &status);
327
328    /**
329     * Creates a unit of duration: year.
330     * Caller owns returned value and must free it.
331     * @param status ICU error code.
332     * @draft ICU 53
333     */
334    static MeasureUnit *createYear(UErrorCode &status);
335
336    /**
337     * Creates a unit of length: centimeter.
338     * Caller owns returned value and must free it.
339     * @param status ICU error code.
340     * @draft ICU 53
341     */
342    static MeasureUnit *createCentimeter(UErrorCode &status);
343
344    /**
345     * Creates a unit of length: foot.
346     * Caller owns returned value and must free it.
347     * @param status ICU error code.
348     * @draft ICU 53
349     */
350    static MeasureUnit *createFoot(UErrorCode &status);
351
352    /**
353     * Creates a unit of length: inch.
354     * Caller owns returned value and must free it.
355     * @param status ICU error code.
356     * @draft ICU 53
357     */
358    static MeasureUnit *createInch(UErrorCode &status);
359
360    /**
361     * Creates a unit of length: kilometer.
362     * Caller owns returned value and must free it.
363     * @param status ICU error code.
364     * @draft ICU 53
365     */
366    static MeasureUnit *createKilometer(UErrorCode &status);
367
368    /**
369     * Creates a unit of length: light-year.
370     * Caller owns returned value and must free it.
371     * @param status ICU error code.
372     * @draft ICU 53
373     */
374    static MeasureUnit *createLightYear(UErrorCode &status);
375
376    /**
377     * Creates a unit of length: meter.
378     * Caller owns returned value and must free it.
379     * @param status ICU error code.
380     * @draft ICU 53
381     */
382    static MeasureUnit *createMeter(UErrorCode &status);
383
384    /**
385     * Creates a unit of length: mile.
386     * Caller owns returned value and must free it.
387     * @param status ICU error code.
388     * @draft ICU 53
389     */
390    static MeasureUnit *createMile(UErrorCode &status);
391
392    /**
393     * Creates a unit of length: millimeter.
394     * Caller owns returned value and must free it.
395     * @param status ICU error code.
396     * @draft ICU 53
397     */
398    static MeasureUnit *createMillimeter(UErrorCode &status);
399
400    /**
401     * Creates a unit of length: picometer.
402     * Caller owns returned value and must free it.
403     * @param status ICU error code.
404     * @draft ICU 53
405     */
406    static MeasureUnit *createPicometer(UErrorCode &status);
407
408    /**
409     * Creates a unit of length: yard.
410     * Caller owns returned value and must free it.
411     * @param status ICU error code.
412     * @draft ICU 53
413     */
414    static MeasureUnit *createYard(UErrorCode &status);
415
416    /**
417     * Creates a unit of mass: gram.
418     * Caller owns returned value and must free it.
419     * @param status ICU error code.
420     * @draft ICU 53
421     */
422    static MeasureUnit *createGram(UErrorCode &status);
423
424    /**
425     * Creates a unit of mass: kilogram.
426     * Caller owns returned value and must free it.
427     * @param status ICU error code.
428     * @draft ICU 53
429     */
430    static MeasureUnit *createKilogram(UErrorCode &status);
431
432    /**
433     * Creates a unit of mass: ounce.
434     * Caller owns returned value and must free it.
435     * @param status ICU error code.
436     * @draft ICU 53
437     */
438    static MeasureUnit *createOunce(UErrorCode &status);
439
440    /**
441     * Creates a unit of mass: pound.
442     * Caller owns returned value and must free it.
443     * @param status ICU error code.
444     * @draft ICU 53
445     */
446    static MeasureUnit *createPound(UErrorCode &status);
447
448    /**
449     * Creates a unit of power: horsepower.
450     * Caller owns returned value and must free it.
451     * @param status ICU error code.
452     * @draft ICU 53
453     */
454    static MeasureUnit *createHorsepower(UErrorCode &status);
455
456    /**
457     * Creates a unit of power: kilowatt.
458     * Caller owns returned value and must free it.
459     * @param status ICU error code.
460     * @draft ICU 53
461     */
462    static MeasureUnit *createKilowatt(UErrorCode &status);
463
464    /**
465     * Creates a unit of power: watt.
466     * Caller owns returned value and must free it.
467     * @param status ICU error code.
468     * @draft ICU 53
469     */
470    static MeasureUnit *createWatt(UErrorCode &status);
471
472    /**
473     * Creates a unit of pressure: hectopascal.
474     * Caller owns returned value and must free it.
475     * @param status ICU error code.
476     * @draft ICU 53
477     */
478    static MeasureUnit *createHectopascal(UErrorCode &status);
479
480    /**
481     * Creates a unit of pressure: inch-hg.
482     * Caller owns returned value and must free it.
483     * @param status ICU error code.
484     * @draft ICU 53
485     */
486    static MeasureUnit *createInchHg(UErrorCode &status);
487
488    /**
489     * Creates a unit of pressure: millibar.
490     * Caller owns returned value and must free it.
491     * @param status ICU error code.
492     * @draft ICU 53
493     */
494    static MeasureUnit *createMillibar(UErrorCode &status);
495
496    /**
497     * Creates a unit of speed: kilometer-per-hour.
498     * Caller owns returned value and must free it.
499     * @param status ICU error code.
500     * @draft ICU 53
501     */
502    static MeasureUnit *createKilometerPerHour(UErrorCode &status);
503
504    /**
505     * Creates a unit of speed: meter-per-second.
506     * Caller owns returned value and must free it.
507     * @param status ICU error code.
508     * @draft ICU 53
509     */
510    static MeasureUnit *createMeterPerSecond(UErrorCode &status);
511
512    /**
513     * Creates a unit of speed: mile-per-hour.
514     * Caller owns returned value and must free it.
515     * @param status ICU error code.
516     * @draft ICU 53
517     */
518    static MeasureUnit *createMilePerHour(UErrorCode &status);
519
520    /**
521     * Creates a unit of temperature: celsius.
522     * Caller owns returned value and must free it.
523     * @param status ICU error code.
524     * @draft ICU 53
525     */
526    static MeasureUnit *createCelsius(UErrorCode &status);
527
528    /**
529     * Creates a unit of temperature: fahrenheit.
530     * Caller owns returned value and must free it.
531     * @param status ICU error code.
532     * @draft ICU 53
533     */
534    static MeasureUnit *createFahrenheit(UErrorCode &status);
535
536    /**
537     * Creates a unit of volume: cubic-kilometer.
538     * Caller owns returned value and must free it.
539     * @param status ICU error code.
540     * @draft ICU 53
541     */
542    static MeasureUnit *createCubicKilometer(UErrorCode &status);
543
544    /**
545     * Creates a unit of volume: cubic-mile.
546     * Caller owns returned value and must free it.
547     * @param status ICU error code.
548     * @draft ICU 53
549     */
550    static MeasureUnit *createCubicMile(UErrorCode &status);
551
552    /**
553     * Creates a unit of volume: liter.
554     * Caller owns returned value and must free it.
555     * @param status ICU error code.
556     * @draft ICU 53
557     */
558    static MeasureUnit *createLiter(UErrorCode &status);
559#endif /* U_HIDE_DRAFT_API */
560
561 protected:
562
563#ifndef U_HIDE_INTERNAL_API
564    /**
565     * For ICU use only.
566     * @internal
567     */
568    void initTime(const char *timeId);
569
570    /**
571     * For ICU use only.
572     * @internal
573     */
574    void initCurrency(const char *isoCurrency);
575
576#endif
577
578private:
579    int32_t fTypeId;
580    int32_t fSubTypeId;
581    char fCurrency[4];
582
583    MeasureUnit(int32_t typeId, int32_t subTypeId) : fTypeId(typeId), fSubTypeId(subTypeId) {
584        fCurrency[0] = 0;
585    }
586    void setTo(int32_t typeId, int32_t subTypeId);
587    int32_t getOffset() const;
588    static MeasureUnit *create(int typeId, int subTypeId, UErrorCode &status);
589};
590
591U_NAMESPACE_END
592
593#endif // !UNCONFIG_NO_FORMATTING
594#endif // __MEASUREUNIT_H__
595