diff options
Diffstat (limited to '')
-rw-r--r-- | src/diagram/BranchStatement.cpp | 23 | ||||
-rw-r--r-- | src/diagram/BranchStatement.hpp | 32 | ||||
-rw-r--r-- | src/diagram/IteratorStatement.cpp | 25 | ||||
-rw-r--r-- | src/diagram/IteratorStatement.hpp | 32 | ||||
-rw-r--r-- | src/diagram/Scope.cpp | 77 | ||||
-rw-r--r-- | src/diagram/Scope.hpp | 59 | ||||
-rw-r--r-- | src/diagram/Statement.cpp | 17 | ||||
-rw-r--r-- | src/diagram/Statement.hpp | 56 | ||||
-rw-r--r-- | src/diagram/Structogram.cpp | 128 | ||||
-rw-r--r-- | src/diagram/Structogram.hpp | 70 | ||||
-rw-r--r-- | src/main.cpp | 20 | ||||
-rw-r--r-- | src/ui/AsciiRenderer.cpp | 100 | ||||
-rw-r--r-- | src/ui/AsciiRenderer.hpp | 37 | ||||
-rw-r--r-- | src/ui/Window.cpp | 20 | ||||
-rw-r--r-- | src/ui/Window.hpp | 9 |
15 files changed, 314 insertions, 391 deletions
diff --git a/src/diagram/BranchStatement.cpp b/src/diagram/BranchStatement.cpp new file mode 100644 index 0000000..d960ef9 --- /dev/null +++ b/src/diagram/BranchStatement.cpp @@ -0,0 +1,23 @@ +/* + * BranchStatement.cpp + * + * Created on: Nov 28, 2017 + * Author: naopross + */ + +#include "BranchStatement.hpp" + +namespace samb { + +BranchStatement::BranchStatement(Type t, const std::string& 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"); + } +} + +} /* namespace samb */ diff --git a/src/diagram/BranchStatement.hpp b/src/diagram/BranchStatement.hpp new file mode 100644 index 0000000..dacf060 --- /dev/null +++ b/src/diagram/BranchStatement.hpp @@ -0,0 +1,32 @@ +/* + * BranchStatement.hpp + * + * Created on: Nov 28, 2017 + * Author: naopross + */ + +#ifndef SRC_DIAGRAM_BRANCHSTATEMENT_HPP_ +#define SRC_DIAGRAM_BRANCHSTATEMENT_HPP_ + +#include "Statement.hpp" + +namespace samb { + +class BranchStatement: public Statement { +public: + BranchStatement(Type t, const std::string& condition, pointer next); + + const std::string& condition() const { return text(); } + void condition(const std::string& condition) { return text(condition); } + + std::vector<std::string> branches(); + std::size_t branchesCount(); + +private: + std::map<std::string, pointer> m_branches; + std::size_t m_branchesCount = 0; +}; + +} /* namespace samb */ + +#endif /* SRC_DIAGRAM_BRANCHSTATEMENT_HPP_ */ diff --git a/src/diagram/IteratorStatement.cpp b/src/diagram/IteratorStatement.cpp new file mode 100644 index 0000000..efb5236 --- /dev/null +++ b/src/diagram/IteratorStatement.cpp @@ -0,0 +1,25 @@ +/* + * IteratorStatement.cpp + * + * Created on: Nov 28, 2017 + * Author: naopross + */ + +#include "IteratorStatement.hpp" + +namespace samb { + +IteratorStatement::IteratorStatement(Statement::Type t, const std::string& condition, Statement::pointer next) + : Statement(t, condition, next), m_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"); + } +} + +} /* namespace samb */ diff --git a/src/diagram/IteratorStatement.hpp b/src/diagram/IteratorStatement.hpp new file mode 100644 index 0000000..82a40d9 --- /dev/null +++ b/src/diagram/IteratorStatement.hpp @@ -0,0 +1,32 @@ +/* + * IteratorStatement.hpp + * + * Created on: Nov 28, 2017 + * Author: naopross + */ + +#ifndef SRC_DIAGRAM_ITERATORSTATEMENT_HPP_ +#define SRC_DIAGRAM_ITERATORSTATEMENT_HPP_ + +#include "Statement.hpp" +#include "Scope.hpp" + +namespace samb { + +class IteratorStatement: public Statement { +public: + IteratorStatement(Type t, const std::string& condition, pointer next); + + /* accessors */ + const Scope& inner() const { return m_inner; } + + inline const std::string& condition() const { return text(); } + inline void condition(const std::string& condition) { return text(condition); } + +private: + Scope m_inner; +}; + +} /* namespace samb */ + +#endif /* SRC_DIAGRAM_ITERATORSTATEMENT_HPP_ */ diff --git a/src/diagram/Scope.cpp b/src/diagram/Scope.cpp new file mode 100644 index 0000000..335cd20 --- /dev/null +++ b/src/diagram/Scope.cpp @@ -0,0 +1,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 */ diff --git a/src/diagram/Scope.hpp b/src/diagram/Scope.hpp new file mode 100644 index 0000000..b1fe2f0 --- /dev/null +++ b/src/diagram/Scope.hpp @@ -0,0 +1,59 @@ +/* + * Created on: Nov 28, 2017 + * Author: naopross + */ + +#ifndef SRC_DIAGRAM_SCOPE_HPP_ +#define SRC_DIAGRAM_SCOPE_HPP_ + +#include "Statement.hpp" + +namespace samb { + +/* The Scope is a forward-iterable object that contains statements. + * A scope is also a valid type of statement. + * + * The Scope object is used inside other complex types of statements such as + * BranchStatement or a IteratorStatement, to hold the statements within their + * scope. + */ +class Scope : public Statement { +public: + class iterator { + public: + iterator(pointer statement); + ~iterator(); + + iterator& operator++(); + iterator& operator++(int); + + Statement& operator*() const; + Statement::pointer operator->() const; + + private: + Statement::pointer m_current; + }; + + Scope(std::string label); + Scope(std::string label, Statement::pointer first); + ~Scope(); + + iterator insert_after(iterator it, Statement::pointer statement); + iterator erase_after(iterator it); + + /* accessors */ + std::size_t size() const { return m_size; } + + /* iterator */ + iterator begin() { return iterator(m_head); } + iterator end() { return iterator(m_tail); } + +private: + Statement::pointer m_head; + Statement::pointer m_tail; + std::size_t m_size = 0; +}; + +} /* namespace samb */ + +#endif /* SRC_DIAGRAM_SCOPE_HPP_ */ diff --git a/src/diagram/Statement.cpp b/src/diagram/Statement.cpp index 60be094..d708dd9 100644 --- a/src/diagram/Statement.cpp +++ b/src/diagram/Statement.cpp @@ -9,17 +9,14 @@ namespace samb { -Statement::pointer Statement::makeStatement(Statement::Type t) { - return std::make_shared<Statement>(Statement(t, "", nullptr, nullptr)); -} - -bool Statement::operator==(const Statement& other) { - return (this->type == other.type) && (this->text == other.text) && - (this->next == other.next) && (this->scope == other.scope); -} +/* Statement */ +Statement::Statement(Type t, const std::string& text, Statement::pointer p): type(t), m_text(text), m_next(p) {} - -Statement::Statement(Type t, std::string txt, Statement::pointer p, Statement::pointer s): type(t), text(txt), next(p), scope(s) {} Statement::~Statement() {} +bool Statement::operator==(const Statement& other) const { + // comparison by pointers + return (this == &other); +} + } /* namespace samb */ diff --git a/src/diagram/Statement.hpp b/src/diagram/Statement.hpp index 1781a97..4eb3b4d 100644 --- a/src/diagram/Statement.hpp +++ b/src/diagram/Statement.hpp @@ -10,35 +10,28 @@ #include <string> #include <memory> +#include <vector> +#include <map> namespace samb { - /* Possible types of statement, according to the NS diagram paper * - * PROCESS, - * DECISION, - * SWITCH, - * WHILE, - * UNTIL, - * SCOPE, - * PARALLEL, + * PROCESS : a statement that does something + * DECISION : splits the program in 2 branches based on a condition + * SWITCH : splits the program in n branches depending on a value + * WHILE : repeat first loop + * UNTIL : repeat last loop + * SCOPE : simple scope to isolate variables + * PARALLEL : parallel operations */ -/* this struct is a link for linked list that stores the data in a tree-like - * structure, BUT it is not a tree because it allows 2 or more nodes to point - * at a single node - * - * Tree: Statements: - * A - B - C - D A - B - C - D - G - * \ \ / - * E - F E - F - * - * Because a statements can be branching elements. (if / switch) - * - * This class is also a *Factory* to make statements. +/* This struct is a statement (link) of the iterable object Scope + * (linked list), that is also a common interface for the various types of + * statements. */ -struct Statement { +class Statement { +public: using pointer = std::shared_ptr<Statement>; enum Type { @@ -56,20 +49,25 @@ struct Statement { * TODO: think of something more elegant to solve this */ END - } type; + }; - std::string text; + const Type type; - pointer next; - pointer scope; // TODO: make iterator aware of scope + Statement(Type type, const std::string& text, pointer next); + virtual ~Statement(); - static Statement::pointer makeStatement(Type t); + bool operator==(const Statement& other) const; - virtual ~Statement(); - bool operator==(const Statement& other); + /* accessors */ + void next(pointer next) { m_next = next; } + pointer next() const { return m_next; } + + void text(const std::string& text) { m_text = text; } + const std::string& text() const { return m_text; } private: - Statement(Type type, std::string txt, pointer next, pointer scope); + std::string m_text; + pointer m_next; }; } /* namespace samb */ diff --git a/src/diagram/Structogram.cpp b/src/diagram/Structogram.cpp deleted file mode 100644 index 0575748..0000000 --- a/src/diagram/Structogram.cpp +++ /dev/null @@ -1,128 +0,0 @@ -/* - * 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 */ diff --git a/src/diagram/Structogram.hpp b/src/diagram/Structogram.hpp index 7b036f0..05c2d4d 100644 --- a/src/diagram/Structogram.hpp +++ b/src/diagram/Structogram.hpp @@ -1,71 +1,17 @@ /* - * Structogram.h + * Structogram.hpp * - * Created on: Nov 14, 2017 + * Created on: Nov 28, 2017 * Author: naopross */ -#ifndef SRC_DIAGRAM_STRUCTOGRAM_H_ -#define SRC_DIAGRAM_STRUCTOGRAM_H_ +#ifndef SRC_DIAGRAM_STRUCTOGRAM_HPP_ +#define SRC_DIAGRAM_STRUCTOGRAM_HPP_ -#include <iostream> -#include <list> - -#include "Statement.hpp" +#include "Scope.hpp" namespace samb { + typedef Scope Structogram; +} -/* A Structogram is a Nassi-Schneiderman diagram, in this implementation it is - * simply and iterable object that holds statements. - * - * The first statement inside a structogram (m_head) is a SCOPE - * statement that holds the entire program inside it. - */ -class Structogram { -public: - /* forward only iterator */ - class iterator { - public: - iterator(Statement::pointer first); - ~iterator(); - - iterator& operator++(); - iterator& operator++(int); - - bool operator==(const iterator& other) const; - bool operator!=(const iterator& other) const; - Statement& operator*() const; - Statement::pointer operator->() const; - - private: - Statement::pointer m_current; - }; - - Structogram(std::string title); - virtual ~Structogram(); - - std::size_t size() const; - - // cannot be implemented because iter is forward only -// iterator insert(iterator it, Statement::pointer statement); -// iterator erase(iterator it); - - iterator insert_after(iterator it, Statement::pointer statement); - iterator erase_after(iterator it); - - /* iterator */ - iterator begin() const; - const iterator end() const; - const Statement& operator[](const Statement& it) = delete; - -private: - std::size_t m_size; - std::string m_title; - - Statement::pointer m_head; - Statement::pointer m_tail; -}; - -} /* namespace structograms */ - -#endif /* SRC_DIAGRAM_STRUCTOGRAM_H_ */ +#endif /* SRC_DIAGRAM_STRUCTOGRAM_HPP_ */ diff --git a/src/main.cpp b/src/main.cpp index 7f70de6..83bba4c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,30 +1,14 @@ #include "diagram/Structogram.hpp" -#include "diagram/Statement.hpp" -#include "ui/AsciiRenderer.hpp" #include <iostream> #include <memory> + using namespace samb; int main(int argc, char *argv[]) { - Structogram st("Demo"); - AsciiRenderer renderer(st, 60); - - Structogram::iterator it = st.begin(); - - auto first = Statement::makeStatement(Statement::Type::PROCESS); - first->text = "Hello World"; - - auto second = Statement::makeStatement(Statement::Type::PROCESS); - second->text = "Hello World Again!"; - - st.insert_after(it, first); - ++it; - - st.insert_after(it, second); - renderer.render(); + Structogram st("Example"); return 0; } diff --git a/src/ui/AsciiRenderer.cpp b/src/ui/AsciiRenderer.cpp deleted file mode 100644 index d06dadb..0000000 --- a/src/ui/AsciiRenderer.cpp +++ /dev/null @@ -1,100 +0,0 @@ -/* - * Renderer.cpp - * - * Created on: Nov 23, 2017 - * Author: naopross - */ - -#include "AsciiRenderer.hpp" - -#include <memory> - -namespace samb { - -AsciiRenderer::AsciiRenderer(Structogram& structogram, unsigned int width): - m_structogram(structogram), m_width(width) {} - -AsciiRenderer::~AsciiRenderer() {} - -void AsciiRenderer::drawPadding(unsigned int amt) { - while (amt--) { - std::cout << " "; - } -} - -void AsciiRenderer::drawLine() const { - for (unsigned int i = 0; i < m_width; i++) { - std::cout << "-"; - } - - std::cout << std::endl; -} - -void AsciiRenderer::drawText(std::string text, unsigned int width, std::string before, std::string after) const { - - if (width == 0) { - width = m_width; - } - - unsigned int textWidth = width - before.length() - after.length(); - - std::cout << before; - for (std::size_t i = 0; i < text.length(); i++) { - if (i % textWidth == 0 && i != 0) { - std::cout << after << "\n" << before; - } - - std::cout << text[i]; - } - - std::cout << std::endl; -} - -void AsciiRenderer::drawDecision(std::string condition, std::string trueText, std::string falseText, unsigned int width) const { - if (width == 0) { - width = m_width -2; - } - - std::cout << "|"; - -// drawPadding(width - condition.length()); -} - -void AsciiRenderer::render() { - - for (Structogram::iterator it = m_structogram.begin(); it != m_structogram.end(); ++it) { - - switch (it->type) { - case Statement::Type::PROCESS: - drawLine(); - drawText(it->text); - drawLine(); - break; - - case Statement::Type::DECISION: - break; - - case Statement::Type::SWITCH: - break; - - case Statement::Type::SCOPE: - std::cout << "Title: " << it->text << std::endl; - break; - - case Statement::Type::WHILE: - break; - - case Statement::Type::UNTIL: - break; - - case Statement::Type::PARALLEL: - break; - - case Statement::END: - // do nothing - break; - } - } -} - -} /* namespace samb */ diff --git a/src/ui/AsciiRenderer.hpp b/src/ui/AsciiRenderer.hpp deleted file mode 100644 index 76d6ff5..0000000 --- a/src/ui/AsciiRenderer.hpp +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Renderer.h - * - * Created on: Nov 23, 2017 - * Author: naopross - */ - -#ifndef SRC_UI_ASCIIRENDERER_HPP_ -#define SRC_UI_ASCIIRENDERER_HPP_ - -#include "../diagram/Structogram.hpp" - -namespace samb { - -/* Sample renderer for debug, - * - * TODO: implement PdfRenderer, UiRenderer with a common interface - */ -class AsciiRenderer { -private: - Structogram m_structogram; - const unsigned int m_width; - - void drawPadding(unsigned int amt); - void drawLine() const; - void drawText(std::string text, unsigned int width = 0, std::string before = "|", std::string after = "|") const; - void drawDecision(std::string condition, std::string trueText, std::string falseText, unsigned int width = 0) const; -public: - AsciiRenderer(Structogram& structogram, unsigned int width); - ~AsciiRenderer(); - - void render(); -}; - -} /* namespace samb */ - -#endif /* SRC_UI_ASCIIRENDERER_HPP_ */ diff --git a/src/ui/Window.cpp b/src/ui/Window.cpp index 4572766..e3d5b52 100644 --- a/src/ui/Window.cpp +++ b/src/ui/Window.cpp @@ -7,15 +7,29 @@ #include "Window.hpp" +#include <QVBoxLayout> +#include <QHBoxLayout> +#include <QApplication> + + namespace samb { -Window::Window() { - // TODO Auto-generated constructor stub +Window::Window(QWidget *parent): QWidget(parent) { + setWindowTitle("OrbitingYeti"); + + QVBoxLayout *vBox = new QVBoxLayout(this); + QHBoxLayout *hBox = new QHBoxLayout(); + m_quitBtn = new QPushButton("Quit", this); + connect(m_quitBtn, SIGNAL(clicked()), qApp, SLOT(quit())); + + hBox->addWidget(m_quitBtn, 0, Qt::AlignRight); + vBox->addStretch(1); + vBox->addLayout(hBox); } Window::~Window() { - // TODO Auto-generated destructor stub + } } /* namespace samb */ diff --git a/src/ui/Window.hpp b/src/ui/Window.hpp index 5afad8c..9b04ebb 100644 --- a/src/ui/Window.hpp +++ b/src/ui/Window.hpp @@ -9,17 +9,18 @@ #define SRC_UI_WINDOW_HPP_ #include <QWidget> +#include <QPushButton> namespace samb { class Window : public QWidget { Q_OBJECT public: - Window(); - virtual ~Window(); + explicit Window(QWidget *parent = 0); + ~Window(); -signals: -public slots: +private: + QPushButton *m_quitBtn; }; } /* namespace samb */ |