summaryrefslogtreecommitdiff
path: root/starts/meaning-vm/helpers.hpp
diff options
context:
space:
mode:
authorolpc user <olpc@xo-5d-f7-86.localdomain>2019-11-21 06:17:34 -0500
committerolpc user <olpc@xo-5d-f7-86.localdomain>2019-11-21 06:17:34 -0500
commit302fc80f638783d17cd3b496284a535044241297 (patch)
treefa0031f7ac530365af6314d17f19ee84b2e83acb /starts/meaning-vm/helpers.hpp
parentd73fda8d05f6225c360f17a92563a9db36c8e922 (diff)
downloadstandingwithresilience-302fc80f638783d17cd3b496284a535044241297.tar.gz
standingwithresilience-302fc80f638783d17cd3b496284a535044241297.zip
simple c++ refs
Diffstat (limited to 'starts/meaning-vm/helpers.hpp')
-rw-r--r--starts/meaning-vm/helpers.hpp49
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;
+ }
+}