forked from nholthaus/units
-
Notifications
You must be signed in to change notification settings - Fork 0
/
units.h
4882 lines (4359 loc) · 231 KB
/
units.h
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: A compile-time c++14 unit conversion library with no dependencies
//
//--------------------------------------------------------------------------------------------------
//
// The MIT License (MIT)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software
// and associated documentation files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//--------------------------------------------------------------------------------------------------
//
// Copyright (c) 2016 Nic Holthaus
//
//--------------------------------------------------------------------------------------------------
//
// ATTRIBUTION:
// Parts of this work have been adapted from:
// http://stackoverflow.com/questions/35069778/create-comparison-trait-for-template-classes-whose-parameters-are-in-a-different
// http://stackoverflow.com/questions/28253399/check-traits-for-all-variadic-template-arguments/28253503
// http://stackoverflow.com/questions/36321295/rational-approximation-of-square-root-of-stdratio-at-compile-time?noredirect=1#comment60266601_36321295
//
//--------------------------------------------------------------------------------------------------
//
/// @file units.h
/// @brief Complete implementation of `units` - a compile-time, header-only, unit conversion
/// library built on c++14 with no dependencies.
//
//--------------------------------------------------------------------------------------------------
#pragma once
#ifndef units_h__
#define units_h__
#ifdef _MSC_VER
# pragma push_macro("pascal")
# undef pascal
# if _MSC_VER <= 1800
# define _ALLOW_KEYWORD_MACROS
# pragma warning(push)
# pragma warning(disable : 4520)
# pragma push_macro("constexpr")
# define constexpr /*constexpr*/
# pragma push_macro("noexcept")
# define noexcept throw()
# endif // _MSC_VER < 1800
#endif // _MSC_VER
#if !defined(_MSC_VER) || _MSC_VER > 1800
# define UNIT_HAS_LITERAL_SUPPORT
# define UNIT_HAS_VARIADIC_TEMPLATE_SUPPORT
#endif
#ifndef UNIT_LIB_DEFAULT_TYPE
# define UNIT_LIB_DEFAULT_TYPE double
#endif
//--------------------
// INCLUDES
//--------------------
#include <chrono>
#include <cstddef>
#include <ratio>
#include <type_traits>
#include <cstdint>
#include <cmath>
#include <limits>
#if !defined(UNIT_LIB_DISABLE_IOSTREAM)
#include <iostream>
#include <string>
#include <locale>
//------------------------------
// STRING FORMATTER
//------------------------------
namespace units
{
namespace detail
{
template <typename T> std::string to_string(const T& t)
{
std::string str{ std::to_string(t) };
int offset{ 1 };
// remove trailing decimal points for integer value units. Locale aware!
struct lconv * lc;
lc = localeconv();
char decimalPoint = *lc->decimal_point;
if (str.find_last_not_of('0') == str.find(decimalPoint)) { offset = 0; }
str.erase(str.find_last_not_of('0') + offset, std::string::npos);
return str;
}
}
}
#endif
namespace units
{
template<typename T> inline constexpr const char* name(const T&);
template<typename T> inline constexpr const char* abbreviation(const T&);
}
//------------------------------
// MACROS
//------------------------------
/**
* @def UNIT_ADD_UNIT_TAGS(namespaceName,nameSingular, namePlural, abbreviation, definition)
* @brief Helper macro for generating the boiler-plate code generating the tags of a new unit.
* @details The macro generates singular, plural, and abbreviated forms
* of the unit definition (e.g. `meter`, `meters`, and `m`), as aliases for the
* unit tag.
* @param namespaceName namespace in which the new units will be encapsulated.
* @param nameSingular singular version of the unit name, e.g. 'meter'
* @param namePlural - plural version of the unit name, e.g. 'meters'
* @param abbreviation - abbreviated unit name, e.g. 'm'
* @param definition - the variadic parameter is used for the definition of the unit
* (e.g. `unit<std::ratio<1>, units::category::length_unit>`)
* @note a variadic template is used for the definition to allow templates with
* commas to be easily expanded. All the variadic 'arguments' should together
* comprise the unit definition.
*/
#define UNIT_ADD_UNIT_TAGS(namespaceName,nameSingular, namePlural, abbreviation, /*definition*/...)\
namespace namespaceName\
{\
/** @name Units (full names plural) */ /** @{ */ typedef __VA_ARGS__ namePlural; /** @} */\
/** @name Units (full names singular) */ /** @{ */ typedef namePlural nameSingular; /** @} */\
/** @name Units (abbreviated) */ /** @{ */ typedef namePlural abbreviation; /** @} */\
}
/**
* @def UNIT_ADD_UNIT_DEFINITION(namespaceName,nameSingular)
* @brief Macro for generating the boiler-plate code for the unit_t type definition.
* @details The macro generates the definition of the unit container types, e.g. `meter_t`
* @param namespaceName namespace in which the new units will be encapsulated.
* @param nameSingular singular version of the unit name, e.g. 'meter'
*/
#define UNIT_ADD_UNIT_DEFINITION(namespaceName,nameSingular)\
namespace namespaceName\
{\
/** @name Unit Containers */ /** @{ */ typedef unit_t<nameSingular> nameSingular ## _t; /** @} */\
}
/**
* @def UNIT_ADD_CUSTOM_TYPE_UNIT_DEFINITION(namespaceName,nameSingular,underlyingType)
* @brief Macro for generating the boiler-plate code for a unit_t type definition with a non-default underlying type.
* @details The macro generates the definition of the unit container types, e.g. `meter_t`
* @param namespaceName namespace in which the new units will be encapsulated.
* @param nameSingular singular version of the unit name, e.g. 'meter'
* @param underlyingType the underlying type
*/
#define UNIT_ADD_CUSTOM_TYPE_UNIT_DEFINITION(namespaceName,nameSingular, underlyingType)\
namespace namespaceName\
{\
/** @name Unit Containers */ /** @{ */ typedef unit_t<nameSingular,underlyingType> nameSingular ## _t; /** @} */\
}
/**
* @def UNIT_ADD_IO(namespaceName,nameSingular, abbreviation)
* @brief Macro for generating the boiler-plate code needed for I/O for a new unit.
* @details The macro generates the code to insert units into an ostream. It
* prints both the value and abbreviation of the unit when invoked.
* @param namespaceName namespace in which the new units will be encapsulated.
* @param nameSingular singular version of the unit name, e.g. 'meter'
* @param abbrev - abbreviated unit name, e.g. 'm'
* @note When UNIT_LIB_DISABLE_IOSTREAM is defined, the macro does not generate any code
*/
#if defined(UNIT_LIB_DISABLE_IOSTREAM)
#define UNIT_ADD_IO(namespaceName, nameSingular, abbrev)
#else
#define UNIT_ADD_IO(namespaceName, nameSingular, abbrev)\
namespace namespaceName\
{\
inline std::ostream& operator<<(std::ostream& os, const nameSingular ## _t& obj) \
{\
os << obj() << " "#abbrev; return os; \
}\
inline std::string to_string(const nameSingular ## _t& obj)\
{\
return units::detail::to_string(obj()) + std::string(" "#abbrev);\
}\
}
#endif
/**
* @def UNIT_ADD_NAME(namespaceName,nameSingular,abbreviation)
* @brief Macro for generating constexpr names/abbreviations for units.
* @details The macro generates names for units. E.g. name() of 1_m would be "meter", and
* abbreviation would be "m".
* @param namespaceName namespace in which the new units will be encapsulated. All literal values
* are placed in the `units::literals` namespace.
* @param nameSingular singular version of the unit name, e.g. 'meter'
* @param abbreviation - abbreviated unit name, e.g. 'm'
*/
#define UNIT_ADD_NAME(namespaceName, nameSingular, abbrev)\
template<> inline constexpr const char* name(const namespaceName::nameSingular ## _t&)\
{\
return #nameSingular;\
}\
template<> inline constexpr const char* abbreviation(const namespaceName::nameSingular ## _t&)\
{\
return #abbrev;\
}
/**
* @def UNIT_ADD_LITERALS(namespaceName,nameSingular,abbreviation)
* @brief Macro for generating user-defined literals for units.
* @details The macro generates user-defined literals for units. A literal suffix is created
* using the abbreviation (e.g. `10.0_m`).
* @param namespaceName namespace in which the new units will be encapsulated. All literal values
* are placed in the `units::literals` namespace.
* @param nameSingular singular version of the unit name, e.g. 'meter'
* @param abbreviation - abbreviated unit name, e.g. 'm'
* @note When UNIT_HAS_LITERAL_SUPPORT is not defined, the macro does not generate any code
*/
#if defined(UNIT_HAS_LITERAL_SUPPORT)
#define UNIT_ADD_LITERALS(namespaceName, nameSingular, abbreviation)\
namespace literals\
{\
inline constexpr namespaceName::nameSingular ## _t operator""_ ## abbreviation(long double d)\
{\
return namespaceName::nameSingular ## _t(static_cast<namespaceName::nameSingular ## _t::underlying_type>(d));\
}\
inline constexpr namespaceName::nameSingular ## _t operator""_ ## abbreviation (unsigned long long d)\
{\
return namespaceName::nameSingular ## _t(static_cast<namespaceName::nameSingular ## _t::underlying_type>(d));\
}\
}
#else
#define UNIT_ADD_LITERALS(namespaceName, nameSingular, abbreviation)
#endif
/**
* @def UNIT_ADD(namespaceName,nameSingular, namePlural, abbreviation, definition)
* @brief Macro for generating the boiler-plate code needed for a new unit.
* @details The macro generates singular, plural, and abbreviated forms
* of the unit definition (e.g. `meter`, `meters`, and `m`), as well as the
* appropriately named unit container (e.g. `meter_t`). A literal suffix is created
* using the abbreviation (e.g. `10.0_m`). It also defines a class-specific
* cout function which prints both the value and abbreviation of the unit when invoked.
* @param namespaceName namespace in which the new units will be encapsulated. All literal values
* are placed in the `units::literals` namespace.
* @param nameSingular singular version of the unit name, e.g. 'meter'
* @param namePlural - plural version of the unit name, e.g. 'meters'
* @param abbreviation - abbreviated unit name, e.g. 'm'
* @param definition - the variadic parameter is used for the definition of the unit
* (e.g. `unit<std::ratio<1>, units::category::length_unit>`)
* @note a variadic template is used for the definition to allow templates with
* commas to be easily expanded. All the variadic 'arguments' should together
* comprise the unit definition.
*/
#define UNIT_ADD(namespaceName, nameSingular, namePlural, abbreviation, /*definition*/...)\
UNIT_ADD_UNIT_TAGS(namespaceName,nameSingular, namePlural, abbreviation, __VA_ARGS__)\
UNIT_ADD_UNIT_DEFINITION(namespaceName,nameSingular)\
UNIT_ADD_NAME(namespaceName,nameSingular, abbreviation)\
UNIT_ADD_IO(namespaceName,nameSingular, abbreviation)\
UNIT_ADD_LITERALS(namespaceName,nameSingular, abbreviation)
/**
* @def UNIT_ADD_WITH_CUSTOM_TYPE(namespaceName,nameSingular, namePlural, abbreviation, underlyingType, definition)
* @brief Macro for generating the boiler-plate code needed for a new unit with a non-default underlying type.
* @details The macro generates singular, plural, and abbreviated forms
* of the unit definition (e.g. `meter`, `meters`, and `m`), as well as the
* appropriately named unit container (e.g. `meter_t`). A literal suffix is created
* using the abbreviation (e.g. `10.0_m`). It also defines a class-specific
* cout function which prints both the value and abbreviation of the unit when invoked.
* @param namespaceName namespace in which the new units will be encapsulated. All literal values
* are placed in the `units::literals` namespace.
* @param nameSingular singular version of the unit name, e.g. 'meter'
* @param namePlural - plural version of the unit name, e.g. 'meters'
* @param abbreviation - abbreviated unit name, e.g. 'm'
* @param underlyingType - the underlying type, e.g. 'int' or 'float'
* @param definition - the variadic parameter is used for the definition of the unit
* (e.g. `unit<std::ratio<1>, units::category::length_unit>`)
* @note a variadic template is used for the definition to allow templates with
* commas to be easily expanded. All the variadic 'arguments' should together
* comprise the unit definition.
*/
#define UNIT_ADD_WITH_CUSTOM_TYPE(namespaceName, nameSingular, namePlural, abbreviation, underlyingType, /*definition*/...)\
UNIT_ADD_UNIT_TAGS(namespaceName,nameSingular, namePlural, abbreviation, __VA_ARGS__)\
UNIT_ADD_CUSTOM_TYPE_UNIT_DEFINITION(namespaceName,nameSingular,underlyingType)\
UNIT_ADD_IO(namespaceName,nameSingular, abbreviation)\
UNIT_ADD_LITERALS(namespaceName,nameSingular, abbreviation)
/**
* @def UNIT_ADD_DECIBEL(namespaceName, nameSingular, abbreviation)
* @brief Macro to create decibel container and literals for an existing unit type.
* @details This macro generates the decibel unit container, cout overload, and literal definitions.
* @param namespaceName namespace in which the new units will be encapsulated. All literal values
* are placed in the `units::literals` namespace.
* @param nameSingular singular version of the base unit name, e.g. 'watt'
* @param abbreviation - abbreviated decibel unit name, e.g. 'dBW'
*/
#define UNIT_ADD_DECIBEL(namespaceName, nameSingular, abbreviation)\
namespace namespaceName\
{\
/** @name Unit Containers */ /** @{ */ typedef unit_t<nameSingular, UNIT_LIB_DEFAULT_TYPE, units::decibel_scale> abbreviation ## _t; /** @} */\
}\
UNIT_ADD_IO(namespaceName, abbreviation, abbreviation)\
UNIT_ADD_LITERALS(namespaceName, abbreviation, abbreviation)
/**
* @def UNIT_ADD_CATEGORY_TRAIT(unitCategory, baseUnit)
* @brief Macro to create the `is_category_unit` type trait.
* @details This trait allows users to test whether a given type matches
* an intended category. This macro comprises all the boiler-plate
* code necessary to do so.
* @param unitCategory The name of the category of unit, e.g. length or mass.
*/
#define UNIT_ADD_CATEGORY_TRAIT_DETAIL(unitCategory)\
namespace traits\
{\
/** @cond */\
namespace detail\
{\
template<typename T> struct is_ ## unitCategory ## _unit_impl : std::false_type {};\
template<typename C, typename U, typename P, typename T>\
struct is_ ## unitCategory ## _unit_impl<units::unit<C, U, P, T>> : std::is_same<units::traits::base_unit_of<typename units::traits::unit_traits<units::unit<C, U, P, T>>::base_unit_type>, units::category::unitCategory ## _unit>::type {};\
template<typename U, typename S, template<typename> class N>\
struct is_ ## unitCategory ## _unit_impl<units::unit_t<U, S, N>> : std::is_same<units::traits::base_unit_of<typename units::traits::unit_t_traits<units::unit_t<U, S, N>>::unit_type>, units::category::unitCategory ## _unit>::type {};\
}\
/** @endcond */\
}
#if defined(UNIT_HAS_VARIADIC_TEMPLATE_SUPPORT)
#define UNIT_ADD_IS_UNIT_CATEGORY_TRAIT(unitCategory)\
namespace traits\
{\
template<typename... T> struct is_ ## unitCategory ## _unit : std::integral_constant<bool, units::all_true<units::traits::detail::is_ ## unitCategory ## _unit_impl<std::decay_t<T>>::value...>::value> {};\
}
#else
#define UNIT_ADD_IS_UNIT_CATEGORY_TRAIT(unitCategory)\
namespace traits\
{\
template<typename T1, typename T2 = T1, typename T3 = T1>\
struct is_ ## unitCategory ## _unit : std::integral_constant<bool, units::traits::detail::is_ ## unitCategory ## _unit_impl<typename std::decay<T1>::type>::value &&\
units::traits::detail::is_ ## unitCategory ## _unit_impl<typename std::decay<T2>::type>::value &&\
units::traits::detail::is_ ## unitCategory ## _unit_impl<typename std::decay<T3>::type>::value>{};\
}
#endif
#define UNIT_ADD_CATEGORY_TRAIT(unitCategory)\
UNIT_ADD_CATEGORY_TRAIT_DETAIL(unitCategory)\
/** @ingroup TypeTraits*/\
/** @brief Trait which tests whether a type represents a unit of unitCategory*/\
/** @details Inherits from `std::true_type` or `std::false_type`. Use `is_ ## unitCategory ## _unit<T>::value` to test the unit represents a unitCategory quantity.*/\
/** @tparam T one or more types to test*/\
UNIT_ADD_IS_UNIT_CATEGORY_TRAIT(unitCategory)
/**
* @def UNIT_ADD_WITH_METRIC_PREFIXES(nameSingular, namePlural, abbreviation, definition)
* @brief Macro for generating the boiler-plate code needed for a new unit, including its metric
* prefixes from femto to peta.
* @details See UNIT_ADD. In addition to generating the unit definition and containers '(e.g. `meters` and 'meter_t',
* it also creates corresponding units with metric suffixes such as `millimeters`, and `millimeter_t`), as well as the
* literal suffixes (e.g. `10.0_mm`).
* @param namespaceName namespace in which the new units will be encapsulated. All literal values
* are placed in the `units::literals` namespace.
* @param nameSingular singular version of the unit name, e.g. 'meter'
* @param namePlural - plural version of the unit name, e.g. 'meters'
* @param abbreviation - abbreviated unit name, e.g. 'm'
* @param definition - the variadic parameter is used for the definition of the unit
* (e.g. `unit<std::ratio<1>, units::category::length_unit>`)
* @note a variadic template is used for the definition to allow templates with
* commas to be easily expanded. All the variadic 'arguments' should together
* comprise the unit definition.
*/
#define UNIT_ADD_WITH_METRIC_PREFIXES(namespaceName, nameSingular, namePlural, abbreviation, /*definition*/...)\
UNIT_ADD(namespaceName, nameSingular, namePlural, abbreviation, __VA_ARGS__)\
UNIT_ADD(namespaceName, femto ## nameSingular, femto ## namePlural, f ## abbreviation, femto<namePlural>)\
UNIT_ADD(namespaceName, pico ## nameSingular, pico ## namePlural, p ## abbreviation, pico<namePlural>)\
UNIT_ADD(namespaceName, nano ## nameSingular, nano ## namePlural, n ## abbreviation, nano<namePlural>)\
UNIT_ADD(namespaceName, micro ## nameSingular, micro ## namePlural, u ## abbreviation, micro<namePlural>)\
UNIT_ADD(namespaceName, milli ## nameSingular, milli ## namePlural, m ## abbreviation, milli<namePlural>)\
UNIT_ADD(namespaceName, centi ## nameSingular, centi ## namePlural, c ## abbreviation, centi<namePlural>)\
UNIT_ADD(namespaceName, deci ## nameSingular, deci ## namePlural, d ## abbreviation, deci<namePlural>)\
UNIT_ADD(namespaceName, deca ## nameSingular, deca ## namePlural, da ## abbreviation, deca<namePlural>)\
UNIT_ADD(namespaceName, hecto ## nameSingular, hecto ## namePlural, h ## abbreviation, hecto<namePlural>)\
UNIT_ADD(namespaceName, kilo ## nameSingular, kilo ## namePlural, k ## abbreviation, kilo<namePlural>)\
UNIT_ADD(namespaceName, mega ## nameSingular, mega ## namePlural, M ## abbreviation, mega<namePlural>)\
UNIT_ADD(namespaceName, giga ## nameSingular, giga ## namePlural, G ## abbreviation, giga<namePlural>)\
UNIT_ADD(namespaceName, tera ## nameSingular, tera ## namePlural, T ## abbreviation, tera<namePlural>)\
UNIT_ADD(namespaceName, peta ## nameSingular, peta ## namePlural, P ## abbreviation, peta<namePlural>)\
/**
* @def UNIT_ADD_WITH_METRIC_AND_BINARY_PREFIXES(nameSingular, namePlural, abbreviation, definition)
* @brief Macro for generating the boiler-plate code needed for a new unit, including its metric
* prefixes from femto to peta, and binary prefixes from kibi to exbi.
* @details See UNIT_ADD. In addition to generating the unit definition and containers '(e.g. `bytes` and 'byte_t',
* it also creates corresponding units with metric suffixes such as `millimeters`, and `millimeter_t`), as well as the
* literal suffixes (e.g. `10.0_B`).
* @param namespaceName namespace in which the new units will be encapsulated. All literal values
* are placed in the `units::literals` namespace.
* @param nameSingular singular version of the unit name, e.g. 'byte'
* @param namePlural - plural version of the unit name, e.g. 'bytes'
* @param abbreviation - abbreviated unit name, e.g. 'B'
* @param definition - the variadic parameter is used for the definition of the unit
* (e.g. `unit<std::ratio<1>, units::category::data_unit>`)
* @note a variadic template is used for the definition to allow templates with
* commas to be easily expanded. All the variadic 'arguments' should together
* comprise the unit definition.
*/
#define UNIT_ADD_WITH_METRIC_AND_BINARY_PREFIXES(namespaceName, nameSingular, namePlural, abbreviation, /*definition*/...)\
UNIT_ADD_WITH_METRIC_PREFIXES(namespaceName, nameSingular, namePlural, abbreviation, __VA_ARGS__)\
UNIT_ADD(namespaceName, kibi ## nameSingular, kibi ## namePlural, Ki ## abbreviation, kibi<namePlural>)\
UNIT_ADD(namespaceName, mebi ## nameSingular, mebi ## namePlural, Mi ## abbreviation, mebi<namePlural>)\
UNIT_ADD(namespaceName, gibi ## nameSingular, gibi ## namePlural, Gi ## abbreviation, gibi<namePlural>)\
UNIT_ADD(namespaceName, tebi ## nameSingular, tebi ## namePlural, Ti ## abbreviation, tebi<namePlural>)\
UNIT_ADD(namespaceName, pebi ## nameSingular, pebi ## namePlural, Pi ## abbreviation, pebi<namePlural>)\
UNIT_ADD(namespaceName, exbi ## nameSingular, exbi ## namePlural, Ei ## abbreviation, exbi<namePlural>)
//--------------------
// UNITS NAMESPACE
//--------------------
/**
* @namespace units
* @brief Unit Conversion Library namespace
*/
namespace units
{
//----------------------------------
// DOXYGEN
//----------------------------------
/**
* @defgroup UnitContainers Unit Containers
* @brief Defines a series of classes which contain dimensioned values. Unit containers
* store a value, and support various arithmetic operations.
*/
/**
* @defgroup UnitTypes Unit Types
* @brief Defines a series of classes which represent units. These types are tags used by
* the conversion function, to create compound units, or to create `unit_t` types.
* By themselves, they are not containers and have no stored value.
*/
/**
* @defgroup UnitManipulators Unit Manipulators
* @brief Defines a series of classes used to manipulate unit types, such as `inverse<>`, `squared<>`, and metric prefixes.
* Unit manipulators can be chained together, e.g. `inverse<squared<pico<time::seconds>>>` to
* represent picoseconds^-2.
*/
/**
* @defgroup CompileTimeUnitManipulators Compile-time Unit Manipulators
* @brief Defines a series of classes used to manipulate `unit_value_t` types at compile-time, such as `unit_value_add<>`, `unit_value_sqrt<>`, etc.
* Compile-time manipulators can be chained together, e.g. `unit_value_sqrt<unit_value_add<unit_value_power<a, 2>, unit_value_power<b, 2>>>` to
* represent `c = sqrt(a^2 + b^2).
*/
/**
* @defgroup UnitMath Unit Math
* @brief Defines a collection of unit-enabled, strongly-typed versions of `<cmath>` functions.
* @details Includes most c++11 extensions.
*/
/**
* @defgroup Conversion Explicit Conversion
* @brief Functions used to convert values of one logical type to another.
*/
/**
* @defgroup TypeTraits Type Traits
* @brief Defines a series of classes to obtain unit type information at compile-time.
*/
//------------------------------
// FORWARD DECLARATIONS
//------------------------------
/** @cond */ // DOXYGEN IGNORE
namespace constants
{
namespace detail
{
static constexpr const UNIT_LIB_DEFAULT_TYPE PI_VAL = 3.14159265358979323846264338327950288419716939937510;
}
}
/** @endcond */ // END DOXYGEN IGNORE
//------------------------------
// RATIO TRAITS
//------------------------------
/**
* @ingroup TypeTraits
* @{
*/
/** @cond */ // DOXYGEN IGNORE
namespace detail
{
/// has_num implementation.
template<class T>
struct has_num_impl
{
template<class U>
static constexpr auto test(U*)->std::is_integral<decltype(U::num)> {return std::is_integral<decltype(U::num)>{}; }
template<typename>
static constexpr std::false_type test(...) { return std::false_type{}; }
using type = decltype(test<T>(0));
};
}
/**
* @brief Trait which checks for the existence of a static numerator.
* @details Inherits from `std::true_type` or `std::false_type`. Use `has_num<T>::value` to test
* whether `class T` has a numerator static member.
*/
template<class T>
struct has_num : units::detail::has_num_impl<T>::type {};
namespace detail
{
/// has_den implementation.
template<class T>
struct has_den_impl
{
template<class U>
static constexpr auto test(U*)->std::is_integral<decltype(U::den)> { return std::is_integral<decltype(U::den)>{}; }
template<typename>
static constexpr std::false_type test(...) { return std::false_type{}; }
using type = decltype(test<T>(0));
};
}
/**
* @brief Trait which checks for the existence of a static denominator.
* @details Inherits from `std::true_type` or `std::false_type`. Use `has_den<T>::value` to test
* whether `class T` has a denominator static member.
*/
template<class T>
struct has_den : units::detail::has_den_impl<T>::type {};
/** @endcond */ // END DOXYGEN IGNORE
namespace traits
{
/**
* @brief Trait that tests whether a type represents a std::ratio.
* @details Inherits from `std::true_type` or `std::false_type`. Use `is_ratio<T>::value` to test
* whether `class T` implements a std::ratio.
*/
template<class T>
struct is_ratio : std::integral_constant<bool,
has_num<T>::value &&
has_den<T>::value>
{};
}
//------------------------------
// UNIT TRAITS
//------------------------------
/** @cond */ // DOXYGEN IGNORE
/**
* @brief void type.
* @details Helper class for creating type traits.
*/
template<class ...>
struct void_t { typedef void type; };
/**
* @brief parameter pack for boolean arguments.
*/
template<bool...> struct bool_pack {};
/**
* @brief Trait which tests that a set of other traits are all true.
*/
template<bool... Args>
struct all_true : std::is_same<units::bool_pack<true, Args...>, units::bool_pack<Args..., true>> {};
/** @endcond */ // DOXYGEN IGNORE
/**
* @brief namespace representing type traits which can access the properties of types provided by the units library.
*/
namespace traits
{
#ifdef FOR_DOXYGEN_PURPOSES_ONLY
/**
* @ingroup TypeTraits
* @brief Traits class defining the properties of units.
* @details The units library determines certain properties of the units passed to
* them and what they represent by using the members of the corresponding
* unit_traits instantiation.
*/
template<class T>
struct unit_traits
{
typedef typename T::base_unit_type base_unit_type; ///< Unit type that the unit was derived from. May be a `base_unit` or another `unit`. Use the `base_unit_of` trait to find the SI base unit type. This will be `void` if type `T` is not a unit.
typedef typename T::conversion_ratio conversion_ratio; ///< `std::ratio` representing the conversion factor to the `base_unit_type`. This will be `void` if type `T` is not a unit.
typedef typename T::pi_exponent_ratio pi_exponent_ratio; ///< `std::ratio` representing the exponent of pi to be used in the conversion. This will be `void` if type `T` is not a unit.
typedef typename T::translation_ratio translation_ratio; ///< `std::ratio` representing a datum translation to the base unit (i.e. degrees C to degrees F conversion). This will be `void` if type `T` is not a unit.
};
#endif
/** @cond */ // DOXYGEN IGNORE
/**
* @brief unit traits implementation for classes which are not units.
*/
template<class T, typename = void>
struct unit_traits
{
typedef void base_unit_type;
typedef void conversion_ratio;
typedef void pi_exponent_ratio;
typedef void translation_ratio;
};
template<class T>
struct unit_traits
<T, typename void_t<
typename T::base_unit_type,
typename T::conversion_ratio,
typename T::pi_exponent_ratio,
typename T::translation_ratio>::type>
{
typedef typename T::base_unit_type base_unit_type; ///< Unit type that the unit was derived from. May be a `base_unit` or another `unit`. Use the `base_unit_of` trait to find the SI base unit type. This will be `void` if type `T` is not a unit.
typedef typename T::conversion_ratio conversion_ratio; ///< `std::ratio` representing the conversion factor to the `base_unit_type`. This will be `void` if type `T` is not a unit.
typedef typename T::pi_exponent_ratio pi_exponent_ratio; ///< `std::ratio` representing the exponent of pi to be used in the conversion. This will be `void` if type `T` is not a unit.
typedef typename T::translation_ratio translation_ratio; ///< `std::ratio` representing a datum translation to the base unit (i.e. degrees C to degrees F conversion). This will be `void` if type `T` is not a unit.
};
/** @endcond */ // END DOXYGEN IGNORE
}
/** @cond */ // DOXYGEN IGNORE
namespace detail
{
/**
* @brief helper type to identify base units.
* @details A non-templated base class for `base_unit` which enables RTTI testing.
*/
struct _base_unit_t {};
}
/** @endcond */ // END DOXYGEN IGNORE
namespace traits
{
/**
* @ingroup TypeTraits
* @brief Trait which tests if a class is a `base_unit` type.
* @details Inherits from `std::true_type` or `std::false_type`. Use `is_base_unit<T>::value` to test
* whether `class T` implements a `base_unit`.
*/
template<class T>
struct is_base_unit : std::is_base_of<units::detail::_base_unit_t, T> {};
}
/** @cond */ // DOXYGEN IGNORE
namespace detail
{
/**
* @brief helper type to identify units.
* @details A non-templated base class for `unit` which enables RTTI testing.
*/
struct _unit {};
template<std::intmax_t Num, std::intmax_t Den = 1>
using meter_ratio = std::ratio<Num, Den>;
}
/** @endcond */ // END DOXYGEN IGNORE
namespace traits
{
/**
* @ingroup TypeTraits
* @brief Traits which tests if a class is a `unit`
* @details Inherits from `std::true_type` or `std::false_type`. Use `is_unit<T>::value` to test
* whether `class T` implements a `unit`.
*/
template<class T>
struct is_unit : std::is_base_of<units::detail::_unit, T>::type {};
}
/** @} */ // end of TypeTraits
//------------------------------
// BASE UNIT CLASS
//------------------------------
/**
* @ingroup UnitTypes
* @brief Class representing SI base unit types.
* @details Base units are represented by a combination of `std::ratio` template parameters, each
* describing the exponent of the type of unit they represent. Example: meters per second
* would be described by a +1 exponent for meters, and a -1 exponent for seconds, thus:
* `base_unit<std::ratio<1>, std::ratio<0>, std::ratio<-1>>`
* @tparam Meter `std::ratio` representing the exponent value for meters.
* @tparam Kilogram `std::ratio` representing the exponent value for kilograms.
* @tparam Second `std::ratio` representing the exponent value for seconds.
* @tparam Radian `std::ratio` representing the exponent value for radians. Although radians are not SI base units, they are included because radians are described by the SI as m * m^-1, which would make them indistinguishable from scalars.
* @tparam Ampere `std::ratio` representing the exponent value for amperes.
* @tparam Kelvin `std::ratio` representing the exponent value for Kelvin.
* @tparam Mole `std::ratio` representing the exponent value for moles.
* @tparam Candela `std::ratio` representing the exponent value for candelas.
* @tparam Byte `std::ratio` representing the exponent value for bytes.
* @sa category for type aliases for SI base_unit types.
*/
template<class Meter = detail::meter_ratio<0>,
class Kilogram = std::ratio<0>,
class Second = std::ratio<0>,
class Radian = std::ratio<0>,
class Ampere = std::ratio<0>,
class Kelvin = std::ratio<0>,
class Mole = std::ratio<0>,
class Candela = std::ratio<0>,
class Byte = std::ratio<0>>
struct base_unit : units::detail::_base_unit_t
{
static_assert(traits::is_ratio<Meter>::value, "Template parameter `Meter` must be a `std::ratio` representing the exponent of meters the unit has");
static_assert(traits::is_ratio<Kilogram>::value, "Template parameter `Kilogram` must be a `std::ratio` representing the exponent of kilograms the unit has");
static_assert(traits::is_ratio<Second>::value, "Template parameter `Second` must be a `std::ratio` representing the exponent of seconds the unit has");
static_assert(traits::is_ratio<Ampere>::value, "Template parameter `Ampere` must be a `std::ratio` representing the exponent of amperes the unit has");
static_assert(traits::is_ratio<Kelvin>::value, "Template parameter `Kelvin` must be a `std::ratio` representing the exponent of kelvin the unit has");
static_assert(traits::is_ratio<Candela>::value, "Template parameter `Candela` must be a `std::ratio` representing the exponent of candelas the unit has");
static_assert(traits::is_ratio<Mole>::value, "Template parameter `Mole` must be a `std::ratio` representing the exponent of moles the unit has");
static_assert(traits::is_ratio<Radian>::value, "Template parameter `Radian` must be a `std::ratio` representing the exponent of radians the unit has");
static_assert(traits::is_ratio<Byte>::value, "Template parameter `Byte` must be a `std::ratio` representing the exponent of bytes the unit has");
typedef Meter meter_ratio;
typedef Kilogram kilogram_ratio;
typedef Second second_ratio;
typedef Radian radian_ratio;
typedef Ampere ampere_ratio;
typedef Kelvin kelvin_ratio;
typedef Mole mole_ratio;
typedef Candela candela_ratio;
typedef Byte byte_ratio;
};
//------------------------------
// UNIT CATEGORIES
//------------------------------
/**
* @brief namespace representing the implemented base and derived unit types. These will not generally be needed by library users.
* @sa base_unit for the definition of the category parameters.
*/
namespace category
{
// SCALAR (DIMENSIONLESS) TYPES
typedef base_unit<> scalar_unit; ///< Represents a quantity with no dimension.
typedef base_unit<> dimensionless_unit; ///< Represents a quantity with no dimension.
// SI BASE UNIT TYPES
// METERS KILOGRAMS SECONDS RADIANS AMPERES KELVIN MOLE CANDELA BYTE --- CATEGORY
typedef base_unit<detail::meter_ratio<1>> length_unit; ///< Represents an SI base unit of length
typedef base_unit<detail::meter_ratio<0>, std::ratio<1>> mass_unit; ///< Represents an SI base unit of mass
typedef base_unit<detail::meter_ratio<0>, std::ratio<0>, std::ratio<1>> time_unit; ///< Represents an SI base unit of time
typedef base_unit<detail::meter_ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<1>> angle_unit; ///< Represents an SI base unit of angle
typedef base_unit<detail::meter_ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<1>> current_unit; ///< Represents an SI base unit of current
typedef base_unit<detail::meter_ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<1>> temperature_unit; ///< Represents an SI base unit of temperature
typedef base_unit<detail::meter_ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<1>> substance_unit; ///< Represents an SI base unit of amount of substance
typedef base_unit<detail::meter_ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<1>> luminous_intensity_unit; ///< Represents an SI base unit of luminous intensity
// SI DERIVED UNIT TYPES
// METERS KILOGRAMS SECONDS RADIANS AMPERES KELVIN MOLE CANDELA BYTE --- CATEGORY
typedef base_unit<detail::meter_ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<2>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>> solid_angle_unit; ///< Represents an SI derived unit of solid angle
typedef base_unit<detail::meter_ratio<0>, std::ratio<0>, std::ratio<-1>> frequency_unit; ///< Represents an SI derived unit of frequency
typedef base_unit<detail::meter_ratio<1>, std::ratio<0>, std::ratio<-1>> velocity_unit; ///< Represents an SI derived unit of velocity
typedef base_unit<detail::meter_ratio<0>, std::ratio<0>, std::ratio<-1>, std::ratio<1>> angular_velocity_unit; ///< Represents an SI derived unit of angular velocity
typedef base_unit<detail::meter_ratio<1>, std::ratio<0>, std::ratio<-2>> acceleration_unit; ///< Represents an SI derived unit of acceleration
typedef base_unit<detail::meter_ratio<1>, std::ratio<1>, std::ratio<-2>> force_unit; ///< Represents an SI derived unit of force
typedef base_unit<detail::meter_ratio<-1>, std::ratio<1>, std::ratio<-2>> pressure_unit; ///< Represents an SI derived unit of pressure
typedef base_unit<detail::meter_ratio<0>, std::ratio<0>, std::ratio<1>, std::ratio<0>, std::ratio<1>> charge_unit; ///< Represents an SI derived unit of charge
typedef base_unit<detail::meter_ratio<2>, std::ratio<1>, std::ratio<-2>> energy_unit; ///< Represents an SI derived unit of energy
typedef base_unit<detail::meter_ratio<2>, std::ratio<1>, std::ratio<-3>> power_unit; ///< Represents an SI derived unit of power
typedef base_unit<detail::meter_ratio<2>, std::ratio<1>, std::ratio<-3>, std::ratio<0>, std::ratio<-1>> voltage_unit; ///< Represents an SI derived unit of voltage
typedef base_unit<detail::meter_ratio<-2>, std::ratio<-1>, std::ratio<4>, std::ratio<0>, std::ratio<2>> capacitance_unit; ///< Represents an SI derived unit of capacitance
typedef base_unit<detail::meter_ratio<2>, std::ratio<1>, std::ratio<-3>, std::ratio<0>, std::ratio<-2>> impedance_unit; ///< Represents an SI derived unit of impedance
typedef base_unit<detail::meter_ratio<-2>, std::ratio<-1>, std::ratio<3>, std::ratio<0>, std::ratio<2>> conductance_unit; ///< Represents an SI derived unit of conductance
typedef base_unit<detail::meter_ratio<2>, std::ratio<1>, std::ratio<-2>, std::ratio<0>, std::ratio<-1>> magnetic_flux_unit; ///< Represents an SI derived unit of magnetic flux
typedef base_unit<detail::meter_ratio<0>, std::ratio<1>, std::ratio<-2>, std::ratio<0>, std::ratio<-1>> magnetic_field_strength_unit; ///< Represents an SI derived unit of magnetic field strength
typedef base_unit<detail::meter_ratio<2>, std::ratio<1>, std::ratio<-2>, std::ratio<0>, std::ratio<-2>> inductance_unit; ///< Represents an SI derived unit of inductance
typedef base_unit<detail::meter_ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<2>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<1>> luminous_flux_unit; ///< Represents an SI derived unit of luminous flux
typedef base_unit<detail::meter_ratio<-2>, std::ratio<0>, std::ratio<0>, std::ratio<2>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<1>> illuminance_unit; ///< Represents an SI derived unit of illuminance
typedef base_unit<detail::meter_ratio<0>, std::ratio<0>, std::ratio<-1>> radioactivity_unit; ///< Represents an SI derived unit of radioactivity
// OTHER UNIT TYPES
// METERS KILOGRAMS SECONDS RADIANS AMPERES KELVIN MOLE CANDELA BYTE --- CATEGORY
typedef base_unit<detail::meter_ratio<2>, std::ratio<1>, std::ratio<-2>> torque_unit; ///< Represents an SI derived unit of torque
typedef base_unit<detail::meter_ratio<2>> area_unit; ///< Represents an SI derived unit of area
typedef base_unit<detail::meter_ratio<3>> volume_unit; ///< Represents an SI derived unit of volume
typedef base_unit<detail::meter_ratio<-3>, std::ratio<1>> density_unit; ///< Represents an SI derived unit of density
typedef base_unit<> concentration_unit; ///< Represents a unit of concentration
typedef base_unit<detail::meter_ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<1>> data_unit; ///< Represents a unit of data size
typedef base_unit<detail::meter_ratio<0>, std::ratio<0>, std::ratio<-1>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<0>, std::ratio<1>> data_transfer_rate_unit; ///< Represents a unit of data transfer rate
}
//------------------------------
// UNIT CLASSES
//------------------------------
/** @cond */ // DOXYGEN IGNORE
/**
* @brief unit type template specialization for units derived from base units.
*/
template <class, class, class, class> struct unit;
template<class Conversion, class... Exponents, class PiExponent, class Translation>
struct unit<Conversion, base_unit<Exponents...>, PiExponent, Translation> : units::detail::_unit
{
static_assert(traits::is_ratio<Conversion>::value, "Template parameter `Conversion` must be a `std::ratio` representing the conversion factor to `BaseUnit`.");
static_assert(traits::is_ratio<PiExponent>::value, "Template parameter `PiExponent` must be a `std::ratio` representing the exponents of Pi the unit has.");
static_assert(traits::is_ratio<Translation>::value, "Template parameter `Translation` must be a `std::ratio` representing an additive translation required by the unit conversion.");
typedef typename units::base_unit<Exponents...> base_unit_type;
typedef Conversion conversion_ratio;
typedef Translation translation_ratio;
typedef PiExponent pi_exponent_ratio;
};
/** @endcond */ // END DOXYGEN IGNORE
/**
* @brief Type representing an arbitrary unit.
* @ingroup UnitTypes
* @details `unit` types are used as tags for the `conversion` function. They are *not* containers
* (see `unit_t` for a container class). Each unit is defined by:
*
* - A `std::ratio` defining the conversion factor to the base unit type. (e.g. `std::ratio<1,12>` for inches to feet)
* - A base unit that the unit is derived from (or a unit category. Must be of type `unit` or `base_unit`)
* - An exponent representing factors of PI required by the conversion. (e.g. `std::ratio<-1>` for a radians to degrees conversion)
* - a ratio representing a datum translation required for the conversion (e.g. `std::ratio<32>` for a fahrenheit to celsius conversion)
*
* Typically, a specific unit, like `meters`, would be implemented as a type alias
* of `unit`, i.e. `using meters = unit<std::ratio<1>, units::category::length_unit`, or
* `using inches = unit<std::ratio<1,12>, feet>`.
* @tparam Conversion std::ratio representing scalar multiplication factor.
* @tparam BaseUnit Unit type which this unit is derived from. May be a `base_unit`, or another `unit`.
* @tparam PiExponent std::ratio representing the exponent of pi required by the conversion.
* @tparam Translation std::ratio representing any datum translation required by the conversion.
*/
template<class Conversion, class BaseUnit, class PiExponent = std::ratio<0>, class Translation = std::ratio<0>>
struct unit : units::detail::_unit
{
static_assert(traits::is_unit<BaseUnit>::value, "Template parameter `BaseUnit` must be a `unit` type.");
static_assert(traits::is_ratio<Conversion>::value, "Template parameter `Conversion` must be a `std::ratio` representing the conversion factor to `BaseUnit`.");
static_assert(traits::is_ratio<PiExponent>::value, "Template parameter `PiExponent` must be a `std::ratio` representing the exponents of Pi the unit has.");
typedef typename units::traits::unit_traits<BaseUnit>::base_unit_type base_unit_type;
typedef typename std::ratio_multiply<typename BaseUnit::conversion_ratio, Conversion> conversion_ratio;
typedef typename std::ratio_add<typename BaseUnit::pi_exponent_ratio, PiExponent> pi_exponent_ratio;
typedef typename std::ratio_add<std::ratio_multiply<typename BaseUnit::conversion_ratio, Translation>, typename BaseUnit::translation_ratio> translation_ratio;
};
//------------------------------
// BASE UNIT MANIPULATORS
//------------------------------
/** @cond */ // DOXYGEN IGNORE
namespace detail
{
/**
* @brief base_unit_of trait implementation
* @details recursively seeks base_unit type that a unit is derived from. Since units can be
* derived from other units, the `base_unit_type` typedef may not represent this value.
*/
template<class> struct base_unit_of_impl;
template<class Conversion, class BaseUnit, class PiExponent, class Translation>
struct base_unit_of_impl<unit<Conversion, BaseUnit, PiExponent, Translation>> : base_unit_of_impl<BaseUnit> {};
template<class... Exponents>
struct base_unit_of_impl<base_unit<Exponents...>>
{
typedef base_unit<Exponents...> type;
};
template<>
struct base_unit_of_impl<void>
{
typedef void type;
};
}
/** @endcond */ // END DOXYGEN IGNORE
namespace traits
{
/**
* @brief Trait which returns the `base_unit` type that a unit is originally derived from.
* @details Since units can be derived from other `unit` types in addition to `base_unit` types,
* the `base_unit_type` typedef will not always be a `base_unit` (or unit category).
* Since compatible
*/
template<class U>
using base_unit_of = typename units::detail::base_unit_of_impl<U>::type;
}
/** @cond */ // DOXYGEN IGNORE
namespace detail
{
/**
* @brief implementation of base_unit_multiply
* @details 'multiples' (adds exponent ratios of) two base unit types. Base units can be found
* using `base_unit_of`.
*/
template<class, class> struct base_unit_multiply_impl;
template<class... Exponents1, class... Exponents2>
struct base_unit_multiply_impl<base_unit<Exponents1...>, base_unit<Exponents2...>> {
using type = base_unit<std::ratio_add<Exponents1, Exponents2>...>;
};
/**
* @brief represents type of two base units multiplied together
*/
template<class U1, class U2>
using base_unit_multiply = typename base_unit_multiply_impl<U1, U2>::type;
/**
* @brief implementation of base_unit_divide
* @details 'dived' (subtracts exponent ratios of) two base unit types. Base units can be found
* using `base_unit_of`.
*/
template<class, class> struct base_unit_divide_impl;
template<class... Exponents1, class... Exponents2>
struct base_unit_divide_impl<base_unit<Exponents1...>, base_unit<Exponents2...>> {
using type = base_unit<std::ratio_subtract<Exponents1, Exponents2>...>;
};
/**
* @brief represents the resulting type of `base_unit` U1 divided by U2.
*/
template<class U1, class U2>
using base_unit_divide = typename base_unit_divide_impl<U1, U2>::type;
/**
* @brief implementation of inverse_base
* @details multiplies all `base_unit` exponent ratios by -1. The resulting type represents
* the inverse base unit of the given `base_unit` type.
*/
template<class> struct inverse_base_impl;
template<class... Exponents>
struct inverse_base_impl<base_unit<Exponents...>> {
using type = base_unit<std::ratio_multiply<Exponents, std::ratio<-1>>...>;
};
/**
* @brief represent the inverse type of `class U`
* @details E.g. if `U` is `length_unit`, then `inverse<U>` will represent `length_unit^-1`.
*/
template<class U> using inverse_base = typename inverse_base_impl<U>::type;
/**
* @brief implementation of `squared_base`
* @details multiplies all the exponent ratios of the given class by 2. The resulting type is
* equivalent to the given type squared.
*/
template<class U> struct squared_base_impl;
template<class... Exponents>
struct squared_base_impl<base_unit<Exponents...>> {
using type = base_unit<std::ratio_multiply<Exponents, std::ratio<2>>...>;
};
/**
* @brief represents the type of a `base_unit` squared.
* @details E.g. `squared<length_unit>` will represent `length_unit^2`.
*/
template<class U> using squared_base = typename squared_base_impl<U>::type;
/**
* @brief implementation of `cubed_base`
* @details multiplies all the exponent ratios of the given class by 3. The resulting type is
* equivalent to the given type cubed.
*/
template<class U> struct cubed_base_impl;
template<class... Exponents>
struct cubed_base_impl<base_unit<Exponents...>> {
using type = base_unit<std::ratio_multiply<Exponents, std::ratio<3>>...>;
};
/**
* @brief represents the type of a `base_unit` cubed.
* @details E.g. `cubed<length_unit>` will represent `length_unit^3`.
*/
template<class U> using cubed_base = typename cubed_base_impl<U>::type;
/**
* @brief implementation of `sqrt_base`
* @details divides all the exponent ratios of the given class by 2. The resulting type is
* equivalent to the square root of the given type.
*/
template<class U> struct sqrt_base_impl;