LUMIERA.clone/src/lib/symbol-table.hpp
Ichthyostega dd45d6110d Library: Literal now default constructible to empty string
The class Literal is used as a thin wrapper to mark the fact that
some string parameter or value is assumed to be given *literally*

For the contract this indicates
- that storage is somewhere
- storage is not owned and managed by Literal
- yet storage guaranteed to exist during the whole lifetime of the program
- Literal can not be altered
- Literal is transparently convertible to const char *


Currently I am in the course of building some path abstraction, and for that
task it makes sense to hold an array of Literals (instead of pointers), just
because it expresses the intent way more clear. I do not see anything in the
above mentioned contract to prohibit a default constructed Literal, with the
empty string being the most obvious choice.

Note: there is the class Symbol, which derives from Literal. Symbol takes
arbitrary strings, but *interns* them into a static symbol table.
2017-09-30 17:45:38 +02:00

92 lines
3.3 KiB
C++

/*
SYMBOL-TABLE.hpp - registry for automatically interned symbol string tokens
Copyright (C) Lumiera.org
2017, Hermann Vosseler <Ichthyostega@web.de>
This program 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.
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.
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.
*/
/** @file symbol-table.hpp
** Registry table for automatically _interned strings_.
** The implementation of the lib::Symbol token relies on unique string pointers,
** such as to create one distinct identity for each distinct "symbol string". The idea
** is to generate unique and distinct numeric token IDs, while still holding onto a human readable
** string. Which in turn requires us to manage a registry of already known symbol strings; when a
** Symbol object with such an already known string is created, it will thus connect internally
** to the known token ID.
**
** @todo as of this writing (4/2017), it is neither clear if we really need such a facility, nor
** do we know enough to judge the implementation approach taken here. It might well turn out
** that a mere hash over the symbol string would be sufficient. Just the general vision for
** Lumiera indicates that we're going to rely heavily on some kind of symbolic or meta processing,
** involving a rules based query system, and in the light of such future developments, introducing
** a distinct lib::Symbol datatype seemed like a good idea. When we started to generate command IDs
** systematically, just literal c-string constants weren't sufficient anymore, leading to this
** very preliminary table based implementation.
**
** @see symbol-impl.cpp
** @see Symbol_test
** @see Symbol_HashtableTest
** @see SessionCommandFunction_test multithreaded access
*/
#ifndef LIB_SYMBOL_TABLE_H
#define LIB_SYMBOL_TABLE_H
#include "lib/sync.hpp"
#include "lib/symbol.hpp"
#include <boost/noncopyable.hpp>
#include <unordered_set>
#include <utility>
#include <string>
namespace lib {
using std::string;
using std::forward;
/**
* Table for automatically _interned strings_.
* This table is used to back the lib::Symbol token type,
* which is implemented by a pointer into this registration table
* for each new distinct "symbol string" created.
* @warning grows eternally, never shrinks
*/
class SymbolTable
: public Sync<>
, boost::noncopyable
{
std::unordered_set<string> table_;
public:
Literal
internedString (string && symbolString)
{
Lock sync(this);
auto res = table_.insert (forward<string> (symbolString));
return res.first->c_str();
}
};
} // namespace lib
#endif /*LIB_SYMBOL_TABLE_H*/