Zakero's C++ Header Libraries
A collection of reusable C++ libraries
Zakero_Base.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2020 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 
55 /******************************************************************************
56  * Includes
57  */
58 
59 #include <chrono>
60 #include <locale>
61 #include <string>
62 #include <vector>
63 
64 
65 /******************************************************************************
66  * Macros
67  */
68 
69 // {{{ Macros
70 
82 #define ZAKERO_CONCAT_(thing_1_, thing_2_) thing_1_ ## thing_2_
83 
101 #define ZAKERO_CONCAT(thing_1_, thing_2_) ZAKERO_CONCAT_(thing_1_, thing_2_)
102 
103 
117 #define ZAKERO_DELETE(ptr_) \
118 { \
119  auto*& var##__LINE__ = ptr_; \
120  delete var##__LINE__; \
121  var##__LINE__ = nullptr; \
122 } \
123 
124 
138 #define ZAKERO_FREE(ptr_) \
139 { \
140  auto*& var##__LINE__ = ptr_; \
141  free(var##__LINE__); \
142  var##__LINE__ = nullptr; \
143 } \
144 
145 
164 #define ZAKERO_MACRO_HAS_VALUE(define_) \
165  ~(~define_ + 0) == 0 && ~(~define_ + 1) == 1
166 
182 #ifdef __linux__
183 # include <sys/types.h>
184 # include <unistd.h>
185 # define ZAKERO_PID getpid()
186 #else
187 # define ZAKERO_PID (pid_t)-1
188 #endif
189 
205 #define ZAKERO_STEADY_TIME_NOW(unit_) \
206  std::chrono::duration_cast<std::chrono::unit_>( \
207  std::chrono::steady_clock::now().time_since_epoch() \
208  ).count() \
209 
225 #define ZAKERO_SYSTEM_TIME_NOW(unit_) \
226 std::chrono::duration_cast<std::chrono::unit_>( \
227  std::chrono::steady_clock::now().time_since_epoch() \
228  ).count() \
229 
230 
236 #define ZAKERO_UNUSED(var_) \
237  (void)(var_);
238 
239 // }}}
240 
241 namespace zakero
242 {
248  enum struct Storage : uint64_t
249  { Byte = 0x0000'0000'0000'0001
250  , Kilobyte = 0x0000'0000'0000'0400
251  , Megabyte = 0x0000'0000'0010'0000
252  , Gigabyte = 0x0000'0000'4000'0000
253  , Terabyte = 0x0000'0100'0000'0000
254  , Petabyte = 0x0004'0000'0000'0000
255  , Exabyte = 0x1000'0000'0000'0000
256  };
257 
258 
259  /*
260  * Doxygen Bug: For some reason doxygen can not parse aliases
261  * only/specifically in this file and in this namespace.
262  */
263 
291  [[nodiscard]]
292  inline uint64_t convert(const uint64_t size
293  , const zakero::Storage from
294  , const zakero::Storage to
295  ) noexcept
296  {
297  return size * static_cast<uint64_t>(from) / static_cast<uint64_t>(to);
298  }
299 
300 
327  [[nodiscard]]
328  inline double convert(const double size
329  , const zakero::Storage from
330  , const zakero::Storage to
331  ) noexcept
332  {
333  return size * static_cast<uint64_t>(from) / static_cast<uint64_t>(to);
334  }
335 
336 
347  [[nodiscard]]
348  inline bool equalish(const float a
349  , const float b
350  , const float delta
351  ) noexcept
352  {
353  return (std::abs(a - b) < delta);
354  }
355 
356 
365  [[nodiscard]]
366  inline std::string tolower(std::string str
367  ) noexcept
368  {
369  std::locale locale;
370 
371  for(auto& c : str)
372  {
373  c = std::tolower(c, locale);
374  }
375 
376  return str;
377  }
378 
379 
401  template <class Type
402  >
403  [[nodiscard]]
404  inline bool vectorContains(const std::vector<Type>& vector
405  , const Type& value
406  ) noexcept
407  {
408  return (std::find(std::begin(vector), std::end(vector), value) != std::end(vector));
409  }
410 
411 
442  template <class InputIter
443  , class Type
444  >
445  [[nodiscard]]
446  inline bool vectorContains(InputIter first
447  , InputIter last
448  , const Type& value
449  ) noexcept
450  {
451  return (std::find(first, last, value) != last);
452  }
453 
454 
474  template <class Type
475  >
476  inline auto vectorErase(std::vector<Type>& vector
477  , const Type& value
478  ) noexcept
479  {
480  return vector.erase(std::remove(std::begin(vector), std::end(vector), value), std::end(vector));
481  }
482 
483 
508  [[nodiscard]]
509  inline bool stob(const std::string_view str
510  ) noexcept
511  {
512  static std::vector<std::string> v =
513  { "enable"
514  , "enabled"
515  , "true"
516  , "t"
517  , "yes"
518  , "y"
519  , "1"
520  };
521 
522  return (vectorContains(v, tolower(std::string(str))));
523  }
524 
525 
533  [[nodiscard]]
534  inline std::string to_string(const bool value
535  ) noexcept
536  {
537  if(value)
538  {
539  return std::string("true");
540  }
541 
542  return std::string("false");
543  }
544 
545 
555  [[nodiscard]]
556  inline std::string to_string(std::chrono::nanoseconds nanoseconds
557  ) noexcept
558  {
559  typedef std::chrono::duration<int64_t, std::ratio<86400>> duration_days;
560 
561  const auto days = std::chrono::duration_cast<duration_days>(nanoseconds);
562  nanoseconds -= days;
563 
564  const auto hours = std::chrono::duration_cast<std::chrono::hours>(nanoseconds);
565  nanoseconds -= hours;
566 
567  const auto minutes = std::chrono::duration_cast<std::chrono::minutes>(nanoseconds);
568  nanoseconds -= minutes;
569 
570  const auto seconds = std::chrono::duration_cast<std::chrono::seconds>(nanoseconds);
571  nanoseconds -= seconds;
572 
573  return "{ \"days\": " + std::to_string((int64_t)days.count())
574  + ", \"hours\": " + std::to_string((int64_t)hours.count())
575  + ", \"minutes\": " + std::to_string((int64_t)minutes.count())
576  + ", \"seconds\": " + std::to_string((int64_t)seconds.count())
577  + ", \"nanoseconds\": " + std::to_string((int64_t)nanoseconds.count())
578  + " }";
579  }
580 
581 
589  [[nodiscard]]
590  inline std::string to_string(const std::error_code& error
591  ) noexcept
592  {
593  return std::string()
594  + "{ \"category\": \"" + error.category().name() + "\""
595  + ", \"value\": " + std::to_string(error.value())
596  + ", \"message\": \"" + error.message() + "\""
597  + " }";
598  }
599 } // namespace zakero
600 
601 
607 [[nodiscard]]
608 inline std::ostream& operator<<(std::ostream& stream
609  , const std::error_code& error
610  ) noexcept
611 {
612  stream
613  << "{ \"category\": \"" << error.category().name() << "\""
614  << ", \"value\": " << std::to_string(error.value())
615  << ", \"message\": \"" << error.message() << "\""
616  << " }"
617  ;
618 
619  return stream;
620 }
621 
622 #endif // zakero_Base_h
std::string to_string(const bool value) noexcept
Convert a bool into a string.
Definition: Zakero_Base.h:534
bool stob(const std::string_view str) noexcept
Convert a string into a boolean value.
Definition: Zakero_Base.h:509
auto vectorErase(std::vector< Type > &vector, const Type &value) noexcept
Erase the contents of a std::vector.
Definition: Zakero_Base.h:476
std::ostream & operator<<(std::ostream &stream, const std::error_code &error) noexcept
Insert std::error_code into an output stream.
Definition: Zakero_Base.h:608
Storage
Conversion Type.
Definition: Zakero_Base.h:249
@ Gigabyte
1024 megabytes
@ Megabyte
1024 kilobytes
@ Terabyte
1024 gigabytes
@ Exabyte
1024 petabytes
@ Petabyte
1024 terabytes
@ Kilobyte
1024 bytes
bool vectorContains(const std::vector< Type > &vector, const Type &value) noexcept
Check the contents of a std::vector.
Definition: Zakero_Base.h:404
uint64_t convert(const uint64_t size, const zakero::Storage from, const zakero::Storage to) noexcept
Convert storage sizes.
Definition: Zakero_Base.h:292
bool equalish(const float a, const float b, const float delta) noexcept
Compare two floats.
Definition: Zakero_Base.h:348
std::string tolower(std::string str) noexcept
Convert a string to lower case.
Definition: Zakero_Base.h:366