impr: Fix various issues with runtime-generated language strings

This commit is contained in:
WerWolv
2024-08-03 11:32:17 +02:00
parent efee128c1c
commit b2fc80f970
12 changed files with 108 additions and 73 deletions

View File

@@ -1273,7 +1273,7 @@ namespace hex {
void stopServices();
}
void registerService(Lang name, const impl::Callback &callback);
void registerService(const UnlocalizedString &unlocalizedString, const impl::Callback &callback);
}
/* Network Communication Interface Registry. Allows adding new communication interface endpoints */

View File

@@ -42,11 +42,13 @@ namespace hex {
struct UnlocalizedString;
class LangConst;
class Lang {
public:
Lang() = default;
explicit Lang(const char *unlocalizedString);
explicit Lang(const std::string &unlocalizedString);
explicit(false) Lang(const LangConst &localizedString);
explicit Lang(const UnlocalizedString &unlocalizedString);
explicit Lang(std::string_view unlocalizedString);
@@ -56,9 +58,22 @@ namespace hex {
const char* get() const;
constexpr static size_t hash(std::string_view string){
private:
std::size_t m_entryHash;
std::string m_unlocalizedString;
};
class LangConst {
public:
[[nodiscard]] operator std::string() const;
[[nodiscard]] operator std::string_view() const;
[[nodiscard]] operator const char *() const;
const char* get() const;
constexpr static size_t hash(std::string_view string) {
constexpr u64 p = 131;
constexpr u64 m = std::numeric_limits<std::uint32_t>::max() - 4; // Largest 32 bit prime
constexpr u64 m = std::numeric_limits<std::uint32_t>::max() - 4;
u64 total = 0;
u64 currentMultiplier = 1;
@@ -71,36 +86,24 @@ namespace hex {
}
private:
constexpr explicit Lang(std::size_t hash, const char *unlocalizedString) : m_entryHash(hash), m_unlocalizedString(unlocalizedString) {}
constexpr explicit LangConst(std::size_t hash, const char *unlocalizedString) : m_entryHash(hash), m_unlocalizedString(unlocalizedString) {}
template<wolv::type::StaticString>
friend consteval Lang operator""_lang();
friend consteval LangConst operator""_lang();
friend class Lang;
private:
std::size_t m_entryHash;
const char *m_unlocalizedString = nullptr;
};
[[nodiscard]] std::string operator+(const std::string &&left, const Lang &&right);
[[nodiscard]] std::string operator+(const Lang &&left, const std::string &&right);
[[nodiscard]] std::string operator+(const std::string_view &&left, const Lang &&right);
[[nodiscard]] std::string operator+(const Lang &&left, const std::string_view &&right);
[[nodiscard]] std::string operator+(const char *left, const Lang &&right);
[[nodiscard]] std::string operator+(const Lang &&left, const char *right);
[[nodiscard]] std::string operator+(const Lang &&left, const Lang &&right);
template<wolv::type::StaticString String>
[[nodiscard]] consteval Lang operator""_lang() {
return Lang(Lang::hash(String.value.data()), String.value.data());
}
struct UnlocalizedString {
public:
UnlocalizedString() = default;
UnlocalizedString(auto && arg) : m_unlocalizedString(std::forward<decltype(arg)>(arg)) {
static_assert(!std::same_as<std::remove_cvref_t<decltype(arg)>, Lang>, "Expected a unlocalized name, got a localized one!");
template<typename T>
UnlocalizedString(T &&arg) : m_unlocalizedString(std::forward<T>(arg)) {
static_assert(!std::same_as<std::remove_cvref_t<T>, Lang>, "Expected a unlocalized name, got a localized one!");
}
[[nodiscard]] operator std::string() const {
@@ -132,9 +135,17 @@ namespace hex {
std::string m_unlocalizedString;
};
// {fmt} formatter for hex::Lang
template<wolv::type::StaticString String>
[[nodiscard]] consteval LangConst operator""_lang() {
return LangConst(LangConst::hash(String.value.data()), String.value.data());
}
// {fmt} formatter for hex::Lang and hex::LangConst
inline auto format_as(const hex::Lang &entry) {
return entry.get();
}
inline auto format_as(const hex::LangConst &entry) {
return entry.get();
}
}

View File

@@ -22,7 +22,7 @@ namespace hex {
class Task {
public:
Task() = default;
Task(Lang name, u64 maxValue, bool background, std::function<void(Task &)> function);
Task(const UnlocalizedString &unlocalizedName, u64 maxValue, bool background, std::function<void(Task &)> function);
Task(const Task&) = delete;
Task(Task &&other) noexcept;
@@ -65,7 +65,7 @@ namespace hex {
void clearException();
[[nodiscard]] std::string getExceptionMessage() const;
[[nodiscard]] const Lang &getName();
[[nodiscard]] const UnlocalizedString &getUnlocalizedName();
[[nodiscard]] u64 getValue() const;
[[nodiscard]] u64 getMaxValue() const;
@@ -77,7 +77,7 @@ namespace hex {
private:
mutable std::mutex m_mutex;
Lang m_name;
UnlocalizedString m_unlocalizedName;
std::atomic<u64> m_currValue = 0, m_maxValue = 0;
std::function<void()> m_interruptCallback;
std::function<void(Task &)> m_function;
@@ -130,20 +130,20 @@ namespace hex {
/**
* @brief Creates a new asynchronous task that gets displayed in the Task Manager in the footer
* @param name Name of the task
* @param unlocalizedName Name of the task
* @param maxValue Maximum value of the task
* @param function Function to be executed
* @return A TaskHolder holding a weak reference to the task
*/
static TaskHolder createTask(Lang name, u64 maxValue, std::function<void(Task &)> function);
static TaskHolder createTask(const UnlocalizedString &unlocalizedName, u64 maxValue, std::function<void(Task &)> function);
/**
* @brief Creates a new asynchronous task that does not get displayed in the Task Manager
* @param name Name of the task
* @param unlocalizedName Name of the task
* @param function Function to be executed
* @return A TaskHolder holding a weak reference to the task
*/
static TaskHolder createBackgroundTask(Lang name, std::function<void(Task &)> function);
static TaskHolder createBackgroundTask(const UnlocalizedString &unlocalizedName, std::function<void(Task &)> function);
/**
* @brief Creates a new synchronous task that will execute the given function at the start of the next frame
@@ -190,7 +190,7 @@ namespace hex {
static void runDeferredCalls();
private:
static TaskHolder createTask(Lang name, u64 maxValue, bool background, std::function<void(Task &)> function);
static TaskHolder createTask(const UnlocalizedString &unlocalizedName, u64 maxValue, bool background, std::function<void(Task &)> function);
};
}