Skip to content

Commit

Permalink
Use auto and reset to avoid explicit shared_ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
taketwo committed Jun 10, 2019
1 parent 37d6b7d commit 26983a4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 100 deletions.
133 changes: 36 additions & 97 deletions io/src/openni2/openni2_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
#include <OpenNI.h>
#include <PS1080.h> // For XN_STREAM_PROPERTY_EMITTER_DCMOS_DISTANCE property

#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <boost/chrono.hpp>

Expand Down Expand Up @@ -62,7 +60,7 @@ pcl::io::openni2::OpenNI2Device::OpenNI2Device (const std::string& device_URI) :
if (status != openni::STATUS_OK)
THROW_IO_EXCEPTION ("Initialize failed\n%s\n", OpenNI::getExtendedError ());

openni_device_ = boost::make_shared<openni::Device>();
openni_device_.reset (new openni::Device);

if (device_URI.length () > 0)
status = openni_device_->open (device_URI.c_str ());
Expand Down Expand Up @@ -106,12 +104,12 @@ pcl::io::openni2::OpenNI2Device::OpenNI2Device (const std::string& device_URI) :
openni_device_->getPlaybackControl ()->setSpeed (1.0f);
}

device_info_ = boost::make_shared<openni::DeviceInfo>();
device_info_.reset (new openni::DeviceInfo);
*device_info_ = openni_device_->getDeviceInfo ();

color_frame_listener = boost::make_shared<OpenNI2FrameListener>();
depth_frame_listener = boost::make_shared<OpenNI2FrameListener>();
ir_frame_listener = boost::make_shared<OpenNI2FrameListener>();
color_frame_listener.reset (new OpenNI2FrameListener);
depth_frame_listener.reset (new OpenNI2FrameListener);
ir_frame_listener.reset (new OpenNI2FrameListener);
}

