Elements  5.12
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 #ifdef ELEMENTSKERNEL_ELEMENTSKERNEL_STORAGE_IMPL_
24 
25 #include <cstdint> // for int64_t
26 #include <cmath> // for pow, round
27 
28 #include "ElementsKernel/Number.h" // for numberCast
29 
30 namespace Elements {
31 namespace Units {
32 
33 template<typename T>
34 ELEMENTS_API T roundToDigits(const T& value, const size_t& max_digits) {
35  std::int64_t factor = std::int64_t(std::pow(10, max_digits));
36  return std::round(value * static_cast<T>(factor))/static_cast<T>(factor);
37 }
38 
39 template<std::size_t max_digits, typename T>
40 ELEMENTS_API T storageConvert(const T& size, StorageType source_unit, StorageType target_unit) {
41 
42  using std::log10;
43 
44  T converted_value = size;
45 
46  if (source_unit != target_unit) {
47  T size_in_bytes = size * T(StorageFactor[source_unit]);
48  int64_t target_factor = StorageFactor[target_unit];
49  double value = roundToDigits(static_cast<double>(size_in_bytes)/static_cast<double>(target_factor),
50  max_digits);
51  converted_value = Elements::numberCast<T>(value);
52  }
53 
54  return converted_value;
55 
56 }
57 
58 template<typename T>
59 ELEMENTS_API T storageConvert(const T& size, StorageType source_unit, StorageType target_unit) {
60 
61  using std::log10;
62 
63  T converted_value = size;
64 
65  if (source_unit != target_unit) {
66  T size_in_bytes = size * T(StorageFactor[source_unit]);
67  int64_t target_factor = StorageFactor[target_unit];
68  double value = roundToDigits(static_cast<double>(size_in_bytes)/static_cast<double>(target_factor),
69  static_cast<size_t>(log10(static_cast<double>(target_factor))));
70  converted_value = Elements::numberCast<T>(value);
71  }
72 
73  return converted_value;
74 
75 }
76 
77 } // namespace Units
78 } // namespace Elements
79 
80 
81 #endif // ELEMENTSKERNEL_ELEMENTSKERNEL_STORAGE_IMPL_
T log10(T...args)
Casting with the correct (closest) rounding.
#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)
T pow(T...args)
ELEMENTS_API T roundToDigits(const T &value, const std::size_t &max_digits)
T round(T...args)
ELEMENTS_API std::map< StorageType, std::int64_t > StorageFactor
Definition: Storage.cpp:49