forked from pocoproject/poco
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnits.h
More file actions
1300 lines (1103 loc) · 32.2 KB
/
Units.h
File metadata and controls
1300 lines (1103 loc) · 32.2 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
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
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Units.h
//
// $Id: //poco/1.4/Util/include/Poco/Util/Units.h#1 $
//
// Library: Util
// Package: Units
// Module: Units
//
// Definitions for the C++ Units library.
//
// Copyright (c) 2007-2010, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
// Adapted for POCO from the following source:
//
// C++ Units by Calum Grant
//
// Written by Calum Grant
// Copyright (C) Calum Grant 2007
//
// Home page: http://calumgrant.net/units
// File location: http://calumgrant.net/units/units.hpp
// Manual: http://calumgrant.net/units/units.html
//
// Copying permitted under the terms of the Boost software license.
//
#ifndef Util_Units_INCLUDED
#define Util_Units_INCLUDED
#include "Poco/Util/Util.h"
#include <cmath>
namespace Poco {
namespace Util {
namespace Units {
namespace Internal
{
template <typename T1, typename T2> struct Convert;
struct None;
template <int Num, int Den, int Div=Num/Den, int Mod=Num%Den>
struct FixedPower;
}
template <typename Unit1, typename Unit2>
struct Compose;
/// Construct a unit equivalent to Unit1*Unit2
template <typename U, int Num, int Den = 1>
struct Scale;
/// Constructs a unit equivalent to U*Num/Den
template <typename U, int Num, int Den = 1>
struct Translate;
/// Constructs a Unit equivalent to U+Num/Den
template <typename U, int Num, int Den = 1>
struct Power;
/// Constructs a Unit equivalent to U^(Num/Den)
typedef Power<Internal::None, 0> Unit;
/// A unit which is effectively no units at all.
template <typename V, typename U>
class Value
/// A Value with a unit.
/// - V is the type you are storing
/// - U is the unit of the Value
///
/// This class is usually not used directly;
/// client code should use the typedef'd
/// instantiations defined in the Values and
/// Constants namespace.
///
/// Example:
///
/// using namespace Poco::Util::Units::Values;
///
/// std::cout << "One mile is " << km(mile(1)) << std::endl;
/// // Output: One mile is 1.60934 km
///
/// std::cout << "Flow rate is " << m3(mile(1)*inch(80)*foot(9))/s(minute(5));
/// // Output: Flow rate is 29.9026 (m)^3.(s)^-1
///
/// hours h;
/// h = cm(3); // Compile-time error: incompatible units
/// h = 4; // Compile-time error: 4 of what?
/// h = days(4); // Ok: h is 96 hours
{
public:
typedef V ValueType;
typedef U Unit;
Value(): _rep()
{
}
explicit Value(const ValueType& v): _rep(v)
{
}
template <typename OV, typename OU>
Value(const Value<OV, OU>& v):
_rep(Internal::Convert<OU, U>::fn(v.get()))
{
}
const ValueType& get() const
{
return _rep;
}
template <typename OV, typename OU>
Value& operator = (const Value<OV, OU>& other)
{
_rep = Value(other).get();
return *this;
}
template <typename OV, typename OU>
Value operator + (const Value<OV, OU>& other) const
{
return Value(get() + Value(other).get());
}
template <typename OV, typename OU>
Value& operator += (const Value<OV, OU>& other)
{
_rep += Value(other).get();
return *this;
}
template <typename OV, typename OU>
Value& operator -= (const Value<OV, OU>& other)
{
_rep -= Value(other).get();
return *this;
}
template <typename OV, typename OU>
Value operator - (const Value<OV, OU>& other) const
{
return Value(get() - Value(other).get());
}
Value operator - () const
{
return Value(-get());
}
template <typename OV, typename OU>
Value<V, Compose<U, OU> >
operator * (const Value<OV, OU>& other) const
{
return Value<V, Compose<U, OU> >(get() * other.get());
}
Value operator * (const ValueType& v) const
{
return Value(get() * v);
}
Value& operator *= (const ValueType& v)
{
_rep *= v;
return *this;
}
template <typename OV, typename OU>
Value<V, Compose<U, Power<OU, -1> > > operator / (const Value<OV, OU>& other) const
{
return Value<V, Compose<U, Power<OU, -1> > >(get() / other.get());
}
Value operator / (const ValueType& v) const
{
return Value(get() / v);
}
Value& operator /= (const ValueType& v)
{
_rep /= v;
return *this;
}
template <typename OV, typename OU>
bool operator == (const Value<OV, OU>& other) const
{
return get() == Value(other).get();
}
template <typename OV, typename OU>
bool operator != (const Value<OV, OU>& other) const
{
return get() != Value(other).get();
}
template <typename OV, typename OU>
bool operator < (const Value<OV, OU>& other) const
{
return get() < Value(other).get();
}
template <typename OV, typename OU>
bool operator <= (const Value<OV, OU>& other) const
{
return get() <= Value(other).get();
}
template <typename OV, typename OU>
bool operator > (const Value<OV, OU>& other) const
{
return get() > Value(other).get();
}
template <typename OV, typename OU>
bool operator >= (const Value<OV, OU>& other) const
{
return get() >= Value(other).get();
}
Value& operator ++ ()
{
++_rep;
return *this;
}
Value operator ++ (int)
{
Value v = *this;
++_rep;
return v;
}
Value& operator -- ()
{
--_rep;
return *this;
}
Value operator -- (int)
{
Value v = *this;
--_rep;
return v;
}
private:
ValueType _rep;
};
template <typename V, typename U>
Value<V, Power<U, -1> > operator / (const V& a, const Value<V, U>& b)
{
return Value<V, Power<U, -1> >(a / b.get());
}
template <typename V, typename U>
Value<V, U > operator * (const V& a, const Value<V, U>& b)
{
return Value<V, U>(a * b.get());
}
template <typename V, typename U>
Value<V, Power<U, 1, 2> > sqrt(const Value<V, U>& a)
{
return Value<V, Power<U, 1, 2> >(std::sqrt(a.get()));
}
template <typename V, typename U>
Value<V, Power<U, 2, 1> > square(const Value<V, U>& a)
{
return Value<V, Power<U, 2, 1> >(std::pow(a.get(), 2));
}
template <typename V, typename U>
Value<V, Power<U, 3, 1> > cube(const Value<V, U>& a)
{
return Value<V, Power<U, 3, 1> >(std::pow(a.get(), 3));
}
template <int Num, int Den, typename V, typename U>
Value<V, Power<U, Num, Den> > raise(const Value<V, U>& a)
{
return Value<V, Power<U, Num, Den> >(Internal::FixedPower<Num, Den>::Power(a.get()));
}
namespace Internal
{
template <typename T1, typename T2>
struct Convertible;
template <typename U>
struct ScalingFactor;
template <typename T1, typename T2>
struct Convert3
/// Converts T1 to T2.
/// Stage 3 - performed after Stage 1 and Stage 2.
/// The reason we perform Convert in stages is so that the compiler
/// can resolve templates in the order we want it to.
{
template <typename V>
static V fn(const V& v)
/// The default implementation assumes that the two quantities are in compatible
/// Units up to some scaling factor. Find the scaling factor and apply it.
{
return v * ScalingFactor<T2>::template fn<V>() / ScalingFactor<T1>::template fn<V>();
}
};
template <typename T1, typename T2>
struct Convert2
/// Converts T1 to T2.
/// Template matches the first argument (T1),
/// this is the fall-through to Convert3.
{
template <typename V>
static V fn(const V& v)
{
return Convert3<T1,T2>::fn(v);
}
};
template <typename T1, typename T2>
struct Convert
/// Converts T1 to T2.
/// If you really want to implement your own conversion routine,
/// specialize this template.
/// The default implementation falls through to Convert2.
{
/// If this fails, then T1 is not Convertible to T2:
poco_static_assert ((Convertible<T1,T2>::Value));
template <typename V>
static V fn(const V& v)
{
return Convert2<T1,T2>::fn(v);
}
};
template <typename T>
struct Convert<T, T>
// Trivial conversion to the same type.
{
template <typename U>
static const U& fn(const U& u) { return u; }
};
template <typename T>
struct Convert3<T, T>
// Convert to same type.
{
template <typename U>
static const U& fn(const U& u) { return u; }
};
template <typename T, typename U, int Num, int Den>
struct Convert2<Scale<T, Num, Den>, U>
// Convert from a scaled Unit.
{
template <typename V>
static V fn(const V& v)
{
return Convert<T, U>::fn((v * Den)/Num);
}
};
template <typename T, typename U, int Num, int Den>
struct Convert3<T, Scale<U, Num, Den> >
// Convert to a scaled Unit.
{
template <typename V>
static V fn(const V& v)
{
return (Convert<T, U>::fn(v) * Num)/ Den;
}
};
template <typename T, typename U, int Num, int Den>
struct Convert2<Translate<T, Num, Den>, U>
// Convert from a translated Unit.
{
template <typename V>
static V fn(const V& v)
{
return Convert<T, U>::fn(v - static_cast<V>(Num) / static_cast<V>(Den));
}
};
template <typename T, typename U, int Num, int Den>
struct Convert3<T, Translate<U, Num, Den> >
// Convert to a translated Unit.
{
template <typename V>
static V fn(const V& v)
{
return Convert<T, U>::fn(v) + static_cast<V>(Num) / static_cast<V>(Den);
}
};
template <typename Term, typename List>
struct CountTerms
/// Count the power to which Unit Term is raised in the Unit List.
/// Returns a rational num/den of the power of term Term in List.
/// The default assumes that Term is not found (num/den=0).
{
static const int num = 0;
static const int den = 1;
};
template <typename Term>
struct CountTerms<Term, Term>
{
static const int num = 1;
static const int den = 1;
};
template <typename Term, typename U, int N, int D>
struct CountTerms<Term, Scale<U, N, D> >
// CountTerms ignores scaling factors - that is taken care of by ScalingFactor.
{
typedef CountTerms<Term, U> result;
static const int num = result::num;
static const int den = result::den;
};
template <typename Term, typename U, int N, int D>
struct CountTerms<Term, Translate<U, N, D> >
// CountTerms ignores translation.
{
typedef CountTerms<Term, U> result;
static const int num = result::num;
static const int den = result::den;
};
template <typename Term, typename T1, typename T2>
struct CountTerms<Term, Compose<T1,T2> >
// Addition of fractions.
{
typedef CountTerms<Term, T1> result1;
typedef CountTerms<Term, T2> result2;
static const int num = result1::num * result2::den + result1::den * result2::num;
static const int den = result1::den * result2::den;
};
template <typename Term, typename U, int N, int D>
struct CountTerms<Term, Power<U, N, D> >
// Multiplication of fractions.
{
typedef CountTerms<Term, U> result;
static const int num = N * result::num;
static const int den = D * result::den;
};
template <typename Term, typename T1, typename T2>
struct CheckTermsEqual
/// Counts the power of the Unit Term in Units T1 and T2.
/// Reports if they are equal, using equality of fractions.
/// Does a depth-first search of the Unit "Term",
/// or counts the terms in the default case.
{
typedef CountTerms<Term, T1> count1;
typedef CountTerms<Term, T2> count2;
static const bool Value =
count1::num * count2::den ==
count1::den * count2::num;
};
template <typename U, int N, int D, typename T1, typename T2>
struct CheckTermsEqual<Power<U, N, D>, T1, T2 >
{
static const bool Value = CheckTermsEqual<U, T1, T2>::Value;
};
template <typename U, int N, int D, typename T1, typename T2>
struct CheckTermsEqual<Scale<U, N, D>, T1, T2 >
{
static const bool Value = CheckTermsEqual<U, T1, T2>::Value;
};
template <typename U, int N, int D, typename T1, typename T2>
struct CheckTermsEqual<Translate<U, N, D>, T1, T2 >
{
static const bool Value = CheckTermsEqual<U, T1, T2>::Value;
};
template <typename T1, typename T2, typename T3, typename T4>
struct CheckTermsEqual<Compose<T1,T2>,T3,T4>
{
static const bool Value =
CheckTermsEqual<T1, T3, T4>::Value &&
CheckTermsEqual<T2, T3, T4>::Value;
};
template <typename T1, typename T2>
struct Convertible
/// Determines whether two types are Convertible.
/// Counts the powers in the LHS and RHS and ensures they are equal.
{
static const bool Value =
CheckTermsEqual<T1,T1,T2>::Value &&
CheckTermsEqual<T2,T1,T2>::Value;
};
template <int Num, int Den, int Div, int Mod>
struct FixedPower
/// A functor that raises a Value to the power Num/Den.
/// The template is specialised for efficiency
/// so that we don't always have to call the std::power function.
{
template <typename T> static T Power(const T& t)
{
return std::pow(t, static_cast<T>(Num)/static_cast<T>(Den));
}
};
template <int N, int D>
struct FixedPower<N, D, 1, 0>
{
template <typename T> static const T& Power(const T& t)
{
return t;
}
};
template <int N, int D>
struct FixedPower<N, D, 2, 0>
{
template <typename T> static T Power(const T& t)
{
return t*t;
}
};
template <int N, int D>
struct FixedPower<N, D, 3, 0>
{
template <typename T> static T Power(const T& t)
{
return t*t*t;
}
};
template <int N, int D>
struct FixedPower<N, D, 4, 0>
{
template <typename T> static const T& Power(const T& t)
{
T u = t*t;
return u*u;
}
};
template <int N, int D>
struct FixedPower<N, D, -1, 0>
{
template <typename T> static T Power(const T& t)
{
return 1/t;
}
};
template <int N, int D>
struct FixedPower<N, D, -2, 0>
{
template <typename T> static T Power(const T& t)
{
return 1/(t*t);
}
};
template <int N, int D>
struct FixedPower<N, D, 0, 0>
{
template <typename T> static T Power(const T& t)
{
return 1;
}
};
template <typename U>
struct ScalingFactor
/// Determine the scaling factor of a Unit in relation to its "base" Units.
/// Default is that U is a primitive Unit and is not scaled.
{
template <typename T>
static T fn() { return 1; }
};
template <typename U1, typename U2>
struct ScalingFactor< Compose<U1, U2> >
{
template <typename T>
static T fn()
{
return
ScalingFactor<U1>::template fn<T>() *
ScalingFactor<U2>::template fn<T>();
}
};
template <typename U, int N, int D>
struct ScalingFactor< Scale<U, N, D> >
{
template <typename T>
static T fn()
{
return
ScalingFactor<U>::template fn<T>() *
static_cast<T>(N) / static_cast<T>(D);
}
};
template <typename U, int N, int D>
struct ScalingFactor< Power<U, N, D> >
{
template <typename T>
static T fn()
{
return FixedPower<N, D>::Power(ScalingFactor<U>::template fn<T>());
}
};
template <typename U, int N, int D>
struct ScalingFactor< Translate<U, N, D> >
{
template <typename T>
static T fn()
{
return ScalingFactor<U>::template fn<T>();
}
};
} // namespace Internal
///
/// Display
///
#define UNIT_DISPLAY_NAME(Unit, string) \
template <> \
struct OutputUnit<Unit> \
{ \
template <typename Stream> \
static void fn(Stream& os) \
{ \
os << string; \
} \
}
namespace Internal
{
template <typename U>
struct OutputUnit2
/// The default Unit formatting mechanism.
{
template <typename Stream>
static void fn(Stream &os)
{
os << "Units";
}
};
}
template <typename U>
struct OutputUnit
/// Functor to write Unit text to stream.
{
template <typename Stream>
static void fn(Stream &os)
{
Internal::OutputUnit2<U>::fn(os);
}
};
UNIT_DISPLAY_NAME(Unit, "1");
namespace Internal
{
template <typename U1, typename U2>
struct OutputUnit2< Compose<U1,U2> >
{
template <typename Stream>
static void fn(Stream &os)
{
OutputUnit<U1>::fn(os);
os << '.';
OutputUnit<U2>::fn(os);
}
};
template <typename U, int Num, int Den>
struct OutputUnit2< Power<U, Num, Den > >
{
template <typename Stream>
static void fn(Stream &os)
{
if(Num!=Den) os << '(';
OutputUnit<U>::fn(os);
if(Num!=Den)
{
os << ')';
os << '^' << Num;
if(Num%Den)
{
os << '/' << Den;
}
}
}
};
template <typename U, int Num, int Den>
struct OutputUnit2< Translate<U, Num, Den > >
{
template <typename Stream>
static void fn(Stream &os)
{
os << '(';
OutputUnit<U>::fn(os);
os << '+' << Num;
if(Den!=1) os << '/' << Den;
os << ')';
}
};
template <typename U, int Num, int Den>
struct OutputUnit2< Scale<U, Num, Den > >
{
template <typename Stream>
static void fn(Stream &os)
{
os << Den;
if(Num != 1)
os << '/' << Num;
os << '.';
OutputUnit<U>::fn(os);
}
};
} // namespace Internal
template <typename Str, typename V, typename U>
Str& operator << (Str& os, const Value<V, U>& value)
{
os << value.get() << ' ';
OutputUnit<U>::fn(os);
return os;
}
///
/// Additional Units
///
namespace Units
{
typedef Poco::Util::Units::Unit Unit;
// SI base Units:
struct m; /// meter
struct kg; /// kilogram
struct s; /// second
struct K; /// Kelvin
struct A; /// Ampere
struct mol; /// mole
struct cd; /// candela
}
UNIT_DISPLAY_NAME(Units::m, "m");
UNIT_DISPLAY_NAME(Units::kg, "kg");
UNIT_DISPLAY_NAME(Units::s, "s");
UNIT_DISPLAY_NAME(Units::K, "K");
UNIT_DISPLAY_NAME(Units::A, "A");
UNIT_DISPLAY_NAME(Units::mol, "mol");
UNIT_DISPLAY_NAME(Units::cd, "cd");
namespace Units
{
// SI derived Units:
typedef Compose<m, Power<m, -1> > rad;
typedef Compose<Power<m, 2>, Power<m, -2> > sr;
typedef Power<s, -1> Hz;
typedef Compose<m, Compose<kg, Power<s, -2> > > N;
typedef Compose<N, Power<m, -2> > Pa;
typedef Compose<N, m> J;
typedef Compose<J, Power<s, -1> > W;
typedef Compose<s, A> C;
typedef Compose<W, Power<A, -1> > V;
typedef Compose<C, Power<V, -1> > F;
typedef Compose<V, Power<A, -1> > Ohm;
typedef Compose<A, Power<V, -1> > S;
typedef Compose<V, s> Wb;
typedef Compose<Wb, Power<m, -2> > T;
typedef Compose<Wb, Power<A, -1> > H;
typedef cd lm;
typedef Compose<lm, Power<m, -2> > lx;
typedef Power<s, -1> Bq;
typedef Compose<J, Power<kg, -1> > Gy;
typedef Gy Sv;
typedef Compose<Power<s, -1>,mol> kat;
}
UNIT_DISPLAY_NAME(Units::rad, "rad");
UNIT_DISPLAY_NAME(Units::sr, "sr");
// UNIT_DISPLAY_NAME(Units::Hz, "Hz"); // Too problematic
UNIT_DISPLAY_NAME(Units::N, "N");
UNIT_DISPLAY_NAME(Units::Pa, "Pa");
UNIT_DISPLAY_NAME(Units::J, "J");
UNIT_DISPLAY_NAME(Units::W, "W");
UNIT_DISPLAY_NAME(Units::C, "C");
UNIT_DISPLAY_NAME(Units::V, "V");
UNIT_DISPLAY_NAME(Units::F, "F");
UNIT_DISPLAY_NAME(Units::Ohm, "Ohm");
UNIT_DISPLAY_NAME(Units::S, "S");
UNIT_DISPLAY_NAME(Units::Wb, "Wb");
UNIT_DISPLAY_NAME(Units::T, "T");
UNIT_DISPLAY_NAME(Units::H, "H");
UNIT_DISPLAY_NAME(Units::lx, "lx");
UNIT_DISPLAY_NAME(Units::Gy, "Gy");
UNIT_DISPLAY_NAME(Units::kat, "kat");
namespace Units
{
// SI prefixes:
template <typename U> struct deca { typedef Scale<U, 1, 10> type; };
template <typename U> struct hecto { typedef Scale<U, 1, 100> type; };
template <typename U> struct kilo { typedef Scale<U, 1, 1000> type; };
template <typename U> struct mega { typedef Scale<typename kilo<U>::type, 1, 1000> type; };
template <typename U> struct giga { typedef Scale<typename mega<U>::type, 1, 1000> type; };
template <typename U> struct tera { typedef Scale<typename giga<U>::type, 1, 1000> type; };
template <typename U> struct peta { typedef Scale<typename tera<U>::type, 1, 1000> type; };
template <typename U> struct exa { typedef Scale<typename peta<U>::type, 1, 1000> type; };
template <typename U> struct zetta { typedef Scale<typename exa<U>::type, 1, 1000> type; };
template <typename U> struct yotta { typedef Scale<typename zetta<U>::type, 1, 1000> type; };
template <typename U> struct deci { typedef Scale<U, 10> type; };
template <typename U> struct centi { typedef Scale<U, 100> type; };
template <typename U> struct milli { typedef Scale<U, 1000> type; };
template <typename U> struct micro { typedef Scale<typename milli<U>::type, 1000> type; };
template <typename U> struct nano { typedef Scale<typename micro<U>::type, 1000> type; };
template <typename U> struct pico { typedef Scale<typename nano<U>::type, 1000> type; };
template <typename U> struct femto { typedef Scale<typename pico<U>::type, 1000> type; };
template <typename U> struct atto { typedef Scale<typename femto<U>::type, 1000> type; };
template <typename U> struct zepto { typedef Scale<typename atto<U>::type, 1000> type; };
template <typename U> struct yocto { typedef Scale<typename zepto<U>::type, 1000> type; };
// Some prefixed SI Units:
typedef centi<m>::type cm;
typedef milli<m>::type mm;
typedef kilo<m>::type km;
typedef milli<kg>::type g;
typedef milli<g>::type mg;
typedef milli<s>::type ms;
class Prefix
/// Parent class for unit prefixes.
/// Use classes inheriting from this class to scale
/// the values.
{
public:
template <typename T>
Prefix(const T& val, double multiplier = 1, const std::string& prefix = ""):
_pHolder(new Holder<T>(val)),
_multiplier(multiplier),
_prefix(prefix)
{
}
double value() const
{
return _pHolder->get() * _multiplier;
}
void addPrefix(std::ostream& os) const
{
os << _prefix;
}
void addUnit(std::ostream& os) const
{
_pHolder->appendUnit(os);
}
private:
Prefix();
class Placeholder
{
public:
virtual ~Placeholder() { }
virtual double get() const = 0;
virtual void appendUnit(std::ostream& os) const = 0;
};
template <typename U>
struct Holder : public Placeholder
{
typedef Value<typename U::ValueType, typename U::Unit> ValueType;
Holder (const U& val): _val(ValueType(val))
{
}
double get() const
{
return _val.get();
}
void appendUnit(std::ostream& os) const
{
OutputUnit<typename U::Unit>::fn(os);
}
ValueType _val;
};
Placeholder* _pHolder;
double _multiplier;
std::string _prefix;
};
}
template <typename Str>
Str& streamOp (Str& os, const Units::Prefix& val)
{
os << val.value() << ' ';
val.addPrefix(os);
val.addUnit(os);
return os;
}
template <typename Str>
Str& operator << (Str& os, const Units::Prefix& val)
/// Streaming operator for prefixed values.
{
return streamOp(os, val);
}
UNIT_DISPLAY_NAME(Units::cm, "cm");
UNIT_DISPLAY_NAME(Units::mm, "mm");
UNIT_DISPLAY_NAME(Units::km, "km");
UNIT_DISPLAY_NAME(Units::g, "g");
UNIT_DISPLAY_NAME(Units::mg, "mg");
UNIT_DISPLAY_NAME(Units::ms, "ms");
namespace Units
{
// Non-SI mass
typedef Scale<kg, 22046223, 10000000> lb;
typedef Scale<lb, 16> oz;
typedef Scale<kg, 1, 1000> tonne;
// Non-SI temperature
typedef Translate<K, -27315, 100> Celsius;
typedef Translate<Scale<Celsius, 9, 5>, 32> Fahrenheit;
// Non-SI time
typedef Scale<s, 1, 60> minute;
typedef Scale<minute, 1, 60> hour;
typedef Scale<hour, 1, 24> day;