Switched exception constants to const char * const (from const std::string) so we don't have to construct them all in every translation unit, and switched the exception class to derive from std::runtime_error (so it handles what() for us)

This commit is contained in:
jbeder
2011-03-02 04:12:57 +00:00
parent 3f6254822d
commit 3192d29e66
5 changed files with 69 additions and 68 deletions

View File

@@ -6,7 +6,7 @@
#include "yaml-cpp/mark.h"
#include "yaml-cpp/traits.h"
#include <exception>
#include <stdexcept>
#include <string>
#include <sstream>
@@ -15,59 +15,59 @@ namespace YAML
// error messages
namespace ErrorMsg
{
const std::string YAML_DIRECTIVE_ARGS = "YAML directives must have exactly one argument";
const std::string YAML_VERSION = "bad YAML version: ";
const std::string YAML_MAJOR_VERSION = "YAML major version too large";
const std::string REPEATED_YAML_DIRECTIVE= "repeated YAML directive";
const std::string TAG_DIRECTIVE_ARGS = "TAG directives must have exactly two arguments";
const std::string REPEATED_TAG_DIRECTIVE = "repeated TAG directive";
const std::string CHAR_IN_TAG_HANDLE = "illegal character found while scanning tag handle";
const std::string TAG_WITH_NO_SUFFIX = "tag handle with no suffix";
const std::string END_OF_VERBATIM_TAG = "end of verbatim tag not found";
const std::string END_OF_MAP = "end of map not found";
const std::string END_OF_MAP_FLOW = "end of map flow not found";
const std::string END_OF_SEQ = "end of sequence not found";
const std::string END_OF_SEQ_FLOW = "end of sequence flow not found";
const std::string MULTIPLE_TAGS = "cannot assign multiple tags to the same node";
const std::string MULTIPLE_ANCHORS = "cannot assign multiple anchors to the same node";
const std::string MULTIPLE_ALIASES = "cannot assign multiple aliases to the same node";
const std::string ALIAS_CONTENT = "aliases can't have any content, *including* tags";
const std::string INVALID_HEX = "bad character found while scanning hex number";
const std::string INVALID_UNICODE = "invalid unicode: ";
const std::string INVALID_ESCAPE = "unknown escape character: ";
const std::string UNKNOWN_TOKEN = "unknown token";
const std::string DOC_IN_SCALAR = "illegal document indicator in scalar";
const std::string EOF_IN_SCALAR = "illegal EOF in scalar";
const std::string CHAR_IN_SCALAR = "illegal character in scalar";
const std::string TAB_IN_INDENTATION = "illegal tab when looking for indentation";
const std::string FLOW_END = "illegal flow end";
const std::string BLOCK_ENTRY = "illegal block entry";
const std::string MAP_KEY = "illegal map key";
const std::string MAP_VALUE = "illegal map value";
const std::string ALIAS_NOT_FOUND = "alias not found after *";
const std::string ANCHOR_NOT_FOUND = "anchor not found after &";
const std::string CHAR_IN_ALIAS = "illegal character found while scanning alias";
const std::string CHAR_IN_ANCHOR = "illegal character found while scanning anchor";
const std::string ZERO_INDENT_IN_BLOCK = "cannot set zero indentation for a block scalar";
const std::string CHAR_IN_BLOCK = "unexpected character in block scalar";
const std::string AMBIGUOUS_ANCHOR = "cannot assign the same alias to multiple nodes";
const std::string UNKNOWN_ANCHOR = "the referenced anchor is not defined";
const char * const YAML_DIRECTIVE_ARGS = "YAML directives must have exactly one argument";
const char * const YAML_VERSION = "bad YAML version: ";
const char * const YAML_MAJOR_VERSION = "YAML major version too large";
const char * const REPEATED_YAML_DIRECTIVE= "repeated YAML directive";
const char * const TAG_DIRECTIVE_ARGS = "TAG directives must have exactly two arguments";
const char * const REPEATED_TAG_DIRECTIVE = "repeated TAG directive";
const char * const CHAR_IN_TAG_HANDLE = "illegal character found while scanning tag handle";
const char * const TAG_WITH_NO_SUFFIX = "tag handle with no suffix";
const char * const END_OF_VERBATIM_TAG = "end of verbatim tag not found";
const char * const END_OF_MAP = "end of map not found";
const char * const END_OF_MAP_FLOW = "end of map flow not found";
const char * const END_OF_SEQ = "end of sequence not found";
const char * const END_OF_SEQ_FLOW = "end of sequence flow not found";
const char * const MULTIPLE_TAGS = "cannot assign multiple tags to the same node";
const char * const MULTIPLE_ANCHORS = "cannot assign multiple anchors to the same node";
const char * const MULTIPLE_ALIASES = "cannot assign multiple aliases to the same node";
const char * const ALIAS_CONTENT = "aliases can't have any content, *including* tags";
const char * const INVALID_HEX = "bad character found while scanning hex number";
const char * const INVALID_UNICODE = "invalid unicode: ";
const char * const INVALID_ESCAPE = "unknown escape character: ";
const char * const UNKNOWN_TOKEN = "unknown token";
const char * const DOC_IN_SCALAR = "illegal document indicator in scalar";
const char * const EOF_IN_SCALAR = "illegal EOF in scalar";
const char * const CHAR_IN_SCALAR = "illegal character in scalar";
const char * const TAB_IN_INDENTATION = "illegal tab when looking for indentation";
const char * const FLOW_END = "illegal flow end";
const char * const BLOCK_ENTRY = "illegal block entry";
const char * const MAP_KEY = "illegal map key";
const char * const MAP_VALUE = "illegal map value";
const char * const ALIAS_NOT_FOUND = "alias not found after *";
const char * const ANCHOR_NOT_FOUND = "anchor not found after &";
const char * const CHAR_IN_ALIAS = "illegal character found while scanning alias";
const char * const CHAR_IN_ANCHOR = "illegal character found while scanning anchor";
const char * const ZERO_INDENT_IN_BLOCK = "cannot set zero indentation for a block scalar";
const char * const CHAR_IN_BLOCK = "unexpected character in block scalar";
const char * const AMBIGUOUS_ANCHOR = "cannot assign the same alias to multiple nodes";
const char * const UNKNOWN_ANCHOR = "the referenced anchor is not defined";
const std::string INVALID_SCALAR = "invalid scalar";
const std::string KEY_NOT_FOUND = "key not found";
const std::string BAD_DEREFERENCE = "bad dereference";
const char * const INVALID_SCALAR = "invalid scalar";
const char * const KEY_NOT_FOUND = "key not found";
const char * const BAD_DEREFERENCE = "bad dereference";
const std::string UNMATCHED_GROUP_TAG = "unmatched group tag";
const std::string UNEXPECTED_END_SEQ = "unexpected end sequence token";
const std::string UNEXPECTED_END_MAP = "unexpected end map token";
const std::string SINGLE_QUOTED_CHAR = "invalid character in single-quoted string";
const std::string INVALID_ANCHOR = "invalid anchor";
const std::string INVALID_ALIAS = "invalid alias";
const std::string INVALID_TAG = "invalid tag";
const std::string EXPECTED_KEY_TOKEN = "expected key token";
const std::string EXPECTED_VALUE_TOKEN = "expected value token";
const std::string UNEXPECTED_KEY_TOKEN = "unexpected key token";
const std::string UNEXPECTED_VALUE_TOKEN = "unexpected value token";
const char * const UNMATCHED_GROUP_TAG = "unmatched group tag";
const char * const UNEXPECTED_END_SEQ = "unexpected end sequence token";
const char * const UNEXPECTED_END_MAP = "unexpected end map token";
const char * const SINGLE_QUOTED_CHAR = "invalid character in single-quoted string";
const char * const INVALID_ANCHOR = "invalid anchor";
const char * const INVALID_ALIAS = "invalid alias";
const char * const INVALID_TAG = "invalid tag";
const char * const EXPECTED_KEY_TOKEN = "expected key token";
const char * const EXPECTED_VALUE_TOKEN = "expected value token";
const char * const UNEXPECTED_KEY_TOKEN = "unexpected key token";
const char * const UNEXPECTED_VALUE_TOKEN = "unexpected value token";
template <typename T>
inline const std::string KEY_NOT_FOUND_WITH_KEY(const T&, typename disable_if<is_numeric<T> >::type * = 0) {
@@ -75,7 +75,9 @@ namespace YAML
}
inline const std::string KEY_NOT_FOUND_WITH_KEY(const std::string& key) {
return KEY_NOT_FOUND + ": " + key;
std::stringstream stream;
stream << KEY_NOT_FOUND << ": " << key;
return stream.str();
}
template <typename T>
@@ -86,22 +88,21 @@ namespace YAML
}
}
class Exception: public std::exception {
class Exception: public std::runtime_error {
public:
Exception(const Mark& mark_, const std::string& msg_)
: mark(mark_), msg(msg_) {
std::stringstream output;
output << "yaml-cpp: error at line " << mark.line+1 << ", column " << mark.column+1 << ": " << msg;
what_ = output.str();
}
: std::runtime_error(build_what(mark_, msg_)), mark(mark_), msg(msg_) {}
virtual ~Exception() throw() {}
virtual const char *what() const throw() { return what_.c_str(); }
Mark mark;
std::string msg;
private:
std::string what_;
static const std::string build_what(const Mark& mark, const std::string& msg) {
std::stringstream output;
output << "yaml-cpp: error at line " << mark.line+1 << ", column " << mark.column+1 << ": " << msg;
return output.str();
}
};
class ParserException: public Exception {

View File

@@ -107,7 +107,7 @@ namespace YAML
}
std::stringstream msg;
throw ParserException(in.mark(), ErrorMsg::INVALID_ESCAPE + ch);
throw ParserException(in.mark(), std::string(ErrorMsg::INVALID_ESCAPE) + ch);
}
}
}

