aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
author Spartan322 <Megacake1234@gmail.com>2023-10-16 08:21:58 +0200
committer Spartan322 <Megacake1234@gmail.com>2023-10-16 08:23:26 +0200
commitc99f8fc82677b1c227a210c38b1496d908a24748 (patch)
tree0802b2532f6a10703ee420f05ba3175d9a19c3b9 /src
parent7c1b2d8cd4a543d201ada8325bcf29e2662d0b46 (diff)
Add constraint to `filename_equals`
Optimize allocations for `filename_equals`
Diffstat (limited to 'src')
-rw-r--r--src/openvic-simulation/dataloader/Dataloader.cpp27
1 files changed, 16 insertions, 11 deletions
diff --git a/src/openvic-simulation/dataloader/Dataloader.cpp b/src/openvic-simulation/dataloader/Dataloader.cpp
index f2e94d7..d0fb55d 100644
--- a/src/openvic-simulation/dataloader/Dataloader.cpp
+++ b/src/openvic-simulation/dataloader/Dataloader.cpp
@@ -45,17 +45,22 @@ static constexpr bool path_equals(std::string_view lhs, std::string_view rhs) {
#endif
}
-template<typename LT, typename RT>
-static bool filename_equals(const LT& lhs, const RT& rhs) {
- std::string left, right;
- if constexpr (std::same_as<LT, std::filesystem::path>)
- left = lhs.filename().string();
- else left = lhs;
-
- if constexpr (std::same_as<RT, std::filesystem::path>)
- right = rhs.filename().string();
- else right = rhs;
-
+template<typename T>
+concept is_filename = std::same_as<T, std::filesystem::path> || std::convertible_to<T, std::string_view>;
+
+bool filename_equals(const is_filename auto& lhs, const is_filename auto& rhs) {
+ auto left = [&lhs] {
+ if constexpr (std::same_as<std::decay_t<decltype(lhs)>, std::filesystem::path>)
+ return lhs.filename().string();
+ else
+ return lhs;
+ }();
+ auto right = [&rhs] {
+ if constexpr (std::same_as<std::decay_t<decltype(rhs)>, std::filesystem::path>)
+ return rhs.filename().string();
+ else
+ return rhs;
+ }();
return path_equals(left, right);
}