From b58ab0a8ca3bdca03da90b9185bf07ab4c1b14ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9a=20Saviot?= Date: Tue, 26 Jun 2018 15:11:04 +0200 Subject: [PATCH] LayoutCursor --- char_layout_node.h | 5 +---- cursor.h | 5 +++-- layout_cursor.cpp | 10 +++++----- layout_cursor.h | 26 ++++++++++++++------------ layout_reference.cpp | 10 ++++++++++ layout_reference.h | 7 ++++--- tree_reference.cpp | 11 +++++++++++ tree_reference.h | 2 +- 8 files changed, 49 insertions(+), 27 deletions(-) create mode 100644 layout_reference.cpp create mode 100644 tree_reference.cpp diff --git a/char_layout_node.h b/char_layout_node.h index fe3ff5bb8..d4ff030d2 100644 --- a/char_layout_node.h +++ b/char_layout_node.h @@ -5,8 +5,6 @@ #include "layout_node.h" #include "layout_cursor.h" -//#define TREE_LOGGING 1 - class CharLayoutNode : public LayoutNode { public: CharLayoutNode() : LayoutNode() {} @@ -38,12 +36,11 @@ public: } } -#if TREE_LOGGING const char * description() const override { static char Description[] = {'C', 'h', 'a', 'r', ' ', m_char, 0}; return Description; } -#endif + void setChar(char c) { m_char = c; } private: char m_char; diff --git a/cursor.h b/cursor.h index a66aeafa0..e5dab3537 100644 --- a/cursor.h +++ b/cursor.h @@ -1,14 +1,15 @@ #ifndef CURSOR_H #define CURSOR_H -#include "char_layout_node.h" #include "tree_node.h" #include "tree_reference.h" class Cursor { template friend class TreeReference; -private: +public: + bool isDefined() const { return m_treeReference.isDefined(); } +protected: Cursor(TreeNode * node) : m_treeReference(node) {} TreeReference m_treeReference; }; diff --git a/layout_cursor.cpp b/layout_cursor.cpp index c092a649d..73445d4b7 100644 --- a/layout_cursor.cpp +++ b/layout_cursor.cpp @@ -13,26 +13,26 @@ bool LayoutCursor::isEquivalentTo(LayoutCursor cursor) { /* Position */ int LayoutCursor::middleLeftPoint() { - int layoutOrigin = m_layoutPointer.absoluteOrigin(); + int layoutOrigin = layoutReference().absoluteOrigin(); return layoutOrigin; } /* Move */ void LayoutCursor::moveLeft(bool * shouldRecomputeLayout) { - m_layoutPointer.moveCursorLeft(this, shouldRecomputeLayout); + layoutReference().node()->moveCursorLeft(this, shouldRecomputeLayout); } void LayoutCursor::moveRight(bool * shouldRecomputeLayout) { - m_layoutPointer.moveCursorRight(this, shouldRecomputeLayout); + layoutReference().node()->moveCursorRight(this, shouldRecomputeLayout); } void LayoutCursor::moveAbove(bool * shouldRecomputeLayout) { - m_layoutPointer.moveCursorUp(this, shouldRecomputeLayout); + layoutReference().node()->moveCursorUp(this, shouldRecomputeLayout); } void LayoutCursor::moveUnder(bool * shouldRecomputeLayout) { - m_layoutPointer.moveCursorDown(this, shouldRecomputeLayout); + layoutReference().node()->moveCursorDown(this, shouldRecomputeLayout); } diff --git a/layout_cursor.h b/layout_cursor.h index a3711e2ae..549f5e63e 100644 --- a/layout_cursor.h +++ b/layout_cursor.h @@ -1,28 +1,23 @@ #ifndef LAYOUT_CURSOR_H #define LAYOUT_CURSOR_H -#include "tree_pool.h" -#include "layout_cursor_reference.h" +#include "cursor.h" +#include "layout_reference.h" #include "layout_node.h" #include -class LayoutCursor { +class LayoutCursor : public Cursor { + template + friend class LayoutReference; public: enum class Position { Left, Right }; - LayoutCursor(Layout * layoutPointer, Position position = Position::Right) : - m_layoutPointer(layoutPointer), - m_position(position) - { - } - - bool isDefined() const { return m_layoutPointer.isDefined(); } /* Debug */ void log() { - printf("Pointed Layout id %d, cursor position ", m_layoutPointer.identifier()); + printf("Pointed Layout id %d, cursor position ", m_treeReference.identifier()); if (m_position == Position::Left) { printf("Left"); } else { @@ -32,6 +27,9 @@ public: } /* Getters and setters */ + LayoutReference layoutReference() { + return LayoutReference(m_treeReference.node()); + } //int pointedLayoutIdentifier() const { return m_layoutIdentifier; } //void setPointedLayoutIdentifier(int layoutID) { m_layoutIdentifier = layoutID; } Position position() const { return m_position; } @@ -51,7 +49,11 @@ public: void moveAbove(bool * shouldRecomputeLayout); void moveUnder(bool * shouldRecomputeLayout); private: - LayoutPointer m_layoutPointer; + LayoutCursor(TreeNode * node, Position position = Position::Right) : + Cursor(node), + m_position(position) + { + } Position m_position; }; diff --git a/layout_reference.cpp b/layout_reference.cpp new file mode 100644 index 000000000..abfb824d5 --- /dev/null +++ b/layout_reference.cpp @@ -0,0 +1,10 @@ +#include "layout_reference.h" +#include "layout_cursor.h" +#include "char_layout_node.h" + +template +LayoutCursor LayoutReference::cursor() const { + return LayoutCursor(this->node()); +} + +template LayoutCursor LayoutReference::cursor() const; diff --git a/layout_reference.h b/layout_reference.h index dd27ba71b..c98dda91f 100644 --- a/layout_reference.h +++ b/layout_reference.h @@ -2,9 +2,10 @@ #define LAYOUT_REFERENCE_H #include "tree_reference.h" -//#include "layout_cursor.h" #include "layout_node.h" +class LayoutCursor; + template class LayoutReference : public TreeReference { friend class LayoutCursor; @@ -22,9 +23,9 @@ public: return (reinterpret_cast *>(this)); } - // LayoutReference cursorReference() const; + LayoutCursor cursor() const; - void addChild(LayoutReference l) { + virtual void addChild(LayoutReference l) { TreeReference::addChild(l); } diff --git a/tree_reference.cpp b/tree_reference.cpp new file mode 100644 index 000000000..d497a941d --- /dev/null +++ b/tree_reference.cpp @@ -0,0 +1,11 @@ +#include "tree_reference.h" +#include "char_layout_node.h" +#include "cursor.h" + +template +Cursor TreeReference::treeCursor() { + return Cursor(node()); +} + +template Cursor TreeReference::treeCursor(); +template Cursor TreeReference::treeCursor(); diff --git a/tree_reference.h b/tree_reference.h index 7ef1bb5b0..6301c00bd 100644 --- a/tree_reference.h +++ b/tree_reference.h @@ -41,7 +41,7 @@ public: int identifier() const { return m_identifier; } - Cursor cursor(); + Cursor treeCursor(); // Hierarchy int numberOfChildren() const {