-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObject.h
71 lines (61 loc) · 2.15 KB
/
Object.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#pragma once
#include "Type.h"
#include <any>
#include <iostream>
using namespace std;
namespace Program{
class Object{
public:
Type type = Type::svoid;
any value = 0;
Object(){}
Object(const Type);
Object(const Type, const string&);
Object(const string& type_str)
: Object(str_to_type[type_str]){}
Object(const string& type_str, const string& value_str)
: Object(str_to_type[type_str], value_str){}
Object operator+(const Object&) const;
Object operator-(const Object&) const;
Object operator*(const Object&) const;
Object operator/(const Object&) const;
Object operator%(const Object&) const;
Object& operator+=(const Object&);
Object& operator-=(const Object&);
Object& operator*=(const Object&);
Object& operator/=(const Object&);
Object& operator%=(const Object&);
bool operator==(const Object&) const;
bool operator!=(const Object&) const;
bool operator<(const Object&) const;
bool operator>(const Object&) const;
bool operator<=(const Object&) const;
bool operator>=(const Object&) const;
friend ostream& operator<<(ostream& os, const Object& object){
#define PROGRAM_OBJECT_TO_STREAM(type_str)\
if(object.type==Type::s ## type_str)os<<any_cast<type_str>(object.value);
if(object.type == Type::svoid){
os << "void";
}
else if(object.type == Type::sbool){
os << (any_cast<bool>(object.value) ? "true" : "false");
}
PROGRAM_OBJECT_TO_STREAM(int)
PROGRAM_OBJECT_TO_STREAM(string)
return os;
}
friend istream& operator>>(istream& is, Object& object){
if(object.type == Type::sint){
int value;
is >> value;
object.value = value;
}
else if(object.type == Type::sstring){
string value;
is >> value;
object.value = value;
}
return is;
}
};
}