Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Throw std::out_of_range.
authorArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Tue, 2 Jul 2019 13:47:05 +0000 (15:47 +0200)
committerArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Tue, 2 Jul 2019 14:12:21 +0000 (16:12 +0200)
src/xbt/config.cpp
src/xbt/config_test.cpp
src/xbt/dict.cpp
src/xbt/dict_test.cpp
src/xbt/dynar.cpp

index 0ac0581..f70eec1 100644 (file)
@@ -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 {
         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);
     }
   }
 }
     }
   }
 }
index dd89d9e..8b41924 100644 (file)
@@ -49,10 +49,7 @@ TEST_CASE("xbt::config: Configuration support", "config")
 
     INFO("Access to a non-existant entry");
 
 
     INFO("Access to a non-existant entry");
 
-    REQUIRE_THROWS_MATCHES(
-        simgrid::config::set_parse("color:blue"), xbt_ex,
-        Catch::Matchers::Predicate<xbt_ex>([](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;
 
     simgrid::config::finalize();
     simgrid_config = temp;
index 94be19e..f6742fb 100644 (file)
@@ -13,6 +13,7 @@
 #include "xbt/log.h"
 #include "xbt/mallocator.h"
 #include "xbt/str.h"
 #include "xbt/log.h"
 #include "xbt/mallocator.h"
 #include "xbt/str.h"
+#include "xbt/string.hpp"
 
 #include <cstdio>
 #include <cstring>
 
 #include <cstdio>
 #include <cstring>
@@ -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
  *
  * @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)
 {
  */
 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)
   }
 
   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;
 }
 
   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
  *
  * @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)
  * 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
  *
  * @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)
  * 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)
   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;
 }
 
   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
  *
  * @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)
 {
  */
 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)
   }
 
   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;
   else {
     if (previous != nullptr) {
       previous->next = current->next;
index 5facbd8..bd54663 100644 (file)
 
 #define STR(str) ((str) ? (str) : "(null)")
 
 
 #define STR(str) ((str) ? (str) : "(null)")
 
-#define REQUIRE_THROWS_XBT_EX(...)                                                                                     \
-  REQUIRE_THROWS_MATCHES((__VA_ARGS__), xbt_ex, Catch::Matchers::Predicate<xbt_ex>(                                    \
-                                                    [](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;
 
 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)");
 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)
 }
 
 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);
     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");
     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");
     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);
     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);
     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);
     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);
     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");
     traverse(head);
 
     INFO("Free dict, create new fresh one, and then reset the dict");
index 2670fc5..8216c76 100644 (file)
@@ -11,6 +11,7 @@
 #include "xbt/ex.h"
 #include "xbt/log.h"
 #include "xbt/misc.h"
 #include "xbt/ex.h"
 #include "xbt/log.h"
 #include "xbt/misc.h"
+#include "xbt/string.hpp"
 #include "xbt/sysdep.h"
 #include <sys/types.h>
 
 #include "xbt/sysdep.h"
 #include <sys/types.h>
 
@@ -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<int>(dynar->used)) {
 static inline void _check_inbound_idx(xbt_dynar_t dynar, int idx)
 {
   if (idx < 0 || idx >= static_cast<int>(dynar->used)) {
-    THROWF(bound_error, idx, "dynar is not that long. You asked %d, but it's only %lu long",
-           idx, static_cast<unsigned long>(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<unsigned long>(dynar->used)));
   }
 }
 
 static inline void _check_populated_dynar(xbt_dynar_t dynar)
 {
   if (dynar->used == 0) {
   }
 }
 
 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
  *
  * }
  * @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)
 {
  */
 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;
     }
 
       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)
 }
 
 /** @brief Returns the position of the element in the dynar (or -1 if not found)