From 7ea534dd1c8bf72200a999cae554d842d9035ba9 Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Mon, 1 Jan 2018 18:44:35 +0100 Subject: New StatementDialog, rename diagram classes Other changes: - Use raw pointer instead of smart pointers to manage resources - Initial Painter implementation --- src/diagram/branch.cpp | 16 +++++++++++++ src/diagram/branchstatement.cpp | 16 ------------- src/diagram/iterator.cpp | 18 +++++++++++++++ src/diagram/iteratorstatement.cpp | 18 --------------- src/diagram/scope.cpp | 47 +++++++++++++++++++++++++++++++++------ src/diagram/statement.cpp | 41 +++++++++++++++++++++++++++++++++- 6 files changed, 114 insertions(+), 42 deletions(-) create mode 100644 src/diagram/branch.cpp delete mode 100644 src/diagram/branchstatement.cpp create mode 100644 src/diagram/iterator.cpp delete mode 100644 src/diagram/iteratorstatement.cpp (limited to 'src/diagram') diff --git a/src/diagram/branch.cpp b/src/diagram/branch.cpp new file mode 100644 index 0000000..ccb17de --- /dev/null +++ b/src/diagram/branch.cpp @@ -0,0 +1,16 @@ +#include "diagram/branch.h" + +using namespace samb; + +Branch::Branch(Type t, const QString &condition, pointer next) : + Statement(t, condition, next) +{ + switch (t) { + case Statement::Type::DECISION: + case Statement::Type::SWITCH: + break; + + default: + throw std::invalid_argument("BranchStatement can only be of type DECISION or SWITCH"); + } +} diff --git a/src/diagram/branchstatement.cpp b/src/diagram/branchstatement.cpp deleted file mode 100644 index 6511c6c..0000000 --- a/src/diagram/branchstatement.cpp +++ /dev/null @@ -1,16 +0,0 @@ -#include "diagram/branchstatement.h" - -using namespace samb; - -BranchStatement::BranchStatement(Type t, const QString &condition, pointer next) : - Statement(t, condition, next) -{ - switch (t) { - case Statement::Type::DECISION: - case Statement::Type::SWITCH: - break; - - default: - throw std::invalid_argument("BranchStatement can only be of type DECISION or SWITCH"); - } -} diff --git a/src/diagram/iterator.cpp b/src/diagram/iterator.cpp new file mode 100644 index 0000000..43085b4 --- /dev/null +++ b/src/diagram/iterator.cpp @@ -0,0 +1,18 @@ +#include "diagram/iterator.h" + +using namespace samb; + +Iterator::Iterator(Statement::Type t, const QString &condition, Statement::pointer next) : + Statement(t, condition, next), + _inner("") +{ + + switch (t) { + case Statement::Type::WHILE: + case Statement::Type::UNTIL: + break; + + default: + throw std::invalid_argument("IteratorStatement can only be of type WHILE or UNTIL"); + } +} diff --git a/src/diagram/iteratorstatement.cpp b/src/diagram/iteratorstatement.cpp deleted file mode 100644 index a68234b..0000000 --- a/src/diagram/iteratorstatement.cpp +++ /dev/null @@ -1,18 +0,0 @@ -#include "diagram/iteratorstatement.h" - -using namespace samb; - -IteratorStatement::IteratorStatement(Statement::Type t, const QString &condition, Statement::pointer next) : - Statement(t, condition, next), - _inner("") -{ - - switch (t) { - case Statement::Type::WHILE: - case Statement::Type::UNTIL: - break; - - default: - throw std::invalid_argument("IteratorStatement can only be of type WHILE or UNTIL"); - } -} diff --git a/src/diagram/scope.cpp b/src/diagram/scope.cpp index b0f56cf..7f64a0d 100644 --- a/src/diagram/scope.cpp +++ b/src/diagram/scope.cpp @@ -1,4 +1,5 @@ #include "diagram/scope.h" +#include "debugtools.h" using namespace samb; @@ -7,6 +8,9 @@ using namespace samb; Scope::iterator::iterator(Statement::pointer statement) : _current(statement) { + if (_current == nullptr) { + debug_err("invalid iterator interating nullptr"); + } } @@ -15,15 +19,24 @@ Scope::iterator::~iterator() } +bool Scope::iterator::operator==(const iterator &other) const +{ + return *_current == *other; +} + +bool Scope::iterator::operator!=(const iterator &other) const +{ + return !(*_current == *other); +} + Scope::iterator& Scope::iterator::operator++() { - if (_current->next() == nullptr) { - // TODO: remove throw - throw std::logic_error("Statement::iterator::operator++() m_current->next() is nullptr"); + if (_current->next() != nullptr) { + _current = _current->next(); + } else { + // throw std::logic_error("Statement::iterator::operator++() m_current->next() is nullptr"); } - _current = _current->next(); - return *this; } @@ -39,7 +52,7 @@ Scope::iterator& Scope::iterator::operator++(int) Statement& Scope::iterator::operator*() const { if (_current == nullptr) - throw std::logic_error("Statement::iterator::operator*() m_current is nullptr"); + throw std::logic_error("Scope::iterator::operator*() m_current is nullptr"); return *_current; } @@ -56,7 +69,7 @@ Scope::Scope(const QString &label) : Statement(Statement::Type::SCOPE, label, nullptr), _head(nullptr), _tail(nullptr) { - + _head = _tail = new Statement(Statement::PROCESS, ""); } Scope::Scope(const QString &label, Statement::pointer first) : @@ -92,3 +105,23 @@ Scope::iterator Scope::erase_after(Scope::iterator it) return it; } + +Scope::iterator Scope::begin() +{ + return iterator(_head); +} + +const Scope::iterator Scope::begin() const +{ + return begin(); +} + +Scope::iterator Scope::end() +{ + return iterator(_tail); +} + +const Scope::iterator Scope::end() const +{ + return end(); +} diff --git a/src/diagram/statement.cpp b/src/diagram/statement.cpp index eae5cd3..7f1109b 100644 --- a/src/diagram/statement.cpp +++ b/src/diagram/statement.cpp @@ -1,9 +1,48 @@ #include "diagram/statement.h" +#include "diagram/branch.h" +#include "diagram/iterator.h" +#include "diagram/scope.h" + +#include "debugtools.h" + using namespace samb; /* Statement */ +template +static Statement::pointer make(Statement::Type t, Args&& ...args) +{ + Statement::pointer stat; + + switch (t) { + case Statement::Type::PROCESS: + stat = new Statement(t, args...); + break; + + case Statement::Type::DECISION: + case Statement::Type::PARALLEL: + case Statement::Type::SWITCH: + stat = new Branch(t, args...); + break; + + case Statement::Type::WHILE: + case Statement::Type::UNTIL: + stat = new Iterator(t, args...); + break; + + case Statement::Type::SCOPE: + stat = new Scope(t, args...); + break; + + default: + debug_err("invalid type"); + break; + } + + return stat; +} + Statement::Statement(Type t, const QString &text) : type(t), _text(text), _next(nullptr) { @@ -21,7 +60,7 @@ Statement::~Statement() } -bool Statement::operator==(const Statement& other) const { +bool Statement::operator==(const Statement &other) const { // comparison by pointers return (this == &other); } -- cgit v1.2.1