The Sparta Modeling Framework
Loading...
Searching...
No Matches
StringManager.hpp
1// <StringManager> -*- C++ -*-
2
3#pragma once
4
5#include <string>
6#include <map>
7#include <memory>
8#include <iostream>
9#include <iomanip>
10
13
14namespace sparta
15{
16 class StringManager;
17
23 {
24
30 class Deleter {
31
35 const bool should_free_;
36
37 public:
38
42 Deleter() = delete;
43
49 Deleter(bool should_free) : should_free_(should_free) {;}
50
54 Deleter(const Deleter&) = default;
55
59 Deleter& operator=(Deleter&) = delete;
60
66 void operator()(std::string* sp) const {
67 if(should_free_){
68 delete sp;
69 }
70 }
71 };
72
73
77 Deleter do_delete_;
78
85 Deleter dont_delete_;
86
87 public:
88
93
97 using StringUniquePtr = std::unique_ptr<std::string, Deleter>;
98 typedef std::map<std::string, StringUniquePtr> StringMap;
99
105 StringMap string_map_;
107
112 const uint64_t is_constructed;
113
117 static const uint64_t IS_CONSTRUCTED_CONST = 0x0123456789abcdef;
118
123 do_delete_(true),
124 dont_delete_(false),
128 { }
129
138 std::string* internString(const std::string& s) {
139 std::string* result = findString(s);
140 if(result){
141 return result;
142 }
143
144 // Unfortunately cannot point to key as it is not guaranteed
145 // stable. Duplicating the string string is minor waste.
146 max_string_len_ = std::max<decltype(max_string_len_)>(max_string_len_, s.size());
147 StringUniquePtr ns(new std::string(s), do_delete_);
148 string_map_.emplace(s, std::move(ns));
149 return string_map_.find(s)->second.get();
150 }
151
157 std::string* findString(const std::string& s) const {
158 auto itr = string_map_.find(s);
159 if(itr != string_map_.end()){
160 std::string* result = itr->second.get();
161 sparta_assert(result != nullptr);
162 return result;
163 }
164 return nullptr;
165 }
166
171 bool hasString(const std::string& s) const {
172 return nullptr != findString(s);
173 }
174
181 bool isInterned(const std::string* s) const {
182 for(auto & p : string_map_){
183 if(p.second.get() == s){
184 return true;
185 }
186 }
187
188 return false;
189 }
190
195 void dumpStrings(std::ostream& o, bool pretty=false) const {
196 (void) pretty;
197
198 std::ios::fmtflags f = o.flags();
199
200 for(auto& pair : string_map_){
201 o << "\"" << pair.first << "\" "
202 << std::setw((max_string_len_+4)-pair.first.size())
203 << " @ " << std::setw(10) << pair.second.get() << "\n";
204 }
205
206 // restore ostream flags
207 o.flags(f);
208 }
209
213 uint32_t getNumStrings() const {
214 return string_map_.size();
215 }
216
218 const std::string* const EMPTY;
219
225 sparta_assert(_GBL_string_manager && _GBL_string_manager->is_constructed == IS_CONSTRUCTED_CONST,
226 "Attempted to access StringManager singleton before it was "
227 "statically constructed.")
228 return *_GBL_string_manager;
229 }
230
231 private:
232
238 static sparta::StringManager* _GBL_string_manager;
239 };
240
241} // namespace sparta
242
Set of macros for Sparta assertions. Caught by the framework.
#define sparta_assert(...)
Simple variadic assertion that will throw a sparta_exception if the condition fails.
Helpers for enforcing StaticInitialization order.
Static-initialization order controller.
Manages string internment for SPARTA. This allows strings to be compared by pointer once interned.
bool hasString(const std::string &s) const
Checks to see if a string is present without adding it.
static StringManager & getStringManager()
Returns the StringManager singleton.
StringManager()
Constructor.
uint32_t getNumStrings() const
Gets the number of strings in this StringManager.
uint32_t max_string_len_
Maximum string length held by the StringManager.
static const uint64_t IS_CONSTRUCTED_CONST
Value of is_constructed after construction.
const uint64_t is_constructed
Has this singleton been constructed yet. Yes if equal to IS_CONSTRUCTED_CONST.
std::unique_ptr< std::string, Deleter > StringUniquePtr
Mapping of strings to their addresses.
StringMap string_map_
Mapping of ID to string for getting the string associated with this ID. Allows lookup with a string f...
bool isInterned(const std::string *s) const
Determines if a string is interned in this string manager by its pointer.
const std::string *const EMPTY
Holds interned empty strings.
void dumpStrings(std::ostream &o, bool pretty=false) const
Write all strings interned in the string manager to an ostream.
std::string * internString(const std::string &s)
Stores a string in shared space within this manager unless it is already stored.
std::string * findString(const std::string &s) const
Find a string in this manager.
Macros for handling exponential backoff.