type-display(#985): improvements and supplements

- a regexp based function to discard non-identifier chars
- nice human readable display of boolean values
This commit is contained in:
Fischlurch 2016-01-10 03:59:01 +01:00
parent 21c02e3015
commit b56f5a8945
5 changed files with 52 additions and 5 deletions

View file

@ -83,8 +83,12 @@ namespace meta {
// pre-allocated failure indicators, which can be returned failsafe.
extern const std::string FAILURE_INDICATOR = "";
extern const std::string VOID_INDICATOR = "void";
extern const string FAILURE_INDICATOR = "";
extern const string VOID_INDICATOR = "void";
extern const string FUNCTION_INDICATOR= "Function";
extern const string BOOL_FALSE_STR = "false";
extern const string BOOL_TRUE_STR = "true";
@ -199,6 +203,7 @@ apologies for that."
"|proc::play::"
"|gui::model"
"|gui::ctrl"
"|lumiera::"
, regex::ECMAScript | regex::optimize};
static regex stdAllocator {"(\\w+<(\\w+)), allocator<\\2>\\s*"
@ -250,6 +255,7 @@ apologies for that."
removeSuffix (typeStr, "&");
if (isnil (typeStr)) return VOID_INDICATOR;
if (')' == typeStr.back()) return FUNCTION_INDICATOR;
auto end = typeStr.end();
auto beg = typeStr.begin();
@ -277,19 +283,29 @@ apologies for that."
string
sanitisedFullTypeName(lib::Literal rawName)
sanitisedFullTypeName (lib::Literal rawName)
{
return util::sanitise (humanReadableTypeID (rawName));
}
string
sanitisedSymbol (string const& text)
{
static regex identifierChars {"[A-Za-z]\\w*", regex::ECMAScript | regex::optimize};
return regex_replace (text, identifierChars, "$&", std::regex_constants::format_no_copy);
}
}}// namespace lib::meta
/* === formatting and pretty printing support uitls === */
/* === formatting and pretty printing support utils === */
namespace util {

View file

@ -84,7 +84,7 @@ namespace meta {
namespace util {
std::string showDouble (double) noexcept;
std::string showFloat (float) noexcept;
std::string showFloat (float) noexcept;
std::string showAddr (void const* addr) noexcept;
/** preconfigured format for pretty-printing of addresses */
@ -135,6 +135,15 @@ namespace util {
return util::showFloat (val);
}
};
template<>
struct StringConv<bool>
{
static std::string
invoke (bool val) noexcept
{
return util::showBool (val);
}
};

View file

@ -199,6 +199,12 @@ namespace meta {
/** build a sanitised ID from full type name */
std::string sanitisedFullTypeName(lib::Literal);
/** condense a string and retain only valid identifiers
* @return string starting with letter, followed by
* letters, digits and underscore
*/
std::string sanitisedSymbol(std::string const&);
/** reverse the effect of C++ name mangling.
* @return string in language-level form of a C++ type or object name,
* or a string with the original input if demangling fails.
@ -211,9 +217,12 @@ namespace meta {
std::string demangleCxx (lib::Literal rawName);
extern const std::string FUNCTION_INDICATOR;
extern const std::string FAILURE_INDICATOR;
extern const std::string VOID_INDICATOR;
extern const std::string BOOL_FALSE_STR;
extern const std::string BOOL_TRUE_STR;
/** failsafe human readable type display
@ -354,6 +363,16 @@ namespace util {
: "⟂ «" + typeStr(ptr) + "»";
}
/** human readable display of boolean values
* @return "`true`" or "`false`"
*/
inline std::string
showBool (bool yes) noexcept
{
return yes? lib::meta::BOOL_TRUE_STR
: lib::meta::BOOL_FALSE_STR;
}
}// namespace util

View file

@ -347,6 +347,7 @@ namespace util {
"Bääääh!!" --> 'Bh'
\endverbatim
* @see sanitised-identifier-test.cpp
* @see lib::meta::sanitisedSymbol()
*/
string sanitise (string const& org);

View file

@ -112,6 +112,8 @@ namespace test{
cout << typeSymbol(&magic) <<endl;
cout << typeStr<Outer<typeof(this)>::Inner>() <<endl;
cout << sanitisedSymbol("") <<endl;
cout << sanitisedSymbol("bääähla7/(6sf*z") <<endl;
}
};