summaryrefslogtreecommitdiff
path: root/starts/meaning-vm/concept.hpp
diff options
context:
space:
mode:
authorolpc user <olpc@xo-5d-f7-86.localdomain>2019-11-21 13:37:18 -0500
committerolpc user <olpc@xo-5d-f7-86.localdomain>2019-11-21 13:37:18 -0500
commit6e81ffa9f5baabe8d4b16fa927ce423fbe26771e (patch)
tree3abbf34ccf1cb05d26f4a83a8dcf3455aebe1c90 /starts/meaning-vm/concept.hpp
parent302fc80f638783d17cd3b496284a535044241297 (diff)
downloadstandingwithresilience-6e81ffa9f5baabe8d4b16fa927ce423fbe26771e.tar.gz
standingwithresilience-6e81ffa9f5baabe8d4b16fa927ce423fbe26771e.zip
direct cast concept from string
Diffstat (limited to 'starts/meaning-vm/concept.hpp')
-rw-r--r--starts/meaning-vm/concept.hpp82
1 files changed, 27 insertions, 55 deletions
diff --git a/starts/meaning-vm/concept.hpp b/starts/meaning-vm/concept.hpp
index 981835f..ce76f5d 100644
--- a/starts/meaning-vm/concept.hpp
+++ b/starts/meaning-vm/concept.hpp
@@ -5,70 +5,42 @@
#include <string>
#include <vector>
-using cid = struct concept *;
+struct concept;
+template <typename T> struct value;
+
+struct ref
+{
+ ref(concept *p) : ptr(p) { }
+ operator concept*() const { return ptr; }
+
+ // helper names
+ ref(std::string);
+ operator std::string();
+ ref(char const * str) : ref(std::string(str)) { }
+
+ concept * ptr;
+};
struct concept
{
// a concept is made of concept-typed links to other concepts
- std::multimap<cid,cid> links;
- using array = std::vector<cid>;
-
- cid id();
- cid get(cid type); // returns first
- array getAll(cid type);
- void link(cid type, cid target);
- void unlink(cid type, cid target);
+ std::multimap<ref,ref,std::less<concept*>> links;
+ using array = std::vector<ref>;
+
+ ref id();
+ ref get(ref type); // returns first
+ array getAll(ref type);
+ void link(ref type, ref target);
+ void unlink(ref type, ref target);
};
template <typename T>
struct value : public concept, public T
{
- static value<T>& of(cid c)
+ value(T const & val) : T(val) {}
+ value(value<T> const & val) = default;
+ static value<T>& of(ref c)
{
- return *static_cast<value<T>*>(c);
+ return *static_cast<value<T>*>(c.ptr);
}
};
-
-cid concept::id()
-{
- return this;
-}
-
-cid concept::get(cid type)
-{
- auto result = links.equal_range(type);
- if (result.first == result.second) {
- throw std::out_of_range("no such concept link to get");
- }
- return result.first->second;
-}
-
-concept::array concept::getAll(cid type)
-{
- array ret;
- for (
- auto range = links.equal_range(type);
- range.first != range.second;
- ++ range.first
- ) {
- ret.push_back(range.first->second);
- }
- return ret;
-}
-
-void concept::link(cid type, cid target)
-{
- links.insert({type, target});
-}
-
-void concept::unlink(cid type, cid target)
-{
- auto ls = links.equal_range(type);
- for (auto l = ls.first; l != ls.second; ++ l) {
- if (l->second == target) {
- links.erase(l);
- return;
- }
- }
- throw std::out_of_range("no such concept link to erase");
-}