schema_editor/comdel/domain/value.cpp

141 lines
3.1 KiB
C++
Raw Normal View History

2022-03-31 21:20:41 +00:00
#include "value.h"
#include <string>
namespace domain {
2022-04-10 12:23:18 +00:00
std::string Value::string() {
switch (type) {
case INT:
return std::to_string(intValue);
case BOOL:
return boolValue ? "true" : "false";
case STRING:
return stringValue;
case NIL:
return "null";
case UNDEFINED:
return "undefined";
case ADDRESS_SPACE:
return "AddressSpace::" + addressSpace->getName();
case WIRE_REFERENCE:
return "Wire::" + reference;
case ADDRESS_SPACE_REFERENCE:
return "AddressSpace::" + reference;
case ATTRIBUTE_REFERENCE:
return "Attribute::" + reference;
default:
return "unknown";
}
}
2022-03-31 21:20:41 +00:00
Value::ValueType Value::getType() {
return type;
}
bool Value::isType(Value::ValueType type) {
return this->type == type;
}
long long Value::asInt() {
if(isType(Value::INT)) {
return intValue;
}
throw std::exception();
}
std::string Value::asString() {
if(isType(Value::STRING)) {
return stringValue;
}
throw std::exception();
}
bool Value::asBool() {
if(isType(Value::BOOL)) {
return boolValue;
}
throw std::exception();
}
AddressSpace Value::asAddressSpace() {
if(isType(Value::ADDRESS_SPACE)) {
return *addressSpace;
}
throw std::exception();
}
std::string Value::asReference() {
2022-04-10 12:23:18 +00:00
if(isType(Value::WIRE_REFERENCE) || isType(Value::ADDRESS_SPACE_REFERENCE) || isType(Value::ATTRIBUTE_REFERENCE) || isType(Value::UNDEFINED)) {
2022-03-31 21:20:41 +00:00
return reference;
}
throw std::exception();
}
2022-04-05 21:48:07 +00:00
void Value::setInt(long long value) {
if(isType(Value::INT)) {
this->intValue = value;
}
throw std::exception();
}
void Value::setString(std::string value) {
if(isType(Value::STRING)) {
this->stringValue = value;
}
throw std::exception();
}
void Value::setBool(bool value) {
if(isType(Value::BOOL)) {
this->boolValue = value;
}
throw std::exception();
}
void Value::setReference(std::string value) {
if(isType(Value::WIRE_REFERENCE)) {
this->reference = value;
}
throw std::exception();
}
2022-03-31 21:20:41 +00:00
Value Value::fromInt(long long value) {
Value val;
val.type = Value::INT;
val.intValue = value;
return val;
}
Value Value::fromString(std::string value) {
Value val;
val.type = Value::STRING;
val.stringValue = value;
return val;
}
Value Value::fromBool(bool value) {
Value val;
val.type = Value::BOOL;
val.boolValue = value;
return val;
}
Value Value::fromAddressSpace(AddressSpace addressSpace) {
Value val;
val.type = Value::ADDRESS_SPACE;
val.addressSpace = addressSpace;
return val;
}
Value Value::fromReference(std::string value, Value::ValueType type) {
Value val;
val.type = type;
val.reference = value;
return val;
}
Value Value::ofType(Value::ValueType type) {
Value val;
val.type = type;
return val;
}
Value Value::fromNull() {
Value val;
val.type = Value::NIL;
return val;
}
2022-03-31 21:20:41 +00:00
} // namespace domain