-
Notifications
You must be signed in to change notification settings - Fork 0
/
nan_union.cpp
168 lines (124 loc) · 4.04 KB
/
nan_union.cpp
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// -*- compile-command: "c++ -g -Wall -std=c++11 nan_union.cpp -o nan_union -lstdc++" -*-
#include "nan_union.hpp"
#include "ref.hpp"
#include <iostream>
struct test {
test() { std::clog << "test()" << std::endl; }
test(const test&) { std::clog << "test(const test&)" << std::endl; }
test(test&&) { std::clog << "test(test&&)" << std::endl; }
test& operator=(const test&) { std::clog << "operator=(const test&)" << std::endl; return *this;}
test& operator=(test&&) { std::clog << "operator=(test&&)" << std::endl; return *this;}
~test() {
std::clog << "~test()" << std::endl;
}
friend std::ostream& operator<<(std::ostream& out, const test& self) {
return out << "test!";
}
};
class nan_boxed {
protected:
ieee754 data;
using value_type = std::int64_t;
using tag_type = ieee754::tag_type;
void release() {
value_type tmp = data.bits.payload;
reinterpret_cast<ref_any&>(tmp) = {};
}
public:
ieee754::tag_type tag() const {
if(data.bits.nan != ieee754::quiet_nan ) throw std::bad_cast();
return data.bits.tag;
}
void tag(ieee754::tag_type tag) {
if(data.bits.nan != ieee754::quiet_nan) throw std::bad_cast();
data.bits.tag = tag;
}
bool is_ref() const { return (data.bits.nan == ieee754::quiet_nan) && data.bits.flag; }
bool is_double() const { return data.bits.nan != ieee754::quiet_nan; }
bool is_value() const { return (data.bits.nan == ieee754::quiet_nan) && !data.bits.flag; }
template<class F>
typename F::value_type visit(const F& f) const {
if(data.bits.nan != ieee754::quiet_nan) {
return f(data.value);
} else if( data.bits.flag ) {
const value_type tmp = data.bits.payload;
return f(reinterpret_cast<const ref_any&>(tmp));
} else {
const value_type tmp = data.bits.payload;
return f(tmp);
}
}
~nan_boxed() {
if(data.bits.flag) release();
}
nan_boxed(const nan_boxed& other) {
if(other.data.bits.flag) {
value_type dst, src;
src = other.data.bits.payload;
new (&dst) ref_any(reinterpret_cast<ref_any&>(src));
data.bits.payload = dst;
data.bits.flag = true;
data.bits.tag = other.data.bits.tag;
data.bits.nan = ieee754::quiet_nan;
} else {
data.value = other.data.value;
}
}
nan_boxed(nan_boxed&& other) {
if(other.data.bits.flag) {
value_type dst, src;
src = other.data.bits.payload;
new (&dst) ref_any( std::move(reinterpret_cast<ref_any&>(src)));
other.data.bits.payload = src;
data.bits.flag = true;
data.bits.nan = ieee754::quiet_nan;
data.bits.tag = other.data.bits.tag;
data.bits.payload = dst;
} else {
data.value = other.data.value;
}
}
nan_boxed(const ref_any& other, const tag_type& tag) {
value_type value;
new (&value) ref_any(other);
data.bits.flag = true;
data.bits.nan = ieee754::quiet_nan;
data.bits.payload = value;
}
nan_boxed(ref_any&& other, const tag_type& tag) {
value_type value;
new (&value) ref_any(std::move(other));
data.bits.flag = true;
data.bits.nan = ieee754::quiet_nan;
data.bits.tag = tag;
data.bits.payload = value;
}
nan_boxed(const double& other) {
data.value = other;
}
nan_boxed(const value_type& value, const tag_type& tag) {
data.bits.flag = false;
data.bits.nan = ieee754::quiet_nan;
data.bits.tag = tag;
data.bits.payload = value;
}
// convenience
template<class T>
T value_cast() const {
static_assert(sizeof(T) <= sizeof(value_type), "type too large");
static_assert(!std::is_same<T, double>::value, "cannot value_cast as double");
if(data.bits.nan == ieee754::quiet_nan && !data.bits.flag) {
value_type tmp = data.bits.payload;
return reinterpret_cast<T&>(tmp);
} else {
throw std::bad_cast();
}
}
};
int main(int, char**) {
nan_boxed x = {make_ref<test>().any(), 0};
nan_boxed y = std::move(x);
// x = 1.0;
std::clog << "finished" << std::endl;
return 0;
}