-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyear.cpp
More file actions
103 lines (82 loc) · 1.46 KB
/
Copy pathyear.cpp
File metadata and controls
103 lines (82 loc) · 1.46 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "year.h"
namespace tba{
Year::Year(int i1):i(i1){
if(!valid()){
std::ostringstream ss;
ss<<"year:"<<i;
throw std::invalid_argument{ss.str()};
}
}
Year& Year::operator=(int x){
i=x;
if(!valid()){
throw std::invalid_argument("year out of range");
}
return *this;
}
bool Year::valid()const{
return i>=1992 && i<2092;
}
Year rand(Year const*){
return Year(1992+::rand()%100);
}
short Year::get()const{
return i;
}
bool Year::operator==(Year const& a)const{
return i==a.i;
}
bool Year::operator==(int a)const{
return i==a;
}
bool Year::operator>=(int a)const{
return i>=a;
}
bool Year::operator<(int a)const{
return i<a;
}
std::ostream& operator<<(std::ostream& o,Year a){
return o<<a.get();
}
Year& operator++(Year& a){
a.i++;
if(!a.valid()){
a.i--;
throw std::range_error{"Year too high"};
}
return a;
}
Year& Year::operator--(){
i--;
if(!valid()){
i++;
throw std::range_error{"Year too low"};
}
return *this;
}
Year operator++(Year& a,int){
auto r=a;
++a;
return r;
}
Year operator-(Year a,int b){
return Year{a.get()-b};
}
Year operator+(Year a,int b){
return Year{a.get()+b};
}
Year decode(JSON_value in,const Year*){
return Year{decode(in,(int*)nullptr)};
}
Year decode(std::string_view a,Year const*){
return Year(stoi(std::string(a)));
}
std::string operator+(std::string const& a,Year b){
std::stringstream ss;
ss<<a<<b;
return ss.str();
}
}
short std::hash<tba::Year>::operator()(tba::Year a){
return a.get();
}