From d3a67b7d29872c59acf328e69c270550bb2a621b Mon Sep 17 00:00:00 2001 From: John Zhao Date: Wed, 13 Jun 2018 18:00:31 +0800 Subject: [PATCH] Change output of processor to smart pointer --- src/api/processor/object.h | 7 +++++ src/api/processor/processor.cc | 4 +-- src/api/processor/processor.h | 2 +- src/api/synthetic.cc | 48 +++++++++++++++++----------------- 4 files changed, 34 insertions(+), 27 deletions(-) diff --git a/src/api/processor/object.h b/src/api/processor/object.h index f800ca1..8f43829 100644 --- a/src/api/processor/object.h +++ b/src/api/processor/object.h @@ -17,6 +17,8 @@ #include +#include + #include "mynteye/mynteye.h" MYNTEYE_BEGIN_NAMESPACE @@ -41,6 +43,11 @@ struct MYNTEYE_API Object { static const T *Cast(const Object *obj) { return dynamic_cast(obj); } + + template + static std::shared_ptr Cast(const std::shared_ptr &obj) { + return std::dynamic_pointer_cast(obj); + } }; /** diff --git a/src/api/processor/processor.cc b/src/api/processor/processor.cc index 7d88bc9..851b464 100644 --- a/src/api/processor/processor.cc +++ b/src/api/processor/processor.cc @@ -139,9 +139,9 @@ bool Processor::Process(const Object &in) { return true; } -Object *Processor::GetOutput() { +std::shared_ptr Processor::GetOutput() { std::lock_guard lk(mtx_result_); - return output_result_.get(); + return std::shared_ptr(std::move(output_result_)); } std::uint64_t Processor::GetDroppedCount() { diff --git a/src/api/processor/processor.h b/src/api/processor/processor.h index 505b38d..82b2681 100644 --- a/src/api/processor/processor.h +++ b/src/api/processor/processor.h @@ -65,7 +65,7 @@ class Processor /*: public std::enable_shared_from_this*/ { * Returns the last output. * @note Returns null if not output now. */ - Object *GetOutput(); + std::shared_ptr GetOutput(); std::uint64_t GetDroppedCount(); diff --git a/src/api/synthetic.cc b/src/api/synthetic.cc index d408be4..3c5a194 100644 --- a/src/api/synthetic.cc +++ b/src/api/synthetic.cc @@ -155,30 +155,30 @@ api::StreamData Synthetic::GetStreamData(const Stream &stream) { auto &&device = api_->device(); return data2api(device->GetLatestStreamData(stream)); } else if (mode == MODE_SYNTHETIC) { - switch (stream) { - case Stream::LEFT_RECTIFIED: { - auto &&processor = find_processor(processor_); - Object *out = processor->GetOutput(); - if (out != nullptr) { - ObjMat2 *output = Object::Cast(out); + if (stream == Stream::LEFT_RECTIFIED || stream == Stream::RIGHT_RECTIFIED) { + static std::shared_ptr output = nullptr; + auto &&processor = find_processor(processor_); + auto &&out = processor->GetOutput(); + if (out != nullptr) { + // Obtain the output, out will be nullptr if get again immediately. + output = Object::Cast(out); + } + if (output != nullptr) { + if (stream == Stream::LEFT_RECTIFIED) { return {nullptr, output->first, nullptr}; - } - VLOG(2) << "Rectify not ready now"; - } break; - case Stream::RIGHT_RECTIFIED: { - auto &&processor = find_processor(processor_); - Object *out = processor->GetOutput(); - if (out != nullptr) { - ObjMat2 *output = Object::Cast(out); + } else { return {nullptr, output->second, nullptr}; } - VLOG(2) << "Rectify not ready now"; - } break; + } + VLOG(2) << "Rectify not ready now"; + return {}; + } + switch (stream) { case Stream::DISPARITY: { auto &&processor = find_processor(processor_); - Object *out = processor->GetOutput(); + auto &&out = processor->GetOutput(); if (out != nullptr) { - ObjMat *output = Object::Cast(out); + auto &&output = Object::Cast(out); return {nullptr, output->value, nullptr}; } VLOG(2) << "Disparity not ready now"; @@ -186,27 +186,27 @@ api::StreamData Synthetic::GetStreamData(const Stream &stream) { case Stream::DISPARITY_NORMALIZED: { auto &&processor = find_processor(processor_); - Object *out = processor->GetOutput(); + auto &&out = processor->GetOutput(); if (out != nullptr) { - ObjMat *output = Object::Cast(out); + auto &&output = Object::Cast(out); return {nullptr, output->value, nullptr}; } VLOG(2) << "Disparity normalized not ready now"; } break; case Stream::POINTS: { auto &&processor = find_processor(processor_); - Object *out = processor->GetOutput(); + auto &&out = processor->GetOutput(); if (out != nullptr) { - ObjMat *output = Object::Cast(out); + auto &&output = Object::Cast(out); return {nullptr, output->value, nullptr}; } VLOG(2) << "Points not ready now"; } break; case Stream::DEPTH: { auto &&processor = find_processor(processor_); - Object *out = processor->GetOutput(); + auto &&out = processor->GetOutput(); if (out != nullptr) { - ObjMat *output = Object::Cast(out); + auto &&output = Object::Cast(out); return {nullptr, output->value, nullptr}; } VLOG(2) << "Depth not ready now";