Elements  5.8
A C++ base framework for the Euclid Software.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Storage.icpp
Go to the documentation of this file.
1 
23 #ifndef ELEMENTSKERNEL__IMPL_STORAGE_ICPP_
24 #define ELEMENTSKERNEL__IMPL_STORAGE_ICPP_
25 
26 #include <cstdint> // for int64_t
27 #include <cmath> // for pow, round
28 
29 #include "ElementsKernel/Number.h" // for numberCast
30 
31 namespace Elements {
32 namespace Units {
33 
34 template<typename T>
35 ELEMENTS_API T roundToDigits(const T& value, const size_t& max_digits) {
36  std::int64_t factor = std::pow(10, max_digits);
37  return std::round(value * static_cast<T>(factor))/static_cast<T>(factor);
38 }
39 
40 template<std::size_t max_digits, typename T>
41 ELEMENTS_API T storageConvert(const T& size, StorageType source_unit, StorageType target_unit) {
42 
43  using std::log10;
44 
45  T converted_value = size;
46 
47  if (source_unit != target_unit) {
48  T size_in_bytes = size * StorageFactor[source_unit];
49  int64_t target_factor = StorageFactor[target_unit];
50  double value = roundToDigits(static_cast<double>(size_in_bytes)/static_cast<double>(target_factor),
51  max_digits);
52  converted_value = Elements::numberCast<T>(value);
53  }
54 
55  return converted_value;
56 
57 }
58 
59 template<typename T>
60 ELEMENTS_API T storageConvert(const T& size, StorageType source_unit, StorageType target_unit) {
61 
62  using std::log10;
63 
64  T converted_value = size;
65 
66  if (source_unit != target_unit) {
67  T size_in_bytes = size * StorageFactor[source_unit];
68  int64_t target_factor = StorageFactor[target_unit];
69  double value = roundToDigits(static_cast<double>(size_in_bytes)/static_cast<double>(target_factor),
70  static_cast<size_t>(log10(static_cast<double>(target_factor))));
71  converted_value = Elements::numberCast<T>(value);
72  }
73 
74  return converted_value;
75 
76 }
77 
78 } // namespace Units
79 } // namespace Elements
80 
81 
82 #endif // ELEMENTSKERNEL__IMPL_STORAGE_ICPP_
T log10(T...args)
Casting with the correct (closest) rounding.
ELEMENTS_API T roundToDigits(const T &value, const size_t &max_digits)
Definition: Storage.icpp:35
#define ELEMENTS_API
Dummy definitions for the backward compatibility mode.
Definition: Export.h:74
ELEMENTS_API T storageConvert(const T &size, StorageType source_unit, StorageType target_unit)
Definition: Storage.icpp:41
T pow(T...args)
T round(T...args)
ELEMENTS_API std::map< StorageType, std::int64_t > StorageFactor
Definition: Storage.cpp:49