-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuantity.hpp
360 lines (252 loc) · 9.3 KB
/
Quantity.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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
//This program (i.e. the unit_lite library) 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.
//This program 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 this program. If not, see the two following links
//https://www.gnu.org/licenses/
//https://www.gnu.org/licenses/lgpl-3.0.txt
//This program was written by Pierre Blavy.
//Documentation is available on
//https://tentacule.be/fossil/cpp-msmodels/wiki?name=unit_lite
#ifndef SRC_LIB_UNIT_LITE_QUANTITY_HPP_
#define SRC_LIB_UNIT_LITE_QUANTITY_HPP_
#include "same_unit.hpp"
#include "Compose_unit.hpp"
#include "impl/tuple_cat.hpp"
#include <ostream>
namespace unit_lite{
//A Quantity holds a value
//A Quantity holds a typedef on the unit type of value
// this typedef is a kind of Compose_unit
template<typename Unit_t,typename Value_t>
struct Quantity;
}
namespace unit_lite{
//user may specialize to multiply his value type by a std::ratio
template< typename Ratio, typename V> struct multiply_by_ratio_t;
template<typename V, std::intmax_t N, std::intmax_t D>
struct multiply_by_ratio_t< std::ratio<N,D>,V >{
typedef typename std::ratio<N,D>::type ratio_t;
static V run(const V& source){
return (source * static_cast<V>(ratio_t::num))/static_cast<V>(ratio_t::den);
}
};
template<typename R,typename V>
inline V multiply_by_ratio(const V&v){return multiply_by_ratio_t<R,V>::run(v);}
}
namespace unit_lite{
namespace impl{
//struct XXX_DEBUG{};
template<typename Ratio>
struct ratio_is_natural{
static constexpr bool value=Ratio::type::den == 1;
};
//Ratio pow
template<typename Ratio_p, size_t I,bool positive>
struct Ratio_pow_r{
typedef typename std::ratio_multiply<
typename Ratio_pow_r<Ratio_p,I-1,true>::type,
Ratio_p>::type type;
};
template<typename Ratio>
struct Ratio_pow_r<Ratio,0,true>{
typedef std::ratio<1,1> type;
};
template<typename Ratio>
struct Ratio_pow_r<Ratio,0,false>{
typedef std::ratio<1,1> type;
};
template<typename Ratio_p, size_t I>
struct Ratio_pow_r<Ratio_p,I,false>{
typedef typename std::ratio_divide<
typename Ratio_pow_r<Ratio_p,I-1,false>::type,
Ratio_p
>::type type;
};
template<int I, bool> struct abs_r {static constexpr int value=I;};
template<int I> struct abs_r<I,false>{static constexpr int value= -I;};
template<int I> struct abs {static constexpr int value= abs_r<I, (I>0) >::value;};
template<typename Ratio, int I>
struct ratio_pow{
typedef typename Ratio_pow_r<Ratio,abs<I>::value, (I>0) >::type type;
};
template<size_t I, typename T> struct multiple_base_one;
template<size_t I, typename T, typename R >
struct multiple_base_one<I, Compose_unit<T,R> >{
typedef Compose_unit<T,R> CU;
static_assert( I<CU::size,"logical error : bad index in multiple_base_one ");
typedef typename CU::template tag<I> tag;
typedef typename tag::base_tag base_tag;
typedef typename tag::factor base_factor;
typedef typename CU::template ratio<I> cu_ratio;
//factor = base_factor^cu_ratio;
//e.g. mm^2, factor = (1/1000)^2
static_assert(ratio_is_natural<cu_ratio>::value,"cannot convert a unit with a non natural (i.e. x/1) power (e.g., m^2 is OK but m^(1/2) is not ).");
static constexpr std::intmax_t p = cu_ratio::type::num;
typedef typename ratio_pow<base_factor,p>::type factor;
};
template<size_t I, typename CU>
struct multiple_base_r;
template<size_t I, typename T, typename R>
struct multiple_base_r< I, Compose_unit<T,R> >{
typedef Compose_unit<T,R> CU;
typedef multiple_base_one<I-1,CU> MBO;
//factor is the product of factors
typedef typename std::ratio_multiply<
typename multiple_base_r<I-1,CU>::factor,
typename MBO::factor
>::type factor;
//unit tag the concatenation of base type
typedef typename impl::tuple_cat_arg<
typename multiple_base_r<I-1,CU>::Tag_t,
typename MBO::base_tag
>::type Tag_t;
//Value and ratio doesn't change
typedef Compose_unit<Tag_t,R> unit_t;
};
template<typename T, typename R>
struct multiple_base_r< 0, Compose_unit<T,R> >{
typedef std::ratio<1,1> factor;
typedef std::tuple<> Tag_t;
};
template<>
struct multiple_base_r< 0, Compose_unit<std::tuple<>,std::tuple<>> >{
typedef std::ratio<1,1> factor;
typedef std::tuple<> Tag_t;
typedef Compose_unit<std::tuple<>,std::tuple<>> unit_t;
};
template<typename T> struct multiple_base;
template<typename T, typename R>
struct multiple_base<Compose_unit<T,R> >{
private:
typedef Compose_unit<T,R> CU;
typedef multiple_base_r<CU::size,CU> MBR;
public:
typedef typename MBR::factor factor;
typedef typename MBR::unit_t unit_t;
};
}
}
namespace unit_lite{
template<typename T, typename R, typename V>
struct Quantity<Compose_unit<T,R>,V>{
public:
typedef Quantity<Compose_unit<T,R>,V> this_t;
typedef Compose_unit<T,R> unit_t;
typedef V value_t;
#include <unit_lite/impl/Quantity_common.inl>
static constexpr bool is_dimensionless=false;
//Convert to an other quantity
//This conversion require a dimension
template<typename T2,typename R2, typename V2>
void to(Quantity<Compose_unit<T2,R2>,V2> &write_here)const;
template<typename AA>
AA to()const{ AA r; this->to(r);return r; }
};
template<typename T, typename R, typename V>
template<typename T2,typename R2, typename V2>
void Quantity<Compose_unit<T,R>,V>::to(Quantity<Compose_unit<T2,R2>,V2> &write_here)const{
typedef unit_t unit_t1;
typedef Compose_unit<T2,R2> unit_t2;
//No conversion between dimensionless
typedef Compose_unit<std::tuple<>,std::tuple<>> dimensionless;
static_assert(
! unit_lite::same_unit<dimensionless,unit_t1>::value,
"cannot convert (x.to) because the source class is dimensionless."
);
typedef Compose_unit<std::tuple<>,std::tuple<>> dimensionless;
static_assert(
! unit_lite::same_unit<dimensionless,unit_t2>::value,
"cannot convert (x.to) because the target class is dimensionless."
);
//[1] We want to convert mm/min into km/s
// First we convert to base with multiple_base
// mm/min --> r1=(1/1000)^1 * (60/1)^-1 , m/s
// km/s --> r2=(1000/1)^1 * (1/1 )^-1 , m/s
typedef typename impl::multiple_base<unit_t1> UR1;
typedef typename impl::multiple_base<unit_t2> UR2;
//[2] Then we assert that multiple_base have the same units (m/s)
static_assert(
unit_lite::same_unit<
typename UR1::unit_t,
typename UR2::unit_t>::value,
"cannot convert (.to) between heterogeneous units."
);
//[3] Then we use their ratio to generate the conversion
//new_value = old_value * (r2/r1)
typedef typename std::ratio_divide<
typename UR2::factor ,
typename UR1::factor
>::type factor;
write_here.value = unit_lite::multiply_by_ratio<factor>(this->value);
}
//dimensionless idem + auto cast to V
template<typename V>
struct Quantity<Compose_unit<std::tuple<>,std::tuple<>>,V>{
typedef Quantity<Compose_unit<std::tuple<>,std::tuple<>>,V> this_t;
typedef Compose_unit<std::tuple<>,std::tuple<> > unit_t;
typedef V value_t;
#include <unit_lite/impl/Quantity_common.inl>
operator V&() {return value;}
operator const V&()const{return value;}
static constexpr bool is_dimensionless=true;
private:
template<typename... A>
struct false_t{static constexpr bool value=false;};
public:
//Cannot convert between dimensionless units
template<typename T2,typename R2, typename V2>
void to(Quantity<Compose_unit<T2,R2>,V2> &write_here)const{
static_assert(
false_t<decltype(write_here)>::value,
"cannot convert (source.to) because the source class is dimensionless."
);
}
template<typename AA>
AA to()const{ AA r; this->to(r);return r; }
/*
template<typename X, typename Y>
Quantity(const Quantity<X,Y> &q){
typedef typename Quantity<X,Y>::unit_t unit_q;
static_assert(same_unit<unit_t, unit_q >::value,"cannot assign heterogeneous units" );
value=q.value;
}
template<typename X, typename Y>
this_t & operator=(const Quantity<X,Y> &q){
typedef typename Quantity<X,Y>::unit_t unit_q;
static_assert(same_unit<unit_t, unit_q >::value,"cannot assign heterogeneous units" );
value=q.value;
return *this;
}*/
};
template<typename V>
using Quantity_dimensionless=Quantity<Compose_unit<std::tuple<>,std::tuple<>>,V>;
}//end namespace unit_lite
namespace unit_lite{
template <typename T> struct print_compose_unit;
}
template<typename T, typename U>
std::ostream & operator<<(std::ostream & out, const unit_lite::Quantity<T,U> &q){
typedef unit_lite::Quantity<T,U> quantity_t;
out << q.value <<" "
<< unit_lite::print_compose_unit<typename quantity_t::unit_t>::str();
return out;
}
//to string
namespace std{
template<typename T, typename U>
std::string to_string(const ::unit_lite::Quantity<T,U> &q){
typedef unit_lite::Quantity<T,U> quantity_t;
std::string r = std::to_string(q.value);
r += " ";
r += unit_lite::print_compose_unit<typename quantity_t::unit_t>::str();
return r;
}
}
#endif /* SRC_LIB_UNIT_LITE_QUANTITY_HPP_ */