aboutsummaryrefslogtreecommitdiffstats
path: root/src/diagram/Structogram.cpp
blob: 0575748a7a7266126ca982dab76950b70d1b2f3f (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
/*
 * Structogram.cpp
 *
 *  Created on: Nov 14, 2017
 *      Author: naopross
 */

#include "../diagram/Structogram.hpp"
#include <memory>
#include <exception>


namespace samb {

/* iterator nested class */
Structogram::iterator::iterator(Statement::pointer first) {
	m_current = first;
}

Structogram::iterator::~iterator() {}

Structogram::iterator& Structogram::iterator::operator++() {
	if (m_current->next != nullptr) {
		m_current = m_current->next;
	}

	return *this;
}

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

	operator++();
	return old;
}

bool Structogram::iterator::operator==(const iterator& other) const {
	if (*this->m_current == *other.m_current) {
		return true;
	}

	return false;
}

bool Structogram::iterator::operator!=(const iterator& other) const {
	return !(other == *this);
}

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

	return *m_current;
}

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


/**
 *  Structogram class methods
 */
Structogram::Structogram(std::string title) {

	m_size = 0;
	m_title = title;

	m_head = Statement::makeStatement(Statement::Type::SCOPE);
	m_tail = Statement::makeStatement(Statement::Type::END);

	m_head->next = m_tail;
	m_head->text = m_title;
}

Structogram::~Structogram() {
	/* no need to destroy anything because of smart pointers */
}

std::size_t Structogram::size() const {
	return m_size;
}

Structogram::iterator Structogram::insert_after(Structogram::iterator it, Statement::pointer statement) {

	if (statement == nullptr) {
		throw std::invalid_argument("structogram: attempt to insert a null statement");
	}

	// TODO: update m_tail

	statement->next = it->next;
	it->next = statement;
	m_size++;

	return ++it;
}

Structogram::iterator Structogram::erase_after(iterator it) {

	// TODO: error handling IE return a std::pair<iterator, bool>

	/* the first statement (scope of the program) cannot be deleted */
	if ((*it) == *m_head) {
		return it;
	}

	if (it->next == nullptr) {
		return it;
	}

	it->next = it->next->next;

	return it;
}

/* iterator related methods */
Structogram::iterator Structogram::begin() const {
	return iterator(m_head);
}

const Structogram::iterator Structogram::end() const {
	return iterator(m_tail);
}

} /* namespace structograms */