Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions include/common/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ namespace IOv2
*/
static constexpr size_t SIMD_PADDING_BYTES = 32;

/**
* @lang{ZH}
* IO 相关异常的基类。
* @endif
*
* @lang{EN}
* Base class for IO-related exceptions.
* @endif
*/
struct io_error : std::runtime_error
{
using std::runtime_error::runtime_error;
};

/**
* @lang{ZH}
* 设备相关错误的异常类。
Expand All @@ -41,9 +55,9 @@ namespace IOv2
* Exception class for device-related errors.
* @endif
*/
struct device_error : std::runtime_error
struct device_error : io_error
{
using std::runtime_error::runtime_error;
using io_error::io_error;
};

/**
Expand All @@ -55,9 +69,9 @@ namespace IOv2
* Exception class for errors during conversion processes (e.g., code conversion, compression, encryption).
* @endif
*/
struct cvt_error : std::runtime_error
struct cvt_error : io_error
{
using std::runtime_error::runtime_error;
using io_error::io_error;
};

/**
Expand All @@ -69,9 +83,10 @@ namespace IOv2
* Exception class for End-Of-File (EOF).
* @endif
*/
struct eof_error : std::exception
struct eof_error : io_error
{
using std::exception::exception;
eof_error() : io_error("end of file") {}
using io_error::io_error;
};

/**
Expand All @@ -83,8 +98,8 @@ namespace IOv2
* Exception class for stream operation errors.
* @endif
*/
struct stream_error : std::runtime_error
struct stream_error : io_error
{
using std::runtime_error::runtime_error;
using io_error::io_error;
};
}
36 changes: 8 additions & 28 deletions include/io/io_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,6 @@ namespace ios_defs
constexpr static iostate cvtfailbit = 1L << 2;
constexpr static iostate strfailbit = 1L << 3;
constexpr static iostate otherfailbit = 1L << 4;

class failure : public std::runtime_error
{
public:
explicit failure(const std::string& msg, const std::error_code& ec = std::io_errc::stream)
: std::runtime_error(msg)
, m_ec(ec) {}

explicit failure(const char* msg, const std::error_code& ec = std::io_errc::stream)
: std::runtime_error(msg)
, m_ec(ec) {}

virtual const std::error_code& code() const noexcept
{
return m_ec;
}

private:
std::error_code m_ec;
};
};

struct io_state_and_exp
Expand All @@ -79,39 +59,39 @@ struct io_state_and_exp
if ((m_stream_state & ios_defs::otherfailbit) == ios_defs::goodbit) m_exp_other_fail = std::exception_ptr{};

ios_defs::iostate state_in_exp = m_exception & m_stream_state;
bool need_throw_exp = false;
if (state_in_exp & ios_defs::devfailbit)
{
need_throw_exp = true;
if (m_exp_dev_fail)
std::rethrow_exception(std::exchange(m_exp_dev_fail, nullptr));
else
throw device_error("device failure bit has been set");
}
else if (state_in_exp & ios_defs::cvtfailbit)
{
need_throw_exp = true;
if (m_exp_cvt_fail)
std::rethrow_exception(std::exchange(m_exp_cvt_fail, nullptr));
else
throw cvt_error("converter failure bit has been set");
}
else if (state_in_exp & ios_defs::strfailbit)
{
need_throw_exp = true;
if (m_exp_str_fail)
std::rethrow_exception(std::exchange(m_exp_str_fail, nullptr));
else
throw stream_error("stream failure bit has been set");
}
else if (state_in_exp & ios_defs::otherfailbit)
{
need_throw_exp = true;
if (m_exp_other_fail)
std::rethrow_exception(std::exchange(m_exp_other_fail, nullptr));
else
throw stream_error("other failure bit has been set");
}
else if (state_in_exp & ios_defs::eofbit)
{
if (!std::current_exception())
throw eof_error{};
}

if (need_throw_exp)
throw ios_defs::failure("failure bit has been set");
}

void setstate(ios_defs::iostate s) { clear(rdstate() | s); }
Expand Down
14 changes: 0 additions & 14 deletions test/io/io_base/test_io_base.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
void test_io_base_failure_1();
void test_io_base_failure_2();
void test_io_base_failure_what_1();
void test_io_base_failure_what_2();
void test_io_base_failure_what_3();
void test_io_base_failure_what_4();

void test_io_base_char_fill_1();
void test_io_base_wchar_t_fill_1();

Expand All @@ -18,13 +11,6 @@ void test_io_base_manipulators();

void test_io_base()
{
test_io_base_failure_1();
test_io_base_failure_2();
test_io_base_failure_what_1();
test_io_base_failure_what_2();
test_io_base_failure_what_3();
test_io_base_failure_what_4();

test_io_base_char_fill_1();
test_io_base_wchar_t_fill_1();

Expand Down
146 changes: 0 additions & 146 deletions test/io/io_base/test_io_base_failure.cpp

This file was deleted.

2 changes: 1 addition & 1 deletion test/io/io_state_and_exp/test_io_state_and_exp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void test_io_state_and_exp_1()
dump_info("Unreachable code\n");
std::abort();
}
catch (IOv2::ios_defs::failure&)
catch (IOv2::cvt_error&)
{
iostate02 = ios_01.exceptions();
}
Expand Down
Loading