pcl::io::openni2::OpenNI2Device::~OpenNI2Device ()
Expand Down Expand Up @@ -174,7 +172,7 @@ pcl::io::openni2::OpenNI2Device::isValid () const
float
pcl::io::openni2::OpenNI2Device::getIRFocalLength () const
{
boost::shared_ptr<openni::VideoStream> stream = getIRVideoStream ();
auto stream = getIRVideoStream ();

int frameWidth = stream->getVideoMode ().getResolutionX ();
float hFov = stream->getHorizontalFieldOfView ();
Expand All @@ -185,7 +183,7 @@ pcl::io::openni2::OpenNI2Device::getIRFocalLength () const
float
pcl::io::openni2::OpenNI2Device::getColorFocalLength () const
{
boost::shared_ptr<openni::VideoStream> stream = getColorVideoStream ();
auto stream = getColorVideoStream ();

int frameWidth = stream->getVideoMode ().getResolutionX ();
float hFov = stream->getHorizontalFieldOfView ();
Expand All @@ -196,7 +194,7 @@ pcl::io::openni2::OpenNI2Device::getColorFocalLength () const
float
pcl::io::openni2::OpenNI2Device::getDepthFocalLength () const
{
boost::shared_ptr<openni::VideoStream> stream = getDepthVideoStream ();
auto stream = getDepthVideoStream ();

int frameWidth = stream->getVideoMode ().getResolutionX ();
float hFov = stream->getHorizontalFieldOfView ();
Expand Down Expand Up @@ -294,9 +292,7 @@ pcl::io::openni2::OpenNI2Device::hasDepthSensor () const
void
pcl::io::openni2::OpenNI2Device::startIRStream ()
{
boost::shared_ptr<openni::VideoStream> stream = getIRVideoStream ();

if (stream)
if (auto stream = getIRVideoStream ())
{
stream->setMirroringEnabled (false);
stream->addNewFrameListener (ir_frame_listener.get ());
Expand All @@ -308,9 +304,7 @@ pcl::io::openni2::OpenNI2Device::startIRStream ()
void
pcl::io::openni2::OpenNI2Device::startColorStream ()
{
boost::shared_ptr<openni::VideoStream> stream = getColorVideoStream ();

if (stream)
if (auto stream = getColorVideoStream ())
{
stream->setMirroringEnabled (false);
stream->addNewFrameListener (color_frame_listener.get ());
Expand All @@ -321,9 +315,7 @@ pcl::io::openni2::OpenNI2Device::startColorStream ()
void
pcl::io::openni2::OpenNI2Device::startDepthStream ()
{
boost::shared_ptr<openni::VideoStream> stream = getDepthVideoStream ();

if (stream)
if (auto stream = getDepthVideoStream ())
{
stream->setMirroringEnabled (false);
stream->addNewFrameListener (depth_frame_listener.get ());
Expand Down Expand Up @@ -442,66 +434,34 @@ pcl::io::openni2::OpenNI2Device::setSynchronization (bool enabled)
const OpenNI2VideoMode
pcl::io::openni2::OpenNI2Device::getIRVideoMode ()
{
OpenNI2VideoMode ret;

boost::shared_ptr<openni::VideoStream> stream = getIRVideoStream ();

if (stream)
{
openni::VideoMode video_mode = stream->getVideoMode ();

ret = openniModeToGrabberMode (video_mode);
}
else
THROW_IO_EXCEPTION ("Could not create video stream.");

return (ret);
if (auto stream = getIRVideoStream ())
return openniModeToGrabberMode (stream->getVideoMode ());
THROW_IO_EXCEPTION ("Could not create video stream.");
return {};
}

const OpenNI2VideoMode
pcl::io::openni2::OpenNI2Device::getColorVideoMode ()
{
OpenNI2VideoMode ret;

boost::shared_ptr<openni::VideoStream> stream = getColorVideoStream ();

if (stream)
{
openni::VideoMode video_mode = stream->getVideoMode ();

ret = openniModeToGrabberMode (video_mode);
}
else
THROW_IO_EXCEPTION ("Could not create video stream.");

return (ret);
if (auto stream = getColorVideoStream ())
return openniModeToGrabberMode (stream->getVideoMode ());
THROW_IO_EXCEPTION ("Could not create video stream.");
return {};
}

const OpenNI2VideoMode
pcl::io::openni2::OpenNI2Device::getDepthVideoMode ()
{
OpenNI2VideoMode ret;

boost::shared_ptr<openni::VideoStream> stream = getDepthVideoStream ();

if (stream)
{
openni::VideoMode video_mode = stream->getVideoMode ();

ret = openniModeToGrabberMode (video_mode);
}
else
THROW_IO_EXCEPTION ("Could not create video stream.");

return (ret);
if (auto stream = getDepthVideoStream ())
return openniModeToGrabberMode (stream->getVideoMode ());
THROW_IO_EXCEPTION ("Could not create video stream.");
return {};
}

void
pcl::io::openni2::OpenNI2Device::setIRVideoMode (const OpenNI2VideoMode& video_mode)
{
boost::shared_ptr<openni::VideoStream> stream = getIRVideoStream ();

if (stream)
if (auto stream = getIRVideoStream ())
{
const openni::VideoMode videoMode = grabberModeToOpenniMode (video_mode);
const openni::Status rc = stream->setVideoMode (videoMode);
Expand All @@ -513,9 +473,7 @@ pcl::io::openni2::OpenNI2Device::setIRVideoMode (const OpenNI2VideoMode& video_m
void
pcl::io::openni2::OpenNI2Device::setColorVideoMode (const OpenNI2VideoMode& video_mode)
{
boost::shared_ptr<openni::VideoStream> stream = getColorVideoStream ();

if (stream)
if (auto stream = getColorVideoStream ())
{
openni::VideoMode videoMode = grabberModeToOpenniMode (video_mode);
const openni::Status rc = stream->setVideoMode (videoMode);
Expand All @@ -527,9 +485,7 @@ pcl::io::openni2::OpenNI2Device::setColorVideoMode (const OpenNI2VideoMode& vide
void
pcl::io::openni2::OpenNI2Device::setDepthVideoMode (const OpenNI2VideoMode& video_mode)
{
boost::shared_ptr<openni::VideoStream> stream = getDepthVideoStream ();

if (stream)
if (auto stream = getDepthVideoStream ())
{
const openni::VideoMode videoMode = grabberModeToOpenniMode (video_mode);
const openni::Status rc = stream->setVideoMode (videoMode);
Expand Down Expand Up @@ -580,13 +536,11 @@ pcl::io::openni2::OpenNI2Device::getDefaultDepthMode () const
const std::vector<OpenNI2VideoMode>&
pcl::io::openni2::OpenNI2Device::getSupportedIRVideoModes () const
{
boost::shared_ptr<openni::VideoStream> stream = getIRVideoStream ();
ir_video_modes_.clear ();

if (stream)
if (auto stream = getIRVideoStream ())
{
const openni::SensorInfo& sensor_info = stream->getSensorInfo ();

ir_video_modes_ = openniModeToGrabberMode (sensor_info.getSupportedVideoModes ());
}

Expand All @@ -596,14 +550,11 @@ pcl::io::openni2::OpenNI2Device::getSupportedIRVideoModes () const
const std::vector<OpenNI2VideoMode>&
pcl::io::openni2::OpenNI2Device::getSupportedColorVideoModes () const
{
boost::shared_ptr<openni::VideoStream> stream = getColorVideoStream ();

color_video_modes_.clear ();

if (stream)
if (auto stream = getColorVideoStream ())
{
const openni::SensorInfo& sensor_info = stream->getSensorInfo ();

color_video_modes_ = openniModeToGrabberMode (sensor_info.getSupportedVideoModes ());
}

Expand All @@ -613,14 +564,11 @@ pcl::io::openni2::OpenNI2Device::getSupportedColorVideoModes () const
const std::vector<OpenNI2VideoMode>&
pcl::io::openni2::OpenNI2Device::getSupportedDepthVideoModes () const
{
boost::shared_ptr<openni::VideoStream> stream = getDepthVideoStream ();

depth_video_modes_.clear ();

if (stream)
if (auto stream = getDepthVideoStream ())
{
const openni::SensorInfo& sensor_info = stream->getSensorInfo ();

depth_video_modes_ = openniModeToGrabberMode (sensor_info.getSupportedVideoModes ());
}

Expand Down Expand Up @@ -712,9 +660,7 @@ pcl::io::openni2::OpenNI2Device::resizingSupported (size_t input_width, size_t i
void
pcl::io::openni2::OpenNI2Device::setAutoExposure (bool enable)
{
boost::shared_ptr<openni::VideoStream> stream = getColorVideoStream ();

if (stream)
if (auto stream = getColorVideoStream ())
{
openni::CameraSettings* camera_seeting = stream->getCameraSettings ();
if (camera_seeting)
Expand All @@ -730,9 +676,7 @@ pcl::io::openni2::OpenNI2Device::setAutoExposure (bool enable)
void
pcl::io::openni2::OpenNI2Device::setAutoWhiteBalance (bool enable)
{
boost::shared_ptr<openni::VideoStream> stream = getColorVideoStream ();

if (stream)
if (auto stream = getColorVideoStream ())
{
openni::CameraSettings* camera_seeting = stream->getCameraSettings ();
if (camera_seeting)
Expand All @@ -741,7 +685,6 @@ pcl::io::openni2::OpenNI2Device::setAutoWhiteBalance (bool enable)
if (rc != openni::STATUS_OK)
THROW_IO_EXCEPTION ("Couldn't set auto white balance: \n%s\n", openni::OpenNI::getExtendedError ());
}

}
}

Expand All @@ -750,9 +693,7 @@ pcl::io::openni2::OpenNI2Device::getAutoExposure () const
{
bool ret = false;

boost::shared_ptr<openni::VideoStream> stream = getColorVideoStream ();

if (stream)
if (auto stream = getColorVideoStream ())
{
openni::CameraSettings* camera_seeting = stream->getCameraSettings ();
if (camera_seeting)
Expand All @@ -767,9 +708,7 @@ pcl::io::openni2::OpenNI2Device::getAutoWhiteBalance () const
{
bool ret = false;

boost::shared_ptr<openni::VideoStream> stream = getColorVideoStream ();

if (stream)
if (auto stream = getColorVideoStream ())
{
openni::CameraSettings* camera_setting = stream->getCameraSettings ();
if (camera_setting)
Expand Down Expand Up @@ -818,7 +757,7 @@ pcl::io::openni2::OpenNI2Device::getIRVideoStream () const
{
if (hasIRSensor ())
{
ir_video_stream_ = boost::make_shared<openni::VideoStream>();
ir_video_stream_.reset (new openni::VideoStream);

const openni::Status rc = ir_video_stream_->create (*openni_device_, openni::SENSOR_IR);
if (rc != openni::STATUS_OK)
Expand All @@ -835,7 +774,7 @@ pcl::io::openni2::OpenNI2Device::getColorVideoStream () const
{
if (hasColorSensor ())
{
color_video_stream_ = boost::make_shared<openni::VideoStream>();
color_video_stream_.reset (new openni::VideoStream);

const openni::Status rc = color_video_stream_->create (*openni_device_, openni::SENSOR_COLOR);
if (rc != openni::STATUS_OK)
Expand All @@ -852,7 +791,7 @@ pcl::io::openni2::OpenNI2Device::getDepthVideoStream () const
{
if (hasDepthSensor ())
{
depth_video_stream_ = boost::make_shared<openni::VideoStream>();
depth_video_stream_.reset (new openni::VideoStream);

const openni::Status rc = depth_video_stream_->create (*openni_device_, openni::SENSOR_DEPTH);
if (rc != openni::STATUS_OK)
Expand Down
6 changes: 3 additions & 3 deletions io/src/openni2_grabber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ void
pcl::io::OpenNI2Grabber::setupDevice (const std::string& device_id, const Mode& depth_mode, const Mode& image_mode)
{
// Initialize the openni device
boost::shared_ptr<OpenNI2DeviceManager> deviceManager = OpenNI2DeviceManager::getInstance ();
auto deviceManager = OpenNI2DeviceManager::getInstance ();

try
{
Expand Down Expand Up @@ -889,9 +889,9 @@ void pcl::io::OpenNI2Grabber::processColorFrame (openni::VideoStream& stream)

// Convert frame to PCL image type, based on pixel format
if (format == openni::PIXEL_FORMAT_YUV422)
image = boost::make_shared<ImageYUV422> (frameWrapper, t_callback);
image.reset (new ImageYUV422 (frameWrapper, t_callback));
else //if (format == PixelFormat::PIXEL_FORMAT_RGB888)
image = boost::make_shared<ImageRGB24> (frameWrapper, t_callback);
image.reset (new ImageRGB24 (frameWrapper, t_callback));

imageCallback (image, nullptr);
}
Expand Down

0 comments on commit 26983a4

Please sign in to comment.