Skip to content

Commit 112b784

Browse files
committed
using xtd::helpers::throw_helper class
1 parent 91709c3 commit 112b784

File tree

11 files changed

+91
-76
lines changed

11 files changed

+91
-76
lines changed

src/xtd.core/include/xtd/helpers/exception_case.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ namespace xtd {
9494
rank,
9595
/// @brief The semaphore is full.
9696
semaphore_full,
97+
/// @brief The socket is not valid.
98+
socket,
9799
/// @brief A software termination occurs.
98100
software_termination,
99101
/// @brief The lock is not valid.

src/xtd.core/include/xtd/helpers/throw_helper.hpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@
88

99
/// @brief The xtd namespace contains all fundamental classes to access Hardware, Os, System, and more.
1010
namespace xtd {
11+
/// @cond
12+
namespace net {
13+
namespace sockets {
14+
enum class socket_error;
15+
}
16+
}
17+
/// @endcond
18+
1119
/// @brief The xtd::helpers namespace contains helpers for xtd objects, sush as exception static class.
1220
namespace helpers {
1321
/// @brief The xtd::helpers::throw_helper class is used to throw an exception in the xtd framework.
@@ -46,13 +54,11 @@ namespace xtd {
4654
/// @param type The type associate to the exception.
4755
/// @remarks This overload can only be used with the xtd::helpers::exception_case::format_not_iformatable value.
4856
[[noreturn]] static void throws(xtd::helpers::exception_case exception_case, const xtd::type& type, const source_location& location = source_location::current());
49-
/// @brief Throws an exption with specified exception type.
50-
/// @tparam exception_t The exception type to throw.
51-
/// @param arguments The arguments of the exception to throw.
52-
template<class exception_t, class ...args_t>
53-
[[noreturn]] static void throws(args_t... arguments) {
54-
throw exception_t(arguments...);
55-
}
57+
/// @brief Throws an exption with specified exception case, and message.
58+
/// @param exception_case The xtd::helpers::exception_case::format_not_iformatable value.
59+
/// @param type The type associate to the exception.
60+
/// @remarks This overload can only be used with the xtd::helpers::exception_case::socket value.
61+
[[noreturn]] static void throws(xtd::helpers::exception_case exception_case, const xtd::net::sockets::socket_error& error, const source_location& location = source_location::current());
5662
};
5763
}
5864
}

src/xtd.core/include/xtd/internal/__print.hpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,8 @@
1919
inline void __xtd_print_with_file_write__(bool new_line, FILE* file, xtd::string&& s) {
2020
if (!file) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::null_pointer);
2121
if (new_line) s += xtd::environment::new_line();
22-
if (fwrite(s.c_str(), 1, s.length(), file) != s.length()) {
23-
auto exception = xtd::io::io_exception {};
24-
exception.h_result(errno);
25-
throw exception;
26-
}
22+
if (fwrite(s.c_str(), 1, s.length(), file) != s.length())
23+
xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::io);
2724
}
2825

2926
inline void __xtd_print_with_ostream_write__(bool new_line, std::ostream& os, xtd::string&& s) {

src/xtd.core/include/xtd/print.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace xtd {
1414
/// @tparam arg_t The type of the value to write.
1515
/// @param file A file output stream.
1616
/// @param value The value to write,
17-
/// @exceprion xtd::null_pointer_exception the `file`pointer is null.
17+
/// @exception xtd::null_pointer_exception the `file`pointer is null.
1818
template<class arg_t>
1919
void print(FILE* file, arg_t&& value) {
2020
__xtd_print_with_file_write__(false, file, string::format("{}", value));
@@ -32,7 +32,7 @@ namespace xtd {
3232
/// @param file A file output stream.
3333
/// @param fmt A composite format string.
3434
/// @param values Values to write,
35-
/// @exceprion xtd::null_pointer_exception the `file`pointer is null.
35+
/// @exception xtd::null_pointer_exception the `file`pointer is null.
3636
template<class ...args_t>
3737
void print(FILE* file, const xtd::string& fmt, args_t&& ... values) {
3838
__xtd_print_with_file_write__(false, file, string::format(fmt, std::forward<args_t>(values)...));
@@ -42,7 +42,7 @@ namespace xtd {
4242
/// @param file A file output stream.
4343
/// @param fmt A composite format string.
4444
/// @param values Values to write,
45-
/// @exceprion xtd::null_pointer_exception the `file`pointer is null.
45+
/// @exception xtd::null_pointer_exception the `file`pointer is null.
4646
template<class ...args_t>
4747
void print(FILE* file, const char* fmt, args_t&& ... values) {
4848
__xtd_print_with_file_write__(false, file, string::format(xtd::string {fmt}, std::forward<args_t>(values)...));
@@ -53,7 +53,7 @@ namespace xtd {
5353
/// @param file A file output stream.
5454
/// @param fmt A composite format string.
5555
/// @param values Values to write,
56-
/// @exceprion xtd::null_pointer_exception the `file`pointer is null.
56+
/// @exception xtd::null_pointer_exception the `file`pointer is null.
5757
template<class ...args_t>
5858
void print(FILE* file, const char8_t* fmt, args_t&& ... values) {
5959
__xtd_print_with_file_write__(false, file, string::format(xtd::string {fmt}, std::forward<args_t>(values)...));
@@ -64,7 +64,7 @@ namespace xtd {
6464
/// @param file A file output stream.
6565
/// @param fmt A composite format string.
6666
/// @param values Values to write,
67-
/// @exceprion xtd::null_pointer_exception the `file`pointer is null.
67+
/// @exception xtd::null_pointer_exception the `file`pointer is null.
6868
template<class ...args_t>
6969
void print(FILE* file, const char16_t* fmt, args_t&& ... values) {
7070
__xtd_print_with_file_write__(false, file, string::format(xtd::string {fmt}, std::forward<args_t>(values)...));
@@ -74,7 +74,7 @@ namespace xtd {
7474
/// @param file A file output stream.
7575
/// @param fmt A composite format string.
7676
/// @param values Values to write,
77-
/// @exceprion xtd::null_pointer_exception the `file`pointer is null.
77+
/// @exception xtd::null_pointer_exception the `file`pointer is null.
7878
template<class ...args_t>
7979
void print(FILE* file, const char32_t* fmt, args_t&& ... values) {
8080
__xtd_print_with_file_write__(false, file, string::format(xtd::string {fmt}, std::forward<args_t>(values)...));
@@ -84,7 +84,7 @@ namespace xtd {
8484
/// @param file A file output stream.
8585
/// @param fmt A composite format string.
8686
/// @param values Values to write,
87-
/// @exceprion xtd::null_pointer_exception the `file`pointer is null.
87+
/// @exception xtd::null_pointer_exception the `file`pointer is null.
8888
template<class ...args_t>
8989
void print(FILE* file, const wchar_t* fmt, args_t&& ... values) {
9090
__xtd_print_with_file_write__(false, file, string::format(xtd::string {fmt}, std::forward<args_t>(values)...));

src/xtd.core/src/xtd/helpers/throw_helper.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include "../../../include/xtd/io/file_not_found_exception.hpp"
99
#include "../../../include/xtd/io/io_exception.hpp"
1010
#include "../../../include/xtd/io/path_too_long_exception.hpp"
11+
#include "../../../include/xtd/net/sockets/socket_error.hpp"
12+
#include "../../../include/xtd/net/sockets/socket_exception.hpp"
1113
#include "../../../include/xtd/threading/abandoned_mutex_exception.hpp"
1214
#include "../../../include/xtd/threading/barrier_post_phase_exception.hpp"
1315
#include "../../../include/xtd/threading/lock_recursion_exception.hpp"
@@ -46,6 +48,7 @@ using namespace xtd::collections::generic;
4648
using namespace xtd::diagnostics;
4749
using namespace xtd::helpers;
4850
using namespace xtd::io;
51+
using namespace xtd::net::sockets;
4952
using namespace xtd::threading;
5053

5154
namespace {
@@ -89,6 +92,7 @@ void throw_helper::throws(enum exception_case exception_case, const source_locat
8992
case exception_case::platform_not_supported: throw platform_not_supported_exception {to_stack_frame(location)};
9093
case exception_case::rank: throw rank_exception {to_stack_frame(location)};
9194
case exception_case::semaphore_full: throw semaphore_full_exception {to_stack_frame(location)};
95+
case exception_case::socket: throw socket_exception {to_stack_frame(location)};
9296
case exception_case::software_termination: throw software_termination_exception {to_stack_frame(location)};
9397
case exception_case::synchronization_lock: throw synchronization_lock_exception {to_stack_frame(location)};
9498
case exception_case::thread_abort: throw thread_abort_exception {to_stack_frame(location)};
@@ -150,7 +154,11 @@ void throw_helper::throws(enum exception_case exception_case, const char* messag
150154
}
151155

152156
void throw_helper::throws(enum exception_case exception_case, const xtd::type& type, const source_location& location) {
153-
if (exception_case == exception_case::format_not_iformatable)
154-
throws(exception_case, string::format("The `{0}` type does not inherit from `xtd::iformat` or the specialisation for the `{0}` type in the `xtd::to_string` specialisation method does not exist.", typeof_(type).full_name()).c_str(), location);
155-
throw argument_exception {"This overload can only be used with the xtd::helpers::exception_case::format_not_iformatable value."};
157+
if (exception_case != exception_case::format_not_iformatable) throw argument_exception {"This overload can only be used with the xtd::helpers::exception_case::format_not_iformatable value."};
158+
throws(exception_case, string::format("The `{0}` type does not inherit from `xtd::iformat` or the specialisation for the `{0}` type in the `xtd::to_string` specialisation method does not exist.", typeof_(type).full_name()).c_str(), location);
159+
}
160+
161+
void throw_helper::throws(xtd::helpers::exception_case exception_case, const xtd::net::sockets::socket_error& error, const source_location& location) {
162+
if (exception_case != exception_case::socket) throw argument_exception {"This overload can only be used with the xtd::helpers::exception_case::socket value."};
163+
throw socket_exception(error, to_stack_frame(location));
156164
}

src/xtd.core/src/xtd/net/ip_address.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,12 @@ bool ip_address::is_ip_v6_teredo() const noexcept {
8787
}
8888

8989
uint32 ip_address::scope_id() const {
90-
if (address_family_ == sockets::address_family::inter_network) throw_helper::throws<socket_exception>(socket_error::operation_not_supported);
90+
if (address_family_ == sockets::address_family::inter_network) throw_helper::throws(exception_case::socket, socket_error::operation_not_supported);
9191
return address_or_scope_id_;
9292
}
9393

9494
ip_address& ip_address::scope_id(uint32 value) {
95-
if (address_family_ == sockets::address_family::inter_network) throw_helper::throws<socket_exception>(socket_error::operation_not_supported);
95+
if (address_family_ == sockets::address_family::inter_network) throw_helper::throws(exception_case::socket, socket_error::operation_not_supported);
9696

9797
address_or_scope_id_ = static_cast<uint32>(value);
9898
return *this;

src/xtd.core/src/xtd/net/ip_end_point.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ ip_end_point& ip_end_point::port(uint16 port) {
3636

3737
xtd::uptr<end_point> ip_end_point::create(const socket_address& socket_address) const {
3838
if (socket_address.address_family() != address_family_ || socket_address.size() < 8) throw_helper::throws(exception_case::argument);
39-
if (address_family_ != address_family::inter_network && address_family_ != address_family::inter_network_v6) throw_helper::throws<socket_exception>(socket_error::address_family_not_supported);
39+
if (address_family_ != address_family::inter_network && address_family_ != address_family::inter_network_v6) throw_helper::throws(exception_case::socket, socket_error::address_family_not_supported);
4040

4141
uint16 current_port = ip_address::network_to_host_order(bit_converter::to_uint16(socket_address.bytes_, 2)); // static_cast<uint16>((socket_address[2] << 8 & 0xFF00) | (socket_address[3]));
4242

0 commit comments

Comments
 (0)