Zakero's C++ Header Libraries
A collection of reusable C++ libraries
Macros | Enumerations | Functions
Zakero_Base.h File Reference

Zakero Base. More...

Include dependency graph for Zakero_Base.h:

Go to the source code of this file.

Macros

#define ZAKERO_CONCAT(thing_1_, thing_2_)
 Concatenate the two things. More...
 
#define ZAKERO_DELETE(ptr_)
 Delete memory. More...
 
#define ZAKERO_DISABLE_IMPLICIT_CASTS(func_name_)
 Don't allow implicit parameter conversion. More...
 
#define ZAKERO_FREE(ptr_)
 Free memory. More...
 
#define ZAKERO_MACRO_HAS_VALUE(define_)
 Check if a macro has a value. More...
 
#define ZAKERO_PID
 Get the Process Id. More...
 
#define ZAKERO_STEADY_TIME_NOW(unit_)
 Get the current time. More...
 
#define ZAKERO_SYSTEM_TIME_NOW(unit_)
 Get the current time. More...
 
#define ZAKERO_UNUSED(var_)
 Prevent "unused variable" compilier warnings. More...
 

Enumerations

enum class  zakero::Storage : uint64_t {
  Byte ,
  Kilobyte ,
  Megabyte ,
  Gigabyte ,
  Terabyte ,
  Petabyte ,
  Exabyte
}
 Conversion Type. More...
 

Functions

double zakero::convert (const double size, const zakero::Storage from, const zakero::Storage to) noexcept
 Convert storage sizes. More...
 
uint64_t zakero::convert (const uint64_t size, const zakero::Storage from, const zakero::Storage to) noexcept
 Convert storage sizes. More...
 
bool zakero::equalish (const float a, const float b, const float delta) noexcept
 Compare two floats. More...
 
std::ostream & operator<< (std::ostream &stream, const std::error_code &error) noexcept
 Insert std::error_code into an output stream. More...
 
bool zakero::stob (const std::string_view str) noexcept
 Convert a string into a boolean value. More...
 
std::string zakero::to_string (const bool value) noexcept
 Convert a bool into a string. More...
 
std::string zakero::to_string (const std::error_code &error) noexcept
 Convert an std::error_code to a std::string. More...
 
std::string zakero::to_string (std::chrono::nanoseconds nanoseconds) noexcept
 Convert nanoseconds into a string. More...
 
std::string zakero::tolower (std::string str) noexcept
 Convert a string to lower case. More...
 
template<class Type >
bool zakero::vectorContains (const std::vector< Type > &vector, const Type &value) noexcept
 Check the contents of a std::vector. More...
 
template<class InputIter , class Type >
bool zakero::vectorContains (InputIter first, InputIter last, const Type &value) noexcept
 Check the contents of a std::vector. More...
 
template<class Type >
auto zakero::vectorErase (std::vector< Type > &vector, const Type &value) noexcept
 Erase the contents of a std::vector. More...
 

Detailed Description

Nothing complicated here, just a collection of helper functions, macros, and templates that may be useful in your projects.

Include this header in your source code to use these features.

Version

v0.9.4

v0.9.3

  • Added stob()
  • Added tolower()
  • Added to_string(const bool)
  • Added to_string(std::chrono::nanoseconds nanoseconds)

v0.9.2

v0.9.1

v0.9.0

  • The initial collection
Author
Andrew "Zakero" Moore
  • Original Author

Macro Definition Documentation

◆ ZAKERO_CONCAT

#define ZAKERO_CONCAT (   thing_1_,
  thing_2_ 
)

Use the C/C++ Preprocessor to create a new symbol name. For example the symbol abcxyz could be created using ZAKERO_CONCAT(abc, xyz).

Example
int foobar = 1;
ZAKERO_CONCAT(foo, bar)++; // foobar == 2
int ZAKERO_CONCAT(magic_, 42) = 123;
// int magic_42 = 123;
#define ZAKERO_CONCAT(thing_1_, thing_2_)
Concatenate the two things.
Definition: Zakero_Base.h:107
Parameters
thing_1_Symbol left side
thing_2_Symbol right side

◆ ZAKERO_DELETE

#define ZAKERO_DELETE (   ptr_)

Using this macro will help catch instances of using a pointer after delete'ing the new'ed memory. It does this by setting the value of ptr_ to nullptr after calling delete.

Parameters
ptr_The value to free.
Note
The ++p and p++ syntax should work:

ZAKERO_DELETE(p++); ZAKERO_DELETE(p++);

