From 158ed299c1c8d7182fa733f53ad7b07deb3a27e6 Mon Sep 17 00:00:00 2001 From: ozantonkal Date: Thu, 4 Jul 2013 15:57:49 +0300 Subject: [PATCH] reference counting in widget --- modules/viz/include/opencv2/viz/widgets.hpp | 8 ++++- modules/viz/src/widget.cpp | 34 ++++++++++++++++++--- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/modules/viz/include/opencv2/viz/widgets.hpp b/modules/viz/include/opencv2/viz/widgets.hpp index 5d1cfe847..9a0b15260 100644 --- a/modules/viz/include/opencv2/viz/widgets.hpp +++ b/modules/viz/include/opencv2/viz/widgets.hpp @@ -14,6 +14,8 @@ namespace temp_viz Widget(const Widget &other); Widget& operator =(const Widget &other); + ~Widget(); + void copyTo(Widget &dst); void setColor(const Color &color); @@ -23,7 +25,11 @@ namespace temp_viz private: class Impl; - cv::Ptr impl_; + Impl* impl_; + + void create(); + void release(); + friend struct WidgetAccessor; }; diff --git a/modules/viz/src/widget.cpp b/modules/viz/src/widget.cpp index cbc18d119..f61379af0 100644 --- a/modules/viz/src/widget.cpp +++ b/modules/viz/src/widget.cpp @@ -4,6 +4,7 @@ class temp_viz::Widget::Impl { public: vtkSmartPointer actor; + int ref_counter; Impl() : actor(vtkSmartPointer::New()) {} @@ -79,23 +80,34 @@ vtkSmartPointer temp_viz::WidgetAccessor::getActor(const Widget& wi /////////////////////////////////////////////////////////////////////////////////////////////// /// widget implementaion -temp_viz::Widget::Widget() +temp_viz::Widget::Widget() : impl_(0) { - impl_ = new Impl(); + create(); } -temp_viz::Widget::Widget(const Widget& other) +temp_viz::Widget::Widget(const Widget& other) : impl_(other.impl_) { - impl_ = other.impl_; + if (impl_) + CV_XADD(&impl_->ref_counter, 1); } temp_viz::Widget& temp_viz::Widget::operator =(const Widget &other) { if (this != &other) + { + release(); impl_ = other.impl_; + if (impl_) + CV_XADD(&impl_->ref_counter, 1); + } return *this; } +temp_viz::Widget::~Widget() +{ + release(); +} + void temp_viz::Widget::copyTo(Widget& /*dst*/) { // TODO Deep copy the data if there is any @@ -106,5 +118,19 @@ void temp_viz::Widget::setPose(const Affine3f& pose) { impl_->setPose(pose); } void temp_viz::Widget::updatePose(const Affine3f& pose) { impl_->updatePose(pose); } temp_viz::Affine3f temp_viz::Widget::getPose() const { return impl_->getPose(); } +void temp_viz::Widget::create() +{ + if (impl_) + release(); + impl_ = new Impl(); + impl_->ref_counter = 1; +} +void temp_viz::Widget::release() +{ + if (impl_ && CV_XADD(&impl_->ref_counter, -1) == 1) + { + delete impl_; + } +}