0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 09:32:32 +01:00

helper mongo::ErrorMsg class that builds error strings. lighter weight than a StringBuilder, albeit less flexible.

This commit is contained in:
dwight 2011-05-23 10:23:51 -04:00
parent 990e708ead
commit 923660edbb
2 changed files with 32 additions and 0 deletions

View File

@ -154,5 +154,22 @@ namespace mongo {
#endif
}
NOINLINE_DECL ErrorMsg::ErrorMsg(const char *msg, char ch) {
int l = strlen(msg);
assert( l < 128);
memcpy(buf, msg, l);
char *p = buf + l;
p[0] = ch;
p[1] = 0;
}
NOINLINE_DECL ErrorMsg::ErrorMsg(const char *msg, unsigned val) {
int l = strlen(msg);
assert( l < 128);
memcpy(buf, msg, l);
char *p = buf + l;
sprintf(p, "%u", val);
}
}

View File

@ -67,6 +67,21 @@ namespace mongo {
int code;
};
/** helper class that builds error strings. lighter weight than a StringBuilder, albeit less flexible.
NOINLINE_DECL used in the constructor implementations as we are assuming this is a cold code path when used.
example:
throw UserException(123, ErrorMsg("blah", num_val));
*/
class ErrorMsg {
public:
ErrorMsg(const char *msg, char ch);
ErrorMsg(const char *msg, unsigned val);
operator string() const { return buf; }
private:
char buf[256];
};
class DBException : public std::exception {
public:
DBException( const ExceptionInfo& ei ) : _ei(ei) {}