◆ ZAKERO_DISABLE_IMPLICIT_CASTS

#define ZAKERO_DISABLE_IMPLICIT_CASTS (   func_name_)

When passing a value to a function's parameter which does not have a matching type, the compiler will try to inject code to convert the value into the function's expected type. Usually, this is fine. But in some instances can lead to very subtle bugs not to mention the possible minor hit in performance. Placing the function name in this macro will prevent the compiler from doing this automatic type conversion.

Parameters
func_name_The name of the function.

◆ ZAKERO_FREE

#define ZAKERO_FREE (   ptr_)

Using this macro will help catch instances of using a pointer after free'ing the memory. It does this by setting the value of ptr_ to nullptr after calling free().

Parameters
ptr_The value to free.
Note
The ++p and p++ syntax should work:

ZAKERO_FREE(p++); ZAKERO_FREE(p++);

◆ ZAKERO_MACRO_HAS_VALUE

#define ZAKERO_MACRO_HAS_VALUE (   define_)

Use this macro function to determine if a macro has a value and is not just defined.

Example
#define BEER
#if ZAKERO_MACRO_HAS_VALUE(BEER)
#warning I can has beer!
#else
#error No beer! // <-- This happens
#endif
Parameters
define_The defined macro to check.

◆ ZAKERO_PID

#define ZAKERO_PID

Get the ID of the current process.

Example
std::cout << "My PID is " << std::to_string(ZAKERO_PID) << '\n';
#define ZAKERO_PID
Get the Process Id.
Definition: Zakero_Base.h:212
Returns
The Process Id (pid_t)
Note
POSIX.1-2008

◆ ZAKERO_STEADY_TIME_NOW

#define ZAKERO_STEADY_TIME_NOW (   unit_)

This macro will get the current time count of the std::chrono::steady_clock.

Note
The std::chrono namespace is automatically prepended to the unit_.
Example
auto time_now = ZAKERO_STEADY_TIME_NOW(milliseconds);
#define ZAKERO_STEADY_TIME_NOW(unit_)
Get the current time.
Definition: Zakero_Base.h:231
Parameters
unit_The time unit to get.

◆ ZAKERO_SYSTEM_TIME_NOW

#define ZAKERO_SYSTEM_TIME_NOW (   unit_)

This macro will get the current time count of the std::chrono::system_clock.

Note
The std::chrono namespace is automatically prepended to the unit_.
Example
auto time_now = ZAKERO_SYSTEM_TIME_NOW(milliseconds);
#define ZAKERO_SYSTEM_TIME_NOW(unit_)
Get the current time.
Definition: Zakero_Base.h:252
Parameters
unit_The time unit to get.

◆ ZAKERO_UNUSED

#define ZAKERO_UNUSED (   var_)
Parameters
var_The "unused variable".

Enumeration Type Documentation

◆ Storage

enum zakero::Storage : uint64_t
strong

Sizes in powers of 2.

Enumerator
Byte 

1 byte

Kilobyte 

1024 bytes

Megabyte 

1024 kilobytes

Gigabyte 

1024 megabytes

Terabyte 

1024 gigabytes

Petabyte 

1024 terabytes

Exabyte 

1024 petabytes

Function Documentation

◆ convert() [1/2]

double zakero::convert ( const double  size,
const zakero::Storage  from,
const zakero::Storage  to 
)
inlinenoexcept

Conversion from one storage unit to another is handled by this method. These storage units are in powers of 2.

The difference between this method and zakero::convert(const uint64_t, const zakero::Storage, const zakero::Storage) is that conversions to a larger unit will be a fraction.

Example
double bytes = zakero::convert(double(16)
, zakero::Storage::Gigabyte
, zakero::Storage::Byte
);
double megs = zakero::convert(double(16)
, zakero::Storage::Kilobyte
, zakero::Storage::Megabyte
);
uint64_t convert(const uint64_t size, const zakero::Storage from, const zakero::Storage to) noexcept
Convert storage sizes.
Definition: Zakero_Base.h:319
Returns
The converted value.
Parameters
sizeThe size to convert
fromThe source unit
toThe destination unit

◆ convert() [2/2]

uint64_t zakero::convert ( const uint64_t  size,
const zakero::Storage  from,
const zakero::Storage  to 
)
inlinenoexcept

Conversion from one storage unit to another is handled by this method. These storage units are in powers of 2.

Converting to a larger size is rounded down and may result in 0 if the from size is not large enough.

