Kaynağa Gözat

refactor: macro cleanup

Sam Jaffe 1 hafta önce
ebeveyn
işleme
344bad7fe2

+ 32 - 0
include/jvalidate/_macro.h

@@ -8,3 +8,35 @@
 #define JVALIDATE_IIF0(IF, ELSE) ELSE
 #define JVALIDATE_IIF1(IF, ELSE) IF
 #define JVALIDATE_IIF(CONDITIONAL, IF, ELSE) JVALIDATE_CONCAT(JVALIDATE_IIF, CONDITIONAL)(IF, ELSE)
+
+/**
+ * @brief Assert a certain pre/post-condition is true, else return the default
+ * expression (or void).
+ *
+ * @param condition A boolean or boolean-like expression that should be TRUE.
+ * If the condition is FALSE, then the other params are used to produce errors.
+ *
+ * @param ... Zero or One arguments representing the return value if the
+ * condition is FALSE. Zero arguments is equivalent to `return void();`, which
+ * doesn't need to be explicitly stated.
+ */
+#define JVALIDATE_RETURN_UNLESS(condition, ...)                                                    \
+  if (!(condition)) [[unlikely]] {                                                                 \
+    return __VA_ARGS__;                                                                            \
+  }
+
+/**
+ * @brief Assert a certain pre/post-condition is false, else return the default
+ * expression (or void).
+ *
+ * @param condition A boolean or boolean-like expression that should be FALSE.
+ * If the condition is TRUE, then the other params are used to produce errors.
+ *
+ * @param ... Zero or One arguments representing the return value if the
+ * condition is TRUE. Zero arguments is equivalent to `return void();`, which
+ * doesn't need to be explicitly stated.
+ */
+#define JVALIDATE_RETURN_IF(condition, ...)                                                        \
+  if (condition) [[unlikely]] {                                                                    \
+    return __VA_ARGS__;                                                                            \
+  }

+ 3 - 27
include/jvalidate/detail/expect.h

@@ -1,15 +1,7 @@
 #pragma once
 
-#include <iostream>
-#include <sstream>
-
-#if defined(__clang__) || defined(__GNUC__)
-#define JVALIDATE_LIKELY(x) __builtin_expect(!!(x), 1)
-#define JVALIDATE_UNLIKELY(x) __builtin_expect(!!(x), 0)
-#else
-#define JVALIDATE_LIKELY(x) (x)
-#define JVALIDATE_UNLIKELY(x) (x)
-#endif
+#include <iostream> // IWYU pragma: keep
+#include <sstream>  // IWYU pragma: keep
 
 #if defined(JVALIDATE_USE_EXCEPTIONS)
 /**
@@ -58,7 +50,7 @@
  * output chain (e.g. `"unsupported index " << i << ", valid items " << items`).
  */
 #define EXPECT_T(condition, extype, message)                                                       \
-  if (JVALIDATE_UNLIKELY(!(condition))) {                                                          \
+  if (!(condition)) [[unlikely]] {                                                                 \
     JVALIDATE_THROW(extype, message);                                                              \
   }
 
@@ -81,19 +73,3 @@
  * If the condition is FALSE, then the other params are used to produce errors.
  */
 #define EXPECT(condition) EXPECT_M(condition, #condition " at " __FILE__ ":" << __LINE__)
-
-/**
- * @brief Assert a certain pre/post-condition is true, else return the default
- * expression (or void).
- *
- * @param condition A boolean or boolean-like expression that should be TRUE.
- * If the condition is FALSE, then the other params are used to produce errors.
- *
- * @param ... Zero or One arguments representing the return value if the
- * condition is FALSE. Zero arguments is equivalent to `return void();`, which
- * doesn't need to be explicitly stated.
- */
-#define RETURN_UNLESS(condition, ...)                                                              \
-  if (JVALIDATE_UNLIKELY(!(condition))) {                                                          \
-    return __VA_ARGS__;                                                                            \
-  }

+ 2 - 4
include/jvalidate/regex.h

@@ -109,10 +109,8 @@ public:
     UErrorCode status = U_ZERO_ERROR;
     icu::UnicodeString const ucs = icu::UnicodeString::fromUTF8(icu::StringPiece(text));
     std::unique_ptr<icu::RegexMatcher> matcher(it->second->matcher(ucs, status));
-    if (U_FAILURE(status)) {
-      // Realistically, never called
-      return false;
-    }
+
+    JVALIDATE_RETURN_IF(U_FAILURE(status), false); // Doesn't appear possilbe
     return matcher->find(status);
   }
 };

+ 4 - 2
include/jvalidate/validation_visitor.h

@@ -35,7 +35,8 @@
     }                                                                                              \
   } while (false)
 
-#define NOOP_UNLESS_TYPE(etype) RETURN_UNLESS(adapter::Type::etype == document.type(), Status::Noop)
+#define NOOP_UNLESS_TYPE(etype)                                                                    \
+  JVALIDATE_RETURN_UNLESS(adapter::Type::etype == document.type(), Status::Noop)
 
 #define BREAK_EARLY_IF_NO_RESULT_TREE()                                                            \
   do {                                                                                             \
@@ -282,7 +283,8 @@ private:
 
   Status visit(constraint::MultipleOfConstraint const & cons, Adapter auto const & document) const {
     adapter::Type const type = document.type();
-    RETURN_UNLESS(type == adapter::Type::Number || type == adapter::Type::Integer, Status::Noop);
+    JVALIDATE_RETURN_UNLESS(type == adapter::Type::Number || type == adapter::Type::Integer,
+                            Status::Noop);
 
     if (double value = document.as_number(); not cons(value)) {
       return result(Status::Reject, value, " is not a multiple of ", cons.value);