quake-tools/source/common.h

72 lines
1.7 KiB
C++

#pragma once
#include <QtEndian>
#include <QtGlobal>
#include <QCoreApplication>
#include <QDebug>
#include <algorithm>
#include <array>
#include <filesystem>
#include <fstream>
#include <ios>
#include <istream>
#include <iterator>
#include <optional>
#include <stdexcept>
#include <string>
#include <utility>
#include <variant>
static inline QString trMain(char const *sourceText,
char const *disambiguation = nullptr,
int n = -1) {
return QCoreApplication::translate("main", sourceText, disambiguation, n);
}
template<typename T>
static inline T readLE(std::istream &st) {
std::array<char, sizeof(T)> b;
st.read(b.data(), b.size());
return qFromLittleEndian<T>(b.data());
}
template<typename T>
static inline T readBE(std::istream &st) {
std::array<char, sizeof(T)> b;
st.read(b.data(), b.size());
return qFromBigEndian<T>(b.data());
}
template<std::size_t N>
static inline std::array<char, N> readBytes(std::istream &st) {
std::array<char, N> b;
st.read(b.data(), b.size());
return std::move(b);
}
static inline QDebug operator<<(QDebug debug, std::string const &t) {
debug << QString::fromStdString(t);
return debug;
}
template<typename T>
static inline QDebug operator<<(QDebug debug, std::unique_ptr<T> const &t) {
debug << *t;
return debug;
}
template<typename T0, typename... Ts>
static inline QDebug operator <<(QDebug debug,
std::variant<T0, Ts...> const &t) {
std::visit([&](auto &&arg) {debug << arg;}, t);
return debug;
}
static inline std::ifstream openReadBin(std::filesystem::path path) {
return std::ifstream{path, std::ios_base::in | std::ios_base::binary};
}
// EOF