From: Arnaud Giersch Date: Tue, 2 Jul 2019 13:47:05 +0000 (+0200) Subject: Throw std::out_of_range. X-Git-Tag: v3.23.2~37 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/0a98e0dd953ca1a907d77f3c4eabdce39e62c1e5 Throw std::out_of_range. --- diff --git a/src/xbt/config.cpp b/src/xbt/config.cpp index 0ac0581661..f70eec1947 100644 --- a/src/xbt/config.cpp +++ b/src/xbt/config.cpp @@ -307,7 +307,7 @@ inline ConfigurationElement* Config::get_dict_element(const std::string& name) XBT_INFO("Option %s has been renamed to %s. Consider switching.", name.c_str(), res->get_key().c_str()); return res; } else { - THROWF(not_found_error, 0, "Bad config key: %s", name.c_str()); + throw std::out_of_range("Bad config key: " + name); } } } diff --git a/src/xbt/config_test.cpp b/src/xbt/config_test.cpp index dd89d9e6e8..8b419246fe 100644 --- a/src/xbt/config_test.cpp +++ b/src/xbt/config_test.cpp @@ -49,10 +49,7 @@ TEST_CASE("xbt::config: Configuration support", "config") INFO("Access to a non-existant entry"); - REQUIRE_THROWS_MATCHES( - simgrid::config::set_parse("color:blue"), xbt_ex, - Catch::Matchers::Predicate([](xbt_ex const& e) { return e.category == not_found_error; }, - "category not_found_error")); + REQUIRE_THROWS_AS(simgrid::config::set_parse("color:blue"), std::out_of_range); simgrid::config::finalize(); simgrid_config = temp; diff --git a/src/xbt/dict.cpp b/src/xbt/dict.cpp index 94be19e07d..f6742fb877 100644 --- a/src/xbt/dict.cpp +++ b/src/xbt/dict.cpp @@ -13,6 +13,7 @@ #include "xbt/log.h" #include "xbt/mallocator.h" #include "xbt/str.h" +#include "xbt/string.hpp" #include #include @@ -194,7 +195,7 @@ void xbt_dict_set(xbt_dict_t dict, const char *key, void *data, void_f_pvoid_t f * @param key_len the size of the @a key * @return the data that we are looking for * - * Search the given @a key. Throws not_found_error when not found. + * Search the given @a key. Throws std::out_of_range when not found. */ void *xbt_dict_get_ext(xbt_dict_t dict, const char *key, int key_len) { @@ -207,7 +208,7 @@ void *xbt_dict_get_ext(xbt_dict_t dict, const char *key, int key_len) } if (current == nullptr) - THROWF(not_found_error, 0, "key %.*s not found", key_len, key); + throw std::out_of_range(simgrid::xbt::string_printf("key %.*s not found", key_len, key)); return current->content; } @@ -254,7 +255,7 @@ char *xbt_dict_get_key(xbt_dict_t dict, const void *data) * @param key the key to find data * @return the data that we are looking for * - * Search the given @a key. Throws not_found_error when not found. + * Search the given @a key. Throws std::out_of_range when not found. * Check xbt_dict_get_or_null() for a version returning nullptr without exception when not found. */ void *xbt_dict_get(xbt_dict_t dict, const char *key) @@ -269,7 +270,7 @@ void *xbt_dict_get(xbt_dict_t dict, const char *key) * @param key the key to find data * @return the s_xbt_dictelm_t that we are looking for * - * Search the given @a key. Throws not_found_error when not found. + * Search the given @a key. Throws std::out_of_range when not found. * Check xbt_dict_get_or_null() for a version returning nullptr without exception when not found. */ xbt_dictelm_t xbt_dict_get_elm(xbt_dict_t dict, const char *key) @@ -277,7 +278,7 @@ xbt_dictelm_t xbt_dict_get_elm(xbt_dict_t dict, const char *key) xbt_dictelm_t current = xbt_dict_get_elm_or_null(dict, key); if (current == nullptr) - THROWF(not_found_error, 0, "key %s not found", key); + throw std::out_of_range(simgrid::xbt::string_printf("key %s not found", key)); return current; } @@ -315,7 +316,7 @@ xbt_dictelm_t xbt_dict_get_elm_or_null(xbt_dict_t dict, const char *key) * @param key the key of the data to be removed * @param key_len the size of the @a key * - * Remove the entry associated with the given @a key (throws not_found) + * Remove the entry associated with the given @a key (throws std::out_of_range) */ void xbt_dict_remove_ext(xbt_dict_t dict, const char *key, int key_len) { @@ -330,7 +331,7 @@ void xbt_dict_remove_ext(xbt_dict_t dict, const char *key, int key_len) } if (current == nullptr) - THROWF(not_found_error, 0, "key %.*s not found", key_len, key); + throw std::out_of_range(simgrid::xbt::string_printf("key %.*s not found", key_len, key)); else { if (previous != nullptr) { previous->next = current->next; diff --git a/src/xbt/dict_test.cpp b/src/xbt/dict_test.cpp index 5facbd8a55..bd54663bb8 100644 --- a/src/xbt/dict_test.cpp +++ b/src/xbt/dict_test.cpp @@ -16,11 +16,6 @@ #define STR(str) ((str) ? (str) : "(null)") -#define REQUIRE_THROWS_XBT_EX(...) \ - REQUIRE_THROWS_MATCHES((__VA_ARGS__), xbt_ex, Catch::Matchers::Predicate( \ - [](xbt_ex const& e) { return e.category == not_found_error; }, \ - "category not_found_error")) - static constexpr int NB_ELM = 20000; static constexpr int SIZEOFKEY = 1024; @@ -95,7 +90,7 @@ static void traverse(xbt_dict_t head) static void search_not_found(xbt_dict_t head, const char* data) { INFO("Search " << STR(data) << " (expected not to be found)"); - REQUIRE_THROWS_XBT_EX(data = (const char*)xbt_dict_get(head, data)); + REQUIRE_THROWS_AS(xbt_dict_get(head, data), std::out_of_range); } static void count(xbt_dict_t dict, int length) @@ -156,7 +151,7 @@ TEST_CASE("xbt::dict: dict data container", "dict") INFO("Traversal and search the empty dictionary"); xbt_dict_t head = xbt_dict_new_homogeneous(&free); traverse(head); - REQUIRE_THROWS_XBT_EX(debugged_remove(head, "12346")); + REQUIRE_THROWS_AS(debugged_remove(head, "12346"), std::out_of_range); xbt_dict_free(&head); INFO("Traverse the full dictionary"); @@ -228,7 +223,7 @@ TEST_CASE("xbt::dict: dict data container", "dict") xbt_dict_t head = new_fixture(); count(head, 7); INFO("Remove non existing data"); - REQUIRE_THROWS_XBT_EX(debugged_remove(head, "Does not exist")); + REQUIRE_THROWS_AS(debugged_remove(head, "Does not exist"), std::out_of_range); traverse(head); xbt_dict_free(&head); @@ -247,7 +242,7 @@ TEST_CASE("xbt::dict: dict data container", "dict") debugged_remove(head, "123456"); traverse(head); count(head, 3); - REQUIRE_THROWS_XBT_EX(debugged_remove(head, "12346")); + REQUIRE_THROWS_AS(debugged_remove(head, "12346"), std::out_of_range); traverse(head); debugged_remove(head, "1234"); traverse(head); @@ -255,7 +250,7 @@ TEST_CASE("xbt::dict: dict data container", "dict") traverse(head); debugged_remove(head, "123"); traverse(head); - REQUIRE_THROWS_XBT_EX(debugged_remove(head, "12346")); + REQUIRE_THROWS_AS(debugged_remove(head, "12346"), std::out_of_range); traverse(head); INFO("Free dict, create new fresh one, and then reset the dict"); diff --git a/src/xbt/dynar.cpp b/src/xbt/dynar.cpp index 2670fc5bc5..8216c76429 100644 --- a/src/xbt/dynar.cpp +++ b/src/xbt/dynar.cpp @@ -11,6 +11,7 @@ #include "xbt/ex.h" #include "xbt/log.h" #include "xbt/misc.h" +#include "xbt/string.hpp" #include "xbt/sysdep.h" #include @@ -29,15 +30,15 @@ static inline void _sanity_check_idx(int idx) static inline void _check_inbound_idx(xbt_dynar_t dynar, int idx) { if (idx < 0 || idx >= static_cast(dynar->used)) { - THROWF(bound_error, idx, "dynar is not that long. You asked %d, but it's only %lu long", - idx, static_cast(dynar->used)); + throw std::out_of_range(simgrid::xbt::string_printf("dynar is not that long. You asked %d, but it's only %lu long", + idx, static_cast(dynar->used))); } } static inline void _check_populated_dynar(xbt_dynar_t dynar) { if (dynar->used == 0) { - THROWF(bound_error, 0, "dynar %p is empty", dynar); + throw std::out_of_range(simgrid::xbt::string_printf("dynar %p is empty", dynar)); } } @@ -431,8 +432,8 @@ void xbt_dynar_remove_n_at(xbt_dynar_t const dynar, const unsigned int n, const * } * @endcode * - * Raises not_found_error if not found. If you have less than 2 millions elements, you probably want to use - * #xbt_dynar_search_or_negative() instead, so that you don't have to TRY/CATCH on element not found. + * Raises std::out_of_range if not found. If you have less than 2 millions elements, you probably want to use + * #xbt_dynar_search_or_negative() instead, so that you don't have to try/catch on element not found. */ unsigned int xbt_dynar_search(xbt_dynar_t const dynar, void* const elem) { @@ -443,7 +444,7 @@ unsigned int xbt_dynar_search(xbt_dynar_t const dynar, void* const elem) return it; } - THROWF(not_found_error, 0, "Element %p not part of dynar %p", elem, dynar); + throw std::out_of_range(simgrid::xbt::string_printf("Element %p not part of dynar %p", elem, dynar)); } /** @brief Returns the position of the element in the dynar (or -1 if not found)