2009-09-24 19:38:11 +02:00
|
|
|
/*
|
|
|
|
|
Symbol(impl) - helpers for working with literal string IDs
|
2010-12-17 23:28:49 +01:00
|
|
|
|
2009-09-24 19:38:11 +02:00
|
|
|
Copyright (C) Lumiera.org
|
|
|
|
|
2009, Hermann Vosseler <Ichthyostega@web.de>
|
2010-12-17 23:28:49 +01:00
|
|
|
|
2009-09-24 19:38:11 +02:00
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the GNU General Public License as
|
2010-12-17 23:28:49 +01:00
|
|
|
published by the Free Software Foundation; either version 2 of
|
|
|
|
|
the License, or (at your option) any later version.
|
|
|
|
|
|
2009-09-24 19:38:11 +02:00
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
2010-12-17 23:28:49 +01:00
|
|
|
|
2009-09-24 19:38:11 +02:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
2010-12-17 23:28:49 +01:00
|
|
|
|
2009-09-24 19:38:11 +02:00
|
|
|
* *****************************************************/
|
|
|
|
|
|
|
|
|
|
/** @file symbol-impl.hpp
|
2009-09-24 23:02:40 +02:00
|
|
|
** Collection of helpers for working with the lib::Symbol.
|
2009-09-24 19:38:11 +02:00
|
|
|
**
|
|
|
|
|
** @todo currently as of 9/09 this is more of a placeholder.
|
|
|
|
|
** And maybe a location for collecting small bits of implementation,
|
|
|
|
|
** which could be usable later for real Symbol and Literal datatypes.
|
|
|
|
|
**
|
2009-09-24 23:02:40 +02:00
|
|
|
** lib::Symbol
|
2009-09-24 19:38:11 +02:00
|
|
|
** control::CommandRegistry for usage example of the hash function.
|
|
|
|
|
**
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-09-24 23:02:40 +02:00
|
|
|
#include "lib/symbol.hpp"
|
2017-04-08 02:27:51 +02:00
|
|
|
#include "lib/symbol-table.hpp"
|
2012-12-11 04:07:06 +01:00
|
|
|
#include "include/limits.h"
|
2009-09-24 23:02:40 +02:00
|
|
|
extern "C" {
|
2009-09-24 19:38:11 +02:00
|
|
|
#include "lib/safeclib.h"
|
2009-09-24 23:02:40 +02:00
|
|
|
}
|
2009-09-24 19:38:11 +02:00
|
|
|
|
|
|
|
|
#include <boost/functional/hash.hpp>
|
|
|
|
|
#include <cstddef>
|
2017-03-31 23:44:24 +02:00
|
|
|
#include <string>
|
2009-09-24 19:38:11 +02:00
|
|
|
|
|
|
|
|
using std::size_t;
|
2017-03-31 23:44:24 +02:00
|
|
|
using std::string;
|
2017-04-07 06:34:41 +02:00
|
|
|
using std::forward;
|
2009-09-24 19:38:11 +02:00
|
|
|
using boost::hash_combine;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-09-24 23:02:40 +02:00
|
|
|
namespace lib {
|
2009-09-24 19:38:11 +02:00
|
|
|
|
2012-12-11 04:07:06 +01:00
|
|
|
const size_t STRING_MAX_RELEVANT = LUMIERA_IDSTRING_MAX_RELEVANT;
|
2011-10-15 23:41:31 +02:00
|
|
|
|
|
|
|
|
|
2017-04-08 02:27:51 +02:00
|
|
|
namespace { // global symbol table
|
2017-04-07 19:25:21 +02:00
|
|
|
|
2017-05-02 01:37:17 +02:00
|
|
|
SymbolTable&
|
|
|
|
|
symbolTable()
|
|
|
|
|
{
|
|
|
|
|
static SymbolTable theSymbolTable;
|
|
|
|
|
return theSymbolTable; // Meyer's Singleton
|
|
|
|
|
}
|
2017-04-07 19:25:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* create Symbol by symbol table lookup.
|
2017-04-08 02:27:51 +02:00
|
|
|
* @note identical strings will be mapped to the same Symbol (embedded pointer)
|
|
|
|
|
* @warning potential lock contention, since each ctor call needs to do a lookup
|
2017-05-02 01:37:17 +02:00
|
|
|
* @remark since lumiera::LifecycleHook entries use a Symbol, the symbol table is
|
|
|
|
|
* implemented as Meyer's singleton and pulled up early, way before main()
|
2017-04-07 19:25:21 +02:00
|
|
|
*/
|
|
|
|
|
Symbol::Symbol (string&& definition)
|
2017-05-02 01:37:17 +02:00
|
|
|
: Literal{symbolTable().internedString (forward<string> (definition))}
|
2017-04-07 19:25:21 +02:00
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* == predefined marker Symbols == */
|
2017-10-02 23:06:23 +02:00
|
|
|
Symbol Symbol::ANY = "*";
|
2017-09-30 02:34:56 +02:00
|
|
|
Symbol Symbol::EMPTY = "";
|
2017-04-07 19:25:21 +02:00
|
|
|
Symbol Symbol::BOTTOM = "⟂";
|
|
|
|
|
Symbol Symbol::FAILURE = "↯";
|
|
|
|
|
|
|
|
|
|
// see also: lib/format-obj.cpp
|
|
|
|
|
// We can not share these definitions due to undefined static init order
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-04-07 18:04:49 +02:00
|
|
|
/** equality on Literal and Symbol values is defined
|
2009-09-24 19:38:11 +02:00
|
|
|
* based on the content, not the address. */
|
2009-09-24 23:02:40 +02:00
|
|
|
bool
|
2017-04-07 18:04:49 +02:00
|
|
|
Literal::operator== (const char* cString) const
|
2009-09-24 19:38:11 +02:00
|
|
|
{
|
2017-04-07 18:04:49 +02:00
|
|
|
return !lumiera_strncmp (this->str_, cString, STRING_MAX_RELEVANT);
|
2009-09-24 19:38:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-04-07 23:34:35 +02:00
|
|
|
/** generate hash value based on the Literal's contents.
|
|
|
|
|
* This function is intended to be picked up by ADL, and should be usable
|
|
|
|
|
* both with `std::hash` and `<boost/functional/hash.hpp>`. It is implemented
|
|
|
|
|
* similar as the boost::hash specialisation for std::string */
|
2015-06-29 02:31:22 +02:00
|
|
|
size_t
|
2017-04-07 23:34:35 +02:00
|
|
|
hash_value (Literal literal)
|
2009-09-24 19:38:11 +02:00
|
|
|
{
|
|
|
|
|
size_t hash=0;
|
2017-04-07 23:34:35 +02:00
|
|
|
if (literal)
|
2009-10-30 05:15:26 +01:00
|
|
|
{
|
2017-04-07 23:34:35 +02:00
|
|
|
const char *pos = literal;
|
2009-10-30 05:15:26 +01:00
|
|
|
size_t maxpos = STRING_MAX_RELEVANT;
|
|
|
|
|
for ( ; *pos && --maxpos; ++pos)
|
|
|
|
|
hash_combine(hash, *pos);
|
|
|
|
|
}
|
2009-09-24 19:38:11 +02:00
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-07 23:34:35 +02:00
|
|
|
/** hash value for Symbols is directly based on the symbol table entry */
|
|
|
|
|
size_t
|
|
|
|
|
hash_value (Symbol sym)
|
|
|
|
|
{
|
|
|
|
|
return sym? boost::hash_value (sym.c())
|
|
|
|
|
: 0;
|
|
|
|
|
}
|
|
|
|
|
|
2009-09-24 19:38:11 +02:00
|
|
|
|
|
|
|
|
|
2009-09-24 23:02:40 +02:00
|
|
|
} // namespace lib
|