From 20622e74de6753c5e4460a112c11ee913707e8e8 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 10 Apr 2021 12:45:50 +0300 Subject: [PATCH] Make Tags convertible back to the original string in debug builds This helps in producing more readable error messages --- source/render/tag.cpp | 41 +++++++++++++++++++++++++++++++++++++++-- source/render/tag.h | 6 ++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/source/render/tag.cpp b/source/render/tag.cpp index c5824aec..b5f86033 100644 --- a/source/render/tag.cpp +++ b/source/render/tag.cpp @@ -1,17 +1,54 @@ #include +#include #include +#include #include "tag.h" +using namespace std; + +namespace { + +#ifdef DEBUG +map tag_names; +#endif + +} + namespace Msp { namespace GL { Tag::Tag(const char *s): id((s && *s) ? hash32(s, strlen(s)) : 0) -{ } +{ +#ifdef DEBUG + if(s) + tag_names.insert(make_pair(*this, string(s))); +#endif +} Tag::Tag(const std::string &s): id(s.empty() ? 0 : hash32(s)) -{ } +{ +#ifdef DEBUG + if(!s.empty()) + tag_names.insert(make_pair(*this, s)); +#endif +} + +string Tag::str() const +{ +#ifdef DEBUG + map::const_iterator i=tag_names.find(*this); + if(i!=tag_names.end()) + return i->second; +#endif + return format("Tag(%d)", id); +} + +void operator<<(LexicalConverter &conv, Tag tag) +{ + conv.result(tag.str()); +} } // namespace GL } // namespace Msp diff --git a/source/render/tag.h b/source/render/tag.h index 93808989..dbc0b3ef 100644 --- a/source/render/tag.h +++ b/source/render/tag.h @@ -2,6 +2,7 @@ #define MSP_GL_TAG_H_ #include +#include namespace Msp { namespace GL { @@ -18,10 +19,15 @@ struct Tag Tag(const char *); Tag(const std::string &s); + std::string str() const; + bool operator<(Tag t) const { return id