All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
datasize.h
Go to the documentation of this file.
1 /**
2  * @file lardataalg/Utilities/quantities/datasize.h
3  * @brief Dimensioned variables representing data size.
4  * @author Gianluca Petrillo (petrillo@slac.stanford.edu)
5  * @date November 2, 2018
6  * @see lardataalg/Utilities/quantities.h
7  *
8  * Set of basic quantities related to data size.
9  * This is mostly a proof of concept for custom prefixes.
10  *
11  * This is a header-only library.
12  *
13  */
14 
15 #ifndef LARDATAALG_UTILITIES_QUANTITIES_DATASIZE_H
16 #define LARDATAALG_UTILITIES_QUANTITIES_DATASIZE_H
17 
18 // LArSoft libraries
20 
21 // C/C++ standard libraries
22 #include <string_view>
23 #include <ratio>
24 
25 
26 //------------------------------------------------------------------------------
27 namespace util::quantities {
28 
29  namespace units {
30 
31  using namespace std::string_view_literals; // for operator""sv()
32 
33  struct Byte: public concepts::UnitBase {
34  static constexpr auto symbol = "B"sv;
35  static constexpr auto name = "byte"sv;
36  };
37 
38  } // namespace units
39 
40 
41  namespace prefixes {
42 
43  /// Factor 1'024 (2^10).
44  using kibi = std::ratio<(1LL << 10)>;
45 
46  /// Factor 1'048'576 (2^20).
47  using mebi = std::ratio<(1LL << 20)>;
48 
49  /// Factor 1'073'741'824 (2^30).
50  using gibi = std::ratio<(1LL << 30)>;
51 
52  /// Factor 2^40.
53  using tebi = std::ratio<(1LL << 40)>;
54 
55  /// Factor 2^50.
56  using pebi = std::ratio<(1LL << 50)>;
57 
58  /// Factor 2^60.
59  using exbi = std::ratio<(1LL << 60)>;
60 
61  } // namespace prefixes
62 
63 
64  // -- BEGIN Data size --------------------------------------------------------
65  /**
66  * @name Data size
67  *
68  * These time quantities are tied to `util::quantities::units::Byte`.
69  *
70  * * most general template, `scaled_byte`, allowing to choose both the scale
71  * of the unit (e.g. `util::quantities::prefixes::mebi` for mebibyte) and
72  * the type of the numerical representation
73  * * generic templates (e.g. `byte_as`), allowing to choose which numerical
74  * representation to use
75  * * unsigned integral number (e.g. `byte`), 64-bit on amd64 architecture,
76  * ready for use
77  *
78  */
79  /// @{
80 
81 
82  /// The most generic `units::Byte`-based quantity.
83  template <typename R, typename T = unsigned long long int>
85 
86  //
87  // byte
88  //
89  /// Type of data size stored in bytes.
90  template <typename T = unsigned long long int>
92 
93  /// Alias for common language habits.
94  template <typename T = unsigned long long int>
96 
97  /// Type of data size stored in bytes, in `long long` precision.
98  using byte = byte_as<>;
99 
100  /// Alias for common language habits.
101  using bytes = byte;
102 
103  //
104  // kibibyte
105  //
106  /// Type of data size stored in kibibytes.
107  template <typename T = unsigned long long int>
109 
110  /// Alias for common language habits.
111  template <typename T = unsigned long long int>
113 
114  /// Type of data size stored in kibibytes, in `long long` precision.
116 
117  /// Alias for common language habits.
119 
120  //
121  // mebibyte
122  //
123  /// Type of data size stored in mebibytes.
124  template <typename T = unsigned long long int>
126 
127  /// Alias for common language habits.
128  template <typename T = unsigned long long int>
130 
131  /// Type of data size stored in mebibytes, in `long long` precision.
133 
134  /// Alias for common language habits.
136 
137  //
138  // gibibyte
139  //
140  /// Type of data size stored in gibibytes.
141  template <typename T = unsigned long long int>
143 
144  /// Alias for common language habits.
145  template <typename T = unsigned long long int>
147 
148  /// Type of data size stored in pebibytes, in `long long` precision.
150 
151  /// Alias for common language habits.
153 
154  //
155  // tebibyte
156  //
157  /// Type of data size stored in tebibytes.
158  template <typename T = unsigned long long int>
160 
161  /// Alias for common language habits.
162  template <typename T = unsigned long long int>
164 
165  /// Type of data size stored in tebibytes, in `long long` precision.
167 
168  /// Alias for common language habits.
170 
171  //
172  // pebibyte
173  //
174  /// Type of data size stored in pebibytes.
175  template <typename T = unsigned long long int>
177 
178  /// Alias for common language habits.
179  template <typename T = unsigned long long int>
181 
182  /// Type of data size stored in pebibytes, in `long long` precision.
184 
185  /// Alias for common language habits.
187 
188  //
189  // exbibyte
190  //
191  /// Type of data size stored in exbibytes.
192  template <typename T = unsigned long long int>
194 
195  /// Alias for common language habits.
196  template <typename T = unsigned long long int>
198 
199  /// Type of data size stored in exbibytes, in `long long` precision.
201 
202  /// Alias for common language habits.
204 
205 
206  /**
207  * @brief Literal constants for data size quantities.
208  *
209  * These functions allow a simplified syntax for specifying a data size
210  * quantity.
211  * In order to use these, their namespace must be used:
212  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
213  * using namespace util::quantities::datasize_literals;
214  *
215  * // definition of `util::quantities::byte` constant:
216  * constexpr auto s_B = 12_B;
217  *
218  * // assignment (likely to a quantity) of
219  * // `util::quantities::kibibyte{512}`
220  * s_B = 512_kiB;
221  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
222  *
223  */
224  namespace datasize_literals {
225 
226  // @{
227  /// Literal second value.
228  constexpr byte operator""_B (long double v)
229  { return byte{ static_cast<unsigned long long int>(v) }; }
230  constexpr byte operator""_B (unsigned long long int v)
231  { return byte{ v }; }
232  // @}
233 
234  // @{
235  /// Literal kibibyte value.
236  constexpr kibibyte operator""_kiB (long double v)
237  { return kibibyte{ static_cast<unsigned long long int>(v) }; }
238  constexpr kibibyte operator""_kiB (unsigned long long int v)
239  { return kibibyte{ v }; }
240  // @}
241 
242  // @{
243  /// Literal mebibyte value.
244  constexpr mebibyte operator""_MiB (long double v)
245  { return mebibyte{ static_cast<unsigned long long int>(v) }; }
246  constexpr mebibyte operator""_MiB (unsigned long long int v)
247  { return mebibyte{ v }; }
248  // @}
249 
250  // @{
251  /// Literal gibibyte value.
252  constexpr gibibyte operator""_GiB (long double v)
253  { return gibibyte{ static_cast<unsigned long long int>(v) }; }
254  constexpr gibibyte operator""_GiB (unsigned long long int v)
255  { return gibibyte{ v }; }
256  // @}
257 
258  // @{
259  /// Literal tebibyte value.
260  constexpr tebibyte operator""_TiB (long double v)
261  { return tebibyte{ static_cast<unsigned long long int>(v) }; }
262  constexpr tebibyte operator""_TiB (unsigned long long int v)
263  { return tebibyte{ v }; }
264  // @}
265 
266  // @{
267  /// Literal pebibyte value.
268  constexpr pebibyte operator""_PiB (long double v)
269  { return pebibyte{ static_cast<unsigned long long int>(v) }; }
270  constexpr pebibyte operator""_PiB (unsigned long long int v)
271  { return pebibyte{ v }; }
272  // @}
273 
274  // @{
275  /// Literal exbibyte value.
276  constexpr exbibyte operator""_EiB (long double v)
277  { return exbibyte{ static_cast<unsigned long long int>(v) }; }
278  constexpr exbibyte operator""_EiB (unsigned long long int v)
279  { return exbibyte{ v }; }
280  // @}
281 
282 
283  } // datasize_literals
284 
285 
286  /// @}
287  // -- END Data size ----------------------------------------------------------
288 
289 } // namespace util::quantities
290 
291 //------------------------------------------------------------------------------
292 //--- Template specializations
293 //------------------------------------------------------------------------------
294 namespace util::quantities::concepts {
295 
296  /// Prefix for 1024 (2^10).
297  template <>
298  struct Prefix<prefixes::kibi> {
299 
300  /// Returns the symbol of the prefix.
301  static constexpr auto symbol() { return "ki"sv; }
302 
303  /// Returns the full name of the prefix.
304  static constexpr auto name() { return "kibi"sv; }
305 
306  }; // struct Prefix<prefixes::kibi>
307 
308 
309  /// Prefix for 1048576 (2^20).
310  template <>
311  struct Prefix<prefixes::mebi> {
312 
313  /// Returns the symbol of the prefix.
314  static constexpr auto symbol() { return "Mi"sv; }
315 
316  /// Returns the full name of the prefix.
317  static constexpr auto name() { return "mebi"sv; }
318 
319  }; // struct Prefix<prefixes::mebi>
320 
321 
322  /// Prefix for 1073741824 (2^30).
323  template <>
324  struct Prefix<prefixes::gibi> {
325 
326  /// Returns the symbol of the prefix.
327  static constexpr auto symbol() { return "Gi"sv; }
328 
329  /// Returns the full name of the prefix.
330  static constexpr auto name() { return "gibi"sv; }
331 
332  }; // struct Prefix<prefixes::gibi>
333 
334 
335  /// Prefix for 2^40.
336  template <>
337  struct Prefix<prefixes::tebi> {
338 
339  /// Returns the symbol of the prefix.
340  static constexpr auto symbol() { return "Ti"sv; }
341 
342  /// Returns the full name of the prefix.
343  static constexpr auto name() { return "tebi"sv; }
344 
345  }; // struct Prefix<prefixes::tebi>
346 
347 
348  /// Prefix for 2^50.
349  template <>
350  struct Prefix<prefixes::pebi> {
351 
352  /// Returns the symbol of the prefix.
353  static constexpr auto symbol() { return "Pi"sv; }
354 
355  /// Returns the full name of the prefix.
356  static constexpr auto name() { return "pebi"sv; }
357 
358  }; // struct Prefix<prefixes::pebi>
359 
360 
361  /// Prefix for 2^60.
362  template <>
363  struct Prefix<prefixes::exbi> {
364 
365  /// Returns the symbol of the prefix.
366  static constexpr auto symbol() { return "Ei"sv; }
367 
368  /// Returns the full name of the prefix.
369  static constexpr auto name() { return "exbi"sv; }
370 
371  }; // struct Prefix<prefixes::exbi>
372 
373 
374 } // namespace util::quantities::concepts
375 
376 
377 //------------------------------------------------------------------------------
378 
379 
380 #endif // LARDATAALG_UTILITIES_QUANTITIES_DATASIZE_H
static constexpr auto name()
Returns the full name of the prefix.
Definition: datasize.h:304
static constexpr auto name()
Returns the full name of the prefix.
Definition: datasize.h:330
static constexpr auto symbol()
Returns the symbol of the prefix.
Definition: datasize.h:340
static constexpr auto symbol()
Returns the symbol of the prefix.
Definition: datasize.h:366
kibibyte_as<> kibibyte
Type of data size stored in kibibytes, in long long precision.
Definition: datasize.h:115
exbibyte_as<> exbibyte
Type of data size stored in exbibytes, in long long precision.
Definition: datasize.h:200
std::ratio<(1LL<< 60)> exbi
Factor 2^60.
Definition: datasize.h:59
std::ratio<(1LL<< 40)> tebi
Factor 2^40.
Definition: datasize.h:53
tebibyte_as<> tebibyte
Type of data size stored in tebibytes, in long long precision.
Definition: datasize.h:166
static constexpr auto symbol()
Returns the symbol of the prefix.
Definition: datasize.h:314
std::ratio<(1LL<< 30)> gibi
Factor 1&#39;073&#39;741&#39;824 (2^30).
Definition: datasize.h:50
static constexpr auto symbol()
Returns the symbol of the prefix.
Definition: datasize.h:327
static constexpr auto name()
Returns the full name of the prefix.
Definition: datasize.h:356
mebibyte_as<> mebibyte
Type of data size stored in mebibytes, in long long precision.
Definition: datasize.h:132
BEGIN_PROLOG units
A value measured in the specified unit.
Definition: quantities.h:566
std::ratio<(1LL<< 50)> pebi
Factor 2^50.
Definition: datasize.h:56
static constexpr auto symbol()
Returns the symbol of the prefix.
Definition: datasize.h:301
static constexpr auto symbol()
Returns the symbol of the prefix.
Definition: datasize.h:353
Numeric variable proxies with embedded unit of measurement.
pebibyte_as<> pebibyte
Type of data size stored in pebibytes, in long long precision.
Definition: datasize.h:183
static constexpr auto name()
Returns the full name of the prefix.
Definition: datasize.h:369
byte_as<> byte
Type of data size stored in bytes, in long long precision.
Definition: datasize.h:98
static constexpr auto name()
Returns the full name of the prefix.
Definition: datasize.h:317
then echo fcl name
gibibyte_as<> gibibyte
Type of data size stored in pebibytes, in long long precision.
Definition: datasize.h:149
std::ratio<(1LL<< 10)> kibi
Factor 1&#39;024 (2^10).
Definition: datasize.h:44
std::ratio<(1LL<< 20)> mebi
Factor 1&#39;048&#39;576 (2^20).
Definition: datasize.h:47
static constexpr auto name()
Returns the full name of the prefix.
Definition: datasize.h:343