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#include "utypeinfo.h"  // for 'typeid' to work
12
13#include "unicode/utypes.h"
14
15#if !UCONFIG_NO_FORMATTING
16
17#include "unicode/measure.h"
18#include "unicode/measunit.h"
19
20U_NAMESPACE_BEGIN
21
22UOBJECT_DEFINE_RTTI_IMPLEMENTATION(Measure)
23
24Measure::Measure() {}
25
26Measure::Measure(const Formattable& _number, MeasureUnit* adoptedUnit,
27                 UErrorCode& ec) :
28    number(_number), unit(adoptedUnit) {
29    if (U_SUCCESS(ec) &&
30        (!number.isNumeric() || adoptedUnit == 0)) {
31        ec = U_ILLEGAL_ARGUMENT_ERROR;
32    }
33}
34
35Measure::Measure(const Measure& other) :
36    UObject(other), unit(0) {
37    *this = other;
38}
39
40Measure& Measure::operator=(const Measure& other) {
41    if (this != &other) {
42        delete unit;
43        number = other.number;
44        unit = (MeasureUnit*) other.unit->clone();
45    }
46    return *this;
47}
48
49UObject *Measure::clone() const {
50    return new Measure(*this);
51}
52
53Measure::~Measure() {
54    delete unit;
55}
56
57UBool Measure::operator==(const UObject& other) const {
58    if (this == &other) {  // Same object, equal
59        return TRUE;
60    }
61    if (typeid(*this) != typeid(other)) { // Different types, not equal
62        return FALSE;
63    }
64    const Measure &m = static_cast<const Measure&>(other);
65    return number == m.number &&
66        ((unit == NULL) == (m.unit == NULL)) &&
67        (unit == NULL || *unit == *m.unit);
68}
69
70U_NAMESPACE_END
71
72#endif // !UCONFIG_NO_FORMATTING
73