osb/source/core/StarFormat.hpp

48 lines
1.1 KiB
C++
Raw Permalink Normal View History

#pragma once
2023-06-20 04:33:09 +00:00
#include "StarMemory.hpp"
#include "StarException.hpp"
2023-06-20 04:33:09 +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 {
STAR_EXCEPTION(FormatException, StarException);
2023-06-20 04:33:09 +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
}
}
template <typename... T>
void format(std::ostream& out, fmt::format_string<T...> fmt, T&&... args) {
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();
}
template <class Type>
inline std::string toString(Type const& t) {
return fmt::to_string(t);
}
2023-06-20 04:33:09 +00:00
}