Commit 87ab283f by Aaron Leung

Refactored Node::clone().

parent 513dc66f
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
namespace Sass { namespace Sass {
Node_Impl* Node_Factory::alloc_Node_Impl(Node::Type type, string* file, size_t line) Node_Impl* Node_Factory::alloc_Node_Impl(Node::Type type, string file, size_t line)
{ {
Node_Impl* ip = new Node_Impl(); Node_Impl* ip = new Node_Impl();
ip->type = type; ip->type = type;
...@@ -12,28 +12,52 @@ namespace Sass { ...@@ -12,28 +12,52 @@ namespace Sass {
return ip; return ip;
} }
Node Node_Factory::operator()(Node::Type type, string* file, size_t line, const Token& t) // returns a deep-copy of its argument
Node_Impl* Node_Factory::alloc_Node_Impl(Node_Impl* ip)
{
Node_Impl* ip_cpy = new Node_Impl(*ip);
if (ip_cpy->has_children) {
for (size_t i = 0, S = ip_cpy->size(); i < S; ++i) {
Node n(ip_cpy->at(i));
n.ip_ = alloc_Node_Impl(n.ip_);
}
}
return ip_cpy;
}
// for cloning nodes
Node Node_Factory::operator()(const Node& n1)
{
Node_Impl* ip_cpy = alloc_Node_Impl(n1.ip_); // deep-copy the implementation object
return Node(ip_cpy);
}
// for making leaf nodes out of terminals/tokens
Node Node_Factory::operator()(Node::Type type, string file, size_t line, const Token& t)
{ {
Node_Impl* ip = alloc_Node_Impl(type, file, line); Node_Impl* ip = alloc_Node_Impl(type, file, line);
ip->value.token = t; ip->value.token = t;
return Node(ip); return Node(ip);
} }
Node Node_Factory::operator()(Node::Type type, string* file, size_t line, size_t size) // for making interior nodes that have children
Node Node_Factory::operator()(Node::Type type, string file, size_t line, size_t size)
{ {
Node_Impl* ip = alloc_Node_Impl(type, file, line); Node_Impl* ip = alloc_Node_Impl(type, file, line);
ip->children.reserve(size); ip->children.reserve(size);
return Node(ip); return Node(ip);
} }
Node Node_Factory::operator()(string* file, size_t line, double v) // for making nodes representing numbers
Node Node_Factory::operator()(string file, size_t line, double v)
{ {
Node_Impl* ip = alloc_Node_Impl(Node::number, file, line); Node_Impl* ip = alloc_Node_Impl(Node::number, file, line);
ip->value.numeric = v; ip->value.numeric = v;
return Node(ip); return Node(ip);
} }
Node Node_Factory::operator()(string* file, size_t line, double v, const Token& t) // for making nodes representing numeric dimensions (e.g. 5px, 3em)
Node Node_Factory::operator()(string file, size_t line, double v, const Token& t)
{ {
Node_Impl* ip = alloc_Node_Impl(Node::numeric_dimension, file, line); Node_Impl* ip = alloc_Node_Impl(Node::numeric_dimension, file, line);
ip->value.dimension.numeric = v; ip->value.dimension.numeric = v;
...@@ -41,7 +65,8 @@ namespace Sass { ...@@ -41,7 +65,8 @@ namespace Sass {
return Node(ip); return Node(ip);
} }
Node Node_Factory::operator()(string* file, size_t line, double r, double g, double b, double a) // for making nodes representing rgba color quads
Node Node_Factory::operator()(string file, size_t line, double r, double g, double b, double a)
{ {
Node color((*this)(Node::numeric_color, file, line, 4)); Node color((*this)(Node::numeric_color, file, line, 4));
color << (*this)(file, line, r) color << (*this)(file, line, r)
...@@ -51,4 +76,7 @@ namespace Sass { ...@@ -51,4 +76,7 @@ namespace Sass {
return color; return color;
} }
void Node_Factory::free()
{ for (size_t i = 0, S = pool_.size(); i < S; ++i) delete pool_[i]; }
} }
\ No newline at end of file
...@@ -12,13 +12,24 @@ namespace Sass { ...@@ -12,13 +12,24 @@ namespace Sass {
class Node_Factory { class Node_Factory {
vector<Node_Impl*> pool_; vector<Node_Impl*> pool_;
Node_Impl* alloc_Node_Impl(Node::Type type, string* file, size_t line); Node_Impl* alloc_Node_Impl(Node::Type type, string file, size_t line);
// returns a deep-copy of its argument
Node_Impl* alloc_Node_Impl(Node_Impl* ip);
public: public:
Node operator()(Node::Type type, string* file, size_t line, const Token& t); // for cloning nodes
Node operator()(Node::Type type, string* file, size_t line, size_t size); Node operator()(const Node& n1);
Node operator()(string* file, size_t line, double v); // for making leaf nodes out of terminals/tokens
Node operator()(string* file, size_t line, double v, const Token& t); Node operator()(Node::Type type, string file, size_t line, const Token& t);
Node operator()(string* file, size_t line, double r, double g, double b, double a = 1.0); // for making interior nodes that have children
Node operator()(Node::Type type, string file, size_t line, size_t size);
// for making nodes representing numbers
Node operator()(string file, size_t line, double v);
// for making nodes representing numeric dimensions (e.g. 5px, 3em)
Node operator()(string file, size_t line, double v, const Token& t);
// for making nodes representing rgba color quads
Node operator()(string file, size_t line, double r, double g, double b, double a = 1.0);
void free();
}; };
} }
\ No newline at end of file
...@@ -17,15 +17,15 @@ int main() ...@@ -17,15 +17,15 @@ int main()
Node_Factory new_Node = Node_Factory(); Node_Factory new_Node = Node_Factory();
Node interior(new_Node(Node::block, 0, 0, 3)); Node interior(new_Node(Node::block, "", 0, 3));
cout << interior.size() << endl; cout << interior.size() << endl;
cout << interior.has_children() << endl; cout << interior.has_children() << endl;
cout << interior.should_eval() << endl << endl; cout << interior.should_eval() << endl << endl;
Node num(new_Node(0, 0, 255, 123, 32)); Node num(new_Node("", 0, 255, 123, 32));
Node num2(new_Node(0, 0, 255, 123, 32)); Node num2(new_Node("", 0, 255, 123, 32));
Node num3(new_Node(0, 0, 255, 122, 20, .75)); Node num3(new_Node("", 0, 255, 122, 20, .75));
cout << num.size() << endl; cout << num.size() << endl;
cout << num.has_children() << endl; cout << num.has_children() << endl;
...@@ -43,5 +43,14 @@ int main() ...@@ -43,5 +43,14 @@ int main()
cout << (num2[2] >= num3[2]) << endl; cout << (num2[2] >= num3[2]) << endl;
cout << (num2[3] != num3[3]) << endl << endl; cout << (num2[3] != num3[3]) << endl << endl;
Node num4(new_Node(num3));
cout << num3[3].numeric_value() << endl;
cout << num4[3].numeric_value() << endl;
num4[3] = new_Node("", 0, 0.4567);
cout << num3[3].numeric_value() << endl;
cout << num4[3].numeric_value() << endl;
new_Node.free();
return 0; return 0;
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment