From f65eff6edfe4d8f654afe17693b7c78c6dc37cd3 Mon Sep 17 00:00:00 2001 From: yyc12345 Date: Wed, 13 Aug 2025 08:51:53 +0800 Subject: [PATCH] fix: replace find with contains in constraint builder - remove useless old files. --- src/YYCCLegacy/Constraints.hpp | 85 ------------------ src/YYCCLegacy/IOHelper.cpp | 37 -------- src/YYCCLegacy/IOHelper.hpp | 38 --------- src/YYCCLegacy/YYCCInternal.hpp | 147 -------------------------------- src/YYCCommonplaceLegacy.hpp | 18 ---- src/yycc/constraint/builder.hpp | 10 +-- src/yycc/patch/fopen.cpp | 3 +- 7 files changed, 5 insertions(+), 333 deletions(-) delete mode 100644 src/YYCCLegacy/Constraints.hpp delete mode 100644 src/YYCCLegacy/IOHelper.cpp delete mode 100644 src/YYCCLegacy/IOHelper.hpp delete mode 100644 src/YYCCLegacy/YYCCInternal.hpp delete mode 100644 src/YYCCommonplaceLegacy.hpp diff --git a/src/YYCCLegacy/Constraints.hpp b/src/YYCCLegacy/Constraints.hpp deleted file mode 100644 index 2b879d4..0000000 --- a/src/YYCCLegacy/Constraints.hpp +++ /dev/null @@ -1,85 +0,0 @@ -#pragma once -#include "YYCCInternal.hpp" - -#include -#include -#include -#include - -/** - * @brief The namespace containing constraint declaration - * and functions generating common used constraint. -*/ -namespace YYCC::Constraints { - - /** - * @brief The constraint applied to settings to limit its stored value. - * @tparam _Ty The data type this constraint need to be processed with. - */ - template - struct Constraint { - /// @brief Return true if value is legal, otherwise false. - using CheckFct_t = std::function; - /// @brief The function pointer used for checking whether given value is valid. - CheckFct_t m_CheckFct; - - /** - * @brief Check whether this constraint is valid for using. - * @return - * True if this constraint is valid, otherwise false. - * If this function return false, it means that there is no contraint. - * And you should not use this constraint provided any function pointer. - */ - bool IsValid() const { - return m_CheckFct != nullptr; - } - }; - - /** - * @brief Get constraint for arithmetic or enum values by minimum and maximum value range. - * @tparam _Ty An arithmetic or enum type (except bool) of underlying stored value. - * @param[in] min_value The minimum value of range (inclusive). - * @param[in] max_value The maximum value of range (inclusive). - * @return The generated constraint instance which can be directly applied. - */ - template && !std::is_same_v<_Ty, bool>, int> = 0> - Constraint<_Ty> GetMinMaxRangeConstraint(_Ty min_value, _Ty max_value) { - if (min_value > max_value) - throw std::invalid_argument("invalid min max value for NumberRangeConstraint"); - return Constraint<_Ty> { - [min_value, max_value](const _Ty& val) -> bool { return (val <= max_value) && (val >= min_value); } - /*[min_value, max_value](const _Ty& val) -> _Ty { return std::clamp(val, min_value, max_value); }*/ - }; - } - - /** - * @brief Get constraint for enum values by enumerating all possible values. - * @tparam _Ty An enum type (except bool) of underlying stored value. - * @param[in] il An initializer list storing all possible values. - * @return The generated constraint instance which can be directly applied. - */ - template, int> = 0> - Constraint<_Ty> GetEnumEnumerationConstraint(const std::initializer_list<_Ty>& il) { - std::set<_Ty> data(il); - return Constraint<_Ty> { - [data](const _Ty& val) -> bool { return data.find(val) != data.end(); } - }; - } - - /** - * @brief Get constraint for string values by enumerating all possible values. - * @param[in] il An initializer list storing all possible values. - * @return The generated constraint instance which can be directly applied. - * @remarks - * Caller must make sure that the string view passed in initializer list is valid until this Constraint life time gone. - * Becasue this generator will not copy your given string view into string. - */ - inline Constraint GetStringEnumerationConstraint(const std::initializer_list& il) { - std::set data(il); - return Constraint { - [data](const yycc_u8string& val) -> bool { return data.find(yycc_u8string_view(val)) != data.end(); } - }; - } - -} - diff --git a/src/YYCCLegacy/IOHelper.cpp b/src/YYCCLegacy/IOHelper.cpp deleted file mode 100644 index dd637dd..0000000 --- a/src/YYCCLegacy/IOHelper.cpp +++ /dev/null @@ -1,37 +0,0 @@ -#include "IOHelper.hpp" - -#include "EncodingHelper.hpp" -#include -#include -#include -#include -#include - -#if defined(YYCC_OS_WINDOWS) -#include "WinImportPrefix.hpp" -#include -#include "WinImportSuffix.hpp" -#endif - -namespace YYCC::IOHelper { - - std::FILE* UTF8FOpen(const yycc_char8_t* u8_filepath, const yycc_char8_t* u8_mode) { -#if defined(YYCC_OS_WINDOWS) - - // convert mode and file path to wchar - std::wstring wmode, wpath; - if (!YYCC::EncodingHelper::UTF8ToWchar(u8_mode, wmode)) - return nullptr; - if (!YYCC::EncodingHelper::UTF8ToWchar(u8_filepath, wpath)) - return nullptr; - - // call microsoft specified fopen which support wchar as argument. - return _wfopen(wpath.c_str(), wmode.c_str()); - -#else - return std::fopen(EncodingHelper::ToOrdinary(u8_filepath), EncodingHelper::ToOrdinary(u8_mode)); -#endif - } - -} - diff --git a/src/YYCCLegacy/IOHelper.hpp b/src/YYCCLegacy/IOHelper.hpp deleted file mode 100644 index 71547d3..0000000 --- a/src/YYCCLegacy/IOHelper.hpp +++ /dev/null @@ -1,38 +0,0 @@ -#pragma once -#include "YYCCInternal.hpp" - -#include -#include - -/** - * @brief Some IO related stuff - * @details - * See also \ref io_helper. -*/ -namespace YYCC::IOHelper { - - /// @brief C++ standard deleter for std::FILE* - class StdFileDeleter { - public: - StdFileDeleter() {} - void operator() (std::FILE* ptr) { - if (ptr != nullptr) { - std::fclose(ptr); - } - } - }; - /// @brief Smart unique pointer of \c std::FILE* - using SmartStdFile = std::unique_ptr; - - /** - * @brief The UTF8 version of \c std::fopen. - * @param[in] u8_filepath The UTF8 encoded path to the file to be opened. - * @param[in] u8_mode UTF8 encoded mode string of the file to be opened. - * @remarks - * This function is suit for Windows because std::fopen do not support UTF8 on Windows. - * On other platforms, this function will delegate request directly to std::fopen. - * @return \c FILE* of the file to be opened, or nullptr if failed. - */ - std::FILE* UTF8FOpen(const yycc_char8_t* u8_filepath, const yycc_char8_t* u8_mode); - -} diff --git a/src/YYCCLegacy/YYCCInternal.hpp b/src/YYCCLegacy/YYCCInternal.hpp deleted file mode 100644 index cc62970..0000000 --- a/src/YYCCLegacy/YYCCInternal.hpp +++ /dev/null @@ -1,147 +0,0 @@ -#pragma once - -#pragma region Library Version and Comparison Macros - -#include "YYCCVersion.hpp" - -/// @brief Return true if left version number is equal to right version number, otherwise false. -#define YYCC_VERCMP_E(av1, av2, av3, bv1, bv2, bv3) ((av1) == (bv1) && (av2) == (bv2) && (av3) == (bv3)) -/// @brief Return true if left version number is not equal to right version number, otherwise false. -#define YYCC_VERCMP_NE(av1, av2, av3, bv1, bv2, bv3) (!YYCC_VERCMP_E(av1, av2, av3, bv1, bv2, bv3)) -/// @brief Return true if left version number is greater than right version number, otherwise false. -#define YYCC_VERCMP_G(av1, av2, av3, bv1, bv2, bv3) ( \ - ((av1) > (bv1)) || \ - ((av1) == (bv1) && (av2) > (bv2)) || \ - ((av1) == (bv1) && (av2) == (bv2) && (av3) > (bv3)) \ -) -/// @brief Return true if left version number is greater than or equal to right version number, otherwise false. -#define YYCC_VERCMP_GE(av1, av2, av3, bv1, bv2, bv3) (YYCC_VERCMP_G(av1, av2, av3, bv1, bv2, bv3) || YYCC_VERCMP_E(av1, av2, av3, bv1, bv2, bv3)) -/// @brief Return true if left version number is not lower than right version number, otherwise false. -#define YYCC_VERCMP_NL(av1, av2, av3, bv1, bv2, bv3) YYCC_VERCMP_GE(av1, av2, av3, bv1, bv2, bv3) -/// @brief Return true if left version number is lower than right version number, otherwise false. -#define YYCC_VERCMP_L(av1, av2, av3, bv1, bv2, bv3) ( \ - ((av1) < (bv1)) || \ - ((av1) == (bv1) && (av2) < (bv2)) || \ - ((av1) == (bv1) && (av2) == (bv2) && (av3) < (bv3)) \ -) -/// @brief Return true if left version number is lower than or equal to right version number, otherwise false. -#define YYCC_VERCMP_LE(av1, av2, av3, bv1, bv2, bv3) (YYCC_VERCMP_L(av1, av2, av3, bv1, bv2, bv3) || YYCC_VERCMP_E(av1, av2, av3, bv1, bv2, bv3)) -/// @brief Return true if left version number is not greater than right version number, otherwise false. -#define YYCC_VERCMP_NG(av1, av2, av3, bv1, bv2, bv3) YYCC_VERCMP_LE(av1, av2, av3, bv1, bv2, bv3) - -#pragma endregion - -#pragma region Operating System Identifier Macros - -// Define operating system macros -#define YYCC_OS_WINDOWS 2 -#define YYCC_OS_LINUX 3 -// Check current operating system. -#if defined(_WIN32) -#define YYCC_OS YYCC_OS_WINDOWS -#else -#define YYCC_OS YYCC_OS_LINUX -#endif - -#pragma endregion - -#pragma region Windows Shitty Behavior Disable Macros - -// If we are in Windows, -// we need add 2 macros to disable Windows shitty warnings and errors of -// depracted functions and not secure functions. -#if defined(YYCC_OS_WINDOWS) - -#if !defined(_CRT_SECURE_NO_WARNINGS) -#define _CRT_SECURE_NO_WARNINGS -#endif -#if !defined(_CRT_NONSTDC_NO_DEPRECATE) -#define _CRT_NONSTDC_NO_DEPRECATE -#endif - -#endif - -#pragma endregion - -#pragma region YYCC UTF8 Types - -// Define the UTF8 char type we used. -// And do a polyfill if no embedded char8_t type. - -#include -#include - -/** - * @brief Library core namespace - * @details Almost library functions are located in this namespace. -*/ -namespace YYCC { -#if defined(__cpp_char8_t) - using yycc_char8_t = char8_t; - using yycc_u8string = std::u8string; - using yycc_u8string_view = std::u8string_view; -#else - using yycc_char8_t = unsigned char; - using yycc_u8string = std::basic_string; - using yycc_u8string_view = std::basic_string_view; -#endif -} -/** - \typedef YYCC::yycc_char8_t - \brief YYCC UTF8 char type. - \details - This char type is an alias to \c std::char8_t if your current C++ standard support it. - Otherwise it is defined as unsigned char as C++ 20 stdandard does. -*/ -/** - \typedef YYCC::yycc_u8string - \brief YYCC UTF8 string container type. - \details - This type is defined as \c std::basic_string. - It is equal to \c std::u8string if your current C++ standard support it. -*/ -/** - \typedef YYCC::yycc_u8string_view - \brief YYCC UTF8 string view type. - \details - This type is defined as \c std::basic_string_view. - It is equal to \c std::u8string_view if your current C++ standard support it. -*/ - -#pragma endregion - -#pragma region Batch Class Move / Copy Function Macros - - /// @brief Explicitly remove copy (\c constructor and \c operator\=) for given class. -#define YYCC_DEL_CLS_COPY(CLSNAME) \ - CLSNAME(const CLSNAME&) = delete; \ - CLSNAME& operator=(const CLSNAME&) = delete; - - /// @brief Explicitly remove move (\c constructor and \c operator\=) for given class. -#define YYCC_DEL_CLS_MOVE(CLSNAME) \ - CLSNAME(CLSNAME&&) = delete; \ - CLSNAME& operator=(CLSNAME&&) = delete; - - /// @brief Explicitly remove (copy and move) (\c constructor and \c operator\=) for given class. -#define YYCC_DEL_CLS_COPY_MOVE(CLSNAME) \ - YYCC_DEL_CLS_COPY(CLSNAME) \ - YYCC_DEL_CLS_MOVE(CLSNAME) - - /// @brief Explicitly set default copy (\c constructor and \c operator\=) for given class. -#define YYCC_DEF_CLS_COPY(CLSNAME) \ - CLSNAME(const CLSNAME&) = default; \ - CLSNAME& operator=(const CLSNAME&) = default; - - /// @brief Explicitly set default move (\c constructor and \c operator\=) for given class. -#define YYCC_DEF_CLS_MOVE(CLSNAME) \ - CLSNAME(CLSNAME&&) = default; \ - CLSNAME& operator=(CLSNAME&&) = default; - - /// @brief Explicitly set default (copy and move) (\c constructor and \c operator\=) for given class. -#define YYCC_DEF_CLS_COPY_MOVE(CLSNAME) \ - YYCC_DEF_CLS_COPY(CLSNAME) \ - YYCC_DEF_CLS_MOVE(CLSNAME) - -#pragma endregion - - diff --git a/src/YYCCommonplaceLegacy.hpp b/src/YYCCommonplaceLegacy.hpp deleted file mode 100644 index 7f7e51b..0000000 --- a/src/YYCCommonplaceLegacy.hpp +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once - -#include "YYCC/YYCCInternal.hpp" - -#include "YYCC/EncodingHelper.hpp" -#include "YYCC/StringHelper.hpp" -#include "YYCC/ConsoleHelper.hpp" -#include "YYCC/COMHelper.hpp" -#include "YYCC/DialogHelper.hpp" -#include "YYCC/ParserHelper.hpp" -#include "YYCC/IOHelper.hpp" -#include "YYCC/WinFctHelper.hpp" -#include "YYCC/StdPatch.hpp" -#include "YYCC/EnumHelper.hpp" -#include "YYCC/ExceptionHelper.hpp" - -#include "YYCC/ConfigManager.hpp" -#include "YYCC/ArgParser.hpp" diff --git a/src/yycc/constraint/builder.hpp b/src/yycc/constraint/builder.hpp index 0d2186e..587ff49 100644 --- a/src/yycc/constraint/builder.hpp +++ b/src/yycc/constraint/builder.hpp @@ -35,14 +35,10 @@ namespace yycc::constraint::builder { T default_entry = il.begin()[default_index]; std::set entries(il); - // TODO: modify it as `contain` once we finish patch namespace. - auto fn_check = [entries](const T& val) -> bool { return entries.find(val) != entries.end(); }; + auto fn_check = [entries](const T& val) -> bool { return entries.contains(val); }; auto fn_clamp = [entries, default_entry](const T& val) -> T { - if (entries.find(val) != entries.end()) { - return val; - } else { - return default_entry; - } + if (entries.contains(val)) return val; + else return default_entry; }; return Constraint(std::move(fn_check), fn_clamp); } diff --git a/src/yycc/patch/fopen.cpp b/src/yycc/patch/fopen.cpp index 772e044..8bf3470 100644 --- a/src/yycc/patch/fopen.cpp +++ b/src/yycc/patch/fopen.cpp @@ -24,7 +24,8 @@ namespace yycc::patch::fopen { // check convertion success if (wmode.has_value() && wpath.has_value()) { - // call microsoft specified fopen which support wchar as argument. + // Call MSVCRT specified fopen which support wchar as argument. + // Reference: https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/fopen-wfopen?view=msvc-170 return _wfopen(wpath.value().c_str(), wmode.value().c_str()); } else { // fail to convert encoding