2023-06-20 04:33:09 +00:00
|
|
|
#ifndef STAR_FORMAT_HPP
|
|
|
|
#define STAR_FORMAT_HPP
|
|
|
|
|
|
|
|
#include "StarMemory.hpp"
|
2023-06-27 14:20:22 +00:00
|
|
|
#include "StarException.hpp"
|
2023-06-20 04:33:09 +00:00
|
|
|
|
2023-06-27 09:24:35 +00:00
|
|
|
#include "fmt/core.h"
|
|
|
|
#include "fmt/ostream.h"
|
|
|
|
#include "fmt/format.h"
|
|
|
|
#include "fmt/ranges.h"
|
|
|
|
|
2023-06-20 04:33:09 +00:00
|
|
|
namespace Star {
|
|
|
|
|
2023-06-27 14:20:22 +00:00
|
|
|
STAR_EXCEPTION(FormatException, StarException);
|
2023-06-20 04:33:09 +00:00
|
|
|
|
2023-06-27 14:20:22 +00:00
|
|
|
template <typename... T>
|
|
|
|
std::string strf(fmt::format_string<T...> fmt, T&&... args) {
|
|
|
|
try { return fmt::format(fmt, args...); }
|
|
|
|
catch (std::exception const& e) {
|
|
|
|
throw FormatException(strf("Exception thrown during string format: {}", e.what()));
|
2023-06-20 04:33:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-27 09:24:35 +00:00
|
|
|
template <typename... T>
|
|
|
|
void format(std::ostream& out, fmt::format_string<T...> fmt, T&&... args) {
|
2023-06-27 14:20:22 +00:00
|
|
|
out << strf(fmt, args...);
|
2023-06-20 04:33:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Automatically flushes, use format to avoid flushing.
|
|
|
|
template <typename... Args>
|
|
|
|
void coutf(char const* fmt, Args const&... args) {
|
|
|
|
format(std::cout, fmt, args...);
|
|
|
|
std::cout.flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Automatically flushes, use format to avoid flushing.
|
|
|
|
template <typename... Args>
|
|
|
|
void cerrf(char const* fmt, Args const&... args) {
|
|
|
|
format(std::cerr, fmt, args...);
|
|
|
|
std::cerr.flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|