summaryrefslogtreecommitdiff
path: root/starts/meaning-vm/concept.hpp
blob: c69ff10872a303572a776c0e7275063797a2ecfe (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#pragma once

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

template <typename T> struct vref;
struct concept;
template <typename T> struct value;

struct ref
{
	ref(concept *p) : ptr(p) { if (p == 0) throw std::logic_error("null reference"); }
	ref(ref const &) = default;
	concept* operator->() { return ptr; }
	bool operator==(ref const & that) const { return this->ptr == that.ptr; }
	bool operator!=(ref const & that) const { return this->ptr != that.ptr; }

	bool operator<(ref const &) const { throw std::logic_error("ref has redefined syntax sugar: do not use in containers"); }

	// for helpers, mostly names
	ref(std::string const &);
	ref(char const * str) : ref(std::string(str)) { }
	ref(bool b) : ref(b ? "true" : "false") { }
	ref() : ref("nothing") { }
	value<std::string> & name() const; // this is a reference so that its char pointer lasts
	operator const char *() const;

	// helper linking syntax sugar
	ref operator=(ref that);
	ref operator<<(ref target);
	ref operator[](ref links);

	bool isa(ref what) const;
	bool isan(ref what) const;

	concept * ptr;
};

template <typename T>
struct vref
{
	vref(value<T> *p) : ptr(p) { }
	value<T>* operator->() { return ptr; }
	operator T const &() const { return *ptr; }

	vref(T const & val);

	vref(ref const & that) : ptr(static_cast<value<T>*>(that.ptr)) { }
	operator ref() { return ptr; }

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

	value<T> * ptr;
};

struct concept
{
	// a concept is made of concept-typed links to other concepts
	std::multimap<concept*,concept*> links;
	using array = std::vector<ref>;

	ref id();
	bool linked(ref type);
	bool linked(ref type, ref target);
	ref get(ref type, bool quick = false); // returns first
	template <typename T>
	vref<T> vget(ref type, bool quick = false) { return get(type, quick); }
	array getAll(ref type);
	void link(ref type, ref target);
	void unlink(ref type, ref target);
	void unlink(ref type);
};

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);
	}
};