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 
61 /******************************************************************************
62  * Includes
63  */
64 
65 #include <chrono>
66 #include <locale>
67 #include <string>
68 #include <vector>
69 
70 
71 /******************************************************************************
72  * Macros
73  */
74 
75 // {{{ Macros
76 
88 #define ZAKERO_CONCAT_(thing_1_, thing_2_) thing_1_ ## thing_2_
89 
107 #define ZAKERO_CONCAT(thing_1_, thing_2_) ZAKERO_CONCAT_(thing_1_, thing_2_)
108 
109 
123 #define ZAKERO_DELETE(ptr_) \
124 { \
125  auto*& var##__LINE__ = ptr_; \
126  delete var##__LINE__; \
127  var##__LINE__ = nullptr; \
128 } \
129 
130 
144 #define ZAKERO_FREE(ptr_) \
145 { \
146  auto*& var##__LINE__ = ptr_; \
147  free(var##__LINE__); \
148  var##__LINE__ = nullptr; \
149 } \
150 
151 
164 #define ZAKERO_DISABLE_IMPLICIT_CASTS(func_name_) \
165  template <typename... T> \
166  void func_name_(T...) = delete \
167  ;
168 
169 
188 #define ZAKERO_MACRO_HAS_VALUE(define_) \
189  ~(~define_ + 0) == 0 && ~(~define_ + 1) == 1
190 
191 
207 #ifdef __linux__
208 # include <sys/types.h>
209 # include <unistd.h>
210 # define ZAKERO_PID getpid()
211 #else
212 # define ZAKERO_PID (pid_t)-1
213 #endif
214 
215 
231 #define ZAKERO_STEADY_TIME_NOW(unit_) \
232  std::chrono::duration_cast<std::chrono::unit_>( \
233  std::chrono::steady_clock::now().time_since_epoch() \
234  ).count() \
235 
236 
252 #define ZAKERO_SYSTEM_TIME_NOW(unit_) \
253 std::chrono::duration_cast<std::chrono::unit_>( \
254  std::chrono::steady_clock::now().time_since_epoch() \
255  ).count() \
256 
257 
263 #define ZAKERO_UNUSED(var_) \
264  (void)(var_);
265 
266 // }}}
267 
268 namespace zakero
269 {
275  enum struct Storage : uint64_t
276  { Byte = 0x0000'0000'0000'0001
277  , Kilobyte = 0x0000'0000'0000'0400
278  , Megabyte = 0x0000'0000'0010'0000
279  , Gigabyte = 0x0000'0000'4000'0000
280  , Terabyte = 0x0000'0100'0000'0000
281  , Petabyte = 0x0004'0000'0000'0000
282  , Exabyte = 0x1000'0000'0000'0000
283  };
284 
285 
286  /*
287  * Doxygen Bug: For some reason doxygen can not parse aliases
288  * only/specifically in this file and in this namespace.
289  */
290 
318  [[nodiscard]]
319  inline uint64_t convert(const uint64_t size
320  , const zakero::Storage from
321  , const zakero::Storage to
322  ) noexcept
323  {
324  return size * static_cast<uint64_t>(from) / static_cast<uint64_t>(to);
325  }
326 
327 
354  [[nodiscard]]
355  inline double convert(const double size
356  , const zakero::Storage from
357  , const zakero::Storage to
358  ) noexcept
359  {
360  return size * static_cast<uint64_t>(from) / static_cast<uint64_t>(to);
361  }
362 
363 
374  [[nodiscard]]
375  inline bool equalish(const float a
376  , const float b
377  , const float delta
378  ) noexcept
379  {
380  return (std::abs(a - b) < delta);
381  }
382 
383 
392  [[nodiscard]]
393  inline std::string tolower(std::string str
394  ) noexcept
395  {
396  std::locale locale;
397 
398  for(auto& c : str)
399  {
400  c = std::tolower(c, locale);
401  }
402 
403  return str;
404  }
405 
406 
428  template <class Type
429  >
430  [[nodiscard]]
431  inline bool vectorContains(const std::vector<Type>& vector
432  , const Type& value
433  ) noexcept
434  {
435  return (std::find(std::begin(vector), std::end(vector), value) != std::end(vector));
436  }
437 
438 
469  template <class InputIter
470  , class Type
471  >
472  [[nodiscard]]
473  inline bool vectorContains(InputIter first
474  , InputIter last
475  , const Type& value
476  ) noexcept
477  {
478  return (std::find(first, last, value) != last);
479  }
480 
481 
501  template <class Type
502  >
503  inline auto vectorErase(std::vector<Type>& vector
504  , const Type& value
505  ) noexcept
506  {
507  return vector.erase(std::remove(std::begin(vector), std::end(vector), value), std::end(vector));
508  }
509 
510 
535  [[nodiscard]]
536  inline bool stob(const std::string_view str
537  ) noexcept
538  {
539  static std::vector<std::string> v =
540  { "enable"
541  , "enabled"
542  , "true"
543  , "t"
544  , "yes"
545  , "y"
546  , "1"
547  };
548 
549  return (vectorContains(v, tolower(std::string(str))));
550  }
551 
552 
560  [[nodiscard]]
561  inline std::string to_string(const bool value
562  ) noexcept
563  {
564  if(value)
565  {
566  return std::string("true");
567  }
568 
569  return std::string("false");
570  }
571 
572 
582  [[nodiscard]]
583  inline std::string to_string(std::chrono::nanoseconds nanoseconds
584  ) noexcept
585  {
586  typedef std::chrono::duration<int64_t, std::ratio<86400>> duration_days;
587 
588  const auto days = std::chrono::duration_cast<duration_days>(nanoseconds);
589  nanoseconds -= days;
590 
591  const auto hours = std::chrono::duration_cast<std::chrono::hours>(nanoseconds);
592  nanoseconds -= hours;
593 
594  const auto minutes = std::chrono::duration_cast<std::chrono::minutes>(nanoseconds);
595  nanoseconds -= minutes;
596 
597  const auto seconds = std::chrono::duration_cast<std::chrono::seconds>(nanoseconds);
598  nanoseconds -= seconds;
599 
600  return "{ \"days\": " + std::to_string((int64_t)days.count())
601  + ", \"hours\": " + std::to_string((int64_t)hours.count())
602  + ", \"minutes\": " + std::to_string((int64_t)minutes.count())
603  + ", \"seconds\": " + std::to_string((int64_t)seconds.count())
604  + ", \"nanoseconds\": " + std::to_string((int64_t)nanoseconds.count())
605  + " }";
606  }
607 
608 
616  [[nodiscard]]
617  inline std::string to_string(const std::error_code& error
618  ) noexcept
619  {
620  return std::string()
621  + "{ \"category\": \"" + error.category().name() + "\""
622  + ", \"value\": " + std::to_string(error.value())
623  + ", \"message\": \"" + error.message() + "\""
624  + " }";
625  }
626 } // namespace zakero
627 
628 
634 [[nodiscard]]
635 inline std::ostream& operator<<(std::ostream& stream
636  , const std::error_code& error
637  ) noexcept
638 {
639  stream
640  << "{ \"category\": \"" << error.category().name() << "\""
641  << ", \"value\": " << std::to_string(error.value())
642  << ", \"message\": \"" << error.message() << "\""
643  << " }"
644  ;
645 
646  return stream;
647 }
648 
649 #endif // zakero_Base_h
std::string to_string(const bool value) noexcept
Convert a bool into a string.
Definition: Zakero_Base.h:561
bool stob(const std::string_view str) noexcept
Convert a string into a boolean value.
Definition: Zakero_Base.h:536
auto vectorErase(std::vector< Type > &vector, const Type &value) noexcept
Erase the contents of a std::vector.
Definition: Zakero_Base.h:503
std::ostream & operator<<(std::ostream &stream, const std::error_code &error) noexcept
Insert std::error_code into an output stream.
Definition: Zakero_Base.h:635
Storage
Conversion Type.
Definition: Zakero_Base.h:276
@ 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:431
uint64_t convert(const uint64_t size, const zakero::Storage from, const zakero::Storage to) noexcept
Convert storage sizes.
Definition: Zakero_Base.h:319
bool equalish(const float a, const float b, const float delta) noexcept
Compare two floats.
Definition: Zakero_Base.h:375
std::string tolower(std::string str) noexcept
Convert a string to lower case.
Definition: Zakero_Base.h:393