aboutsummaryrefslogtreecommitdiffstats
path: root/al/eax/exception.cpp
diff options
context:
space:
mode:
authorChris Robinson <[email protected]>2022-05-16 02:08:18 -0700
committerChris Robinson <[email protected]>2022-05-16 02:08:18 -0700
commit65e4c20c27f2acf853e58fd4c26ebc0e3eb926c6 (patch)
tree4fb9a3bffbda4ab8dc1363caa2426cf8e8bbf30e /al/eax/exception.cpp
parent83238973ed08225adf03e76b6933e0c209f93fd9 (diff)
Move EAX files to their own sub-directory
Diffstat (limited to 'al/eax/exception.cpp')
-rw-r--r--al/eax/exception.cpp62
1 files changed, 62 insertions, 0 deletions
diff --git a/al/eax/exception.cpp b/al/eax/exception.cpp
new file mode 100644
index 00000000..3b319648
--- /dev/null
+++ b/al/eax/exception.cpp
@@ -0,0 +1,62 @@
+#include "config.h"
+
+#include "exception.h"
+
+#include <cassert>
+#include <string>
+
+
+EaxException::EaxException(
+ const char* context,
+ const char* message)
+ :
+ std::runtime_error{make_message(context, message)}
+{
+}
+
+std::string EaxException::make_message(
+ const char* context,
+ const char* message)
+{
+ const auto context_size = (context ? std::string::traits_type::length(context) : 0);
+ const auto has_contex = (context_size > 0);
+
+ const auto message_size = (message ? std::string::traits_type::length(message) : 0);
+ const auto has_message = (message_size > 0);
+
+ if (!has_contex && !has_message)
+ {
+ return std::string{};
+ }
+
+ static constexpr char left_prefix[] = "[";
+ const auto left_prefix_size = std::string::traits_type::length(left_prefix);
+
+ static constexpr char right_prefix[] = "] ";
+ const auto right_prefix_size = std::string::traits_type::length(right_prefix);
+
+ const auto what_size =
+ (
+ has_contex ?
+ left_prefix_size + context_size + right_prefix_size :
+ 0) +
+ message_size +
+ 1;
+
+ auto what = std::string{};
+ what.reserve(what_size);
+
+ if (has_contex)
+ {
+ what.append(left_prefix, left_prefix_size);
+ what.append(context, context_size);
+ what.append(right_prefix, right_prefix_size);
+ }
+
+ if (has_message)
+ {
+ what.append(message, message_size);
+ }
+
+ return what;
+}