schema_editor/domain/library/value.h

101 lines
2.1 KiB
C++

#ifndef DOMAIN_LIBRARY_VALUE_H
#define DOMAIN_LIBRARY_VALUE_H
#include <optional>
#include <string>
#include <comdel/parser/astnode.h>
class Value: AstNode
{
public:
enum ValueType {
INT,
STRING,
BOOL,
IDENTIFIER
};
private:
ValueType type;
std::optional<long long> intValue;
std::optional<std::string> stringValue;
std::optional<bool> boolValue;
std::optional<std::string> identifierValue;
public:
Value();
ValueType getType() {
return type;
}
long long asInt() {
if(is(INT)) {
return intValue.value();
}
throw "cannot convert type to int";
}
std::string asString() {
if(is(STRING)) {
return stringValue.value();
}
throw "cannot convert type to string";
}
std::string asIdentifier() {
if(is(IDENTIFIER)) {
return identifierValue.value();
}
throw "cannot convert type to identifier";
}
bool asBool() {
if(is(BOOL)) {
return boolValue.value();
}
throw "cannot convert type to bool";
}
bool is(ValueType type) {
return this->type == type;
}
static Value ofBool(bool _value) {
Value value;
value.type = BOOL;
value.boolValue = std::optional<bool>(_value);
return value;
}
static Value ofInt(long long _value) {
Value value;
value.type = INT;
value.intValue = std::optional<long long>(_value);
return value;
}
static Value ofString(std::string _value) {
Value value;
value.type = STRING;
value.stringValue = std::optional<std::string>(_value);
return value;
}
static Value ofIdentifier(std::string _value) {
Value value;
value.type = IDENTIFIER;
value.identifierValue = std::optional<std::string>(_value);
return value;
}
};
class PropertyNode: public AstNode {
public:
IdentifierNode key;
Value value;
PropertyNode() {}
};
#endif // DOMAIN_LIBRARY_VALUE_H