Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[xbt] Move throwpoint out of xbt_ex in WithContextException
authorGabriel Corona <gabriel.corona@loria.fr>
Tue, 12 Jul 2016 10:46:30 +0000 (12:46 +0200)
committerGabriel Corona <gabriel.corona@loria.fr>
Tue, 12 Jul 2016 10:46:30 +0000 (12:46 +0200)
include/xbt/ex.hpp
include/xbt/exception.hpp
src/bindings/java/JavaContext.cpp
src/simix/smx_private.h
src/xbt/cunit.cpp
src/xbt/ex.cpp

index 1afc1bb..c1dcfb8 100644 (file)
 
 #include <xbt/ex.h>
 
+/** (Deprecated) Generic exception
+ *
+ *  An error is defined by a category and a value within that category.
+ *
+ *  This used to be a structure for C exceptions but it has been retrofitted
+ *  as a C++ exception and some of its data has been moved in the
+ *  WithContextException base class. We should deprecate it and replace it
+ *  with either C++ different exceptions or `std::system_error` which already
+ *  provides this (category + error code) logic.
+ */
 struct XBT_PUBLIC() xbt_ex :
   public std::runtime_error,
   public simgrid::xbt::WithContextException {
 public:
-  xbt_ex() : std::runtime_error("") {}
-  xbt_ex(const char* message) : std::runtime_error(message) {}
+
+  xbt_ex() :
+    std::runtime_error("")
+  {}
+
+  /**
+   *
+   * @param throwpoint Throw point (use XBT_THROW_POINT)
+   * @param message    Exception message
+   */
+  xbt_ex(simgrid::xbt::ThrowPoint throwpoint, const char* message) :
+    std::runtime_error(message),
+    simgrid::xbt::WithContextException(throwpoint, simgrid::xbt::backtrace())
+  {}
+
   ~xbt_ex() override;
 
-  xbt_errcat_t category;        /**< category like HTTP (what went wrong) */
-  int value;                    /**< like errno (why did it went wrong) */
-  const char *file;             /**< Thrown point */
-  int line;                     /**< Thrown point */
-  const char *func;             /**< Thrown point */
+  /** Category (what went wrong) */
+  xbt_errcat_t category;
+
+  /** Why did it went wrong */
+  int value;
+
 };
 
 #endif
index c5d1358..e0807e3 100644 (file)
@@ -22,6 +22,15 @@ namespace xbt {
 
 typedef std::vector<xbt_backtrace_location_t> Backtrace;
 
+struct ThrowPoint {
+  ThrowPoint() {}
+  ThrowPoint(const char* file, int line, const char* function) :
+    file(file), line(line), function(function) {}
+  const char* file = nullptr;
+  int line = 0;
+  const char* function = nullptr;
+};
+
 /** A polymorphic mixin class for adding context to an exception */
 XBT_PUBLIC_CLASS WithContextException {
 public:
@@ -35,6 +44,12 @@ public:
     procname_(xbt_procname()),
     pid_(xbt_getpid())
   {}
+  WithContextException(ThrowPoint throwpoint, Backtrace bt) :
+    backtrace_(std::move(bt)),
+    procname_(xbt_procname()),
+    pid_(xbt_getpid()),
+    throwpoint_(throwpoint)
+  {}
   virtual ~WithContextException();
   Backtrace const& backtrace() const
   {
@@ -42,10 +57,12 @@ public:
   }
   int pid() const { return pid_; }
   std::string const& processName() const { return procname_; }
+  ThrowPoint& throwPoint() { return throwpoint_; }
 private:
   Backtrace backtrace_;
   std::string procname_; /**< Name of the process who thrown this */
   int pid_;              /**< PID of the process who thrown this */
+  ThrowPoint throwpoint_;
 };
 
 /** Internal class used to mixin the two classes */
@@ -53,12 +70,17 @@ template<class E>
 class WithContext : public E, public WithContextException
 {
 public:
-  WithContext(E exception)
-    : E(std::move(exception)) {}
-  WithContext(E exception, Backtrace backtrace)
-    : E(std::move(exception)), WithContextException(std::move(backtrace)) {}
-  WithContext(E exception, WithContextException context)
-    : E(std::move(exception)), WithContextException(std::move(context)) {}
+  WithContext(E exception) :
+    E(std::move(exception)) {}
+  WithContext(E exception, ThrowPoint throwpoint, Backtrace backtrace) :
+    E(std::move(exception)),
+    WithContextException(throwpoint, std::move(backtrace)) {}
+  WithContext(E exception, Backtrace backtrace) :
+    E(std::move(exception)),
+    WithContextException(std::move(backtrace)) {}
+  WithContext(E exception, WithContextException context) :
+    E(std::move(exception)),
+    WithContextException(std::move(context)) {}
   ~WithContext() override {}
 };
 
@@ -75,9 +97,25 @@ throwWithContext(
   // Thanks to the default argument, we are taking the backtrace in the caller:
   Backtrace backtrace = simgrid::xbt::backtrace())
 {
- throw WithContext<E>(std::move(exception), std::move(backtrace));
+  throw WithContext<E>(std::move(exception), std::move(backtrace));
+}
+
+template<class E>
+[[noreturn]] inline
+typename std::enable_if< !std::is_base_of<WithContextException,E>::value >::type
+throwWithContext(
+  E exception,
+  ThrowPoint throwpoint,
+  // Thanks to the default argument, we are taking the backtrace in the caller:
+  Backtrace backtrace = simgrid::xbt::backtrace())
+{
+  throw WithContext<E>(std::move(exception), throwpoint, std::move(backtrace));
 }
 
+#define XBT_THROW_POINT ::simgrid::xbt::ThrowPoint(__FILE__, __LINE__, __func__)
+#define XBT_THROW(e) \
+  ::simgrid::xbt::throwWithContext(std::move(e), XBT_THROW_POINT)
+
 }
 }
 
