fix: use new way to manage iconv token.

- use new way to manage iconv token, instead of std::unique_ptr which cause template error (no viable sizeof) in pycodec.
- fix the wrong name in pycodec.
- remove useless code.
This commit is contained in:
2025-08-12 19:40:23 +08:00
parent 9ce52e8d4b
commit 2576523dbb
3 changed files with 25 additions and 17 deletions

View File

@ -397,14 +397,6 @@ namespace yycc::carton::pycodec {
#pragma region Char -> UTF8 #pragma region Char -> UTF8
// CharToUtf8::CharToUtf8(const EncodingName& name) :
// #if defined(YYCC_PYCODEC_WIN32_BACKEND)
// code_page(fetch)
// #else
// inner(fetch_iconv_name())
// #endif
// {}
CharToUtf8::CharToUtf8(const EncodingName& name) : inner(std::nullopt) { CharToUtf8::CharToUtf8(const EncodingName& name) : inner(std::nullopt) {
#if defined(YYCC_PYCODEC_WIN32_BACKEND) #if defined(YYCC_PYCODEC_WIN32_BACKEND)
auto rv = fetch_code_page(name); auto rv = fetch_code_page(name);
@ -436,7 +428,7 @@ namespace yycc::carton::pycodec {
if (rv.has_value()) inner = rv.value(); if (rv.has_value()) inner = rv.value();
#else #else
auto rv = fetch_iconv_name(name); auto rv = fetch_iconv_name(name);
if (rv.has_value()) inner = YYCC_PYCODEC_BACKEND_NS::CharToUtf8(rv.value()); if (rv.has_value()) inner = YYCC_PYCODEC_BACKEND_NS::Utf8ToChar(rv.value());
#endif #endif
} }

View File

@ -71,11 +71,11 @@ namespace yycc::encoding::iconv {
that_iconv_close(this->inner); that_iconv_close(this->inner);
} }
} }
PrivToken(PrivToken&& rhs) : inner(rhs.inner) { PrivToken(PrivToken&& rhs) noexcept : inner(rhs.inner) {
// Reset rhs inner // Reset rhs inner
rhs.inner = INVALID_ICONV_TOKEN; rhs.inner = INVALID_ICONV_TOKEN;
} }
PrivToken& operator=(PrivToken&& rhs) { PrivToken& operator=(PrivToken&& rhs) noexcept {
// Free self first // Free self first
if (this->inner != INVALID_ICONV_TOKEN) { if (this->inner != INVALID_ICONV_TOKEN) {
that_iconv_close(this->inner); that_iconv_close(this->inner);
@ -99,16 +99,32 @@ namespace yycc::encoding::iconv {
#pragma region Token #pragma region Token
Token::Token(const CodeName& from_code, const CodeName& to_code) : inner(std::make_unique<PrivToken>(from_code, to_code)) {} Token::Token(const CodeName& from_code, const CodeName& to_code) : inner(nullptr) {
this->inner = new PrivToken(from_code, to_code);
}
Token::~Token() {} Token::~Token() {
if (this->inner != nullptr) {
delete this->inner;
}
}
Token::Token(Token&& rhs) noexcept : inner(rhs.inner) {
rhs.inner = nullptr;
}
Token& Token::operator=(Token&& rhs) noexcept {
this->inner = rhs.inner;
rhs.inner = nullptr;
return *this;
}
bool Token::is_valid() const { bool Token::is_valid() const {
return this->inner->is_valid(); return this->inner->is_valid();
} }
PrivToken* Token::get_inner() const { PrivToken* Token::get_inner() const {
return this->inner.get(); return this->inner;
} }
#pragma endregion #pragma endregion

View File

@ -4,7 +4,6 @@
#include <string> #include <string>
#include <string_view> #include <string_view>
#include <expected> #include <expected>
#include <memory>
namespace yycc::encoding::iconv { namespace yycc::encoding::iconv {
@ -25,14 +24,15 @@ namespace yycc::encoding::iconv {
public: public:
Token(const CodeName& from_code, const CodeName& to_code); Token(const CodeName& from_code, const CodeName& to_code);
~Token(); ~Token();
Token(Token&& rhs) noexcept;
Token& operator=(Token&& rhs) noexcept;
YYCC_DELETE_COPY(Token) YYCC_DELETE_COPY(Token)
YYCC_DEFAULT_MOVE(Token)
bool is_valid() const; bool is_valid() const;
PrivToken* get_inner() const; PrivToken* get_inner() const;
private: private:
std::unique_ptr<PrivToken> inner; PrivToken* inner;
}; };
/// @brief The possible error occurs in this module. /// @brief The possible error occurs in this module.