summaryrefslogtreecommitdiff
path: root/src/third_party/libmongocrypt/dist/src/mlib/check.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/third_party/libmongocrypt/dist/src/mlib/check.hpp')
-rw-r--r--src/third_party/libmongocrypt/dist/src/mlib/check.hpp89
1 files changed, 89 insertions, 0 deletions
diff --git a/src/third_party/libmongocrypt/dist/src/mlib/check.hpp b/src/third_party/libmongocrypt/dist/src/mlib/check.hpp
new file mode 100644
index 00000000000..18642b2f624
--- /dev/null
+++ b/src/third_party/libmongocrypt/dist/src/mlib/check.hpp
@@ -0,0 +1,89 @@
+#ifndef MLIB_CHECK_HPP_INCLUDED
+#define MLIB_CHECK_HPP_INCLUDED
+
+#include <iostream>
+#include <cstdio>
+#include <string>
+
+namespace mlib
+{
+namespace detail
+{
+struct check_info {
+ const char *filename;
+ int line;
+ const char *expr;
+};
+
+struct nil {
+};
+
+template <typename Left> struct bound_lhs {
+ check_info info;
+ Left value;
+
+#define DEFOP(Oper) \
+ template <typename Rhs> nil operator Oper (Rhs rhs) const noexcept \
+ { \
+ if (value Oper rhs) { \
+ return {}; \
+ } \
+ std::fprintf (stderr, \
+ "%s:%d: CHECK( %s ) failed!\n", \
+ info.filename, \
+ info.line, \
+ info.expr); \
+ std::cerr << "Expanded expression: " << value << " " #Oper " " << rhs \
+ << '\n'; \
+ std::exit (2); \
+ }
+ DEFOP (==)
+ DEFOP (!=)
+ DEFOP (<)
+ DEFOP (<=)
+ DEFOP (>)
+ DEFOP (>=)
+#undef DEFOP
+};
+
+struct check_magic {
+ check_info info;
+
+ template <typename Oper>
+ bound_lhs<Oper>
+ operator->*(Oper op)
+ {
+ return bound_lhs<Oper>{info, op};
+ }
+};
+
+struct check_consume {
+ void
+ operator= (nil)
+ {
+ }
+
+ void
+ operator= (bound_lhs<bool> const &l)
+ {
+ // Invoke the test for truthiness:
+ (void) (l == true);
+ }
+};
+
+/**
+ * @brief Create an assertion that prints the expanded expression upon failure.
+ *
+ * Only supports simple comparison binary expressions, and plain boolean
+ * expressions
+ */
+#define MLIB_CHECK(Cond) \
+ ::mlib::detail::check_consume{} = \
+ ::mlib::detail::check_magic{ \
+ ::mlib::detail::check_info{__FILE__, __LINE__, #Cond}} \
+ ->*Cond
+
+} // namespace detail
+} // namespace mlib
+
+#endif // MLIB_CHECK_HPP_INCLUDED