Change output of processor to smart pointer

This commit is contained in:
John Zhao 2018-06-13 18:00:31 +08:00
parent a119b36f72
commit d3a67b7d29
4 changed files with 34 additions and 27 deletions

View File

@ -17,6 +17,8 @@
#include <opencv2/core/core.hpp> #include <opencv2/core/core.hpp>
#include <memory>
#include "mynteye/mynteye.h" #include "mynteye/mynteye.h"
MYNTEYE_BEGIN_NAMESPACE MYNTEYE_BEGIN_NAMESPACE
@ -41,6 +43,11 @@ struct MYNTEYE_API Object {
static const T *Cast(const Object *obj) { static const T *Cast(const Object *obj) {
return dynamic_cast<const T *>(obj); return dynamic_cast<const T *>(obj);
} }
template <typename T>
static std::shared_ptr<T> Cast(const std::shared_ptr<Object> &obj) {
return std::dynamic_pointer_cast<T>(obj);
}
}; };
/** /**

View File

@ -139,9 +139,9 @@ bool Processor::Process(const Object &in) {
return true; return true;
} }
Object *Processor::GetOutput() { std::shared_ptr<Object> Processor::GetOutput() {
std::lock_guard<std::mutex> lk(mtx_result_); std::lock_guard<std::mutex> lk(mtx_result_);
return output_result_.get(); return std::shared_ptr<Object>(std::move(output_result_));
} }
std::uint64_t Processor::GetDroppedCount() { std::uint64_t Processor::GetDroppedCount() {

View File

@ -65,7 +65,7 @@ class Processor /*: public std::enable_shared_from_this<Processor>*/ {
* Returns the last output. * Returns the last output.
* @note Returns null if not output now. * @note Returns null if not output now.
*/ */
Object *GetOutput(); std::shared_ptr<Object> GetOutput();
std::uint64_t GetDroppedCount(); std::uint64_t GetDroppedCount();

View File

@ -155,30 +155,30 @@ api::StreamData Synthetic::GetStreamData(const Stream &stream) {
auto &&device = api_->device(); auto &&device = api_->device();
return data2api(device->GetLatestStreamData(stream)); return data2api(device->GetLatestStreamData(stream));
} else if (mode == MODE_SYNTHETIC) { } else if (mode == MODE_SYNTHETIC) {
switch (stream) { if (stream == Stream::LEFT_RECTIFIED || stream == Stream::RIGHT_RECTIFIED) {
case Stream::LEFT_RECTIFIED: { static std::shared_ptr<ObjMat2> output = nullptr;
auto &&processor = find_processor<RectifyProcessor>(processor_); auto &&processor = find_processor<RectifyProcessor>(processor_);
Object *out = processor->GetOutput(); auto &&out = processor->GetOutput();
if (out != nullptr) { if (out != nullptr) {
ObjMat2 *output = Object::Cast<ObjMat2>(out); // Obtain the output, out will be nullptr if get again immediately.
return {nullptr, output->first, nullptr}; output = Object::Cast<ObjMat2>(out);
} }
VLOG(2) << "Rectify not ready now"; if (output != nullptr) {
} break; if (stream == Stream::LEFT_RECTIFIED) {
case Stream::RIGHT_RECTIFIED: { return {nullptr, output->first, nullptr};
auto &&processor = find_processor<RectifyProcessor>(processor_); } else {
Object *out = processor->GetOutput();
if (out != nullptr) {
ObjMat2 *output = Object::Cast<ObjMat2>(out);
return {nullptr, output->second, nullptr}; return {nullptr, output->second, nullptr};
} }
}
VLOG(2) << "Rectify not ready now"; VLOG(2) << "Rectify not ready now";
} break; return {};
}
switch (stream) {
case Stream::DISPARITY: { case Stream::DISPARITY: {
auto &&processor = find_processor<DisparityProcessor>(processor_); auto &&processor = find_processor<DisparityProcessor>(processor_);
Object *out = processor->GetOutput(); auto &&out = processor->GetOutput();
if (out != nullptr) { if (out != nullptr) {
ObjMat *output = Object::Cast<ObjMat>(out); auto &&output = Object::Cast<ObjMat>(out);
return {nullptr, output->value, nullptr}; return {nullptr, output->value, nullptr};
} }
VLOG(2) << "Disparity not ready now"; VLOG(2) << "Disparity not ready now";
@ -186,27 +186,27 @@ api::StreamData Synthetic::GetStreamData(const Stream &stream) {
case Stream::DISPARITY_NORMALIZED: { case Stream::DISPARITY_NORMALIZED: {
auto &&processor = auto &&processor =
find_processor<DisparityNormalizedProcessor>(processor_); find_processor<DisparityNormalizedProcessor>(processor_);
Object *out = processor->GetOutput(); auto &&out = processor->GetOutput();
if (out != nullptr) { if (out != nullptr) {
ObjMat *output = Object::Cast<ObjMat>(out); auto &&output = Object::Cast<ObjMat>(out);
return {nullptr, output->value, nullptr}; return {nullptr, output->value, nullptr};
} }
VLOG(2) << "Disparity normalized not ready now"; VLOG(2) << "Disparity normalized not ready now";
} break; } break;
case Stream::POINTS: { case Stream::POINTS: {
auto &&processor = find_processor<PointsProcessor>(processor_); auto &&processor = find_processor<PointsProcessor>(processor_);
Object *out = processor->GetOutput(); auto &&out = processor->GetOutput();
if (out != nullptr) { if (out != nullptr) {
ObjMat *output = Object::Cast<ObjMat>(out); auto &&output = Object::Cast<ObjMat>(out);
return {nullptr, output->value, nullptr}; return {nullptr, output->value, nullptr};
} }
VLOG(2) << "Points not ready now"; VLOG(2) << "Points not ready now";
} break; } break;
case Stream::DEPTH: { case Stream::DEPTH: {
auto &&processor = find_processor<DepthProcessor>(processor_); auto &&processor = find_processor<DepthProcessor>(processor_);
Object *out = processor->GetOutput(); auto &&out = processor->GetOutput();
if (out != nullptr) { if (out != nullptr) {
ObjMat *output = Object::Cast<ObjMat>(out); auto &&output = Object::Cast<ObjMat>(out);
return {nullptr, output->value, nullptr}; return {nullptr, output->value, nullptr};
} }
VLOG(2) << "Depth not ready now"; VLOG(2) << "Depth not ready now";