From: ronag Date: Tue, 14 Dec 2010 22:44:37 +0000 (+0000) Subject: 2.0.0.2: X-Git-Tag: 2.0.1~911 X-Git-Url: https://git.sesse.net/?a=commitdiff_plain;h=852dacfae88898d42e79817b761f3e309a53153d;hp=a552b9a5367c61fb23272acd73d53dd69dd1f7cf;p=casparcg 2.0.0.2: - Further improvements to safe_ptr. Added cast functions and implicit conversion to shared_ptr. git-svn-id: https://casparcg.svn.sourceforge.net/svnroot/casparcg/server/branches/2.0.0.2@293 362d55ac-95cf-4e76-9f9a-cbaa9c17b72d --- diff --git a/common/utility/safe_ptr.h b/common/utility/safe_ptr.h index 93cbc6d77..9eeb923a4 100644 --- a/common/utility/safe_ptr.h +++ b/common/utility/safe_ptr.h @@ -13,69 +13,69 @@ class safe_ptr public: typedef T element_type; - safe_ptr() : impl_(std::make_shared()){static_assert(!std::is_abstract::value, "Cannot construct abstract class.");} + safe_ptr() : impl_(std::make_shared()){} safe_ptr(const safe_ptr& other) : impl_(other.impl_){} - template - safe_ptr(const safe_ptr& other, typename std::enable_if::value, void*>::type = 0) : impl_(other.impl_){} + template + safe_ptr(const safe_ptr& other, typename std::enable_if::value, void*>::type = 0) : impl_(other.impl_){} - template - safe_ptr(const Y& impl, typename std::enable_if::type, typename std::add_pointer::type>::value, void>::type* = 0) - : impl_(std::make_shared(impl)) {} + template + safe_ptr(const U& impl, typename std::enable_if::type, typename std::add_pointer::type>::value, void>::type* = 0) + : impl_(std::make_shared(impl)) {} - template - safe_ptr(const Y& impl, D dtor, typename std::enable_if::type, typename std::add_pointer::type>::value, void>::type* = 0) - : impl_(new Y(impl), dtor) {} + template + safe_ptr(const U& impl, D dtor, typename std::enable_if::type, typename std::add_pointer::type>::value, void>::type* = 0) + : impl_(new U(impl), dtor) {} - template - safe_ptr(Y&& impl, typename std::enable_if::type, typename std::add_pointer::type>::value, void>::type* = 0) - : impl_(std::make_shared(std::forward(impl))) {} + template + safe_ptr(U&& impl, typename std::enable_if::type, typename std::add_pointer::type>::value, void>::type* = 0) + : impl_(std::make_shared(std::forward(impl))) {} - template - safe_ptr(Y&& impl, D dtor, typename std::enable_if::type, typename std::add_pointer::type>::value, void>::type* = 0) - : impl_(new Y(std::forward(impl)), dtor) {} + template + safe_ptr(U&& impl, D dtor, typename std::enable_if::type, typename std::add_pointer::type>::value, void>::type* = 0) + : impl_(new U(std::forward(impl)), dtor) {} - template - explicit safe_ptr(const std::shared_ptr& impl, typename std::enable_if::value, void*>::type = 0) : impl_(impl) + template + explicit safe_ptr(const std::shared_ptr& impl, typename std::enable_if::value, void*>::type = 0) : impl_(impl) { if(!impl_) throw std::invalid_argument("impl"); } - template - explicit safe_ptr(std::shared_ptr&& impl, typename std::enable_if::value, void*>::type = 0) : impl_(std::move(impl)) + template + explicit safe_ptr(std::shared_ptr&& impl, typename std::enable_if::value, void*>::type = 0) : impl_(std::move(impl)) { if(!impl_) throw std::invalid_argument("impl"); } - template - explicit safe_ptr(Y* impl, typename std::enable_if::value, void*>::type = 0) : impl_(impl) + template + explicit safe_ptr(U* impl, typename std::enable_if::value, void*>::type = 0) : impl_(impl) { if(!impl_) throw std::invalid_argument("impl"); } - template - explicit safe_ptr(Y* impl, D dtor, typename std::enable_if::value, void*>::type = 0) : impl_(impl, dtor) + template + explicit safe_ptr(U* impl, D dtor, typename std::enable_if::value, void*>::type = 0) : impl_(impl, dtor) { if(!impl_) throw std::invalid_argument("impl"); } - template - typename std::enable_if::value, safe_ptr&>::type - operator=(const safe_ptr& other) + template + typename std::enable_if::value, safe_ptr&>::type + operator=(const safe_ptr& other) { safe_ptr temp(other); temp.swap(*this); return *this; } - template - typename std::enable_if::type, typename std::add_pointer::type>::value, safe_ptr&>::type - operator=(Y&& impl) + template + typename std::enable_if::type, typename std::add_pointer::type>::value, safe_ptr&>::type + operator=(U&& impl) { safe_ptr temp(std::forward(impl)); temp.swap(*this); @@ -94,66 +94,129 @@ public: void swap(safe_ptr& other) { impl_.swap(other.impl_); } - std::shared_ptr get_shared() const { return impl_; } + operator std::shared_ptr() const { return impl_;} + + template + bool owner_before(const safe_ptr& ptr){ return impl_.owner_before(ptr.impl_); } + + template + bool owner_before(const std::shared_ptr& ptr){ return impl_.owner_before(ptr); } + + template + D* get_deleter(safe_ptr const& ptr) { return impl_.get_deleter(); } private: std::shared_ptr impl_; }; template -bool operator==(safe_ptr const & a, safe_ptr const & b) +bool operator==(const safe_ptr& a, const safe_ptr& b) { return a.get() == b.get(); } template -bool operator!=(safe_ptr const & a, safe_ptr const & b) +bool operator!=(const safe_ptr& a, const safe_ptr& b) { return a.get() != b.get(); } template -bool operator<(safe_ptr const & a, safe_ptr const & b) +bool operator<(const safe_ptr& a, const safe_ptr& b) { return a.get() < b.get(); } -template void swap(safe_ptr & a, safe_ptr & b) +template +bool operator>(const safe_ptr& a, const safe_ptr& b) +{ + return a.get() > b.get(); +} + +template +bool operator>=(const safe_ptr& a, const safe_ptr& b) +{ + return a.get() >= b.get(); +} + +template +bool operator<=(const safe_ptr& a, const safe_ptr& b) +{ + return a.get() <= b.get(); +} + +template +std::basic_ostream& operator<<(std::basic_ostream& out, const safe_ptr& p) +{ + return out << p.get(); +} + +template +void swap(safe_ptr& a, safe_ptr& b) { a.swap(b); } -template T* get_pointer(safe_ptr const & p) +template +T* get_pointer(safe_ptr const& p) { return p.get(); } +template +safe_ptr static_pointer_cast(const safe_ptr& p) +{ + return safe_ptr(std::static_pointer_cast(std::shared_ptr(p))); +} + +template +safe_ptr const_pointer_cast(const safe_ptr& p) +{ + return safe_ptr(std::const_pointer_cast(std::shared_ptr(p))); +} + +template +safe_ptr dynamic_pointer_cast(const safe_ptr& p) +{ + auto temp = std::dynamic_pointer_cast(std::shared_ptr(p)); + if(!temp) + throw std::bad_cast(); + return safe_ptr(temp); +} + template safe_ptr make_safe() { - static_assert(!std::is_abstract::value, "Cannot construct abstract class."); return safe_ptr(); } template safe_ptr make_safe(P0&& p0) { - static_assert(!std::is_abstract::value, "Cannot construct abstract class."); return safe_ptr(std::make_shared(std::forward(p0))); } template safe_ptr make_safe(P0&& p0, P1&& p1) { - static_assert(!std::is_abstract::value, "Cannot construct abstract class."); return safe_ptr(std::make_shared(std::forward(p0), std::forward(p1))); } template safe_ptr make_safe(P0&& p0, P1&& p1, P2&& p2) { - static_assert(!std::is_abstract::value, "Cannot construct abstract class."); return safe_ptr(std::make_shared(std::forward(p0), std::forward(p1), std::forward(p2))); } +template +safe_ptr make_safe(P0&& p0, P1&& p1, P2&& p2, P3&& p3) +{ + return safe_ptr(std::make_shared(std::forward(p0), std::forward(p1), std::forward(p2), std::forward(p3))); +} + +template +safe_ptr make_safe(P0&& p0, P1&& p1, P2&& p2, P3&& p3, P4&&) +{ + return safe_ptr(std::make_shared(std::forward(p0), std::forward(p1), std::forward(p2), std::forward(p3), std::forward(p4))); +} } \ No newline at end of file diff --git a/core/StdAfx.h b/core/StdAfx.h index a7dd5106b..78566bcfa 100644 --- a/core/StdAfx.h +++ b/core/StdAfx.h @@ -66,6 +66,7 @@ #include #include "../common/utility/string_convert.h" +#include "../common/utility/safe_ptr.h" #include "../common/log/Log.h" #include "../common/exception/exceptions.h" diff --git a/core/producer/ffmpeg/video/video_transformer.cpp b/core/producer/ffmpeg/video/video_transformer.cpp index cc2f7422f..4a0f9b233 100644 --- a/core/producer/ffmpeg/video/video_transformer.cpp +++ b/core/producer/ffmpeg/video/video_transformer.cpp @@ -143,7 +143,7 @@ struct video_transformer::implementation : boost::noncopyable void initialize(const safe_ptr& frame_processor) { - frame_processor_ = frame_processor.get_shared(); + frame_processor_ = frame_processor; } std::shared_ptr frame_processor_; diff --git a/core/producer/flash/cg_producer.cpp b/core/producer/flash/cg_producer.cpp index 909e9c54a..161d67c5d 100644 --- a/core/producer/flash/cg_producer.cpp +++ b/core/producer/flash/cg_producer.cpp @@ -30,10 +30,7 @@ struct cg_producer::implementation : boost::noncopyable { public: - implementation() : ver_(template_version::invalid), flash_producer_(create_flash()) - { - - } + implementation() : ver_(template_version::invalid), flash_producer_(create_flash()){} safe_ptr create_flash() { @@ -144,7 +141,7 @@ public: void initialize(const safe_ptr& frame_processor) { - frame_processor_ = frame_processor.get_shared(); + frame_processor_ = frame_processor; flash_producer_->initialize(frame_processor); } @@ -160,14 +157,16 @@ public: safe_ptr get_default_cg_producer(const safe_ptr& channel, int render_layer) { - auto producer = std::dynamic_pointer_cast(channel->foreground(render_layer).get().get_shared()); - if(producer == nullptr) + try { - producer = std::make_shared(); - channel->load(render_layer, safe_ptr(producer), load_option::auto_play); + return dynamic_pointer_cast(channel->foreground(render_layer).get()); + } + catch(std::bad_cast&) + { + auto producer = make_safe(); + channel->load(render_layer, producer, load_option::auto_play); + return producer; } - - return safe_ptr(producer); } cg_producer::cg_producer() : impl_(new implementation()){} diff --git a/core/producer/flash/flash_producer.cpp b/core/producer/flash/flash_producer.cpp index 31f1fbfd5..ca777dff2 100644 --- a/core/producer/flash/flash_producer.cpp +++ b/core/producer/flash/flash_producer.cpp @@ -182,7 +182,7 @@ struct flash_producer::implementation stop(); frame_buffer_.clear(); - frame_buffer_.try_push(draw_frame::eof().get_shared()); // EOF + frame_buffer_.try_push(draw_frame::eof()); // EOF current_frame_ = nullptr; }); @@ -227,9 +227,9 @@ struct flash_producer::implementation std::shared_ptr frame; if(is_progressive) - frame = do_receive().get_shared(); + frame = do_receive(); else - frame = composite_frame::interlace(do_receive(), do_receive(), format_desc.mode).get_shared(); + frame = std::shared_ptr(composite_frame::interlace(do_receive(), do_receive(), format_desc.mode)); frame_buffer_.push(frame); is_empty_ = flashax_container_->IsEmpty(); @@ -261,7 +261,7 @@ struct flash_producer::implementation void initialize(const safe_ptr& frame_processor) { - frame_processor_ = frame_processor.get_shared(); + frame_processor_ = frame_processor; auto format_desc = frame_processor_->get_video_format_desc(); bmp_frame_ = std::make_shared(format_desc.width, format_desc.height); start(false); diff --git a/core/producer/image/image_producer.cpp b/core/producer/image/image_producer.cpp index e4cec3ba3..92fefc7f5 100644 --- a/core/producer/image/image_producer.cpp +++ b/core/producer/image/image_producer.cpp @@ -25,7 +25,7 @@ struct image_producer : public frame_producer void initialize(const safe_ptr& frame_processor) { - frame_processor_ = frame_processor.get_shared(); + frame_processor_ = frame_processor; auto bitmap = load_image(filename_); FreeImage_FlipVertical(bitmap.get()); auto frame = frame_processor->create_frame(FreeImage_GetWidth(bitmap.get()), FreeImage_GetHeight(bitmap.get())); diff --git a/core/producer/transition/transition_producer.cpp b/core/producer/transition/transition_producer.cpp index 61790a309..9dbcf87eb 100644 --- a/core/producer/transition/transition_producer.cpp +++ b/core/producer/transition/transition_producer.cpp @@ -143,7 +143,7 @@ struct transition_producer::implementation : boost::noncopyable void initialize(const safe_ptr& frame_processor) { dest_producer_->initialize(frame_processor); - frame_processor_ = frame_processor.get_shared(); + frame_processor_ = frame_processor; } std::wstring print() const diff --git a/core/protocol/amcp/AMCPProtocolStrategy.cpp b/core/protocol/amcp/AMCPProtocolStrategy.cpp index 8eacc63e7..78b4a46fd 100644 --- a/core/protocol/amcp/AMCPProtocolStrategy.cpp +++ b/core/protocol/amcp/AMCPProtocolStrategy.cpp @@ -48,7 +48,7 @@ const std::wstring AMCPProtocolStrategy::MessageDelimiter = TEXT("\r\n"); inline std::shared_ptr GetChannelSafe(unsigned int index, const std::vector>& channels) { - return index < channels.size() ? channels[index].get_shared() : nullptr; + return index < channels.size() ? std::shared_ptr(channels[index]) : nullptr; } AMCPProtocolStrategy::AMCPProtocolStrategy(const std::vector>& channels) : channels_(channels) {