Skip to content

Commit

Permalink
chore: Remove anonymous namespace from GeneralUtils.h (#1460)
Browse files Browse the repository at this point in the history
* remove anonymous namespace from GeneralUtils.h

* Put helper functions in nested details namespace to hide from hinting

* rename implementation functions to use lower case, leading underscore and move definitions to source file
  • Loading branch information
jadebenn authored Feb 24, 2024
1 parent f38537a commit 5ae8fd8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 34 deletions.
50 changes: 36 additions & 14 deletions dCommon/GeneralUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,28 +294,50 @@ std::u16string GeneralUtils::ReadWString(RakNet::BitStream* inStream) {

std::vector<std::string> GeneralUtils::GetSqlFileNamesFromFolder(const std::string& folder) {
// Because we dont know how large the initial number before the first _ is we need to make it a map like so.
std::map<uint32_t, std::string> filenames{};
std::map<uint32_t, std::string> filenames{};
for (auto& t : std::filesystem::directory_iterator(folder)) {
auto filename = t.path().filename().string();
auto index = std::stoi(GeneralUtils::SplitString(filename, '_').at(0));
filenames.insert(std::make_pair(index, filename));
auto filename = t.path().filename().string();
auto index = std::stoi(GeneralUtils::SplitString(filename, '_').at(0));
filenames.insert(std::make_pair(index, filename));
}

// Now sort the map by the oldest migration.
std::vector<std::string> sortedFiles{};
auto fileIterator = filenames.begin();
std::map<uint32_t, std::string>::iterator oldest = filenames.begin();
while (!filenames.empty()) {
auto fileIterator = filenames.begin();
std::map<uint32_t, std::string>::iterator oldest = filenames.begin();
while (!filenames.empty()) {
if (fileIterator == filenames.end()) {
sortedFiles.push_back(oldest->second);
filenames.erase(oldest);
fileIterator = filenames.begin();
oldest = filenames.begin();
continue;
sortedFiles.push_back(oldest->second);
filenames.erase(oldest);
fileIterator = filenames.begin();
oldest = filenames.begin();
continue;
}
if (oldest->first > fileIterator->first) oldest = fileIterator;
fileIterator++;
if (oldest->first > fileIterator->first) oldest = fileIterator;
fileIterator++;
}

return sortedFiles;
}

#ifdef DARKFLAME_PLATFORM_MACOS

// MacOS floating-point parse function specializations
namespace GeneralUtils::details {
template <>
[[nodiscard]] float _parse<float>(const std::string_view str, size_t& parseNum) {
return std::stof(std::string{ str }, &parseNum);
}

template <>
[[nodiscard]] double _parse<double>(const std::string_view str, size_t& parseNum) {
return std::stod(std::string{ str }, &parseNum);
}

template <>
[[nodiscard]] long double _parse<long double>(const std::string_view str, size_t& parseNum) {
return std::stold(std::string{ str }, &parseNum);
}
}

#endif
26 changes: 6 additions & 20 deletions dCommon/GeneralUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,25 +168,10 @@ namespace GeneralUtils {

#ifdef DARKFLAME_PLATFORM_MACOS

// Anonymous namespace containing MacOS floating-point parse function specializations
namespace {
// MacOS floating-point parse helper function specializations
namespace details {
template <std::floating_point T>
[[nodiscard]] T Parse(const std::string_view str, size_t* parseNum);

template <>
[[nodiscard]] float Parse<float>(const std::string_view str, size_t* parseNum) {
return std::stof(std::string{ str }, parseNum);
}

template <>
[[nodiscard]] double Parse<double>(const std::string_view str, size_t* parseNum) {
return std::stod(std::string{ str }, parseNum);
}

template <>
[[nodiscard]] long double Parse<long double>(const std::string_view str, size_t* parseNum) {
return std::stold(std::string{ str }, parseNum);
}
[[nodiscard]] T _parse(const std::string_view str, size_t& parseNum);
}

/**
Expand All @@ -196,9 +181,10 @@ namespace GeneralUtils {
* @returns An std::optional containing the desired value if it is equivalent to the string
*/
template <std::floating_point T>
[[nodiscard]] std::optional<T> TryParse(const std::string_view str) noexcept try {
[[nodiscard]] std::optional<T> TryParse(const std::string_view str) noexcept
try {
size_t parseNum;
const T result = Parse<T>(str, &parseNum);
const T result = details::_parse<T>(str, parseNum);
const bool isParsed = str.length() == parseNum;

return isParsed ? result : std::optional<T>{};
Expand Down

0 comments on commit 5ae8fd8

Please sign in to comment.