12e5b6d6dSopenharmony_ci// © 2016 and later: Unicode, Inc. and others.
22e5b6d6dSopenharmony_ci// License & terms of use: http://www.unicode.org/copyright.html
32e5b6d6dSopenharmony_ci/*
42e5b6d6dSopenharmony_ci**********************************************************************
52e5b6d6dSopenharmony_ci* Copyright (c) 2004-2014, International Business Machines
62e5b6d6dSopenharmony_ci* Corporation and others.  All Rights Reserved.
72e5b6d6dSopenharmony_ci**********************************************************************
82e5b6d6dSopenharmony_ci* Author: Alan Liu
92e5b6d6dSopenharmony_ci* Created: April 26, 2004
102e5b6d6dSopenharmony_ci* Since: ICU 3.0
112e5b6d6dSopenharmony_ci**********************************************************************
122e5b6d6dSopenharmony_ci*/
132e5b6d6dSopenharmony_ci#include "utypeinfo.h"  // for 'typeid' to work
142e5b6d6dSopenharmony_ci
152e5b6d6dSopenharmony_ci#include "unicode/utypes.h"
162e5b6d6dSopenharmony_ci
172e5b6d6dSopenharmony_ci#if !UCONFIG_NO_FORMATTING
182e5b6d6dSopenharmony_ci
192e5b6d6dSopenharmony_ci#include "unicode/measure.h"
202e5b6d6dSopenharmony_ci#include "unicode/measunit.h"
212e5b6d6dSopenharmony_ci
222e5b6d6dSopenharmony_ciU_NAMESPACE_BEGIN
232e5b6d6dSopenharmony_ci
242e5b6d6dSopenharmony_ciUOBJECT_DEFINE_RTTI_IMPLEMENTATION(Measure)
252e5b6d6dSopenharmony_ci
262e5b6d6dSopenharmony_ciMeasure::Measure() : unit(nullptr) {}
272e5b6d6dSopenharmony_ci
282e5b6d6dSopenharmony_ciMeasure::Measure(const Formattable& _number, MeasureUnit* adoptedUnit,
292e5b6d6dSopenharmony_ci                 UErrorCode& ec) :
302e5b6d6dSopenharmony_ci    number(_number), unit(adoptedUnit) {
312e5b6d6dSopenharmony_ci    if (U_SUCCESS(ec) &&
322e5b6d6dSopenharmony_ci        (!number.isNumeric() || adoptedUnit == 0)) {
332e5b6d6dSopenharmony_ci        ec = U_ILLEGAL_ARGUMENT_ERROR;
342e5b6d6dSopenharmony_ci    }
352e5b6d6dSopenharmony_ci}
362e5b6d6dSopenharmony_ci
372e5b6d6dSopenharmony_ciMeasure::Measure(const Measure& other) :
382e5b6d6dSopenharmony_ci    UObject(other), unit(nullptr) {
392e5b6d6dSopenharmony_ci    *this = other;
402e5b6d6dSopenharmony_ci}
412e5b6d6dSopenharmony_ci
422e5b6d6dSopenharmony_ciMeasure& Measure::operator=(const Measure& other) {
432e5b6d6dSopenharmony_ci    if (this != &other) {
442e5b6d6dSopenharmony_ci        delete unit;
452e5b6d6dSopenharmony_ci        number = other.number;
462e5b6d6dSopenharmony_ci        if (other.unit != nullptr) {
472e5b6d6dSopenharmony_ci            unit = other.unit->clone();
482e5b6d6dSopenharmony_ci        } else {
492e5b6d6dSopenharmony_ci            unit = nullptr;
502e5b6d6dSopenharmony_ci        }
512e5b6d6dSopenharmony_ci    }
522e5b6d6dSopenharmony_ci    return *this;
532e5b6d6dSopenharmony_ci}
542e5b6d6dSopenharmony_ci
552e5b6d6dSopenharmony_ciMeasure *Measure::clone() const {
562e5b6d6dSopenharmony_ci    return new Measure(*this);
572e5b6d6dSopenharmony_ci}
582e5b6d6dSopenharmony_ci
592e5b6d6dSopenharmony_ciMeasure::~Measure() {
602e5b6d6dSopenharmony_ci    delete unit;
612e5b6d6dSopenharmony_ci}
622e5b6d6dSopenharmony_ci
632e5b6d6dSopenharmony_cibool Measure::operator==(const UObject& other) const {
642e5b6d6dSopenharmony_ci    if (this == &other) {  // Same object, equal
652e5b6d6dSopenharmony_ci        return true;
662e5b6d6dSopenharmony_ci    }
672e5b6d6dSopenharmony_ci    if (typeid(*this) != typeid(other)) { // Different types, not equal
682e5b6d6dSopenharmony_ci        return false;
692e5b6d6dSopenharmony_ci    }
702e5b6d6dSopenharmony_ci    const Measure &m = static_cast<const Measure&>(other);
712e5b6d6dSopenharmony_ci    return number == m.number &&
722e5b6d6dSopenharmony_ci        ((unit == NULL) == (m.unit == NULL)) &&
732e5b6d6dSopenharmony_ci        (unit == NULL || *unit == *m.unit);
742e5b6d6dSopenharmony_ci}
752e5b6d6dSopenharmony_ci
762e5b6d6dSopenharmony_ciU_NAMESPACE_END
772e5b6d6dSopenharmony_ci
782e5b6d6dSopenharmony_ci#endif // !UCONFIG_NO_FORMATTING
79