summaryrefslogtreecommitdiff
path: root/starts/meaning-vm/concept.hpp
blob: a80b1573afdd003cfccaba1a2d78bfada635e0ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#pragma once

#include <map>
#include <stdexcept>
#include <string>
#include <vector>

struct concept;
template <typename T> struct value;

struct ref
{
	ref(concept *p) : ptr(p) { }
	concept* operator->() { return ptr; }

	// for use by containers
	bool operator<(ref const & other) const { return ptr < other.ptr; }

	// for helpers
	ref(std::string const &);
	ref(char const * str) : ref(std::string(str)) { }
	ref() : ref("nothing") { }
	value<std::string> & name() const;
	operator const char *() const;

	concept * ptr;
};

struct concept
{
	// a concept is made of concept-typed links to other concepts
	std::multimap<ref,ref> 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
{
	value(T const & val) : T(val) {}
	value(value<T> const & val) = default;
	static value<T>& of(ref c)
	{
		return *static_cast<value<T>*>(c.ptr);
	}
};