1
0

feat: add various detector.

- add endian and compiler detector, and modify os detector.
- now we use CMake to add detector-used macro, instead of using some C++ features to detect them.
- change Windows environment detection according to the change of os detector.
This commit is contained in:
2025-07-23 10:18:01 +08:00
parent 6043609709
commit 821a592f02
29 changed files with 84 additions and 49 deletions

View File

@ -1,8 +1,9 @@
#include "iconv.hpp"
#if YYCC_FEAT_ICONV || (YYCC_OS != YYCC_OS_WINDOWS)
#if YYCC_FEAT_ICONV || !defined(YYCC_OS_WINDOWS)
#include "../string/reinterpret.hpp"
#include "../macro/endian_detector.hpp"
#include <cerrno>
#include <stdexcept>
#include <cstdint>
@ -190,20 +191,30 @@ namespace yycc::encoding::iconv {
// That's not what we expected.
// So we need manually check runtime endian and explicitly specify endian in code name.
// TODO: fix this encoding endian issue.
static const NS_YYCC_STRING::u8char* UTF8_CODENAME_LITERAL = YYCC_U8("UTF-8");
static const NS_YYCC_STRING::u8char* WCHAR_CODENAME_LITERAL = YYCC_U8("WCHAR_T");
static const NS_YYCC_STRING::u8char* fetch_utf16_codename() {
return YYCC_U8("UTF16");
#if defined(YYCC_ENDIAN_LITTLE)
return YYCC_U8("UTF16LE");
#else
return YYCC_U8("UTF16BE");
#endif
}
static const NS_YYCC_STRING::u8char* UTF16_CODENAME_LITERAL = fetch_utf16_codename();
static const NS_YYCC_STRING::u8char* fetch_utf32_codename() {
return YYCC_U8("UTF32");
#if defined(YYCC_ENDIAN_LITTLE)
return YYCC_U8("UTF32LE");
#else
return YYCC_U8("UTF32BE");
#endif
}
static const NS_YYCC_STRING::u8char* UTF32_CODENAME_LITERAL = fetch_utf32_codename();
// TODO: There is a memory copy in this function. Consider removing it in future.
// TODO:
// There is a memory copy in this function. Consider optimizing it in future.
// A possible solution is that create a std::vector-like wrapper for std::basic_string and std::basic_string_view.
// We call them VecString and VecStringView, and use them in "iconv_kernel" instead of real std::vector.
// They exposed interface are std::vector-like but its inner is std::basic_string and std::basic_string_view.
#define CONVFN_TYPE0(src_char_type, dst_char_type) \
namespace expected = NS_YYCC_PATCH_EXPECTED; \
auto rv = iconv_kernel(this->token, reinterpret_cast<const uint8_t*>(src.data()), src.size()); \

View File

@ -1,7 +1,7 @@
#pragma once
#include "../macro/os_detector.hpp"
#if YYCC_FEAT_ICONV || (YYCC_OS != YYCC_OS_WINDOWS)
#if YYCC_FEAT_ICONV || !defined(YYCC_OS_WINDOWS)
#include "../macro/class_copy_move.hpp"
#include "../patch/expected.hpp"

View File

@ -1,6 +1,6 @@
#include "windows.hpp"
#if YYCC_OS == YYCC_OS_WINDOWS
#if defined(YYCC_OS_WINDOWS)
#include "../string/reinterpret.hpp"
#include <limits>

View File

@ -1,7 +1,7 @@
#pragma once
#include "../macro/os_detector.hpp"
#if YYCC_OS == YYCC_OS_WINDOWS
#if defined(YYCC_OS_WINDOWS)
#include "../patch/expected.hpp"
#include "../string.hpp"

View File

@ -0,0 +1,5 @@
#pragma once
#if (defined(YYCC_CC_MSVC) + defined(YYCC_CC_GCC) + defined(YYCC_CC_CLANG)) != 1
#error "Current compiler is not supported!"
#endif

View File

@ -0,0 +1,7 @@
#pragma once
// Check endian
#if (defined(YYCC_ENDIAN_LITTLE) + defined(YYCC_ENDIAN_BIG)) != 1
#error "Current system endian (byte order) is not supported!"
#endif

View File

@ -1,11 +1,6 @@
#pragma once
// 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
// Check OS macro
#if (defined(YYCC_OS_WINDOWS) + defined(YYCC_OS_LINUX)) != 1
#error "Current operating system is not supported!"
#endif

View File

@ -12,7 +12,7 @@ namespace yycc::patch::path {
// So we need add feature test macro at the same time.
std::filesystem::path to_std_path(const NS_YYCC_STRING::u8string_view& u8_path) {
// #if YYCC_OS == YYCC_OS_WINDOWS
// #if defined(YYCC_OS_WINDOWS)
// // convert path to wchar
// std::wstring wpath;
@ -29,7 +29,7 @@ namespace yycc::patch::path {
}
NS_YYCC_STRING::u8string to_u8string(const std::filesystem::path& path) {
// #if YYCC_OS == YYCC_OS_WINDOWS
// #if defined(YYCC_OS_WINDOWS)
// // get and convert to utf8
// NS_YYCC_STRING::u8string u8_path;

View File

@ -1,5 +1,8 @@
#pragma once
// Suppress unsafe warning on Windows
#include "../windows/unsafe_suppressor.hpp"
// Prelude section
#include "../string.hpp"
namespace yycc::prelude {

View File

@ -4,7 +4,7 @@
#include "../macro/os_detector.hpp"
#if YYCC_OS == YYCC_OS_WINDOWS
#if defined(YYCC_OS_WINDOWS)
// Define 2 macros to disallow Windows generate MIN and MAX macros
// which cause std::min and std::max can not function as normal.

View File

@ -4,7 +4,7 @@
#include "../macro/os_detector.hpp"
#if YYCC_OS == YYCC_OS_WINDOWS
#if defined(YYCC_OS_WINDOWS)
// Windows also will generate following macros
// which may cause the function sign is different in Windows and other platforms.

View File

@ -4,7 +4,7 @@
// 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 YYCC_OS == YYCC_OS_WINDOWS
#if defined(YYCC_OS_WINDOWS)
#if !defined(_CRT_SECURE_NO_WARNINGS)
#define _CRT_SECURE_NO_WARNINGS