Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Variable.cpp « ObjectModel « src - github.com/Duet3D/RepRapFirmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 65e6b55fed97881c7dde2d0e881de4888b32763c (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
 * Variable.cpp
 *
 *  Created on: 6 Mar 2021
 *      Author: David
 */

#include "Variable.h"
#include <Platform/OutputMemory.h>

Variable::Variable(const char *str, ExpressionValue pVal, int8_t pScope) noexcept : name(str), val(pVal), scope(pScope)
{
}

Variable::~Variable()
{
	name.Delete();
	val.Release();
}

Variable* VariableSet::Lookup(const char *str) noexcept
{
	Variable *v;
	for (v = root; v != nullptr; v = v->next)
	{
		auto vname = v->name.Get();
		if (strcmp(vname.Ptr(), str) == 0)
		{
			break;
		}
	}
	return v;
}

const Variable* VariableSet::Lookup(const char *str) const noexcept
{
	const Variable *v;
	for (v = root; v != nullptr; v = v->next)
	{
		auto vname = v->name.Get();
		if (strcmp(vname.Ptr(), str) == 0)
		{
			break;
		}
	}
	return v;
}

void VariableSet::Insert(Variable *toInsert) noexcept
{
	toInsert->next = root;
	root = toInsert;
}

// Remove all variables with a scope greater than the parameter
void VariableSet::EndScope(uint8_t blockNesting) noexcept
{
	Variable *prev = nullptr;
	for (Variable *v = root; v != nullptr; )
	{
		if (v->scope > blockNesting)
		{
			Variable *temp = v;
			v = v->next;
			if (prev == nullptr)
			{
				root = v;
			}
			else
			{
				prev->next = v;
			}
			delete temp;
		}
		else
		{
			prev = v;
			v = v->next;
		}
	}
}

void VariableSet::Delete(const char *str) noexcept
{
	Variable *prev = nullptr;
	for (Variable *v = root; v != nullptr; v = v->next)
	{
		auto vname = v->name.Get();
		if (strcmp(vname.Ptr(), str) == 0)
		{
			if (prev == nullptr)
			{
				root = v->next;
			}
			else
			{
				prev->next = v->next;
			}
			delete v;
			break;
		}
		prev = v;
	}
}

void VariableSet::Clear() noexcept
{
	while (root != nullptr)
	{
		Variable *v = root;
		root = v->next;
		delete v;
	}
}

VariableSet::~VariableSet()
{
	Clear();
}

// Delete any existing variables and instead use the variables from the argument, taking ownership of them
void VariableSet::AssignFrom(VariableSet& other) noexcept
{
	Clear();
	root = other.root;
	other.root = nullptr;
}

void VariableSet::IterateWhile(stdext::inplace_function<bool(unsigned int, const Variable&) /*noexcept*/ > func) const noexcept
{
	unsigned int num = 0;
	for (const Variable *v = root; v != nullptr; v = v->GetNext())
	{
		if (!func(num, *v))
		{
			break;
		}
		++num;
	}
}

// End