index 2f63009..8460d04 100644 (file)
@@ -82,12 +82,9 @@ JavaContext::JavaContext(std::function<void()> code,
         "Failed to create context #%d. You may want to switch to Java coroutines to increase your limits (error: %s)."
         "See the Install section of simgrid-java documentation (in doc/install.html) for more on coroutines.",
         thread_amount, ex.what());
-      xbt_ex new_exception(str);
+      xbt_ex new_exception(XBT_THROW_POINT, str);
       new_exception.category = ex.category;
       new_exception.value = ex.value;
-      new_exception.file = __FILE__;
-      new_exception.line = __LINE__;
-      new_exception.func = __func__;
       std::throw_with_nested(std::move(new_exception));
     }
   } else {
index ad1ff19..108b770 100644 (file)
@@ -109,12 +109,9 @@ XBT_PUBLIC(void) SIMIX_clean(void);
 #define SMX_EXCEPTION(issuer, cat, val, msg) \
   if (1) { \
   smx_process_t _smx_throw_issuer = (issuer); /* evaluate only once */ \
-  xbt_ex e(msg); \
+  xbt_ex e(XBT_THROW_POINT, msg); \
   e.category = cat; \
   e.value = val; \
-  e.file = __FILE__; \
-  e.line = __LINE__; \
-  e.func = __func__; \
   _smx_throw_issuer->exception = std::make_exception_ptr(e); \
   } else ((void)0)
 
index 3be162f..e9439c2 100644 (file)
@@ -670,7 +670,7 @@ void _xbt_test_fail(const char *file, int line, const char *fmt, ...)
 
 void xbt_test_exception(xbt_ex_t e)
 {
-  _xbt_test_fail(e.file, e.line, "Exception %s raised: %s", xbt_ex_catname(e.category), e.what());
+  _xbt_test_fail(e.throwPoint().file, e.throwPoint().line, "Exception %s raised: %s", xbt_ex_catname(e.category), e.what());
 }
 
 void xbt_test_expect_failure(void)
index 0c11397..516e0b6 100644 (file)
@@ -92,13 +92,10 @@ void xbt_throw(
   char* message, xbt_errcat_t errcat, int value, 
   const char* file, int line, const char* func)
 {
-  xbt_ex e(message);
+  xbt_ex e(simgrid::xbt::ThrowPoint(file, line, func), message);
   free(message);
   e.category = errcat;
   e.value = value;
-  e.file = file;
-  e.line = line;
-  e.func = func;
   throw e;
 }