From dbbed1e4e1d4b3f268c71236c89f1d673fa0c165 Mon Sep 17 00:00:00 2001 From: olpc user Date: Sun, 24 Nov 2019 16:15:08 -0800 Subject: add a linking mixin for use by ref classes --- starts/meaning-vm/level-0/level-0.hpp | 1 + starts/meaning-vm/level-0/ref-mixin.hpp | 33 +++++++++++++++++++++++++++++++++ starts/meaning-vm/level-0/ref.hpp | 3 ++- starts/meaning-vm/level0.cpp | 18 +++++++++--------- 4 files changed, 45 insertions(+), 10 deletions(-) create mode 100644 starts/meaning-vm/level-0/ref-mixin.hpp (limited to 'starts/meaning-vm') diff --git a/starts/meaning-vm/level-0/level-0.hpp b/starts/meaning-vm/level-0/level-0.hpp index b44b6e0..10df5b7 100644 --- a/starts/meaning-vm/level-0/level-0.hpp +++ b/starts/meaning-vm/level-0/level-0.hpp @@ -5,5 +5,6 @@ #include "errors.hpp" #include "memorystore.hpp" #include "ref.hpp" +#include "ref-mixin.hpp" #include "value.hpp" #include "vref.hpp" diff --git a/starts/meaning-vm/level-0/ref-mixin.hpp b/starts/meaning-vm/level-0/ref-mixin.hpp new file mode 100644 index 0000000..ba5fe09 --- /dev/null +++ b/starts/meaning-vm/level-0/ref-mixin.hpp @@ -0,0 +1,33 @@ +#pragma once + +#include + +namespace intellect { +namespace level0 { + +template typename vref> +struct refmixin { + using array = std::vector; + + void link(ref const & type, ref const & target) { ptr()->link(type, target); } + void unlink(ref const & type, ref const & target) { ptr()->unlink(type, target); } + void unlink(ref const & type) { ptr()->unlink(type); } + + bool linked(ref const & type) const { return ptr()->linked(type); } + bool linked(ref const & type, ref const & target) const { return ptr()->linked(type, target); } + + array getAll(ref const & type) const { return conv(ptr()->getAll(type)); } + + ref get(ref const & type) const { return conv(ptr()->get(type)); } + void set(ref const & type, ref const & target) { ptr()->set(type, target); } + + template + vref vget(ref const & type) const { return conv>(get(type)); } + +private: + inline concept * ptr() const { return conv(this)->ptr; } + template + static inline OUT conv(IN r) { return *(OUT*)&r; } +}; +} +} diff --git a/starts/meaning-vm/level-0/ref.hpp b/starts/meaning-vm/level-0/ref.hpp index 951676b..c1fa7fa 100644 --- a/starts/meaning-vm/level-0/ref.hpp +++ b/starts/meaning-vm/level-0/ref.hpp @@ -1,13 +1,14 @@ #pragma once #include "common.hpp" +#include "ref-mixin.hpp" #include namespace intellect { namespace level0 { -struct ref +struct ref : public refmixin { ref(concept *p); operator concept*() const { return ptr; } diff --git a/starts/meaning-vm/level0.cpp b/starts/meaning-vm/level0.cpp index 8cb60f9..a69da91 100644 --- a/starts/meaning-vm/level0.cpp +++ b/starts/meaning-vm/level0.cpp @@ -21,22 +21,22 @@ int main() ref skip = alloc(); - a->link(b, c); - a->link(d, e); - e->link(b, a); - c->link(b, e); - a->link(numlink, num); - a->link(codelink, code); + a.set(b, c); + a.link(d, e); + e.link(b, a); + c.link(b, e); + a.link(numlink, num); + a.link(codelink, code); std::cout << "Num: " << ref(num).dump(skip, skip); std::cout << "Code: " << ref(code).dump(skip, skip); std::cout << a.dump(skip, skip); - std::cout << "Num: " << a->vget(numlink).val() << std::endl; - std::cout << "Code: "; a->vget>(codelink).val()(); + std::cout << "Num: " << a.vget(numlink).val() << std::endl; + std::cout << "Code: "; a.vget>(codelink).val()(); std::cout << allocated() << " allocated" << std::endl; - e->unlink(b, a); + e.unlink(b, a); dealloc(a); dealloc(c); dealloc(e); -- cgit v1.2.3