Type &Type::operator~ () throw (IncompatibleTypes) { // Strong memory cannot negate or complement. if (CurrentType == STRONG_MEMORY) throw IncompatibleTypes(); // Cannot operate on SHORT, NEAR or FAR expressions if (DistanceType != UNDEFINED) throw IncompatibleTypes(); if (CurrentType != UNKNOWN) CurrentType = SCALAR; Size = 0; return *this; }
Type &Type::operator*= (const Type &t) throw (IncompatibleTypes) { // Strong memory cannot multiply or divide. if ((CurrentType == STRONG_MEMORY) || (t.CurrentType == STRONG_MEMORY)) throw IncompatibleTypes(); // Cannot operate on SHORT, NEAR or FAR expressions if ((DistanceType != UNDEFINED) && (t.DistanceType != UNDEFINED)) throw IncompatibleTypes(); // Returns UNKNOWN if either object is unknown if ((CurrentType == UNKNOWN) || (t.CurrentType == UNKNOWN)) { CurrentType = UNKNOWN; return *this; } // The result of any valid operation will be scalar CurrentType = SCALAR; Size = 0; return *this; }
Quantity & operator -=(const Quantity & rhs) { if (meters_ != rhs.meters_ || seconds_ != rhs.seconds_ || grams_ != rhs.grams_) { throw IncompatibleTypes(); } value_ -= rhs.value_; return *this; }
Type &Type::operator+= (const Type &t) throw (IncompatibleTypes) { // Cannot operate on SHORT, NEAR or FAR expressions if ((DistanceType != UNDEFINED) && (t.DistanceType != UNDEFINED)) throw IncompatibleTypes(); switch (t.CurrentType) { case SCALAR: switch (CurrentType) { case SCALAR: Size = 0; break; // SCALAR + SCALAR = SCALAR case WEAK_MEMORY: CurrentType = WEAK_MEMORY; // WEAK_MEMORY + SCALAR = WEAK_MEMORY break; case STRONG_MEMORY: throw IncompatibleTypes(); // STRONG_MEMORY + SCALAR = error! break; case UNKNOWN: // UNKNOWN + anything = UNKNOWN break; } break; case WEAK_MEMORY: switch (CurrentType) { case SCALAR: CurrentType = WEAK_MEMORY; // SCALAR + WEAK_MEMORY = WEAK_MEMORY Size = t.Size; break; case WEAK_MEMORY: CurrentType = SCALAR; // WEAK_MEMORY + WEAK_MEMORY = SCALAR Size = 0; break; case STRONG_MEMORY: throw IncompatibleTypes(); // STRONG_MEMORY + WEAK_MEMORY = error! break; case UNKNOWN: // UNKNOWN + anything = UNKNOWN break; } break; case STRONG_MEMORY: throw IncompatibleTypes (); // Anything + STRONG_MEMORY = error! case UNKNOWN: CurrentType = UNKNOWN; // Anything + UNKNOWN = UNKNOWN break; } return *this; }