TreeReferences do not deepcopy unless asked

This commit is contained in:
Léa Saviot
2018-06-26 17:59:30 +02:00
parent 2906d72aee
commit f67eb9c0a8
9 changed files with 44 additions and 30 deletions

View File

@@ -36,9 +36,9 @@ public:
*/
};
class Addition : public ExpressionReference<AdditionNode> {
class AdditionRef : public ExpressionReference<AdditionNode> {
public:
Addition(Expression e1, Expression e2) :
AdditionRef(ExpressionRef e1, ExpressionRef e2) :
ExpressionReference<AdditionNode>()
{
addOperand(e2);
@@ -46,7 +46,4 @@ public:
}
};
//typedef ExpressionReference<AdditionNode> Addition;
#endif

View File

@@ -47,9 +47,9 @@ private:
char m_char;
};
class CharLayout : public LayoutReference<CharLayoutNode> {
class CharLayoutRef : public LayoutReference<CharLayoutNode> {
public:
CharLayout(char c) : LayoutReference<CharLayoutNode>() {
CharLayoutRef(char c) : LayoutReference<CharLayoutNode>() {
this->node()->setChar(c);
}
};

View File

@@ -10,8 +10,12 @@ class Cursor {
public:
TreeReference<TreeNode> treeReference() { return m_treeReference; }
int treeReferenceIdentifier() { return m_treeReference.identifier(); }
void setTreeReference(TreeReference<TreeNode> t) { m_treeReference = t; }
void setTreeNode(TreeNode * t) { m_treeReference = TreeReference<TreeNode>(t); }
void setTreeReference(TreeReference<TreeNode> t) {
m_treeReference = t;
}
void setTreeNode(TreeNode * t) {
m_treeReference = TreeReference<TreeNode>(t);
}
bool isDefined() const { return m_treeReference.isDefined(); }
protected:
Cursor(TreeNode * node) : m_treeReference(node) {}

View File

@@ -44,6 +44,6 @@ public:
*/
};
typedef ExpressionReference<ExpressionNode> Expression;
typedef ExpressionReference<ExpressionNode> ExpressionRef;
#endif

View File

@@ -28,13 +28,11 @@ private:
float m_value;
};
class Float : public ExpressionReference<FloatNode> {
class FloatRef : public ExpressionReference<FloatNode> {
public:
Float(float f) : ExpressionReference<FloatNode>() {
FloatRef(float f) : ExpressionReference<FloatNode>() {
this->node()->setFloat(f);
}
};
//typedef ExpressionReference<FloatNode> Float;
#endif

View File

@@ -47,6 +47,6 @@ public:
}
};
typedef LayoutReference<LayoutNode> Layout;
typedef LayoutReference<LayoutNode> LayoutRef;
#endif

View File

@@ -5,10 +5,10 @@
#include "cursor.h"
#include <stdio.h>
Addition buildAddition() {
Float smallFloat(0.2f);
Float bigFloat(3.4f);
Addition a(smallFloat, bigFloat);
AdditionRef buildAddition() {
FloatRef smallFloat(0.2f);
FloatRef bigFloat(3.4f);
AdditionRef a(smallFloat, bigFloat);
TreePool::sharedPool()->log();
return a;
}
@@ -23,21 +23,22 @@ int main() {
TreePool::sharedPool()->log();*/
printf("\nCHAR LAYOUT\n");
CharLayout aChar('c');
CharLayout bChar('b');
CharLayoutRef aChar('c');
CharLayoutRef bChar('b');
TreePool::sharedPool()->log();
HorizontalLayout h(aChar, bChar);
HorizontalLayoutRef h(aChar, bChar);
TreePool::sharedPool()->log();
LayoutCursor cursor = h.childAtIndex(1).cursor();
LayoutCursor cursor2 = aChar.cursor();
cursor.log();
// LayoutCursor cursor2 = aChar.cursor();
/*cursor.log();
bool recompute = false;
cursor.moveLeft(&recompute);
cursor.log();
cursor.moveLeft(&recompute);
cursor.log();
cursor.log();*/
TreePool::sharedPool()->log();
/*cursor.log();
bool recompute = false;

View File

@@ -1,6 +1,7 @@
#include "tree_node.h"
#include "tree_pool.h"
#include "expression_node.h"
#include <stdio.h>
// Node operations

View File

@@ -18,9 +18,22 @@ class TreeReference {
friend class LayoutReference;
public:
TreeReference(const TreeReference & tr) {
int trNodeIdentifier = tr.identifier();
TreeNode * nodeCopy = TreePool::sharedPool()->deepCopy(TreePool::sharedPool()->node(trNodeIdentifier));
m_identifier = nodeCopy->identifier();
setTo(tr);
}
TreeReference& operator=(const TreeReference& tr) {
setTo(tr);
return *this;
}
void setTo(const TreeReference & tr) {
m_identifier = tr.identifier();
TreePool::sharedPool()->node(m_identifier)->retain();
}
TreeReference<T> clone() const {
TreeNode * nodeCopy = TreePool::sharedPool()->deepCopy(node());
return TreeReference<T>(nodeCopy);
}
~TreeReference() {
@@ -63,8 +76,8 @@ public:
// Hierarchy operations
void addChild(TreeReference<TreeNode> t) {
TreeNode * deepCopy = TreePool::sharedPool()->deepCopy(t.node());
TreePool::sharedPool()->move(deepCopy, node()->next());
t.node()->retain();
TreePool::sharedPool()->move(t.node(), node()->next());
}
void removeChild(TreeReference<TreeNode> t) {