-
Notifications
You must be signed in to change notification settings - Fork 5
/
fixed_quantity.hpp
741 lines (670 loc) · 22.2 KB
/
fixed_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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
#ifndef QUAN_FIXED_QUANTITY_HPP_INCLUDED
#define QUAN_FIXED_QUANTITY_HPP_INCLUDED
/*
Copyright (c) 2003-2014 Andy Little.
This program 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 http://www.gnu.org/licenses./
*/
/*
fixed_quantity class template definition
*/
#include <quan/config.hpp>
#include <quan/fixed_quantity/fixed_quantity_fwd.hpp>
#include <quan/meta/is_lossless_calculation.hpp>
#include <quan/archetypes/meta/static_unit_concept.hpp>
#include <quan/detail/united_value/united_value.hpp>
#include <quan/meta/is_numeric.hpp>
#include <quan/meta/unit.hpp>
#include <quan/meta/and.hpp>
#include <quan/meta/or.hpp>
#include <quan/meta/not.hpp>
#include <quan/meta/eq.hpp>
#include <quan/where.hpp>
#include <quan/meta/unary_operation.hpp>
#include <quan/meta/is_scalar.hpp>
#include <quan/meta/is_fixed_quantity.hpp>
#include <quan/meta/is_valid_binary_op.hpp>
#ifndef QUAN_AVR_NO_CPP_STDLIB
#include <type_traits>
#else
#include <quan/std/tr1/is_convertible.hpp>
#include <quan/std/tr1/integral_constant.hpp>
#endif
namespace quan {
template<
typename StaticUnit,
typename NumericType
>
class fixed_quantity {
template <
typename StaticUnit_In,
typename NumericType_In
>
friend class fixed_quantity;
public:
typedef StaticUnit unit;
typedef NumericType value_type;
private:
typedef detail::united_value<
typename meta::get_conversion_factor<
unit
>::type,
value_type
> united_value;
united_value m_united_value;
QUAN_CONSTEXPR
united_value
get_united_value()const
{
return this->m_united_value;
}
public:
// maybe add a check for a const value_type
template <typename NumericType_In>
struct is_compatible_value_type : meta::and_<
std::is_convertible<
NumericType_In,
value_type
>,
quan::meta::is_numeric<NumericType_In>
>{};
private:
template <
typename StaticUnit_In,
typename NumericType_In
>
struct m_is_unit_convertible : meta::and_<
meta::dimensionally_equivalent<
StaticUnit_In,
unit
>,
is_compatible_value_type<
NumericType_In
>
>{};
template <
typename StaticUnit_In,
typename NumericType_In
>
struct m_is_convertible : meta::and_<
meta::dimensionally_equivalent<
StaticUnit_In,
unit
>,
meta::and_<
meta::or_<
meta::allow_implicit_unit_conversions<
StaticUnit
>,
meta::is_math_same_conversion_factor<
typename meta::get_conversion_factor<
StaticUnit
>::type,
typename meta::get_conversion_factor<
StaticUnit_In
>::type
>
>,
is_compatible_value_type<
NumericType_In
>
>
>{};
public:
// A dimensionless type is replaced by
// its value_type;
typedef typename meta::if_<
quan ::meta::is_dimensionless<
unit
>,
value_type,
fixed_quantity
>::type type;
QUAN_CONSTEXPR
fixed_quantity():m_united_value{}{}
// value initialisation ctor
template<typename NumericType_In>
explicit
QUAN_CONSTEXPR fixed_quantity(
NumericType_In const& value_in,
typename quan::where_<
is_compatible_value_type<NumericType_In>,
void*
>::type = 0
) : m_united_value(value_in){}
template<
typename StaticUnit_In,
typename NumericType_In
>
QUAN_CONSTEXPR
fixed_quantity(
fixed_quantity<
StaticUnit_In,
NumericType_In
> const & in,
typename quan::where_<
m_is_convertible<
StaticUnit_In,NumericType_In
>,
void*
>::type = 0
) : m_united_value(in.m_united_value){}
template<
typename StaticUnit_In,
typename NumericType_In
>
QUAN_CONSTEXPR
explicit fixed_quantity(
fixed_quantity<
StaticUnit_In,
NumericType_In
> const & in,
typename quan::where_<
meta::and_<
m_is_unit_convertible<
StaticUnit_In,NumericType_In
>,
meta::not_<
m_is_convertible<
StaticUnit_In,NumericType_In
>
>
>,
void*
>::type = 0
) : m_united_value(in.m_united_value){}
// assignment
template<
typename StaticUnit_In,
typename NumericType_In
>
typename quan::where_<
m_is_convertible<
StaticUnit_In,NumericType_In
>,
fixed_quantity&
>::type
operator = (
fixed_quantity<
StaticUnit_In,
NumericType_In
> const & in
)
{
this->m_united_value = in.m_united_value;
return *this;
}
template<
typename StaticUnit_In,
typename NumericType_In
>
typename quan::where_<
m_is_convertible<
StaticUnit_In,NumericType_In
>,
fixed_quantity&
>::type
operator += (
fixed_quantity<
StaticUnit_In,
NumericType_In
> const & in
)
{
this->m_united_value += in.m_united_value;
return *this;
}
template<
typename StaticUnit_In,
typename NumericType_In
>
typename quan::where_<
m_is_convertible<
StaticUnit_In,NumericType_In
>,
fixed_quantity&
>::type
operator -= (
fixed_quantity<
StaticUnit_In,
NumericType_In
> const & in
)
{
this->m_united_value -= in.m_united_value;
return *this;
}
template <typename NumericType_In>
typename quan::where_<
/* is_compatible_value_type<
NumericType_In
>,*/
std::is_convertible<
typename quan::meta::binary_op<
value_type, quan::meta::times,NumericType_In
>::type,
value_type
>,
/* >
quan::meta::and_<
quan::meta::is_valid_binary_op<
value_type, quan::meta::times,NumericType_In
>,*/
fixed_quantity&
>::type
operator *= (NumericType_In const & in)
{
this->m_united_value *= in;
return *this;
}
template <typename NumericType_In>
typename quan::where_<
/*is_compatible_value_type<
NumericType_In
>,*/
std::is_convertible<
typename quan::meta::binary_op<
value_type, quan::meta::divides,NumericType_In
>::type,
value_type
>,
fixed_quantity&
>::type
operator /= (NumericType_In const & in)
{
this->m_united_value /= in;
return *this;
}
QUAN_CONSTEXPR
fixed_quantity
operator +()const
{
return *this;
}
QUAN_CONSTEXPR
fixed_quantity
operator -()const
{
return fixed_quantity ( - this->m_united_value.raw_value());
}
QUAN_CONSTEXPR
bool operator!()const
{
return this->m_united_value.raw_value() == 0;
}
fixed_quantity& operator++()
{
++ this->m_united_value.raw_value_ref();
return *this;
}
fixed_quantity operator++(int)
{
fixed_quantity result = *this;
++ this->m_united_value.raw_value_ref();
return result;
}
fixed_quantity& operator--()
{
--this->m_united_value.raw_value_ref();
return *this;
}
fixed_quantity operator--(int)
{
fixed_quantity result = *this;
--this->m_united_value.raw_value_ref();
return result;
}
QUAN_CONSTEXPR
typename std::remove_const<
NumericType
>::type
numeric_value()const
{
return this->m_united_value.raw_value();
}
template <typename Quantity>
typename quan::where_<
std::is_same<Quantity,fixed_quantity>,
void
>::type
set_numeric_value(NumericType const & v)
{
this->m_united_value.raw_value_ref() = v;
}
template <typename Quantity>
typename quan::where_<
std::is_same<Quantity,fixed_quantity>,
NumericType&
>::type
reference_numeric_value()
{
return this->m_united_value.raw_value_ref() ;
}
template <typename Quantity>
QUAN_CONSTEXPR
typename quan::where_<
std::is_same<Quantity,fixed_quantity>,
NumericType const &
>::type
reference_numeric_value()const
{
return this->m_united_value.raw_value_ref() ;
}
template <typename Quantity>
QUAN_CONSTEXPR
typename quan::where_<
std::is_same<Quantity,fixed_quantity>,
NumericType const &
>::type
const_reference_numeric_value()const
{
return this->m_united_value.raw_value_ref() ;
}
static
QUAN_CONSTEXPR
default_fixed_quantity_unit<unit>
get_unit()
{
return default_fixed_quantity_unit<unit>();
}
}; // fixed_quantity
} // quan
namespace quan { namespace meta {
template<
typename StaticUnit,
typename NumericType
> struct unary_operation<
reciprocal,
quan::fixed_quantity<
StaticUnit,
NumericType
>
>{
typedef fixed_quantity<
typename unary_operation<
reciprocal,
StaticUnit
>::type,
typename unary_operation<
reciprocal,
NumericType
>::type
> type;
};
template <
typename StaticUnit_L,
typename NumericType_L,
typename StaticUnit_R,
typename NumericType_R
> struct dimensionally_equivalent<
quan ::fixed_quantity<
StaticUnit_L,
NumericType_L
>,
quan ::fixed_quantity<
StaticUnit_R,
NumericType_R
>
> : dimensionally_equivalent<
StaticUnit_L,StaticUnit_R
>{};
namespace impl{
template <
typename StaticUnit,
typename NumericType
>
struct is_scalar_impl<
quan ::fixed_quantity<
StaticUnit,
NumericType
>
> : std::true_type{};
}//impl
template <
typename StaticUnit,
typename NumericType
>
struct is_named_quantity<
quan ::fixed_quantity<
StaticUnit,
NumericType
>
> : is_named_quantity<StaticUnit>{};
template <
typename StaticUnitL,
typename NumericTypeL,
typename StaticUnitR,
typename NumericTypeR
>
struct is_same_quantity<
quan ::fixed_quantity<
StaticUnitL,
NumericTypeL
>,
quan ::fixed_quantity<
StaticUnitR,
NumericTypeR
>
> : is_same_quantity<
StaticUnitL,
StaticUnitR
>{};
template <
typename StaticUnit,
typename NumericType
>
struct get_nearest_si<
quan::fixed_quantity<
StaticUnit,
NumericType
>
> {
typedef typename quan::fixed_quantity<
typename get_nearest_si<StaticUnit>::type,
NumericType
>::type type;
};
template <
typename StaticUnit,
typename NumericType
>
struct make_anonymous<
fixed_quantity<
StaticUnit,
NumericType
>
>{
typedef fixed_quantity<
typename make_anonymous<StaticUnit>::type,
NumericType
> type;
};
template <
typename StaticUnitL, typename NumericTypeL,
typename Op,
typename StaticUnitR, typename NumericTypeR
> struct is_lossless_calculation<
quan::fixed_quantity<StaticUnitL,NumericTypeL>,
Op,
quan::fixed_quantity<StaticUnitR,NumericTypeR> ,
typename quan::where_<
quan::meta::or_<
quan::meta::is_additive_operator<Op>,
quan::meta::is_equality_operator<Op>,
quan::meta::is_relational_operator<Op>
>
>::type
> : is_math_same_conversion_factor<
typename get_conversion_factor<StaticUnitL>::type,
typename get_conversion_factor<StaticUnitR>::type
>{};
//// multiply conditions dimensionfull dimensionless
namespace detail{
template <typename StaticUnitL, typename StaticUnitR>
struct is_lossless_dimensioned_mux{
typedef typename quan::meta::get_conversion_factor<StaticUnitL>::type convL;
typedef typename quan::meta::get_conversion_factor<StaticUnitR>::type convR;
typedef typename quan::meta::get_multiplier<convL>::type muxL;
typedef typename quan::meta::get_multiplier<convR>::type muxR;
typedef typename quan::meta::binary_op<
muxL, quan::meta::times,muxR
>::type result_mux;
typedef typename quan::meta::eq_one<result_mux>::type type;
static constexpr bool value = type::value;
};
template <typename StaticUnitL, typename StaticUnitR>
struct is_lossless_dimensionless_mux{
typedef typename quan::meta::get_conversion_factor<StaticUnitL>::type convL;
typedef typename quan::meta::get_conversion_factor<StaticUnitR>::type convR;
typedef typename quan::meta::get_multiplier<convL>::type muxL;
typedef typename quan::meta::get_multiplier<convR>::type muxR;
typedef typename quan::meta::binary_op<
muxL, quan::meta::times,muxR
>::type result_mux;
typedef typename quan::meta::get_exponent<convL>::type expL;
typedef typename quan::meta::get_exponent<convR>::type expR;
typedef typename quan::meta::binary_op<
expL, quan::meta::plus,expR
>::type result_exp;
typedef typename quan::meta::and_<
quan::meta::eq_one<result_mux>,
quan::meta::eq_zero<result_exp>
>::type type;
static constexpr bool value = type::value;
};
template <typename StaticUnitL,typename StaticUnitR>
struct is_lossless_mux : quan::meta::eval_if<
quan::meta::is_dimensionless<
typename quan::meta::binary_op<
StaticUnitL, quan::meta::times, StaticUnitR
>::type
>,
is_lossless_dimensionless_mux<StaticUnitL,StaticUnitR>,
is_lossless_dimensioned_mux<StaticUnitL,StaticUnitR>
>{};
}//detail
template <
typename StaticUnitL, typename NumericTypeL,
typename StaticUnitR, typename NumericTypeR
> struct is_lossless_calculation<
quan::fixed_quantity<StaticUnitL,NumericTypeL>,
quan::meta::times,
quan::fixed_quantity<StaticUnitR,NumericTypeR>
> : detail::is_lossless_mux<StaticUnitL,StaticUnitR>{};
template <
typename StaticUnit, typename NumericType,
typename NumericTypeR
> struct is_lossless_calculation<
quan::fixed_quantity<StaticUnit,NumericType>,
quan::meta::times,
NumericTypeR,
typename quan::where_<
quan::meta::is_numeric<NumericTypeR>
>::type
> : std::true_type{};
template <
typename NumericTypeL,
typename StaticUnit, typename NumericType
> struct is_lossless_calculation<
NumericTypeL,
quan::meta::times,
quan::fixed_quantity<StaticUnit,NumericType>,
typename quan::where_<
quan::meta::is_numeric<NumericTypeL>
>::type
> : std::true_type{};
template<
typename StaticUnit, typename NumericType,
typename NumericTypeR
> struct is_lossless_calculation<
quan::fixed_quantity<StaticUnit,NumericType>,
quan::meta::divides,
NumericTypeR,
typename quan::where_<
quan::meta::is_numeric<NumericTypeR>
>::type
> : std::true_type{};
namespace detail{
template <typename StaticUnit>
struct is_lossless_divide_into_numeric{
typedef typename quan::meta::get_conversion_factor<
StaticUnit
>::type conv_factor;
typedef typename quan::meta::get_multiplier<conv_factor>::type mux;
typedef typename quan::meta::eq_one<mux>::type type;
static constexpr bool value = type::value;
};
}
template <
typename NumericTypeL,
typename StaticUnit, typename NumericType
> struct is_lossless_calculation<
NumericTypeL,
quan::meta::divides,
quan::fixed_quantity<StaticUnit,NumericType>,
typename quan::where_<
quan::meta::is_numeric<NumericTypeL>
>::type
> : detail::is_lossless_divide_into_numeric<StaticUnit>{};
namespace detail{
template <typename StaticUnitL, typename StaticUnitR>
struct is_lossless_dimensioned_divide{
typedef typename quan::meta::get_conversion_factor<StaticUnitL>::type convL;
typedef typename quan::meta::get_conversion_factor<StaticUnitR>::type convR;
typedef typename quan::meta::get_multiplier<convL>::type muxL;
typedef typename quan::meta::get_multiplier<convR>::type muxR;
typedef typename quan::meta::binary_op<
muxL, quan::meta::divides,muxR
>::type result_mux;
typedef typename quan::meta::eq_one<result_mux>::type type;
static constexpr bool value = type::value;
};
template <typename StaticUnitL, typename StaticUnitR>
struct is_lossless_dimensionless_divide{
typedef typename quan::meta::get_conversion_factor<StaticUnitL>::type convL;
typedef typename quan::meta::get_conversion_factor<StaticUnitR>::type convR;
typedef typename quan::meta::get_multiplier<convL>::type muxL;
typedef typename quan::meta::get_multiplier<convR>::type muxR;
typedef typename quan::meta::binary_op<
muxL, quan::meta::divides,muxR
>::type result_mux;
typedef typename quan::meta::get_exponent<convL>::type expL;
typedef typename quan::meta::get_exponent<convR>::type expR;
typedef typename quan::meta::binary_op<
expL, quan::meta::minus,expR
>::type result_exp;
typedef typename quan::meta::and_<
quan::meta::eq_one<result_mux>,
quan::meta::eq_zero<result_exp>
>::type type;
static constexpr bool value = type::value;
};
template <typename StaticUnitL,typename StaticUnitR>
struct is_lossless_divide : quan::meta::eval_if<
quan::meta::is_dimensionless<
typename quan::meta::binary_op<
StaticUnitL, quan::meta::divides, StaticUnitR
>::type
>,
is_lossless_dimensionless_divide<StaticUnitL,StaticUnitR>,
is_lossless_dimensioned_divide<StaticUnitL,StaticUnitR>
>{};
}//detail
template <
typename StaticUnitL, typename NumericTypeL,
typename StaticUnitR, typename NumericTypeR
> struct is_lossless_calculation<
quan::fixed_quantity<StaticUnitL,NumericTypeL>,
quan::meta::divides,
quan::fixed_quantity<StaticUnitR,NumericTypeR>
> : quan::meta::detail::is_lossless_divide<StaticUnitL,StaticUnitR>{};
} } // quan::meta
#endif