99
1010#include " Types.hpp"
1111#include " libobsensor/h/StreamProfile.h"
12+ #include " libobsensor/h/Error.h"
1213#include < iostream>
1314#include < memory>
1415
@@ -19,9 +20,7 @@ class StreamProfile : public std::enable_shared_from_this<StreamProfile> {
1920 const ob_stream_profile_t *impl_ = nullptr ;
2021
2122public:
22- explicit StreamProfile (const ob_stream_profile_t *impl) : impl_(impl) {}
23-
24- StreamProfile (StreamProfile &streamProfile) = delete ;
23+ StreamProfile (StreamProfile &streamProfile) = delete ;
2524 StreamProfile &operator =(StreamProfile &streamProfile) = delete ;
2625
2726 StreamProfile (StreamProfile &&streamProfile) noexcept : impl_(streamProfile.impl_) {
@@ -106,7 +105,7 @@ class StreamProfile : public std::enable_shared_from_this<StreamProfile> {
106105 throw std::runtime_error (" Unsupported operation. Object's type is not the required type." );
107106 }
108107
109- return std::static_pointer_cast <T>(shared_from_this ());
108+ return std::dynamic_pointer_cast <T>(shared_from_this ());
110109 }
111110
112111 /* *
@@ -123,7 +122,6 @@ class StreamProfile : public std::enable_shared_from_this<StreamProfile> {
123122 return std::static_pointer_cast<const T>(shared_from_this ());
124123 }
125124
126- public:
127125 // The following interfaces are deprecated and are retained here for compatibility purposes.
128126 OBFormat format () const {
129127 return getFormat ();
@@ -132,6 +130,9 @@ class StreamProfile : public std::enable_shared_from_this<StreamProfile> {
132130 OBStreamType type () const {
133131 return getType ();
134132 }
133+
134+ protected:
135+ explicit StreamProfile (const ob_stream_profile_t *impl) : impl_(impl) {}
135136};
136137
137138/* *
@@ -351,6 +352,33 @@ template <typename T> bool StreamProfile::is() const {
351352 return false ;
352353}
353354
355+ class StreamProfileFactory {
356+ public:
357+ static std::shared_ptr<StreamProfile> create (const ob_stream_profile_t *impl) {
358+ ob_error *error = nullptr ;
359+ const auto type = ob_stream_profile_get_type (impl, &error);
360+ Error::handle (&error);
361+ switch (type) {
362+ case OB_STREAM_IR:
363+ case OB_STREAM_IR_LEFT:
364+ case OB_STREAM_IR_RIGHT:
365+ case OB_STREAM_DEPTH:
366+ case OB_STREAM_COLOR:
367+ case OB_STREAM_VIDEO:
368+ return std::make_shared<VideoStreamProfile>(impl);
369+ case OB_STREAM_ACCEL:
370+ return std::make_shared<AccelStreamProfile>(impl);
371+ case OB_STREAM_GYRO:
372+ return std::make_shared<GyroStreamProfile>(impl);
373+ default : {
374+ ob_error *err = ob_create_error (OB_STATUS_ERROR, " Unsupported stream type." , " StreamProfileFactory::create" , " " , OB_EXCEPTION_TYPE_INVALID_VALUE);
375+ Error::handle (&err);
376+ return nullptr ;
377+ }
378+ }
379+ }
380+ };
381+
354382class StreamProfileList {
355383protected:
356384 const ob_stream_profile_list_t *impl_;
@@ -385,7 +413,7 @@ class StreamProfileList {
385413 ob_error *error = nullptr ;
386414 auto profile = ob_stream_profile_list_get_profile (impl_, index, &error);
387415 Error::handle (&error);
388- return std::make_shared<StreamProfile> (profile);
416+ return StreamProfileFactory::create (profile);
389417 }
390418
391419 /* *
@@ -403,7 +431,8 @@ class StreamProfileList {
403431 ob_error *error = nullptr ;
404432 auto profile = ob_stream_profile_list_get_video_stream_profile (impl_, width, height, format, fps, &error);
405433 Error::handle (&error);
406- return std::make_shared<VideoStreamProfile>(profile);
434+ auto vsp = StreamProfileFactory::create (profile);
435+ return vsp->as <VideoStreamProfile>();
407436 }
408437
409438 /* *
@@ -417,7 +446,8 @@ class StreamProfileList {
417446 ob_error *error = nullptr ;
418447 auto profile = ob_stream_profile_list_get_accel_stream_profile (impl_, fullScaleRange, sampleRate, &error);
419448 Error::handle (&error);
420- return std::make_shared<AccelStreamProfile>(profile);
449+ auto asp = StreamProfileFactory::create (profile);
450+ return asp->as <AccelStreamProfile>();
421451 }
422452
423453 /* *
@@ -431,7 +461,8 @@ class StreamProfileList {
431461 ob_error *error = nullptr ;
432462 auto profile = ob_stream_profile_list_get_gyro_stream_profile (impl_, fullScaleRange, sampleRate, &error);
433463 Error::handle (&error);
434- return std::make_shared<GyroStreamProfile>(profile);
464+ auto gsp = StreamProfileFactory::create (profile);
465+ return gsp->as <GyroStreamProfile>();
435466 }
436467
437468public:
@@ -442,4 +473,3 @@ class StreamProfileList {
442473};
443474
444475} // namespace ob
445-
0 commit comments