* Lumiera source code always was copyrighted by individual contributors * there is no entity "Lumiera.org" which holds any copyrights * Lumiera source code is provided under the GPL Version 2+ == Explanations == Lumiera as a whole is distributed under Copyleft, GNU General Public License Version 2 or above. For this to become legally effective, the ''File COPYING in the root directory is sufficient.'' The licensing header in each file is not strictly necessary, yet considered good practice; attaching a licence notice increases the likeliness that this information is retained in case someone extracts individual code files. However, it is not by the presence of some text, that legally binding licensing terms become effective; rather the fact matters that a given piece of code was provably copyrighted and published under a license. Even reformatting the code, renaming some variables or deleting parts of the code will not alter this legal situation, but rather creates a derivative work, which is likewise covered by the GPL! The most relevant information in the file header is the notice regarding the time of the first individual copyright claim. By virtue of this initial copyright, the first author is entitled to choose the terms of licensing. All further modifications are permitted and covered by the License. The specific wording or format of the copyright header is not legally relevant, as long as the intention to publish under the GPL remains clear. The extended wording was based on a recommendation by the FSF. It can be shortened, because the full terms of the license are provided alongside the distribution, in the file COPYING.
128 lines
3.3 KiB
C++
128 lines
3.3 KiB
C++
/*
|
||
Symbol(impl) - helpers for working with literal string IDs
|
||
|
||
Copyright (C)
|
||
2009, Hermann Vosseler <Ichthyostega@web.de>
|
||
|
||
**Lumiera** is free software; you can redistribute it and/or modify it
|
||
under the terms of the GNU General Public License as published by the
|
||
Free Software Foundation; either version 2 of the License, or (at your
|
||
option) any later version. See the file COPYING for further details.
|
||
|
||
* *****************************************************************/
|
||
|
||
/** @file symbol-impl.cpp
|
||
** Collection of helpers for working with the lib::Symbol.
|
||
**
|
||
** @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.
|
||
**
|
||
** lib::Symbol
|
||
** control::CommandRegistry for usage example of the hash function.
|
||
**
|
||
*/
|
||
|
||
|
||
|
||
#include "lib/symbol.hpp"
|
||
#include "lib/symbol-table.hpp"
|
||
|
||
#include <boost/functional/hash.hpp>
|
||
#include <cstddef>
|
||
#include <cstring>
|
||
#include <string>
|
||
|
||
using std::size_t;
|
||
using std::string;
|
||
using std::forward;
|
||
using boost::hash_combine;
|
||
|
||
|
||
|
||
|
||
namespace lib {
|
||
|
||
const size_t STRING_MAX_RELEVANT = LUMIERA_IDSTRING_MAX_RELEVANT;
|
||
|
||
|
||
namespace { // global symbol table
|
||
|
||
SymbolTable&
|
||
symbolTable()
|
||
{
|
||
static SymbolTable theSymbolTable;
|
||
return theSymbolTable; // Meyer's Singleton
|
||
}
|
||
|
||
inline int
|
||
strNcmp (CStr a, CStr b, size_t len)
|
||
{
|
||
return a == b ? 0 : std::strncmp (a?a:"", b?b:"", len);
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* create Symbol by symbol table lookup.
|
||
* @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
|
||
* @remark since lumiera::LifecycleHook entries use a Symbol, the symbol table is
|
||
* implemented as Meyer's singleton and pulled up early, way before main()
|
||
*/
|
||
Symbol::Symbol (string&& definition)
|
||
: Literal{symbolTable().internedString (forward<string> (definition))}
|
||
{ }
|
||
|
||
|
||
/* == predefined marker Symbols == */
|
||
Symbol Symbol::ANY = "*";
|
||
Symbol Symbol::EMPTY = "";
|
||
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
|
||
Literal::operator== (CStr charPtr) const
|
||
{
|
||
return 0 == strNcmp (this->str_, charPtr, STRING_MAX_RELEVANT);
|
||
}
|
||
|
||
|
||
/** 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 */
|
||
HashVal
|
||
hash_value (Literal literal)
|
||
{
|
||
size_t hash=0;
|
||
if (literal)
|
||
{
|
||
size_t cnt = 1;
|
||
const char *pos = literal;
|
||
for ( ; cnt <= STRING_MAX_RELEVANT and *pos ; ++cnt, ++pos )
|
||
hash_combine (hash, *pos);
|
||
}
|
||
|
||
return hash;
|
||
}
|
||
|
||
/** hash value for Symbols is directly based on the symbol table entry */
|
||
HashVal
|
||
hash_value (Symbol sym)
|
||
{
|
||
return sym? boost::hash_value (sym.c())
|
||
: 0;
|
||
}
|
||
|
||
|
||
|
||
} // namespace lib
|