Value* NumberValue::multiply(const Value &v) const { const NumberValue* n; NumberValue* ret; if (isNumber(v)) { n = static_cast<const NumberValue*>(&v); ret = new NumberValue(getValue()); if (type == NUMBER) ret->setType(*n); else { ret->setType(*this); ret->verifyUnits(*n); } ret->setValue(ret->getValue() * n->getValue()); return ret; } else if (v.type == COLOR) { return static_cast<const Color*>(&v)->multiply(*this); } else if(v.type == STRING) { return static_cast<const StringValue*>(&v)->multiply(*this); } else { throw new ValueException("Unsupported type.", *this->getTokens()); } }
Value* NumberValue::add(const Value &v) const { const NumberValue* n; const StringValue* s; NumberValue* nret; StringValue* sret; if (isNumber(v)) { n = static_cast<const NumberValue*>(&v); nret = new NumberValue(getValue()); if (type == NUMBER) nret->setType(*n); else { nret->setType(*this); nret->verifyUnits(*n); } nret->setValue(nret->getValue() + n->getValue()); return nret; } else if (v.type == COLOR) { return static_cast<const Color*>(&v)->add(*this); } else if (v.type == STRING) { s = static_cast<const StringValue*>(&v); sret = new StringValue(*this, s->getQuotes()); sret->append(v); return sret; } else { throw new ValueException("Unsupported type.", *this->getTokens()); } }
Value* NumberValue::divide(const Value &v) const { const NumberValue* n; NumberValue* ret; if (isNumber(v)) { n = static_cast<const NumberValue*>(&v); ret = new NumberValue(getValue()); if (type == NUMBER) ret->setType(*n); else { ret->setType(*this); ret->verifyUnits(*n); } ret->setValue(ret->getValue() / n->getValue()); return ret; } else throw new ValueException("You can only divide a number " "by a *number*.", *this->getTokens()); }
Value* NumberValue::substract(const Value &v) const { const NumberValue* n; NumberValue* ret; if (isNumber(v)) { n = static_cast<const NumberValue*>(&v); ret = new NumberValue(getValue()); if (type == NUMBER) ret->setType(*n); else { ret->setType(*this); ret->verifyUnits(*n); } ret->setValue(ret->getValue() - n->getValue()); return ret; } else throw new ValueException("You can only substract a " "*number* from a number.", *this->getTokens()); }