Example
uint64_t bytes = zakero::convert(uint64_t(16)
, zakero::Storage::Gigabyte
, zakero::Storage::Byte
);
uint64_t megs = zakero::convert(uint64_t(16)
, zakero::Storage::Kilobyte
, zakero::Storage::Megabyte
);
// megs == 0
Returns
The converted value.
Parameters
sizeThe size to convert
fromThe source unit
toThe destination unit

◆ equalish()

bool zakero::equalish ( const float  a,
const float  b,
const float  delta 
)
inlinenoexcept

Compare two floats for some degree of equality. For the float values to be considered "equal", the difference between the values must be less than the specified delta.

Return values
trueThe values are equal
falseThe values are not equal
Parameters
aThe first value
bThe second value
deltaThe maximum difference

◆ operator<<()

std::ostream& operator<< ( std::ostream &  stream,
const std::error_code &  error 
)
inlinenoexcept
Returns
The stream.
Parameters
streamThe stream to use
errorThe value in insert into the stream

◆ stob()

bool zakero::stob ( const std::string_view  str)
inlinenoexcept
Todo:
Make the list of "true" string values convertible.
(Compile-Time or Run-Time?)

This is intended to be a complement to the std::stod() and std::stoi() family of functions. However, the main restriction is this method only accepts certain string values to represent a boolean true value:

  • "enable"
  • "enabled"
  • "true"
  • "t"
  • "yes"
  • "y"
  • "1" If the provided str is not one of the above strings, false will be returned.
Note
The string comparison is not case-sensitive.
Returns
The string converted to a boolean value.
Parameters
strThe value to convert.

◆ to_string() [1/3]

std::string zakero::to_string ( const bool  value)
inlinenoexcept

Converts a bool value in to either "true" or "false".

Returns
A string
Parameters
valueThe value

◆ to_string() [2/3]

std::string zakero::to_string ( const std::error_code &  error)
inlinenoexcept

The provided error will be converted to a string.

Returns
A string
Parameters
errorThe value

◆ to_string() [3/3]

std::string zakero::to_string ( std::chrono::nanoseconds  nanoseconds)
inlinenoexcept

The nanoseconds value will be converted into a string that contains "days", "hours", "minutes", "seconds", and (of course) "nanoseconds".

Returns
A string
Parameters
nanosecondsThe value

◆ tolower()

std::string zakero::tolower ( std::string  str)
inlinenoexcept

The provided string, str, will be converted to lower-case using the default locale.

Returns
The lower-case string.
Parameters
strThe string to convert

◆ vectorContains() [1/2]

template<class Type >
bool zakero::vectorContains ( const std::vector< Type > &  vector,
const Type &  value 
)
inlinenoexcept

A convience method to make searching a vector easier, like std::map::contains().

Example
std::vector<int> v = { 0, 1, 2, 3 };
if(vectorContains(v, 1))
{
// Found it
}
bool vectorContains(const std::vector< Type > &vector, const Type &value) noexcept
Check the contents of a std::vector.
Definition: Zakero_Base.h:431
Return values
trueThe value was found.
falseThe value was not found.
Parameters
vectorThe vector to search
valueThe value to look for

◆ vectorContains() [2/2]

template<class InputIter , class Type >
bool zakero::vectorContains ( InputIter  first,
InputIter  last,
const Type &  value 
)
inlinenoexcept

A convenience method to make searching a vector easier. While this method does not save that many keystrokes, it does lead to more readable code.

Example
std::vector<int> v = { 0, 1, 2, 3 };
if(vectorContains(std::begin(v), std::end(v), 1)
{
// Found it
}
// Compare with the "long" form, the
// value that is searched for gets lost
// very easily.
if(std::find(std::begin(v), std::end(v), 1) != std::end(v))
{
// Found it
}
Return values
trueThe value was found.
falseThe value was not found.
Parameters
firstStart searching here
lastStop searching here
valueThe value to look for

◆ vectorErase()

template<class Type >
auto zakero::vectorErase ( std::vector< Type > &  vector,
const Type &  value 
)
inlinenoexcept

A convenience method to make removing content from a vector easier.
Uses the "Erase/Remove Idiom".

Example
std::vector<int> v = { 0, 1, 2, 3 };
// v = { 0, 1, 3 };
auto vectorErase(std::vector< Type > &vector, const Type &value) noexcept
Erase the contents of a std::vector.
Definition: Zakero_Base.h:503
Return values
trueThe value was found.
falseThe value was not found.
Parameters
vectorThe vector to search
valueThe value to look for