summaryrefslogtreecommitdiff
path: root/starts/meaning-vm/level-1/baseref.hpp
blob: 5055d44ce28067ee7337ee33e95e3d5376dae07f (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
#pragma once

#include "common.hpp"
#include "funcs.hpp"

#include "../level-0/ref.hpp"

#include <functional>

namespace intellect {
namespace level1 {

template <typename ref>
struct baseref : public level0::baseref<ref>
{
	baseref(concept * p) : level0::baseref<ref>(p) { }
	baseref(level0::ref const & other) : baseref(other.ptr()) { }
	baseref(std::string const & name) : baseref(getnamed(name)) { }
	baseref(const char *name) : baseref(std::string(name)) { }
	baseref(bool b) : baseref(b ? "true" : "false") { }
	baseref() : baseref("nothing") { }

	bool isa(ref group) const { return level1::isa(self, group); }
	bool isan(ref group) const { return isa(group); }

	std::string const & name() const { return getname(self); }
	operator std::string const &() const { return getname(self); }
	explicit operator char const *() const { return getname(self)->data.c_str(); }

	ref operator-(ref other) const { return hyphenate(self, other); }
	ref operator[](ref subref) const { return self.get(subref); }

	template <typename T>
	void vset(ref const & type, T const & v) { self.set(type, level1::alloc(level0::concepts::allocations(), v)); }

	template <typename... Ref>
	std::function<ref(Ref...)> & fun() { return self.template val<std::function<ref(Ref...)>>(); }
	template <typename... Ref>
	void fun(std::function<ref(Ref...)> const & f) { self.val(f); }
	template <typename... Ref>
	void fun(std::function<void(Ref...)> const & f) { self.val(voidtoret(f)); }
	template <typename... Ref>
	void fget(ref const & type) { return self.template vget<std::function<ref(Ref...)>>(type); }
	template <typename... Ref>
	void fset(ref const & type, std::function<ref(Ref...)> f) { self.vset(type, f); }
	template <typename... Ref>
	void fset(ref const & type, std::function<void(Ref...)> f) { fset(type, voidtoret(f)); }

	template <typename... Ref>
	ref operator()(Ref... args) { return self.template fun<Ref...>()(args...); }

	std::string dump(ref skipmarkertype, ref skipmarkertarget) { return level1::dump(self, skipmarkertype, skipmarkertarget); };

private:
	template <typename... Refs>
	std::function<ref(Refs...)> voidtoret(std::function<void(Refs...)> f)
	{
		return [f](Refs... args) -> ref
		{
			std::initializer_list<ref const *>({&args...});
			f(args...);
			return concepts::nothing;
		};
	}
};

}
}