aboutsummaryrefslogtreecommitdiffstats
path: root/src/diagram/Scope.cpp
blob: 335cd203ea5633f3f1e27a00e3a17acc5cdc68a3 (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
/*
 *  Created on: Nov 28, 2017
 *      Author: naopross
 */

#include "Scope.hpp"

namespace samb {

/* Scope::iterator */

Scope::iterator::iterator(Statement::pointer statement): m_current(statement) {}
Scope::iterator::~iterator() {}

Scope::iterator& Scope::iterator::operator++() {
	if (m_current->next() == nullptr) {
		// TODO: remote throw
		throw std::logic_error("Statement::iterator::operator++() m_current->next() is nullptr");
	}

	m_current = m_current->next();

	return *this;
}

Scope::iterator& Scope::iterator::operator++(int) {
	static Scope::iterator old(*this);

	old = *this;
	operator++();
	return old;
}

Statement& Scope::iterator::operator*() const {
	if (m_current == nullptr) {
		throw std::logic_error("Statement::iterator::operator*() m_current is nullptr");
	}

	return *m_current;
}

Statement::pointer Scope::iterator::operator->() const {
	return m_current;
}


/* Scope */

Scope::Scope(std::string label): Statement(Statement::Type::SCOPE, label, nullptr), m_head(nullptr), m_tail(nullptr) {}
Scope::Scope(std::string label, Statement::pointer first): Statement(Statement::Type::SCOPE, label, first), m_head(first), m_tail(first) {}

Scope::~Scope() {}

Scope::iterator Scope::insert_after(Scope::iterator it, Statement::pointer statement) {
	if (statement == nullptr) {
		throw std::invalid_argument("Statement::insert_after() cannot insert nullptr");
	}

	statement->next(it->next());
	it->next(statement);

	m_size++;

	return it;
}

Scope::iterator Scope::erase_after(Scope::iterator it) {
	if (it->next() == nullptr) {
		return end();
	}

	it->next(it->next()->next());

	return it;
}

} /* namespace samb */