diff options
author | olpc user <olpc@xo-5d-f7-86.localdomain> | 2019-11-21 06:17:34 -0500 |
---|---|---|
committer | olpc user <olpc@xo-5d-f7-86.localdomain> | 2019-11-21 06:17:34 -0500 |
commit | 302fc80f638783d17cd3b496284a535044241297 (patch) | |
tree | fa0031f7ac530365af6314d17f19ee84b2e83acb /starts/meaning-vm/helpers.hpp | |
parent | d73fda8d05f6225c360f17a92563a9db36c8e922 (diff) | |
download | standingwithresilience-302fc80f638783d17cd3b496284a535044241297.tar.gz standingwithresilience-302fc80f638783d17cd3b496284a535044241297.zip |
simple c++ refs
Diffstat (limited to 'starts/meaning-vm/helpers.hpp')
-rw-r--r-- | starts/meaning-vm/helpers.hpp | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/starts/meaning-vm/helpers.hpp b/starts/meaning-vm/helpers.hpp new file mode 100644 index 0000000..f21952a --- /dev/null +++ b/starts/meaning-vm/helpers.hpp @@ -0,0 +1,49 @@ +#include "concept.hpp" + +// Makes string values interchangeable with the concepts +// they name. +template<> +struct value<std::string> : public concept, public std::string +{ + //using std::string::basic_string; + value(std::string); + value(cid); + operator cid(); + static value& of(cid c) + { + return *static_cast<value*>(c); + } +}; + +using str = value<std::string>; + +cid operator"" _c(const char* str, std::size_t len) +{ + return value<std::string>({str,len}); +} + +#include <unordered_map> + +concept namesByConcept; +std::unordered_map<value<std::string>,concept,std::hash<std::string>> conceptsByName; + +value<std::string>::value(std::string s) +: std::string(s) +{ } + +value<std::string>::value(cid c) +: std::string(of(namesByConcept.get(c))) +{ } + +value<std::string>::operator cid() +{ + try { + return &conceptsByName.at(*this); + } catch (std::out_of_range) { + auto insertion = conceptsByName.emplace(*this, concept()); + cid con = &insertion.first->second; + cid nam = const_cast<cid>((const cid)&insertion.first->first); + namesByConcept.link(con, nam); + return con; + } +} |