BPMN-OS
BPMN for optimization and simulation
Loading...
Searching...
No Matches
CollectionRegistry.cpp
Go to the documentation of this file.
2#include "Keywords.h"
3#include <strutil.h>
4#include <cassert>
5
6using namespace BPMNOS;
7
8Collection::Collection(const std::string& collection)
9 : collection(strutil::trim_copy(collection))
10{
11 // get values
12 if ( !strutil::starts_with(collection,"[") || !strutil::ends_with(collection,"]") ) {
13 throw std::runtime_error("Collection: string '" + collection + "' must start with '[' and must end with ']'");
14 }
15 // determine list of comma separated values
16 auto list = collection.substr(1,collection.size()-2);
17 if ( list.empty() ) return;
18
19 auto elements = strutil::split( list, "," );
20 for ( auto& element : elements ) {
21 strutil::trim(element);
22 if ( strutil::starts_with(element,"\"") && strutil::ends_with(element,"\"") ) {
23assert(!"Should not be reached");
24 values.push_back( BPMNOS::to_number( element.substr(1,element.size()-2), STRING ) );
25 }
26 else if ( element == Keyword::False ) {
27 values.push_back( BPMNOS::to_number( false , BOOLEAN) );
28 }
29 else if ( element == Keyword::True ) {
30 values.push_back( BPMNOS::to_number( true , BOOLEAN) );
31 }
32 else if ( element == Keyword::Undefined ) {
33 values.push_back( std::nullopt );
34 }
35 else {
36 try {
37 // try to convert to number
38 values.push_back( BPMNOS::stod(element) );
39 }
40 catch(...) {
41 throw std::runtime_error("Collection: illegal value '" + element + "' in '" + collection + "'");
42 }
43 }
44 }
45}
46
48 // register empty collection with index 0
49 (*this)("[]");
50}
51
53 return registeredCollections[i];
54}
55
56size_t CollectionRegistry::operator()(const std::string& collection) {
57 std::lock_guard<std::mutex> lock(registryMutex);
58
59 auto it = index.find(collection);
60 if ( it == index.end() ) {
61// Values values; // TODO: parse values
62 registeredCollections.emplace_back(collection);
63 index[collection] = index.size();
64 return registeredCollections.size()-1;
65 }
66 return it->second;
67}
68
70 registeredCollections.clear();
71 index.clear();
72 (*this)("[]");
73}
74
75// Create global registry
CollectionRegistry collectionRegistry
const std::string False
Definition Keywords.h:8
const std::string Undefined
Definition Keywords.h:10
const std::string True
Definition Keywords.h:9
number to_number(const std::string &valueString, const ValueType &type)
Converts a string to a number.
Definition Number.cpp:57
double stod(const std::string &str)
Definition Number.cpp:37
@ STRING
Definition Value.h:9
@ BOOLEAN
Definition Value.h:9
Utility class for representing collections by numeric values.
CollectionRegistry()
Constructor adds empty collection at index 0.
const Collection & operator[](size_t i) const
Operator providing access to a registered collections by index.
size_t operator()(const std::string &collection)
Operator to register a collection by its string representation and return its index.
Collection(const std::string &collection)