Skip to content

Commit

Permalink
refactor: change helper class to namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
cieslarmichal committed Jun 27, 2024
1 parent d725fc7 commit 641c4aa
Show file tree
Hide file tree
Showing 43 changed files with 805 additions and 790 deletions.
620 changes: 307 additions & 313 deletions include/faker-cxx/Helper.h

Large diffs are not rendered by default.

157 changes: 79 additions & 78 deletions include/faker-cxx/Number.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,92 +6,93 @@

namespace faker::number
{
namespace{
std::mt19937 pseudoRandomGenerator;
}
/**
* @brief Generates a random integer number in the given range, bounds included.
*
* @param min The minimum value of the range.
* @param max The maximum value of the range.
*
* @tparam I the type of the generated number, must be an integral type (int, long, long long, etc.).
*
* @throws std::invalid_argument if min is greater than max.
*
* @return T a random integer number
*/
template <std::integral I>
I integer(I min, I max)
/**
* @brief Generates a random integer number in the given range, bounds included.
*
* @param min The minimum value of the range.
* @param max The maximum value of the range.
*
* @tparam I the type of the generated number, must be an integral type (int, long, long long, etc.).
*
* @throws std::invalid_argument if min is greater than max.
*
* @return T a random integer number
*/
template <std::integral I>
I integer(I min, I max)
{
if (min > max)
{
if (min > max)
{
throw std::invalid_argument("Minimum value must be smaller than maximum value.");
}
throw std::invalid_argument("Minimum value must be smaller than maximum value.");
}

std::uniform_int_distribution<I> distribution(min, max);
static std::mt19937 pseudoRandomGenerator{std::random_device{}()};

return distribution(pseudoRandomGenerator);
}
std::uniform_int_distribution<I> distribution(min, max);

return distribution(pseudoRandomGenerator);
}

/**
* @brief Generates a random integer between 0 and the given maximum value, bounds included.
*
* @tparam I the type of the generated number, must be an integral type (int, long, long long, etc.).
* @param max the maximum value of the range.
*
* @throws std::invalid_argument if min is greater than max.
*
* @see integer<I>(I, I)
*
* @return T a random integer number
*/
template <std::integral I>
I integer(I max)
{
return integer<I>(static_cast<I>(0), max);
}

/**
* @brief Generates a random integer between 0 and the given maximum value, bounds included.
*
* @tparam I the type of the generated number, must be an integral type (int, long, long long, etc.).
* @param max the maximum value of the range.
*
* @throws std::invalid_argument if min is greater than max.
*
* @see integer<I>(I, I)
*
* @return T a random integer number
*/
template <std::integral I>
I integer(I max)
/**
* @brief Generates a random decimal number in the given range, bounds included.
*
* @tparam F the type of the generated number, must be a floating point type (float, double, long double).
*
* @param min The minimum value of the range.
* @param max The maximum value of the range.
*
* @throws std::invalid_argument if min is greater than max.
*
* @return F a random decimal number.
*/
template <std::floating_point F>
F decimal(F min, F max)
{
if (min > max)
{
return integer<I>(static_cast<I>(0), max);
throw std::invalid_argument("Minimum value must be smaller than maximum value.");
}

/**
* @brief Generates a random decimal number in the given range, bounds included.
*
* @tparam F the type of the generated number, must be a floating point type (float, double, long double).
*
* @param min The minimum value of the range.
* @param max The maximum value of the range.
*
* @throws std::invalid_argument if min is greater than max.
*
* @return F a random decimal number.
*/
template <std::floating_point F>
F decimal(F min, F max)
{
if (min > max)
{
throw std::invalid_argument("Minimum value must be smaller than maximum value.");
}
static std::mt19937 pseudoRandomGenerator{std::random_device{}()};

std::uniform_real_distribution<F> distribution(min, max);
std::uniform_real_distribution<F> distribution(min, max);

return distribution(pseudoRandomGenerator);
}
return distribution(pseudoRandomGenerator);
}

