Zakero's C++ Header Libraries
A collection of reusable C++ libraries
Zakero_Base.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2010-2019 Andrew Moore
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
7  */
8 
9 #ifndef zakero_Base_h
10 #define zakero_Base_h
11 
40 /******************************************************************************
41  * Includes
42  */
43 
44 #include <chrono>
45 #include <vector>
46 
47 
48 /******************************************************************************
49  * Macros
50  */
51 
52 // {{{ Macros
53 
65 #define ZAKERO_CONCAT_(thing_1_, thing_2_) thing_1_ ## thing_2_
66 
84 #define ZAKERO_CONCAT(thing_1_, thing_2_) ZAKERO_CONCAT_(thing_1_, thing_2_)
85 
104 #define ZAKERO_MACRO_HAS_VALUE(define_) \
105  ~(~define_ + 0) == 0 && ~(~define_ + 1) == 1
106 
121 #define ZAKERO_STEADY_TIME_NOW(unit_) \
122  std::chrono::duration_cast<std::chrono::unit_>( \
123  std::chrono::steady_clock::now().time_since_epoch() \
124  ).count() \
125 
126 
140 #define ZAKERO_SYSTEM_TIME_NOW(unit_) \
141 std::chrono::duration_cast<std::chrono::unit_>( \
142  std::chrono::steady_clock::now().time_since_epoch() \
143  ).count() \
144 
145 // }}}
146 
147 namespace zakero
148 {
154  enum struct Storage : uint64_t
155  { Byte = 0x0000'0000'0000'0001 ///< 1 byte
156  , Kilobyte = 0x0000'0000'0000'0400
157  , Megabyte = 0x0000'0000'0010'0000 ///< 1024 kilobytes
158  , Gigabyte = 0x0000'0000'4000'0000
159  , Terabyte = 0x0000'0100'0000'0000 ///< 1024 gigabytes
160  , Petabyte = 0x0004'0000'0000'0000
161  , Exabyte = 0x1000'0000'0000'0000 ///< 1024 petabytes
162  };
163 
164 
165  /*
166  * Doxygen Bug: For some reason doxygen can not parse aliases
167  * only/specifically in this file and in this namespace.
168  */
169 
197  [[nodiscard]]
198  inline uint64_t convert(const uint64_t size ///< The size to convert
199  , const zakero::Storage from ///< The source unit
200  , const zakero::Storage to ///< The destination unit
201  ) noexcept
202  {
203  return size * static_cast<uint64_t>(from) / static_cast<uint64_t>(to);
204  }
205 
206 
233  [[nodiscard]]
234  inline double convert(const double size ///< The size to convert
235  , const zakero::Storage from ///< The source unit
236  , const zakero::Storage to ///< The destination unit
237  ) noexcept
238  {
239  return size * static_cast<uint64_t>(from) / static_cast<uint64_t>(to);
240  }
241 
242 
264  template <class Type ///< The vector type
265  >
266  [[nodiscard]]
267  inline bool vectorContains(const std::vector<Type>& vector ///< The vector to search
268  , const Type& value ///< The value to look for
269  ) noexcept
270  {
271  return (std::find(std::begin(vector), std::end(vector), value) != std::end(vector));
272  }
273 
274 
305  template <class InputIter ///< The iterator type
306  , class Type ///< The vector type
307  >
308  [[nodiscard]]
309  inline bool vectorContains(InputIter first ///< Start searching here
310  , InputIter last ///< Stop seaching here
311  , const Type& value ///< The value to look for
312  ) noexcept
313  {
314  return (std::find(first, last, value) != last);
315  }
316 
317 
336  template <class Type ///< The vector type
337  >
338  inline auto vectorErase(std::vector<Type>& vector ///< The vector to search
339  , const Type& value ///< The value to look for
340  ) noexcept
341  {
342  return vector.erase(std::remove(std::begin(vector), std::end(vector), value), std::end(vector));
343  }
344 
345 
353  std::string to_string(const std::error_code& error ///< The value
354  ) noexcept
355  {
356  std::string str = std::string(error.category().name())
357  + "," + std::to_string(error.value())
358  + "," + std::string(error.message())
359  ;
360 
361  return str;
362  }
363 
364 #if 0 // Future?
365  using VectorString = std::vector<std::string>;
366 
367  [[nodiscard]]
368  std::string to_string(const Yetani::VectorString&) noexcept;
369 
370  [[nodiscard]]
371  std::string to_string(const Yetani::VectorString& vector ///< The value
372  ) noexcept
373  {
374  if(vector.empty())
375  {
376  return "[]";
377  }
378 
379  std::string string = "";
380  std::string delim = "[ \"";
381 
382  for(const auto& str : vector)
383  {
384  string += delim + str;
385 
386  delim = "\", \"";
387  }
388 
389  string += "\" ]";
390 
391  return string;
392  }
393 #endif
394 }
395 
396 #endif // zakero_Base_h
zakero::Storage
Storage
Conversion Type.
Definition: Zakero_Base.h:155