Symbol: rework initialisation and introduce a "bottom" Symbol

Up to now, we tolerated null pointers in Literal instances.
But we can not tolerate passing a null cString to Symbol initialisation.
Rather, hereby we introduce a dedicated "bottom" Symbol, a valid "null object"
This commit is contained in:
Fischlurch 2017-04-07 19:25:21 +02:00
parent 23c412bc42
commit 4df59678a3
3 changed files with 49 additions and 35 deletions

View file

@ -59,6 +59,33 @@ namespace lib {
const size_t STRING_MAX_RELEVANT = LUMIERA_IDSTRING_MAX_RELEVANT;
namespace { // symbol table implementation
/** @warning grows eternally, never shrinks */
std::unordered_set<string> symbolTable;
}
/**
* create Symbol by symbol table lookup.
* @note identical strings will be mapped to
* the same Symbol (embedded pointer)
*/
Symbol::Symbol (string&& definition)
: Literal{symbolTable.insert(forward<string> (definition)).first->c_str()}
{ }
/* == predefined marker Symbols == */
Symbol Symbol::BOTTOM = "⟂";
Symbol Symbol::FAILURE = "↯";
// see also: lib/format-obj.cpp
// We can not share these definitions due to undefined static init order
/** equality on Literal and Symbol values is defined
* based on the content, not the address. */
bool
@ -90,20 +117,4 @@ namespace lib {
namespace { // quick-n-dirty symbol table implementation
/** @warning grows eternally, never shrinks */
std::unordered_set<string> symbolTable; ////////////////////////////////TICKET #158 replace by symbol table class
}
/** create Symbol by symbol table lookup.
* @note identical strings will be mapped to
* the same Symbol (embedded pointer)
*/
Symbol::Symbol (string&& definition)
: Literal{symbolTable.insert(forward<string> (definition)).first->c_str()}
{ }
} // namespace lib

View file

@ -96,25 +96,28 @@ namespace lib {
: public Literal
{
public:
explicit
Symbol (std::string&& definition);
static Symbol BOTTOM;
static Symbol FAILURE;
Symbol (const char* lit =NULL)
: Symbol{std::string(lit? lit : BOTTOM.c())}
{ }
explicit
Symbol (std::string&& definition);
Symbol (std::string const& str)
: Symbol{std::string(str)}
{ }
Symbol (Literal const& base, std::string ext)
: Symbol{std::string(base)+"."+ext}
{ }
Symbol (Literal const& base, const char* ext)
: Symbol{base, std::string(ext)}
{ }
Symbol()
: Symbol{""}
{ }
Symbol (const char* lit)
: Symbol{std::string(lit)}
{ }
Symbol (Literal const& base, std::string ext)
: Symbol{std::string(base)+"."+ext}
{ }
Symbol (Literal const& base, const char* ext)
: Symbol{base, std::string(ext)}
{ }
Symbol (Symbol const&) = default;
Symbol (Symbol &&) = default;

View file

@ -113,7 +113,7 @@ namespace test{
CHECK (sy1 == sy2);
CHECK (not isSameObject (l1,sy1));
CHECK (not isSameObject (sy1,sy2));
CHECK (l1.c() == sy1.c());
CHECK (l1.c() != sy1.c());
CHECK (sy1.c() == sy2.c());
Symbol sy3;