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

Variable.h « GCodes « src - github.com/Duet3D/RepRapFirmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fa936e4e8100c69e467b2ddbf04edfa695da78b1 (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
/*
 * Variable.h
 *
 *  Created on: 6 Mar 2021
 *      Author: David
 */

#ifndef SRC_GCODES_VARIABLE_H_
#define SRC_GCODES_VARIABLE_H_

#include <RepRapFirmware.h>
#include <General/FreelistManager.h>
#include <Platform/Heap.h>
#include <ObjectModel/ObjectModel.h>

// Class to represent a variable having a name and a value
class Variable
{
public:
	friend class VariableSet;

	void* operator new(size_t sz) noexcept { return FreelistManager::Allocate<Variable>(); }
	void operator delete(void* p) noexcept { FreelistManager::Release<Variable>(p); }

	Variable(const char *str, ExpressionValue pVal, int8_t pScope) noexcept;
	~Variable();

	ExpressionValue GetValue() const noexcept { return val; }
	int8_t GetScope() const noexcept { return scope; }
	void Assign(ExpressionValue ev) noexcept { val = ev; }

private:
	Variable *next;
	StringHandle name;
	ExpressionValue val;
	int8_t scope;								// -1 for a parameter, else the block nesting level when it was created
};

// Class to represent a collection of variables.
// For now this is just a linked list, but it could be changed to a hash table for faster lookup and insertion.
class VariableSet
{
public:
	VariableSet() noexcept : root(nullptr) { }
	~VariableSet();

	Variable *Lookup(const char *str) noexcept;
	void Insert(Variable *toInsert) noexcept;
	void EndScope(uint8_t blockNesting) noexcept;
	void Clear() noexcept;

private:
	Variable *root;
};

#endif /* SRC_GCODES_VARIABLE_H_ */