BPMN-OS
BPMN for optimization and simulation
Loading...
Searching...
No Matches
StringRegistry.cpp
Go to the documentation of this file.
1#include "StringRegistry.h"
2#include "Keywords.h"
3#include <cassert>
4
5using namespace BPMNOS;
6
8 // register false with index 0 and true with index 1
9 (*this)(Keyword::False);
10 (*this)(Keyword::True);
11}
12
13std::string StringRegistry::operator[](size_t i) const {
14 assert( i < registeredStrings.size() );
15 std::shared_lock read_lock(registryMutex);
16 return registeredStrings[i];
17}
18
19size_t StringRegistry::operator()(const std::string& string) {
20 std::shared_lock read_lock(registryMutex);
21 if ( auto it = index.find(string);
22 it != index.end()
23 ) {
24 return it->second;
25 }
26 read_lock.unlock();
27
28 std::unique_lock write_lock(registryMutex);
29 auto [it, inserted] = index.try_emplace(string, index.size());
30
31 if ( !inserted ) {
32 assert( index.size() == registeredStrings.size() );
33 return it->second;
34 }
35
36 registeredStrings.push_back(string);
37
38 assert( index.size() == registeredStrings.size() );
39 return registeredStrings.size()-1;
40}
41
42size_t StringRegistry::size() const {
43 std::shared_lock read_lock(registryMutex);
44 return registeredStrings.size();
45}
46
48 std::unique_lock write_lock(registryMutex);
49
50 index.clear();
51 registeredStrings.clear();
52
53 index.emplace(Keyword::False,0);
54 registeredStrings.push_back(Keyword::False);
55
56 index.emplace(Keyword::True,1);
57 registeredStrings.push_back(Keyword::True);
58
59 assert( index.size() == registeredStrings.size() );
60}
61
62// Create global registry
StringRegistry stringRegistry
const std::string False
Definition Keywords.h:8
const std::string True
Definition Keywords.h:9
Utility class for representing strings by numeric values.
StringRegistry()
Constructor adds "false" and "true" at indices 0 and 1.
size_t operator()(const std::string &string)
Operator to register a string and return its index.
std::string operator[](size_t i) const
Operator providing access to a registered string by index.