/**
* @brief Generates a random decimal number between 0 and the given maximum value, bounds included.
*
* @tparam F The type of the generated number, must be a floating point type (float, double, long double).
* @param max The maximum value of the range.
*
* @throws std::invalid_argument if max is less than 0.
*
* @see decimal<F>(F, F)
*
* @return F, a random decimal number.
*/
template <std::floating_point F>
F decimal(F max)
{
return decimal<F>(static_cast<F>(0.), max);
}
/**
* @brief Generates a random decimal number between 0 and the given maximum value, bounds included.
*
* @tparam F The type of the generated number, must be a floating point type (float, double, long double).
* @param max The maximum value of the range.
*
* @throws std::invalid_argument if max is less than 0.
*
* @see decimal<F>(F, F)
*
* @return F, a random decimal number.
*/
template <std::floating_point F>
F decimal(F max)
{
return decimal<F>(static_cast<F>(0.), max);
}
}
10 changes: 5 additions & 5 deletions src/modules/airline/Airline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,28 @@ namespace faker::airline
{
std::string_view aircraftType()
{
return Helper::arrayElement(aircraftTypes);
return helper::arrayElement(aircraftTypes);
}

Airplane airplane()
{
return Helper::arrayElement(airplanes);
return helper::arrayElement(airplanes);
}

AirlineInfo airline()
{
return Helper::arrayElement(airlines);
return helper::arrayElement(airlines);
}

Airport airport()
{
return Helper::arrayElement(airports);
return helper::arrayElement(airports);
}

std::string seat(AircraftType aircraftType)
{
return std::to_string(number::integer(1, aircraftTypeMaxRows.at(aircraftType))) +
Helper::arrayElement<char>(aircraftTypeSeatLetters.at(aircraftType));
helper::arrayElement<char>(aircraftTypeSeatLetters.at(aircraftType));
}

std::string recordLocator(bool allowNumerics)
Expand Down
30 changes: 15 additions & 15 deletions src/modules/animal/Animal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,76 +9,76 @@ namespace faker::animal
{
std::string_view bear()
{
return Helper::arrayElement(bears);
return helper::arrayElement(bears);
}

std::string_view bird()
{
return Helper::arrayElement(birds);
return helper::arrayElement(birds);
}

std::string_view cat()
{
return Helper::arrayElement(cats);
return helper::arrayElement(cats);
}

std::string_view cetacean()
{
return Helper::arrayElement(cetaceans);
return helper::arrayElement(cetaceans);
}

std::string_view cow()
{
return Helper::arrayElement(cows);
return helper::arrayElement(cows);
}

std::string_view crocodile()
{
return Helper::arrayElement(crocodiles);
return helper::arrayElement(crocodiles);
}

std::string_view dog()
{
return Helper::arrayElement(dogs);
return helper::arrayElement(dogs);
}

std::string_view fish()
{
return Helper::arrayElement(fishes);
return helper::arrayElement(fishes);
}

std::string_view horse()
{
return Helper::arrayElement(horses);
return helper::arrayElement(horses);
}

std::string_view insect()
{
return Helper::arrayElement(insects);
return helper::arrayElement(insects);
}

std::string_view lion()
{
return Helper::arrayElement(lions);
return helper::arrayElement(lions);
}

std::string_view rabbit()
{
return Helper::arrayElement(rabbits);
return helper::arrayElement(rabbits);
}

std::string_view rodent()
{
return Helper::arrayElement(rodents);
return helper::arrayElement(rodents);
}

std::string_view snake()
{
return Helper::arrayElement(snakes);
return helper::arrayElement(snakes);
}

std::string_view type()
{
return Helper::arrayElement(types);
return helper::arrayElement(types);
}
}
12 changes: 6 additions & 6 deletions src/modules/book/Book.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,31 @@ namespace faker::book
{
std::string_view title()
{
return Helper::arrayElement(titles);
return helper::arrayElement(titles);
}

std::string_view genre()
{
return Helper::arrayElement(bookGenres);
return helper::arrayElement(bookGenres);
}

std::string_view author()
{
return Helper::arrayElement(authors);
return helper::arrayElement(authors);
}

std::string_view publisher()
{
return Helper::arrayElement(publishers);
return helper::arrayElement(publishers);
}

std::string_view format()
{
return Helper::arrayElement(bookFormats);
return helper::arrayElement(bookFormats);
}

std::string_view series()
{
return Helper::arrayElement(bookSeries);
return helper::arrayElement(bookSeries);
}
}
2 changes: 1 addition & 1 deletion src/modules/color/Color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace faker::color
{
std::string_view name()
{
return Helper::arrayElement(colors);
return helper::arrayElement(colors);
}

std::string rgb(bool includeAlpha)
Expand Down
Loading

0 comments on commit 641c4aa

Please sign in to comment.