-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdimension.h
More file actions
266 lines (240 loc) · 8.27 KB
/
dimension.h
File metadata and controls
266 lines (240 loc) · 8.27 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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
@copyright Russell Standish 2019
@author Russell Standish
This file is part of Civita.
Civita is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Civita is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Civita. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef CIVITA_DIMENSION_H
#define CIVITA_DIMENSION_H
#include <boost/date_time.hpp>
#include <string>
#include <map>
#include <stdexcept>
namespace civita
{
struct Dimension
{
enum Type {string, time, value};
Type type=string;
std::string units; // for values, or parser string for time conversion
Dimension() {}
Dimension(Type t,const std::string& s): type(t), units(s) {}
bool operator!=(const Dimension& dim) const {return dim.type!=type || dim.units!=units;}
};
struct NamedDimension
{
std::string name;
Dimension dimension;
NamedDimension(const std::string& name={}, const Dimension& dimension={}): name(name), dimension(dimension) {}
};
template <class T> Dimension::Type dimensionTypeOf();
template <> inline Dimension::Type dimensionTypeOf<std::string>() {return Dimension::string;}
template <> inline Dimension::Type dimensionTypeOf<boost::posix_time::ptime>() {return Dimension::time;}
template <> inline Dimension::Type dimensionTypeOf<double>() {return Dimension::value;}
/// a variant type representing a value of a dimension
// TODO - when we move to c++17, consider using std::variant
struct any
{
Dimension::Type type=Dimension::string;
boost::posix_time::ptime time;
double value=0;
std::string string;
size_t hash() const;
any()=default;
any(Dimension::Type type): type(type) {}
any(const boost::posix_time::ptime& x): type(Dimension::time), time(x) {}
any(const std::string& x): type(Dimension::string), string(x) {}
any(const char* x): type(Dimension::string), string(x) {}
// compilers get confused between char* and ints, which is why we need this templated overload
template <class T>
any(T x, typename std::enable_if<std::is_integral<T>::value, int>::type dummy=0):
type(Dimension::value), value(x) {}
any(double x): type(Dimension::value), value(x) {}
template <class T> any& operator=(const T&x) {return *this=any(x);}
/// true if this is a default constructed object
bool empty() const {return type==Dimension::string && string.empty();}
};
inline size_t any::hash() const {
switch (type) {
case Dimension::string: return std::hash<std::string>()(string);
case Dimension::time: return std::hash<size_t>()((time-boost::posix_time::ptime()).ticks());
case Dimension::value: return std::hash<double>()(value);
}
assert(false);
return 0;
}
inline bool operator<(const any& x, const any& y) {
if (x.type==y.type)
switch (x.type) {
case Dimension::string: return x.string<y.string;
case Dimension::time: return x.time<y.time;
case Dimension::value: return x.value<y.value;
}
return x.type<y.type;
}
inline bool operator<=(const any& x, const any& y) {
if (x.type==y.type)
switch (x.type) {
case Dimension::string: return x.string<=y.string;
case Dimension::time: return x.time<=y.time;
case Dimension::value: return x.value<=y.value;
}
return x.type<y.type;
}
inline bool operator>(const any& x, const any& y) {return !(x<=y);}
inline bool operator>=(const any& x, const any& y) {return !(x<y);}
inline bool operator==(const any& x, const any& y) {
if (x.type!=y.type) return false;
switch (x.type) {
case Dimension::string: return x.string==y.string;
// peculiar syntax to work around compiler bug in implementing C++20 ambiguity rules
case Dimension::time: return x.time.operator==(y.time);
case Dimension::value: return x.value==y.value;
}
assert(false);
return false; // should never be here
}
inline bool operator!=(const any& x, const any& y) {
return !(x==y);
}
/// interpolate betwwen x and y with fraction a (between 0 & 1)
/// if x&y are different types or are strings, return x
inline any interpolate(const any& x, const any& y, double a)
{
if (x.type!=y.type) return x;
switch (x.type)
{
case Dimension::string: return a<=0.5? x: y;
case Dimension::value: return y.value*a+x.value*(1-a);
case Dimension::time: return x.time + (y.time-x.time)*a;
}
assert(false);
return {}; // unreachable code to satisfy CodeQL
}
#ifdef CLASSDESC_STRINGKEYMAP_H
using classdesc::StringKeyMap;
#else
template <class T> using StringKeyMap=std::map<std::string, T>;
#endif
typedef StringKeyMap<Dimension> Dimensions;
typedef std::map<std::string, double> ConversionsMap;
struct Conversions: public ConversionsMap
{
double convert(double val, const std::string& from, const std::string& to)
{
if (from==to) return val;
auto i=find(from+":"+to);
if (i!=end())
return i->second*val;
i=find(to+":"+from);
if (i!=end())
return val/i->second;
throw std::runtime_error("inconvertible types "+from+" and "+to);
}
Conversions& operator=(const ConversionsMap& x)
{ConversionsMap::operator=(x); return *this;}
};
/// \a format - can be any format string suitable for a
/// boost::date_time time_facet. eg "%Y-%m-%d %H:%M:%S"
std::string str(const any&, const std::string& format="");
inline std::ostream& operator<<(std::ostream& o, const any& x)
{return o<<str(x);}
}
#ifdef CLASSDESC
#pragma omit pack civita::any
#pragma omit unpack civita::any
#pragma omit json_pack civita::any
#pragma omit json_unpack civita::any
#pragma omit RESTProcess civita::any
#include <json_pack_base.h>
#include <pack_base.h>
#include <random_init_base.h>
namespace classdesc_access
{
#if defined(CLASSDESC_JSON_PACK_BASE_H) || defined(JSON_PACK_BASE_H)
template <>
struct access_json_pack<civita::any>
{
inline void operator()(classdesc::json_pack_t& j, const std::string&, const civita::any& x)
{j<<civita::str(x);}
};
template <>
struct access_json_unpack<civita::any>: public classdesc::NullDescriptor<classdesc::json_pack_t> {};
#endif
template <>
struct access_pack<civita::any>
{
inline void operator()(classdesc::pack_t& b, const std::string&, const civita::any& x)
{
b<<static_cast<int>(x.type);
switch (x.type)
{
case civita::Dimension::string:
b<<x.string;
break;
case civita::Dimension::value:
b<<x.value;
break;
case civita::Dimension::time:
b<<x.time;
break;
}
}
};
template <>
struct access_unpack<civita::any> {
inline void operator()(classdesc::pack_t& b, const std::string&, civita::any& x)
{
int type;
b>>type;
x.type=static_cast<civita::Dimension::Type>(type);
switch(x.type)
{
case civita::Dimension::string:
b>>x.string;
break;
case civita::Dimension::value:
b>>x.value;
break;
case civita::Dimension::time:
b>>x.time;
break;
}
}
};
template <>
struct access_pack<boost::posix_time::ptime> {
template <class T>
void operator()(classdesc::pack_t& b,const std::string&,T& x)
{
auto tm=to_tm(x);
b.packraw(reinterpret_cast<char*>(&tm),sizeof(tm));
}
};
template <>
struct access_unpack<boost::posix_time::ptime> {
template <class T>
void operator()(classdesc::pack_t& b,const std::string&,T& x)
{
struct tm tm;
b.unpackraw(reinterpret_cast<char*>(&tm),sizeof(tm));
x=boost::posix_time::ptime_from_tm(tm);
}
};
template <>
struct access_random_init<boost::posix_time::ptime> {
void operator()(classdesc::random_init_t& r, const std::string&, boost::posix_time::ptime& x)
{x=boost::posix_time::from_time_t(time_t(r.rand()*3600.0*24*365*8029));}
};
}
#endif
#endif