From 9759e52b3c84e73cea268215e2aedcd5233c09c7 Mon Sep 17 00:00:00 2001 From: Jonas Kulla Date: Sun, 29 Dec 2013 18:05:11 +0100 Subject: [PATCH] Exception: Constructor now takes printf style arguments --- binding-mri/binding-util.cpp | 4 +--- binding-mruby/binding-mruby.cpp | 4 +--- binding-mruby/binding-util.cpp | 4 +--- binding-mruby/mrb-ext/marshal.cpp | 4 ++-- src/disposable.h | 2 +- src/exception.h | 21 +++++++++++---------- src/texpool.cpp | 11 +++-------- 7 files changed, 20 insertions(+), 30 deletions(-) diff --git a/binding-mri/binding-util.cpp b/binding-mri/binding-util.cpp index 9fe55a9..d2d924a 100644 --- a/binding-mri/binding-util.cpp +++ b/binding-mri/binding-util.cpp @@ -94,9 +94,7 @@ void raiseRbExc(const Exception &exc) RbData *data = getRbData(); VALUE excClass = data->exc[excToRbExc[exc.type]]; - static char buffer[512]; - exc.snprintf(buffer, sizeof(buffer)); - rb_raise(excClass, buffer); + rb_raise(excClass, exc.msg.c_str()); } int diff --git a/binding-mruby/binding-mruby.cpp b/binding-mruby/binding-mruby.cpp index 1e9cdc8..cac342a 100644 --- a/binding-mruby/binding-mruby.cpp +++ b/binding-mruby/binding-mruby.cpp @@ -275,9 +275,7 @@ runRMXPScripts(mrb_state *mrb, mrbc_context *ctx) } catch (const Exception &e) { - char buffer[512]; - snprintf(buffer, sizeof(buffer), e.fmt.c_str(), e.arg1.c_str(), e.arg2.c_str()); - readError = std::string(": ") + std::string(buffer); + readError = std::string(": ") + e.msg; } SDL_RWclose(&ops); diff --git a/binding-mruby/binding-util.cpp b/binding-mruby/binding-util.cpp index 935e07e..8602344 100644 --- a/binding-mruby/binding-util.cpp +++ b/binding-mruby/binding-util.cpp @@ -138,9 +138,7 @@ void raiseMrbExc(mrb_state *mrb, const Exception &exc) MrbData *data = getMrbData(mrb); RClass *excClass = data->exc[excToMrbExc[exc.type]]; - static char buffer[512]; - exc.snprintf(buffer, sizeof(buffer)); - mrb_raise(mrb, excClass, buffer); + mrb_raise(mrb, excClass, exc.msg.c_str()); } MRB_METHOD_PUB(inspectObject) diff --git a/binding-mruby/mrb-ext/marshal.cpp b/binding-mruby/mrb-ext/marshal.cpp index 421e317..378813a 100644 --- a/binding-mruby/mrb-ext/marshal.cpp +++ b/binding-mruby/mrb-ext/marshal.cpp @@ -548,8 +548,8 @@ read_value(MarshalContext *ctx) break; default : - throw Exception(Exception::MKXPError, "Marshal.load: unsupported value type '%s'", - std::string(1, (char)type)); + throw Exception(Exception::MKXPError, "Marshal.load: unsupported value type '%c'", + (char) type); } mrb_gc_arena_restore(mrb, arena); diff --git a/src/disposable.h b/src/disposable.h index 3c10f9d..9ecc971 100644 --- a/src/disposable.h +++ b/src/disposable.h @@ -58,6 +58,6 @@ private: /* Every cpp needs to define DISP_CLASS_NAME for itself (lowercase) */ #define GUARD_DISPOSED \ -{ if (isDisposed()) throw Exception(Exception::RGSSError, "disposed %S", DISP_CLASS_NAME); } +{ if (isDisposed()) throw Exception(Exception::RGSSError, "disposed %s", DISP_CLASS_NAME); } #endif // DISPOSABLE_H diff --git a/src/exception.h b/src/exception.h index 9254572..0157915 100644 --- a/src/exception.h +++ b/src/exception.h @@ -24,6 +24,7 @@ #include #include +#include struct Exception { @@ -44,19 +45,19 @@ struct Exception }; Type type; - std::string fmt; - std::string arg1; - std::string arg2; + std::string msg; - Exception(Type type, std::string fmt, - std::string arg1 = std::string(), - std::string arg2 = std::string()) - : type(type), fmt(fmt), arg1(arg1), arg2(arg2) - {} - void snprintf(char *buffer, size_t bufSize) const + Exception(Type type, const char *format, ...) + : type(type) { - ::snprintf(buffer, bufSize, fmt.c_str(), arg1.c_str(), arg2.c_str()); + va_list ap; + va_start(ap, format); + + msg.resize(512); + vsnprintf(&msg[0], msg.size(), format, ap); + + va_end(ap); } }; diff --git a/src/texpool.cpp b/src/texpool.cpp index 06433c7..f2cecd5 100644 --- a/src/texpool.cpp +++ b/src/texpool.cpp @@ -128,14 +128,9 @@ TEXFBO TexPool::request(int width, int height) int maxSize = glState.caps.maxTexSize; if (width > maxSize || height > maxSize) - { - char buffer[128]; - snprintf(buffer, sizeof(buffer), - "Texture dimensions [%d, %d] exceed hardware capabilities", - width, height); - - throw Exception(Exception::MKXPError, buffer); - } + throw Exception(Exception::MKXPError, + "Texture dimensions [%d, %d] exceed hardware capabilities", + width, height); /* Nope, create it instead */ TEXFBO::init(cnode.obj);