From 3a20c7dbf7efa851a373f8ab8d4659bb977d6961 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Sun, 22 Feb 2015 18:37:37 +0100 Subject: Start using shared_ptr inside Flowgraph --- src/Flowgraph.cpp | 51 ++++++++++++++++++++++++++++++++------------------- src/Flowgraph.h | 18 ++++++++++-------- 2 files changed, 42 insertions(+), 27 deletions(-) diff --git a/src/Flowgraph.cpp b/src/Flowgraph.cpp index dd9c68b..373533b 100644 --- a/src/Flowgraph.cpp +++ b/src/Flowgraph.cpp @@ -38,9 +38,12 @@ #include #endif +#include -typedef std::vector::iterator NodeIterator; -typedef std::vector::iterator EdgeIterator; +using namespace boost; + +typedef std::vector >::iterator NodeIterator; +typedef std::vector >::iterator EdgeIterator; Node::Node(ModPlugin* plugin) : @@ -64,7 +67,7 @@ Node::~Node() } -Edge::Edge(Node* srcNode, Node* dstNode) : +Edge::Edge(shared_ptr& srcNode, shared_ptr& dstNode) : mySrcNode(srcNode), myDstNode(dstNode) { @@ -73,7 +76,7 @@ Edge::Edge(Node* srcNode, Node* dstNode) : dstNode->plugin()->name(), dstNode, this); - myBuffer = new Buffer(); + myBuffer = shared_ptr(new Buffer()); srcNode->myOutputBuffers.push_back(myBuffer); dstNode->myInputBuffers.push_back(myBuffer); } @@ -83,7 +86,7 @@ Edge::~Edge() { PDEBUG("Edge::~Edge() @ %p\n", this); - std::vector::iterator buffer; + std::vector >::iterator buffer; if (myBuffer != NULL) { for (buffer = mySrcNode->myOutputBuffers.begin(); buffer != mySrcNode->myOutputBuffers.end(); @@ -102,7 +105,6 @@ Edge::~Edge() break; } } - delete myBuffer; } } @@ -112,7 +114,24 @@ int Node::process() PDEBUG("Edge::process()\n"); PDEBUG(" Plugin name: %s (%p)\n", myPlugin->name(), myPlugin); - return myPlugin->process(myInputBuffers, myOutputBuffers); + // the plugin process() still wants vector + // arguments. + std::vector inBuffers; + std::vector >::iterator buffer; + for (buffer = myInputBuffers.begin(); + buffer != myInputBuffers.end(); + ++buffer) { + inBuffers.push_back(buffer->get()); + } + + std::vector outBuffers; + for (buffer = myOutputBuffers.begin(); + buffer != myOutputBuffers.end(); + ++buffer) { + outBuffers.push_back(buffer->get()); + } + + return myPlugin->process(inBuffers, outBuffers); } @@ -128,15 +147,10 @@ Flowgraph::~Flowgraph() { PDEBUG("Flowgraph::~Flowgraph() @ %p\n", this); - std::vector::const_iterator edge; - for (edge = edges.begin(); edge != edges.end(); ++edge) { - delete *edge; - } - if (myProcessTime) { fprintf(stderr, "Process time:\n"); } - std::vector::const_iterator node; + std::vector >::const_iterator node; for (node = nodes.begin(); node != nodes.end(); ++node) { if (myProcessTime) { fprintf(stderr, " %30s: %10u us (%2.2f %%)\n", @@ -144,7 +158,6 @@ Flowgraph::~Flowgraph() (unsigned)(*node)->processTime(), (*node)->processTime() * 100.0 / myProcessTime); } - delete *node; } if (myProcessTime) { fprintf(stderr, " %30s: %10u us (100.00 %%)\n", "total", @@ -167,7 +180,7 @@ void Flowgraph::connect(ModPlugin* input, ModPlugin* output) } } if (inputNode == nodes.end()) { - inputNode = nodes.insert(nodes.end(), new Node(input)); + inputNode = nodes.insert(nodes.end(), shared_ptr(new Node(input))); } for (outputNode = nodes.begin(); outputNode != nodes.end(); ++outputNode) { @@ -176,14 +189,14 @@ void Flowgraph::connect(ModPlugin* input, ModPlugin* output) } } if (outputNode == nodes.end()) { - outputNode = nodes.insert(nodes.end(), new Node(output)); + outputNode = nodes.insert(nodes.end(), shared_ptr(new Node(output))); for (inputNode = nodes.begin(); inputNode != nodes.end(); ++inputNode) { if ((*inputNode)->plugin() == input) { break; } } } else if (inputNode > outputNode) { - Node* node = *outputNode; + shared_ptr node = *outputNode; nodes.erase(outputNode); outputNode = nodes.insert(nodes.end(), node); for (inputNode = nodes.begin(); inputNode != nodes.end(); ++inputNode) { @@ -196,7 +209,7 @@ void Flowgraph::connect(ModPlugin* input, ModPlugin* output) assert((*inputNode)->plugin() == input); assert((*outputNode)->plugin() == output); - edges.push_back(new Edge(*inputNode, *outputNode)); + edges.push_back(shared_ptr(new Edge(*inputNode, *outputNode))); } @@ -204,7 +217,7 @@ bool Flowgraph::run() { PDEBUG("Flowgraph::run()\n"); - std::vector::const_iterator node; + std::vector >::const_iterator node; timeval start, stop; time_t diff; diff --git a/src/Flowgraph.h b/src/Flowgraph.h index 178b6a9..00b8d42 100644 --- a/src/Flowgraph.h +++ b/src/Flowgraph.h @@ -32,6 +32,7 @@ #include #include +#include class Node @@ -44,8 +45,8 @@ public: ModPlugin* plugin() { return myPlugin; } - std::vector myInputBuffers; - std::vector myOutputBuffers; + std::vector > myInputBuffers; + std::vector > myOutputBuffers; int process(); time_t processTime() { return myProcessTime; } @@ -62,15 +63,15 @@ protected: class Edge { public: - Edge(Node* src, Node* dst); + Edge(boost::shared_ptr& src, boost::shared_ptr& dst); ~Edge(); Edge(const Edge&); Edge& operator=(const Edge&); protected: - Node* mySrcNode; - Node* myDstNode; - Buffer* myBuffer; + boost::shared_ptr mySrcNode; + boost::shared_ptr myDstNode; + boost::shared_ptr myBuffer; }; @@ -86,10 +87,11 @@ public: bool run(); protected: - std::vector nodes; - std::vector edges; + std::vector > nodes; + std::vector > edges; time_t myProcessTime; }; #endif // FLOWGRAPH_H + -- cgit v1.2.3