Skip to content

Commit 68aff95

Browse files
committed
using xtd::helpers::throw_helper class
1 parent 22346c9 commit 68aff95

File tree

8 files changed

+56
-35
lines changed

8 files changed

+56
-35
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ namespace xtd {
6464
io,
6565
/// @brief The key is not found.
6666
key_not_found,
67+
/// @brief The lock recursion is not valid.
68+
lock_recursion,
6769
/// @brief The method or operation is not implemented.
6870
not_implemented,
6971
/// @brief The method or operation is not supported.
@@ -82,6 +84,10 @@ namespace xtd {
8284
platform_not_supported,
8385
/// @brief The rank is not valid.
8486
rank,
87+
/// @brief The semaphore is full.
88+
semaphore_full,
89+
/// @brief The lock is not valid.
90+
synchronization_lock,
8591
/// @brief The access is denied.
8692
unauthorized_access,
8793
};

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ namespace xtd {
4242
/// @param message The message associate to the exception.
4343
[[noreturn]] static void throws(xtd::helpers::exception_case exception_case, const char* message, const source_location& location = source_location::current());
4444
/// @brief Throws an exption with specified exception case, and message.
45-
/// @param exception_case One of xtd::helpers::exception_case values.
45+
/// @param exception_case The xtd::helpers::exception_case::format_not_iformatable value.
4646
/// @param type The type associate to the exception.
47-
/// @remarks This overload is used with the xtd::helpers::exception_case::format_not_iformatable value.
47+
/// @remarks This overload can only be used with the xtd::helpers::exception_case::format_not_iformatable value.
4848
[[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.
50-
/// @tparam exception_t The exceptio type to throw.
51-
/// @param arguments The arguments of the exceptio to throw.
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.
5252
template<class exception_t, class ...args_t>
5353
[[noreturn]] static void throws(args_t... arguments) {
5454
throw exception_t(arguments...);

src/xtd.core/include/xtd/threading/lock_recursion_exception.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
namespace xtd {
1111
/// @brief The xtd::threading namespace provides classes and interfaces that enable multithreaded programming. In addition to classes for synchronizing thread activities and access to data ( xtd::threading::mutex, xtd::threading::monitor, xtd::threading::interlocked, xtd::threading::auto_reset_event, and so on), this namespace includes a xtd::threading::thread_pool class that allows you to use a pool of system-supplied threads, and a xtd::threading::timer class that executes callback methods on thread pool threads.
1212
namespace threading {
13-
/// @brief The exception that is thrown when a Thread is in an invalid ThreadState for the method call.
13+
/// @brief The exception that is thrown when recursive entry into a lock is not compatible with the recursion policy for the lock.
1414
/// ```cpp
1515
/// class lock_recursion_exception : public xtd::system_exception
1616
/// ```

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
#include "../../../include/xtd/io/path_too_long_exception.hpp"
1111
#include "../../../include/xtd/threading/abandoned_mutex_exception.hpp"
1212
#include "../../../include/xtd/threading/barrier_post_phase_exception.hpp"
13+
#include "../../../include/xtd/threading/lock_recursion_exception.hpp"
14+
#include "../../../include/xtd/threading/semaphore_full_exception.hpp"
15+
#include "../../../include/xtd/threading/synchronization_lock_exception.hpp"
1316
#include "../../../include/xtd/argument_exception.hpp"
1417
#include "../../../include/xtd/argument_null_exception.hpp"
1518
#include "../../../include/xtd/argument_out_of_range_exception.hpp"
@@ -61,6 +64,7 @@ void throw_helper::throws(enum exception_case exception_case, const source_locat
6164
case exception_case::invalid_operation: throw invalid_operation_exception {to_stack_frame(location)};
6265
case exception_case::io: throw io_exception {to_stack_frame(location)};
6366
case exception_case::key_not_found: throw key_not_found_exception {to_stack_frame(location)};
67+
case exception_case::lock_recursion: throw lock_recursion_exception {to_stack_frame(location)};
6468
case exception_case::not_implemented: throw not_implemented_exception {to_stack_frame(location)};
6569
case exception_case::not_supported: throw not_supported_exception {to_stack_frame(location)};
6670
case exception_case::null_pointer: throw null_pointer_exception {to_stack_frame(location)};
@@ -70,6 +74,8 @@ void throw_helper::throws(enum exception_case exception_case, const source_locat
7074
case exception_case::path_too_long: throw path_too_long_exception {to_stack_frame(location)};
7175
case exception_case::platform_not_supported: throw platform_not_supported_exception {to_stack_frame(location)};
7276
case exception_case::rank: throw rank_exception {to_stack_frame(location)};
77+
case exception_case::semaphore_full: throw semaphore_full_exception {to_stack_frame(location)};
78+
case exception_case::synchronization_lock: throw synchronization_lock_exception {to_stack_frame(location)};
7379
case exception_case::unauthorized_access: throw unauthorized_access_exception {to_stack_frame(location)};
7480
default: throw argument_exception {"Invalid xtd::helpers::exception_case value"};
7581
}
@@ -96,6 +102,7 @@ void throw_helper::throws(enum exception_case exception_case, const char* messag
96102
case exception_case::invalid_operation: throw invalid_operation_exception {message, to_stack_frame(location)};
97103
case exception_case::io: throw io_exception {message, to_stack_frame(location)};
98104
case exception_case::key_not_found: throw key_not_found_exception {message, to_stack_frame(location)};
105+
case exception_case::lock_recursion: throw lock_recursion_exception {message, to_stack_frame(location)};
99106
case exception_case::not_implemented: throw not_implemented_exception {message, to_stack_frame(location)};
100107
case exception_case::not_supported: throw not_supported_exception {message, to_stack_frame(location)};
101108
case exception_case::null_pointer: throw null_pointer_exception {message, to_stack_frame(location)};
@@ -105,11 +112,15 @@ void throw_helper::throws(enum exception_case exception_case, const char* messag
105112
case exception_case::path_too_long: throw path_too_long_exception {message, to_stack_frame(location)};
106113
case exception_case::platform_not_supported: throw platform_not_supported_exception {message, to_stack_frame(location)};
107114
case exception_case::rank: throw rank_exception {message, to_stack_frame(location)};
115+
case exception_case::semaphore_full: throw semaphore_full_exception {message, to_stack_frame(location)};
116+
case exception_case::synchronization_lock: throw synchronization_lock_exception {message, to_stack_frame(location)};
108117
case exception_case::unauthorized_access: throw unauthorized_access_exception {message, to_stack_frame(location)};
109118
default: throw argument_exception {"Invalid xtd::helpers::exception_case value"};
110119
}
111120
}
112121

113122
void throw_helper::throws(enum exception_case exception_case, const xtd::type& type, const source_location& location) {
114-
throws(exception_case, (exception_case == exception_case::format_not_iformatable ? 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()) : typeof_(type).full_name()).c_str(), location);
123+
if (exception_case == exception_case::format_not_iformatable)
124+
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);
125+
throw argument_exception {"This overload can only be used with the xtd::helpers::exception_case::format_not_iformatable value."};
115126
}

src/xtd.core/src/xtd/threading/monitor.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
using namespace xtd;
1919
using namespace xtd::diagnostics;
20+
using namespace xtd::helpers;
2021
using namespace xtd::threading;
2122

2223
struct monitor::item {
@@ -46,7 +47,7 @@ void monitor::exit_ptr(object_ptr obj) {
4647
get_static_data().monitor_items_critical_section.enter();
4748
if (!is_entered_ptr(obj)) {
4849
get_static_data().monitor_items_critical_section.leave();
49-
throw synchronization_lock_exception {};
50+
throw_helper::throws(exception_case::synchronization_lock);
5051
}
5152

5253
item saved;
@@ -88,7 +89,7 @@ void monitor::pulse_ptr(object_ptr obj) {
8889
get_static_data().monitor_items_critical_section.leave();
8990

9091
if (monitor_item == nullptr) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_operation);
91-
if (monitor_item->thread_id.value() != thread::current_thread().thread_id()) throw synchronization_lock_exception {};
92+
if (monitor_item->thread_id.value() != thread::current_thread().thread_id()) throw_helper::throws(exception_case::synchronization_lock);
9293

9394
monitor_item->condition_variable.pulse();
9495
}
@@ -100,7 +101,7 @@ void monitor::pulse_all_ptr(object_ptr obj) {
100101
get_static_data().monitor_items_critical_section.leave();
101102

102103
if (monitor_item == nullptr) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_operation);
103-
if (monitor_item->thread_id.value() != thread::current_thread().thread_id()) throw synchronization_lock_exception {};
104+
if (monitor_item->thread_id.value() != thread::current_thread().thread_id()) throw_helper::throws(exception_case::synchronization_lock);
104105

105106
monitor_item->condition_variable.pulse_all();
106107
}
@@ -131,7 +132,7 @@ bool monitor::wait_ptr(object_ptr obj, int32 milliseconds_timeout) {
131132
get_static_data().monitor_items_critical_section.leave();
132133

133134
if (monitor_item == nullptr) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_operation);
134-
if (monitor_item->thread_id.value() != thread::current_thread().thread_id()) throw synchronization_lock_exception {};
135+
if (monitor_item->thread_id.value() != thread::current_thread().thread_id()) throw_helper::throws(exception_case::synchronization_lock);
135136

136137
return monitor_item->condition_variable.wait(monitor_item->critical_section, milliseconds_timeout);
137138
}

src/xtd.core/src/xtd/threading/mutex.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "../../../include/xtd/threading/abandoned_mutex_exception.hpp"
88

99
using namespace xtd;
10+
using namespace xtd::helpers;
1011
using namespace xtd::threading;
1112

1213
mutex::mutex() : mutex(false) {
@@ -22,7 +23,7 @@ mutex::mutex(const string& name, bool& created_new) : mutex(false, name, created
2223
}
2324

2425
mutex::mutex(bool initially_owned, const string& name) : name_(name) {
25-
if (name.size() > native::named_mutex::max_name_size()) throw io::path_too_long_exception {};
26+
if (name.size() > native::named_mutex::max_name_size()) throw_helper::throws(exception_case::path_too_long);
2627
auto created_new = false;
2728
create(initially_owned, created_new);
2829
}
@@ -72,14 +73,14 @@ void mutex::lock() {
7273

7374
mutex mutex::open_existing(const string& name) {
7475
if (name.empty()) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::argument);
75-
if (name.size() > native::named_mutex::max_name_size()) throw io::path_too_long_exception {};
76+
if (name.size() > native::named_mutex::max_name_size()) throw_helper::throws(exception_case::path_too_long);
7677
auto result = mutex{};
77-
if (!try_open_existing(name, result)) throw io::io_exception {};
78+
if (!try_open_existing(name, result)) throw_helper::throws(exception_case::io);
7879
return result;
7980
}
8081

8182
void mutex::release_mutex() {
82-
if (!signal()) throw io::io_exception {};
83+
if (!signal()) throw_helper::throws(exception_case::io);
8384
}
8485

8586
bool mutex::try_lock() noexcept {
@@ -123,19 +124,19 @@ void mutex::unlock() {
123124
}
124125

125126
bool mutex::signal() {
126-
if (!mutex_) throw object_closed_exception {};
127+
if (!mutex_) throw_helper::throws(exception_case::object_closed);
127128
auto io_error = false;
128129
auto result = mutex_->signal(io_error);
129-
if (io_error) throw io::io_exception {};
130+
if (io_error) throw_helper::throws(exception_case::io);
130131
return result;
131132
}
132133

133134
bool mutex::wait(int32 milliseconds_timeout) {
134-
if (!mutex_) throw object_closed_exception {};
135+
if (!mutex_) throw_helper::throws(exception_case::object_closed);
135136
if (milliseconds_timeout < -1) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::argument_out_of_range);
136137
auto result = mutex_->wait(milliseconds_timeout);
137-
if (result == 0xFFFFFFFF) throw io::io_exception {};
138-
if (result == 0x00000080) throw abandoned_mutex_exception {};
138+
if (result == 0xFFFFFFFF) throw_helper::throws(exception_case::io);
139+
if (result == 0x00000080) throw_helper::throws(exception_case::abandoned_mutex);
139140
if (result == 0x00000102) return false;
140141
return true;
141142
}
@@ -144,10 +145,10 @@ void mutex::create(bool initially_owned, bool& created_new) {
144145
created_new = true;
145146
if (name_.empty()) {
146147
mutex_ = xtd::new_sptr<mutex::unnamed_mutex>();
147-
if (!mutex_->create(initially_owned)) throw io::io_exception {};
148+
if (!mutex_->create(initially_owned)) throw_helper::throws(exception_case::io);
148149
} else {
149150
mutex_ = xtd::new_sptr<mutex::named_mutex>();
150151
created_new = mutex_->create(initially_owned, name_);
151-
if (!created_new && !mutex_->open(name_)) throw io::io_exception {};
152+
if (!created_new && !mutex_->open(name_)) throw_helper::throws(exception_case::io);
152153
}
153154
}

src/xtd.core/src/xtd/threading/semaphore.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <atomic>
99

1010
using namespace xtd;
11+
using namespace xtd::helpers;
1112
using namespace xtd::threading;
1213

1314
struct semaphore::data {
@@ -44,7 +45,7 @@ semaphore::semaphore(int32 initial_count, int32 maximum_count, const string& nam
4445
}
4546

4647
semaphore::semaphore(int32 initial_count, int32 maximum_count, const string& name, bool& created_new) : data_(xtd::new_sptr<data>()) {
47-
if (name.size() > native::named_semaphore::max_name_size()) throw io::path_too_long_exception {};
48+
if (name.size() > native::named_semaphore::max_name_size()) throw_helper::throws(exception_case::path_too_long);
4849
if (initial_count > maximum_count) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::argument);
4950
if (maximum_count < 1 || initial_count < 0) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::argument_out_of_range);
5051
data_->name = name;
@@ -84,9 +85,9 @@ bool semaphore::equals(const semaphore& other) const noexcept {
8485

8586
semaphore semaphore::open_existing(const string& name) {
8687
if (name.empty()) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::argument);
87-
if (name.size() > native::named_semaphore::max_name_size()) throw io::path_too_long_exception {};
88+
if (name.size() > native::named_semaphore::max_name_size()) throw_helper::throws(exception_case::path_too_long);
8889
auto result = semaphore{};
89-
if (!try_open_existing(name, result)) throw io::io_exception {};
90+
if (!try_open_existing(name, result)) throw_helper::throws(exception_case::io);
9091
return result;
9192
}
9293

@@ -96,13 +97,13 @@ int32 semaphore::release() {
9697

9798
int32 semaphore::release(int32 release_count) {
9899
if (release_count < 1) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::argument_out_of_range);
99-
if (!semaphore_) throw object_closed_exception {};
100-
if (data_->count + release_count > data_->maximum_count) throw semaphore_full_exception {};
100+
if (!semaphore_) throw_helper::throws(exception_case::object_closed);
101+
if (data_->count + release_count > data_->maximum_count) throw_helper::throws(exception_case::semaphore_full);
101102
auto io_error = false;
102103
auto previous_count = -1;
103104
semaphore_->signal(io_error, release_count, previous_count);
104105
if (previous_count != -1) data_->count.exchange(previous_count);
105-
if (io_error) throw io::io_exception {};
106+
if (io_error) throw_helper::throws(exception_case::io);
106107
previous_count = data_->count;
107108
data_->count += release_count;
108109
return previous_count;
@@ -126,11 +127,11 @@ bool semaphore::signal() {
126127
}
127128

128129
bool semaphore::wait(int32 milliseconds_timeout) {
129-
if (!semaphore_) throw object_closed_exception {};
130+
if (!semaphore_) throw_helper::throws(exception_case::object_closed);
130131
if (milliseconds_timeout < -1) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::argument_out_of_range);
131132
auto result = semaphore_->wait(milliseconds_timeout);
132-
if (result == 0xFFFFFFFF) throw io::io_exception {};
133-
if (result == 0x00000080) throw abandoned_mutex_exception {};
133+
if (result == 0xFFFFFFFF) throw_helper::throws(exception_case::io);
134+
if (result == 0x00000080) throw_helper::throws(exception_case::abandoned_mutex);
134135
if (result == 0x00000102) return false;
135136
--data_->count;
136137
return true;
@@ -142,10 +143,10 @@ void semaphore::create(int32 initial_count, int32 maximum_count, bool& created_n
142143
created_new = true;
143144
if (data_->name.empty()) {
144145
semaphore_ = xtd::new_sptr<semaphore::unnamed_semaphore>();
145-
if (!semaphore_->create(initial_count, maximum_count)) throw io::io_exception {};
146+
if (!semaphore_->create(initial_count, maximum_count)) throw_helper::throws(exception_case::io);
146147
} else {
147148
semaphore_ = xtd::new_sptr<semaphore::named_semaphore>();
148149
created_new = semaphore_->create(initial_count, maximum_count, data_->name);
149-
if (!created_new && !semaphore_->open(data_->name)) throw io::io_exception {};
150+
if (!created_new && !semaphore_->open(data_->name)) throw_helper::throws(exception_case::io);
150151
}
151152
}

src/xtd.core/src/xtd/threading/spin_lock.cpp

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

99
using namespace xtd;
1010
using namespace xtd::diagnostics;
11+
using namespace xtd::helpers;
1112
using namespace xtd::threading;
1213

1314
struct spin_lock::data {
@@ -46,7 +47,7 @@ void spin_lock::exit() {
4647
}
4748

4849
void spin_lock::exit(bool use_memory_barrier) {
49-
if (data_->enable_thread_owner_tracking && data_->thread_id != thread::current_thread().thread_id()) throw synchronization_lock_exception {};
50+
if (data_->enable_thread_owner_tracking && data_->thread_id != thread::current_thread().thread_id()) throw_helper::throws(exception_case::synchronization_lock);
5051
if (use_memory_barrier) std::atomic_thread_fence(std::memory_order_acquire);
5152
if (data_->enable_thread_owner_tracking) data_->thread_id = thread::invalid_thread_id;
5253
data_->flag.clear(std::memory_order_release);
@@ -62,7 +63,7 @@ void spin_lock::try_enter(const time_span& timeout, bool& lock_taken) {
6263
}
6364

6465
void spin_lock::try_enter(int32 milliseconds_timeout, bool& lock_taken) {
65-
if (data_->enable_thread_owner_tracking && data_->thread_id == thread::current_thread().thread_id()) throw lock_recursion_exception {};
66+
if (data_->enable_thread_owner_tracking && data_->thread_id == thread::current_thread().thread_id()) throw_helper::throws(exception_case::lock_recursion);
6667
lock_taken = false;
6768
auto sw = stopwatch::start_new();
6869
while (data_->flag.test_and_set(std::memory_order_acquire)) {

0 commit comments

Comments
 (0)