-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchecked.hpp
47 lines (43 loc) · 2.17 KB
/
checked.hpp
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
#ifndef checked_hpp
#define checked_hpp
#include "flagged.hpp"
#include <iostream>
#include <compare>
namespace checked {
class Checked;
Checked operator+(const Checked&) noexcept;
Checked operator-(const Checked&) noexcept;
Checked operator+(const Checked&, const Checked&) noexcept;
Checked operator-(const Checked&, const Checked&) noexcept;
Checked operator*(const Checked&, const Checked&) noexcept;
Checked operator/(const Checked&, const Checked&) noexcept;
Checked operator%(const Checked&, const Checked&) noexcept;
std::strong_ordering operator<=>(const Checked&, const Checked&) noexcept;
bool operator==(const Checked&, const Checked&) noexcept;
std::ostream& operator<<(std::ostream&, const Checked&) noexcept;
class Checked {
private:
Flagged integer;
public:
Checked() noexcept : integer(0) {}
Checked(const Itype& v) noexcept : integer(v) {}
Checked(const Itype& v, const HadOverflowed& s) noexcept : integer(v, s) {}
Checked(const Checked& c) noexcept : integer(c.integer) {};
Itype get_value() const noexcept { return this->integer.value; }
HadOverflowed get_status() const noexcept { return this->integer.status; }
Flagged get_flagged() const noexcept { return this->integer; }
Checked operator= (const Checked& c) noexcept { this->integer = c.integer; return (*this); }
Checked operator+=(const Checked& c) noexcept { (*this) = (*this) + c; return (*this); }
Checked operator-=(const Checked& c) noexcept { (*this) = (*this) - c; return (*this); }
Checked operator*=(const Checked& c) noexcept { (*this) = (*this) * c; return (*this); }
Checked operator/=(const Checked& c) noexcept { (*this) = (*this) / c; return (*this); }
Checked operator%=(const Checked& c) noexcept { (*this) = (*this) % c; return (*this); }
Checked operator++(int) noexcept { (*this) = (*this) + 1; return (*this); }
Checked operator--(int) noexcept { (*this) = (*this) - 1; return (*this); }
Checked operator++() noexcept { (*this) = (*this) + 1; return (*this); }
Checked operator--() noexcept { (*this) = (*this) - 1; return (*this); }
private:
friend std::istream& operator>>(std::istream&, Checked&) noexcept;
};
}
#endif // checked_hpp