From cae16e6c034808199f12a37ec0bdb4822c988dca Mon Sep 17 00:00:00 2001 From: olpc user Date: Tue, 26 Nov 2019 12:25:58 -0800 Subject: switch data to use std::any and merge value struct with concept --- starts/meaning-vm/level-0/baseref.hpp | 6 +++--- starts/meaning-vm/level-0/common.hpp | 2 -- starts/meaning-vm/level-0/concept.hpp | 8 ++++++-- starts/meaning-vm/level-0/level-0.hpp | 1 - starts/meaning-vm/level-0/memorystore.cpp | 5 +++-- starts/meaning-vm/level-0/memorystore.hpp | 7 +++---- starts/meaning-vm/level-0/value.hpp | 25 ------------------------- 7 files changed, 15 insertions(+), 39 deletions(-) delete mode 100644 starts/meaning-vm/level-0/value.hpp (limited to 'starts/meaning-vm/level-0') diff --git a/starts/meaning-vm/level-0/baseref.hpp b/starts/meaning-vm/level-0/baseref.hpp index 849e205..205b422 100644 --- a/starts/meaning-vm/level-0/baseref.hpp +++ b/starts/meaning-vm/level-0/baseref.hpp @@ -49,12 +49,12 @@ public: } template - T& vget(ref const & type) const { return p->vget(type.p)->data; } + T& vget(ref const & type) const { return p->vget(type.p); } template - void vset(ref const & type, T const & v) { p->set(type.p, valloc(v)); } + void vset(ref const & type, T const & v) { p->set(type.p, alloc(v)); } template - T& val() { return p->val()->data; } + T& val() { return p->val(); } operator concept*() const { return p; } concept*& ptr() { return p; } diff --git a/starts/meaning-vm/level-0/common.hpp b/starts/meaning-vm/level-0/common.hpp index 74031cc..6e86907 100644 --- a/starts/meaning-vm/level-0/common.hpp +++ b/starts/meaning-vm/level-0/common.hpp @@ -7,8 +7,6 @@ namespace level0 { struct concept; struct ref; -template struct value; -template struct vref; } namespace level1 { struct ref; template struct vref; } diff --git a/starts/meaning-vm/level-0/concept.hpp b/starts/meaning-vm/level-0/concept.hpp index 3bd1609..d57ccfc 100644 --- a/starts/meaning-vm/level-0/concept.hpp +++ b/starts/meaning-vm/level-0/concept.hpp @@ -2,6 +2,7 @@ #include "common.hpp" +#include #include #include @@ -12,6 +13,9 @@ struct concept { // a concept is made of concept-typed links to other concepts std::multimap links; + // and optional associated arbitrary data + std::any data; + using array = std::vector; concept* id(); @@ -30,10 +34,10 @@ struct concept void set(concept* type, concept* target); template - value* vget(concept* type) const { return static_cast*>(get(type)); } + T & vget(concept* type) const { return get(type)->val(); } template - value* val() { return this; } + T & val() { return std::any_cast(data); } }; } diff --git a/starts/meaning-vm/level-0/level-0.hpp b/starts/meaning-vm/level-0/level-0.hpp index 7062be0..56cd7dd 100644 --- a/starts/meaning-vm/level-0/level-0.hpp +++ b/starts/meaning-vm/level-0/level-0.hpp @@ -5,4 +5,3 @@ #include "errors.hpp" #include "memorystore.hpp" #include "ref.hpp" -#include "value.hpp" diff --git a/starts/meaning-vm/level-0/memorystore.cpp b/starts/meaning-vm/level-0/memorystore.cpp index b092ca0..adae8b2 100644 --- a/starts/meaning-vm/level-0/memorystore.cpp +++ b/starts/meaning-vm/level-0/memorystore.cpp @@ -14,8 +14,9 @@ static auto & concepts() return concepts; } -ref alloc(concept * moved) { - ref r = moved ? moved : new concept(); +ref alloc(std::any data) { + concept * r = new concept(); + r->data = data; concepts().insert(r); return r; } diff --git a/starts/meaning-vm/level-0/memorystore.hpp b/starts/meaning-vm/level-0/memorystore.hpp index fd752a0..7843513 100644 --- a/starts/meaning-vm/level-0/memorystore.hpp +++ b/starts/meaning-vm/level-0/memorystore.hpp @@ -1,14 +1,13 @@ #pragma once #include "common.hpp" -#include "value.hpp" + +#include namespace intellect { namespace level0 { -ref alloc(concept * moved = 0); -template -value * valloc(T const & v) { return static_cast*>(alloc(new value(v)).ptr()); } +ref alloc(std::any data = {}); void dealloc(ref); std::size_t allocated(); diff --git a/starts/meaning-vm/level-0/value.hpp b/starts/meaning-vm/level-0/value.hpp deleted file mode 100644 index ff704bf..0000000 --- a/starts/meaning-vm/level-0/value.hpp +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once - -#include "common.hpp" - -#include "concept.hpp" - -namespace intellect { -namespace level0 { - -template -struct value : public concept -{ - value(T const & val) : data(val) { } - - value(value const & val) = default; - - operator T&() { return data; } - operator T const &() const { return data; } - - T data; -}; - - -} -} -- cgit v1.2.3