Skip to content

Commit

Permalink
Make some types format-able
Browse files Browse the repository at this point in the history
  • Loading branch information
wichtounet committed Nov 23, 2024
1 parent 4aa7a7d commit 8c704fb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
8 changes: 8 additions & 0 deletions include/date.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <ctime>
#include <limits>
#include <chrono>
#include <format>

#include "cpp_utils/assert.hpp"

Expand Down Expand Up @@ -582,4 +583,11 @@ struct hash<budget::date> {
}
};

template <>
struct formatter<budget::date> : std::formatter<std::string> {
auto format(budget::date p, format_context& ctx) const {
return formatter<string>::format(date_to_string(p), ctx);
}
};

} // end of namespace std
20 changes: 20 additions & 0 deletions include/money.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <string>
#include <ostream>
#include <format>

#include "utils.hpp"

Expand Down Expand Up @@ -230,3 +231,22 @@ inline std::string to_string(money amount){
}

} //end of namespace budget

namespace std {

template <>
struct hash<budget::money> {
std::size_t operator()(budget::money d) const noexcept {
const std::hash<long> hasher;
return hasher(d.value);
}
};

template <>
struct formatter<budget::money> : std::formatter<std::string> {
auto format(budget::money p, format_context& ctx) const {
return formatter<string>::format(money_to_string(p), ctx);
}
};

} // end of namespace std

0 comments on commit 8c704fb

Please sign in to comment.