View File

@@ -112,7 +112,7 @@ namespace YAML
str.get();
str >> m_pDirectives->version.minor;
if(!str || str.peek() != EOF)
throw ParserException(token.mark, ErrorMsg::YAML_VERSION + token.params[0]);
throw ParserException(token.mark, std::string(ErrorMsg::YAML_VERSION) + token.params[0]);
if(m_pDirectives->version.major > 1)
throw ParserException(token.mark, ErrorMsg::YAML_MAJOR_VERSION);

View File

@@ -669,21 +669,21 @@ namespace Test
try {
doc["bad key"];
} catch(const YAML::Exception& e) {
if(e.msg != YAML::ErrorMsg::KEY_NOT_FOUND + ": bad key")
if(e.msg != std::string(YAML::ErrorMsg::KEY_NOT_FOUND) + ": bad key")
throw;
}
try {
doc[5];
} catch(const YAML::Exception& e) {
if(e.msg != YAML::ErrorMsg::KEY_NOT_FOUND + ": 5")
if(e.msg != std::string(YAML::ErrorMsg::KEY_NOT_FOUND) + ": 5")
throw;
}
try {
doc[2.5];
} catch(const YAML::Exception& e) {
if(e.msg != YAML::ErrorMsg::KEY_NOT_FOUND + ": 2.5")
if(e.msg != std::string(YAML::ErrorMsg::KEY_NOT_FOUND) + ": 2.5")
throw;
}

View File

@@ -888,7 +888,7 @@ namespace Test {
YAML::Node doc;
parser.GetNextDocument(doc);
} catch(const YAML::ParserException& e) {
YAML_ASSERT(e.msg == YAML::ErrorMsg::INVALID_ESCAPE + "c");
YAML_ASSERT(e.msg == std::string(YAML::ErrorMsg::INVALID_ESCAPE) + "c");
return true;
}