From 69168303d6f2af27eca400338169785b55db16a1 Mon Sep 17 00:00:00 2001 From: TinyOh Date: Tue, 22 Jan 2019 11:23:54 +0800 Subject: [PATCH] refactor(synthetic): use shared_ptr for parent_ --- src/mynteye/api/processor.cc | 12 ++++-- src/mynteye/api/processor.h | 42 ++++++++++++++++--- src/mynteye/api/processor/depth_processor.cc | 3 +- src/mynteye/api/processor/depth_processor.h | 3 +- .../api/processor/depth_processor_ocv.cc | 3 +- .../api/processor/depth_processor_ocv.h | 3 +- .../disparity_normalized_processor.cc | 3 +- .../disparity_normalized_processor.h | 3 +- .../api/processor/disparity_processor.cc | 4 +- .../api/processor/disparity_processor.h | 3 +- src/mynteye/api/processor/points_processor.cc | 3 +- src/mynteye/api/processor/points_processor.h | 3 +- .../api/processor/points_processor_ocv.cc | 3 +- .../api/processor/points_processor_ocv.h | 3 +- .../api/processor/rectify_processor.cc | 3 +- src/mynteye/api/processor/rectify_processor.h | 3 +- .../api/processor/rectify_processor_ocv.cc | 3 +- .../api/processor/rectify_processor_ocv.h | 3 +- src/mynteye/api/synthetic.cc | 15 ++++--- src/mynteye/api/synthetic.h | 15 ++++--- 20 files changed, 97 insertions(+), 36 deletions(-) diff --git a/src/mynteye/api/processor.cc b/src/mynteye/api/processor.cc index 2583b5a..8d65b35 100644 --- a/src/mynteye/api/processor.cc +++ b/src/mynteye/api/processor.cc @@ -52,7 +52,7 @@ std::string Processor::Name() { } void Processor::AddChild(const std::shared_ptr &child) { - child->parent_ = this; + child->parent_ = shared_from_this(); childs_.push_back(child); } @@ -81,7 +81,8 @@ void Processor::Activate(bool parents) { return; if (parents) { // Activate all parents - Processor *parent = parent_; + auto parent = parent_; + // Processor *parent = parent_; while (parent != nullptr) { parent->Activate(); parent = parent->parent_; @@ -97,7 +98,8 @@ void Processor::Deactivate(bool childs) { return; if (childs) { // Deactivate all childs - iterate_processors(GetChilds(), [](std::shared_ptr proc) { + iterate_processors_PtoC_after(GetChilds(), + [](std::shared_ptr proc) { proc->Deactivate(); }); } @@ -152,6 +154,10 @@ std::uint64_t Processor::GetDroppedCount() { return dropped_count_; } +std::shared_ptr Processor::GetParent() { + return parent_; +} + void Processor::Run() { VLOG(2) << Name() << " thread start"; diff --git a/src/mynteye/api/processor.h b/src/mynteye/api/processor.h index 11fa6f7..df5d42d 100644 --- a/src/mynteye/api/processor.h +++ b/src/mynteye/api/processor.h @@ -23,18 +23,22 @@ #include #include #include +#include +// #include "mynteye/api/synthetic.h" #include "mynteye/mynteye.h" #include "mynteye/api/object.h" MYNTEYE_BEGIN_NAMESPACE -class Processor /*: public std::enable_shared_from_this*/ { +class Processor : + public std::enable_shared_from_this { public: using PreProcessCallback = std::function; using PostProcessCallback = std::function; using ProcessCallback = std::function; + Object *const in, Object *const out, + std::shared_ptr const parent)>; explicit Processor(std::int32_t proc_period = 0); virtual ~Processor(); @@ -45,6 +49,7 @@ class Processor /*: public std::enable_shared_from_this*/ { void RemoveChild(const std::shared_ptr &child); + std::shared_ptr GetParent(); std::list> GetChilds(); void SetPreProcessCallback(PreProcessCallback callback); @@ -71,7 +76,8 @@ class Processor /*: public std::enable_shared_from_this*/ { protected: virtual Object *OnCreateOutput() = 0; virtual bool OnProcess( - Object *const in, Object *const out, Processor *const parent) = 0; + Object *const in, Object *const out, + std::shared_ptr const parent) = 0; private: /** Run in standalone thread. */ @@ -101,20 +107,44 @@ class Processor /*: public std::enable_shared_from_this*/ { PostProcessCallback post_callback_; ProcessCallback callback_; - Processor *parent_; + // Processor *parent_; + std::shared_ptr parent_; std::list> childs_; std::thread thread_; }; template -void iterate_processors( +void iterate_processors_PtoC_after( const T &processors, std::function)> fn) { for (auto &&proc : processors) { fn(proc); - iterate_processors(proc->GetChilds(), fn); + iterate_processors_PtoC_after(proc->GetChilds(), fn); } } +template +void iterate_processors_PtoC_before( + const T &processors, std::function)> fn) { + for (auto &&proc : processors) { + iterate_processors_PtoC_before(proc->GetChilds(), fn); + fn(proc); + } +} + +template +void iterate_processors_CtoP_before( + T processor, std::function)> fn) { + if (processor->GetParent() != nullptr) + iterate_processors_CtoP_before(processor->GetParent(), fn); + fn(processor); +} +template +void iterate_processors_CtoP_after( + T processor, std::function)> fn) { + fn(processor); + if (processor->GetParent() != nullptr) + iterate_processors_CtoP_after(processor->GetParent(), fn); +} MYNTEYE_END_NAMESPACE diff --git a/src/mynteye/api/processor/depth_processor.cc b/src/mynteye/api/processor/depth_processor.cc index 9441132..b1cc940 100644 --- a/src/mynteye/api/processor/depth_processor.cc +++ b/src/mynteye/api/processor/depth_processor.cc @@ -45,7 +45,8 @@ Object *DepthProcessor::OnCreateOutput() { } bool DepthProcessor::OnProcess( - Object *const in, Object *const out, Processor *const parent) { + Object *const in, Object *const out, + std::shared_ptr const parent) { MYNTEYE_UNUSED(parent) const ObjMat *input = Object::Cast(in); ObjMat *output = Object::Cast(out); diff --git a/src/mynteye/api/processor/depth_processor.h b/src/mynteye/api/processor/depth_processor.h index 6cfe591..69f2f6d 100644 --- a/src/mynteye/api/processor/depth_processor.h +++ b/src/mynteye/api/processor/depth_processor.h @@ -36,7 +36,8 @@ class DepthProcessor : public Processor { protected: Object *OnCreateOutput() override; bool OnProcess( - Object *const in, Object *const out, Processor *const parent) override; + Object *const in, Object *const out, + std::shared_ptr const parent) override; private: std::shared_ptr calib_infos_; }; diff --git a/src/mynteye/api/processor/depth_processor_ocv.cc b/src/mynteye/api/processor/depth_processor_ocv.cc index 8bdb6a9..0afb381 100644 --- a/src/mynteye/api/processor/depth_processor_ocv.cc +++ b/src/mynteye/api/processor/depth_processor_ocv.cc @@ -39,7 +39,8 @@ Object *DepthProcessorOCV::OnCreateOutput() { } bool DepthProcessorOCV::OnProcess( - Object *const in, Object *const out, Processor *const parent) { + Object *const in, Object *const out, + std::shared_ptr const parent) { MYNTEYE_UNUSED(parent) const ObjMat *input = Object::Cast(in); ObjMat *output = Object::Cast(out); diff --git a/src/mynteye/api/processor/depth_processor_ocv.h b/src/mynteye/api/processor/depth_processor_ocv.h index bd503a2..1618713 100644 --- a/src/mynteye/api/processor/depth_processor_ocv.h +++ b/src/mynteye/api/processor/depth_processor_ocv.h @@ -33,7 +33,8 @@ class DepthProcessorOCV : public Processor { protected: Object *OnCreateOutput() override; bool OnProcess( - Object *const in, Object *const out, Processor *const parent) override; + Object *const in, Object *const out, + std::shared_ptr const parent) override; }; MYNTEYE_END_NAMESPACE diff --git a/src/mynteye/api/processor/disparity_normalized_processor.cc b/src/mynteye/api/processor/disparity_normalized_processor.cc index 9a629e8..dfa79fc 100644 --- a/src/mynteye/api/processor/disparity_normalized_processor.cc +++ b/src/mynteye/api/processor/disparity_normalized_processor.cc @@ -43,7 +43,8 @@ Object *DisparityNormalizedProcessor::OnCreateOutput() { } bool DisparityNormalizedProcessor::OnProcess( - Object *const in, Object *const out, Processor *const parent) { + Object *const in, Object *const out, + std::shared_ptr const parent) { MYNTEYE_UNUSED(parent) const ObjMat *input = Object::Cast(in); ObjMat *output = Object::Cast(out); diff --git a/src/mynteye/api/processor/disparity_normalized_processor.h b/src/mynteye/api/processor/disparity_normalized_processor.h index 99d395b..1b61420 100644 --- a/src/mynteye/api/processor/disparity_normalized_processor.h +++ b/src/mynteye/api/processor/disparity_normalized_processor.h @@ -33,7 +33,8 @@ class DisparityNormalizedProcessor : public Processor { protected: Object *OnCreateOutput() override; bool OnProcess( - Object *const in, Object *const out, Processor *const parent) override; + Object *const in, Object *const out, + std::shared_ptr const parent) override; }; MYNTEYE_END_NAMESPACE diff --git a/src/mynteye/api/processor/disparity_processor.cc b/src/mynteye/api/processor/disparity_processor.cc index 70270f2..34a45d9 100644 --- a/src/mynteye/api/processor/disparity_processor.cc +++ b/src/mynteye/api/processor/disparity_processor.cc @@ -66,7 +66,6 @@ DisparityProcessor::DisparityProcessor(DisparityProcessorType type, #endif #ifdef WITH_BM_SOBEL_FILTER } else if (type_ == DisparityProcessorType::BM) { - int bmWinSize = 3; #ifdef WITH_OPENCV2 LOG(ERROR) << "not supported in opencv 2.x"; // int bmWinSize = 3; @@ -147,7 +146,8 @@ Object *DisparityProcessor::OnCreateOutput() { } bool DisparityProcessor::OnProcess( - Object *const in, Object *const out, Processor *const parent) { + Object *const in, Object *const out, + std::shared_ptr const parent) { MYNTEYE_UNUSED(parent) const ObjMat2 *input = Object::Cast(in); ObjMat *output = Object::Cast(out); diff --git a/src/mynteye/api/processor/disparity_processor.h b/src/mynteye/api/processor/disparity_processor.h index 026de30..0bf90f2 100644 --- a/src/mynteye/api/processor/disparity_processor.h +++ b/src/mynteye/api/processor/disparity_processor.h @@ -49,7 +49,8 @@ class DisparityProcessor : public Processor { protected: Object *OnCreateOutput() override; bool OnProcess( - Object *const in, Object *const out, Processor *const parent) override; + Object *const in, Object *const out, + std::shared_ptr const parent) override; private: cv::Ptr sgbm_matcher; diff --git a/src/mynteye/api/processor/points_processor.cc b/src/mynteye/api/processor/points_processor.cc index fd8f204..6903121 100644 --- a/src/mynteye/api/processor/points_processor.cc +++ b/src/mynteye/api/processor/points_processor.cc @@ -73,7 +73,8 @@ Object *PointsProcessor::OnCreateOutput() { } bool PointsProcessor::OnProcess( - Object *const in, Object *const out, Processor *const parent) { + Object *const in, Object *const out, + std::shared_ptr const parent) { MYNTEYE_UNUSED(parent) float fx = calib_infos_->left.K[0]; diff --git a/src/mynteye/api/processor/points_processor.h b/src/mynteye/api/processor/points_processor.h index a2dbcdb..5cdf958 100644 --- a/src/mynteye/api/processor/points_processor.h +++ b/src/mynteye/api/processor/points_processor.h @@ -38,7 +38,8 @@ class PointsProcessor : public Processor { protected: Object *OnCreateOutput() override; bool OnProcess( - Object *const in, Object *const out, Processor *const parent) override; + Object *const in, Object *const out, + std::shared_ptr const parent) override; private: std::shared_ptr calib_infos_; diff --git a/src/mynteye/api/processor/points_processor_ocv.cc b/src/mynteye/api/processor/points_processor_ocv.cc index d5aeda0..e0b4391 100644 --- a/src/mynteye/api/processor/points_processor_ocv.cc +++ b/src/mynteye/api/processor/points_processor_ocv.cc @@ -41,7 +41,8 @@ Object *PointsProcessorOCV::OnCreateOutput() { } bool PointsProcessorOCV::OnProcess( - Object *const in, Object *const out, Processor *const parent) { + Object *const in, Object *const out, + std::shared_ptr const parent) { MYNTEYE_UNUSED(parent) const ObjMat *input = Object::Cast(in); ObjMat *output = Object::Cast(out); diff --git a/src/mynteye/api/processor/points_processor_ocv.h b/src/mynteye/api/processor/points_processor_ocv.h index cba2ee9..a0d2c0a 100644 --- a/src/mynteye/api/processor/points_processor_ocv.h +++ b/src/mynteye/api/processor/points_processor_ocv.h @@ -35,7 +35,8 @@ class PointsProcessorOCV : public Processor { protected: Object *OnCreateOutput() override; bool OnProcess( - Object *const in, Object *const out, Processor *const parent) override; + Object *const in, Object *const out, + std::shared_ptr const parent) override; private: cv::Mat Q_; diff --git a/src/mynteye/api/processor/rectify_processor.cc b/src/mynteye/api/processor/rectify_processor.cc index a601e28..6648b8a 100644 --- a/src/mynteye/api/processor/rectify_processor.cc +++ b/src/mynteye/api/processor/rectify_processor.cc @@ -417,7 +417,8 @@ Object *RectifyProcessor::OnCreateOutput() { } bool RectifyProcessor::OnProcess( - Object *const in, Object *const out, Processor *const parent) { + Object *const in, Object *const out, + std::shared_ptr const parent) { MYNTEYE_UNUSED(parent) const ObjMat2 *input = Object::Cast(in); ObjMat2 *output = Object::Cast(out); diff --git a/src/mynteye/api/processor/rectify_processor.h b/src/mynteye/api/processor/rectify_processor.h index 7ebe2d8..029ed6d 100644 --- a/src/mynteye/api/processor/rectify_processor.h +++ b/src/mynteye/api/processor/rectify_processor.h @@ -77,7 +77,8 @@ class RectifyProcessor : public Processor { protected: Object *OnCreateOutput() override; bool OnProcess( - Object *const in, Object *const out, Processor *const parent) override; + Object *const in, Object *const out, + std::shared_ptr const parent) override; private: void InitParams(IntrinsicsEquidistant in_left, diff --git a/src/mynteye/api/processor/rectify_processor_ocv.cc b/src/mynteye/api/processor/rectify_processor_ocv.cc index 5decbb1..ca09be0 100644 --- a/src/mynteye/api/processor/rectify_processor_ocv.cc +++ b/src/mynteye/api/processor/rectify_processor_ocv.cc @@ -61,7 +61,8 @@ Object *RectifyProcessorOCV::OnCreateOutput() { } bool RectifyProcessorOCV::OnProcess( - Object *const in, Object *const out, Processor *const parent) { + Object *const in, Object *const out, + std::shared_ptr const parent) { MYNTEYE_UNUSED(parent) const ObjMat2 *input = Object::Cast(in); ObjMat2 *output = Object::Cast(out); diff --git a/src/mynteye/api/processor/rectify_processor_ocv.h b/src/mynteye/api/processor/rectify_processor_ocv.h index e67c4ae..9c450ec 100644 --- a/src/mynteye/api/processor/rectify_processor_ocv.h +++ b/src/mynteye/api/processor/rectify_processor_ocv.h @@ -51,7 +51,8 @@ class RectifyProcessorOCV : public Processor { protected: Object *OnCreateOutput() override; bool OnProcess( - Object *const in, Object *const out, Processor *const parent) override; + Object *const in, Object *const out, + std::shared_ptr const parent) override; private: void InitParams(IntrinsicsPinhole in_left, diff --git a/src/mynteye/api/synthetic.cc b/src/mynteye/api/synthetic.cc index 54963d3..f6365d4 100644 --- a/src/mynteye/api/synthetic.cc +++ b/src/mynteye/api/synthetic.cc @@ -809,7 +809,8 @@ void Synthetic::ProcessNativeStream( } bool Synthetic::OnRectifyProcess( - Object *const in, Object *const out, Processor *const parent) { + Object *const in, Object *const out, + std::shared_ptr const parent) { MYNTEYE_UNUSED(parent) if (plugin_ && plugin_->OnRectifyProcess(in, out)) { return true; @@ -819,7 +820,8 @@ bool Synthetic::OnRectifyProcess( } bool Synthetic::OnDisparityProcess( - Object *const in, Object *const out, Processor *const parent) { + Object *const in, Object *const out, + std::shared_ptr const parent) { MYNTEYE_UNUSED(parent) if (plugin_ && plugin_->OnDisparityProcess(in, out)) { return true; @@ -828,7 +830,8 @@ bool Synthetic::OnDisparityProcess( } bool Synthetic::OnDisparityNormalizedProcess( - Object *const in, Object *const out, Processor *const parent) { + Object *const in, Object *const out, + std::shared_ptr const parent) { MYNTEYE_UNUSED(parent) if (plugin_ && plugin_->OnDisparityNormalizedProcess(in, out)) { return true; @@ -837,7 +840,8 @@ bool Synthetic::OnDisparityNormalizedProcess( } bool Synthetic::OnPointsProcess( - Object *const in, Object *const out, Processor *const parent) { + Object *const in, Object *const out, + std::shared_ptr const parent) { MYNTEYE_UNUSED(parent) if (plugin_ && plugin_->OnPointsProcess(in, out)) { return true; @@ -846,7 +850,8 @@ bool Synthetic::OnPointsProcess( } bool Synthetic::OnDepthProcess( - Object *const in, Object *const out, Processor *const parent) { + Object *const in, Object *const out, + std::shared_ptr const parent) { MYNTEYE_UNUSED(parent) if (plugin_ && plugin_->OnDepthProcess(in, out)) { return true; diff --git a/src/mynteye/api/synthetic.h b/src/mynteye/api/synthetic.h index 3625a9f..976a378 100644 --- a/src/mynteye/api/synthetic.h +++ b/src/mynteye/api/synthetic.h @@ -88,15 +88,20 @@ class Synthetic { void ProcessNativeStream(const Stream &stream, const api::StreamData &data); bool OnRectifyProcess( - Object *const in, Object *const out, Processor *const parent); + Object *const in, Object *const out, + std::shared_ptr const parent); bool OnDisparityProcess( - Object *const in, Object *const out, Processor *const parent); + Object *const in, Object *const out, + std::shared_ptr const parent); bool OnDisparityNormalizedProcess( - Object *const in, Object *const out, Processor *const parent); + Object *const in, Object *const out, + std::shared_ptr const parent); bool OnPointsProcess( - Object *const in, Object *const out, Processor *const parent); + Object *const in, Object *const out, + std::shared_ptr const parent); bool OnDepthProcess( - Object *const in, Object *const out, Processor *const parent); + Object *const in, Object *const out, + std::shared_ptr const parent); void OnRectifyPostProcess(Object *const out); void OnDisparityPostProcess(Object *const out);