aboutsummaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorChris Robinson <[email protected]>2020-12-21 18:00:43 -0800
committerChris Robinson <[email protected]>2020-12-24 22:49:55 -0800
commiteedc42890fa1358a6639051a39f7e73f8d4d3b07 (patch)
tree927a986e1004db66cdf58741be908d8fb8eb6134 /core
parent63ea62bea1604ad97066809596da258751b48114 (diff)
Move alexcpt to core
Diffstat (limited to 'core')
-rw-r--r--core/except.cpp31
-rw-r--r--core/except.h31
2 files changed, 62 insertions, 0 deletions
diff --git a/core/except.cpp b/core/except.cpp
new file mode 100644
index 00000000..07bb410a
--- /dev/null
+++ b/core/except.cpp
@@ -0,0 +1,31 @@
+
+#include "config.h"
+
+#include "except.h"
+
+#include <cstdio>
+#include <cstdarg>
+
+#include "opthelpers.h"
+
+
+namespace al {
+
+/* Defined here to avoid inlining it. */
+base_exception::~base_exception() { }
+
+void base_exception::setMessage(const char* msg, std::va_list args)
+{
+ std::va_list args2;
+ va_copy(args2, args);
+ int msglen{std::vsnprintf(nullptr, 0, msg, args)};
+ if LIKELY(msglen > 0)
+ {
+ mMessage.resize(static_cast<size_t>(msglen)+1);
+ std::vsnprintf(&mMessage[0], mMessage.length(), msg, args2);
+ mMessage.pop_back();
+ }
+ va_end(args2);
+}
+
+} // namespace al
diff --git a/core/except.h b/core/except.h
new file mode 100644
index 00000000..0e28e9df
--- /dev/null
+++ b/core/except.h
@@ -0,0 +1,31 @@
+#ifndef CORE_EXCEPT_H
+#define CORE_EXCEPT_H
+
+#include <cstdarg>
+#include <exception>
+#include <string>
+#include <utility>
+
+
+namespace al {
+
+class base_exception : public std::exception {
+ std::string mMessage;
+
+protected:
+ base_exception() = default;
+ virtual ~base_exception();
+
+ void setMessage(const char *msg, std::va_list args);
+
+public:
+ const char *what() const noexcept override { return mMessage.c_str(); }
+};
+
+} // namespace al
+
+#define START_API_FUNC try
+
+#define END_API_FUNC catch(...) { std::terminate(); }
+
+#endif /* CORE_EXCEPT_H */