diff options
author | Ben Longbons <b.r.longbons@gmail.com> | 2013-06-11 21:55:13 -0700 |
---|---|---|
committer | Ben Longbons <b.r.longbons@gmail.com> | 2013-06-11 23:27:33 -0700 |
commit | 8b5370313dcc00a45ea5c3e8b4c497bc00fd8e13 (patch) | |
tree | 15e8a4841af992e17794f26fc7991ed40c35bd51 /src/common/intern-pool.hpp | |
parent | 8c6072df499ef9068346fbe8313b63dbba1e4e82 (diff) | |
download | tmwa-8b5370313dcc00a45ea5c3e8b4c497bc00fd8e13.tar.gz tmwa-8b5370313dcc00a45ea5c3e8b4c497bc00fd8e13.tar.bz2 tmwa-8b5370313dcc00a45ea5c3e8b4c497bc00fd8e13.tar.xz tmwa-8b5370313dcc00a45ea5c3e8b4c497bc00fd8e13.zip |
Allegedly remove all manual memory management
Diffstat (limited to 'src/common/intern-pool.hpp')
-rw-r--r-- | src/common/intern-pool.hpp | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/common/intern-pool.hpp b/src/common/intern-pool.hpp new file mode 100644 index 0000000..caa54e4 --- /dev/null +++ b/src/common/intern-pool.hpp @@ -0,0 +1,35 @@ +#ifndef INTERN_POOL_HPP +#define INTERN_POOL_HPP + +#include <cassert> + +#include <map> +#include <string> +#include <vector> + +class InternPool +{ + std::map<std::string, size_t> known; + std::vector<std::string> names; +public: + size_t intern(const std::string& name) + { + auto pair = known.insert({name, known.size()}); + if (pair.second) + names.push_back(name); + assert (known.size() == names.size()); + return pair.first->second; + } + + const std::string& outtern(size_t sz) const + { + return names[sz]; + } + + size_t size() const + { + return known.size(); + } +}; + +#endif //INTERN_POOL_HPP |