/* gstreamermm - a C++ wrapper for gstreamer * * Copyright 2008 The gstreamermm Development Team * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include #include _DEFS(gstreamermm,gst) namespace Gst { _WRAP_ENUM(IteratorItem, GstIteratorItem) _WRAP_ENUM(IteratorResult, GstIteratorResult) /** The base class for classes that retrieve multiple elements in a thread * safe way. * * Classes derived from Gst::IteratorBase are used to retrieve multiple objects * from another object in a thread safe way. They are implemented as C++ like * input iterators so they do not support multi-passing, but they are usable * enough for iterating through a list of items and referencing them in a * single pass. * * Various GStreamer objects provide access to their internal structures using * an iterator. */ template class IteratorBase { public: /** Moves to the next iterator item. * * @return The result of the iteration. MT safe. */ virtual IteratorResult next(); /** Resynchronize the iterator. This function is mostly called after next() * returns Gst::ITERATOR_RESYNC. A result of Gst::ITERATOR_RESYNC from * next() means that a concurrent update was made to the iterator list during * iteration and the iterator needs to be resynchronized before continuing. * Use this function to resynchronize. */ void resync(); /** Tells if the iterator is at the start of the list (not on the first item, * but just before it). Increment the iterator or use * Gst::IteratorBasic::begin() to go to the first item. * * @return true if the iterator is at the start of the list, false otherwise. */ bool is_start() const; /** Tells if the iterator is at the end of the list (just after the last * element). * * @return true if the iterator is at the end of the list, false otherwise. */ bool is_end() const; /** Tells whether the iterator is valid and can be dereferenced. */ operator bool() const; ///Provides access to the underlying C GObject. GstIterator* cobj() { return cobject_; }; ///Provides access to the underlying C GObject. const GstIterator* cobj() const { return cobject_; }; /** Frees the underlying C instance if a take_ownership value of true was * used to wrap it. */ virtual ~IteratorBase(); protected: /// Default constructor. IteratorBase(); /** Copy constructor. Please note that copying and assigning merely shares * the underlying C object. Operations on the copy are also performed in the * underlying C object of the original and if the original is destroyed, the * copy is invalid. */ IteratorBase(const IteratorBase&); /** Constructs an IteratorBase from an underlying C object. * @param castitem The underlying C object. * @param take_ownership Whether to take over the underlying C object. If * true, C object is freed when wrapper is destroyed. */ explicit IteratorBase(GstIterator* castitem, bool take_ownership = true); /** Assignment operator. It replaces the contents of the iterator with the * contents of the new one freeing the underlying C object if a * take_ownership value of true was used when wrapping it. Please note that * copying and assigning merely shares the underlying C object. Operations * on the copy are also performed in the underlying C object of the original * and if the original is destroyed, the copy is invalid. */ IteratorBase& operator=(const IteratorBase& other); #ifndef DOXYGEN_SHOULD_SKIP_THIS GValue current; // The current element the iterator is referencing. IteratorResult current_result; // The current result of a next() call. #endif /* DOXYGEN_SHOULD_SKIP_THIS */ private: #ifndef DOXYGEN_SHOULD_SKIP_THIS GstIterator* cobject_; // The underlying C object. bool take_ownership; // Whether to destroy C object with the wrapper. #endif /* DOXYGEN_SHOULD_SKIP_THIS */ }; /** A class used to retrieve multiple elements in a thread safe way. * Gst::IteratorBasic iterates specifically through elements that are not * reference counted. Gst::Iterator is used for iterating through reference * counted objects. */ template class IteratorBasic : public IteratorBase { public: ///Default constructor. IteratorBasic(); /** Creates a Gst::IteratorBasic wrapper for a GstIterator object. The * underlying @a castitem will be freed with the Gst::IteratorBasic * destruction if a take_ownership value of true is given. * * @param castitem The C instance to wrap. * @param take_ownership Whether to take over the underlying C object. If * true, C object is freed when wrapper is destroyed. */ explicit IteratorBasic(GstIterator* castitem, bool take_ownership = true); /** Resynchronizes the iterator and moves the iterator to the first item. * * @throw std::runtime_error (if a Gst::ITERATOR_ERROR is encountered or if a * concurrent update to the iterator occurs while it is advanced to the first * element). */ void begin(); /** Dereferences the iterator and obtains the underlying object. */ CppType operator*() const; /** Accesses underlying object member through the iterator. */ CppType* operator->() const; /** Prefix auto-increment operator. It advances to the next item in the * iterator. It is faster than the postfix operator. * @throw std::runtime_error (if a Gst::ITERATOR_ERROR is encountered or if a * concurrent update to the iterator occurs while it iterates). */ IteratorBasic& operator++(); /** Postfix auto-increment operator. It advances to the next item in the * iterator. * @throw std::runtime_error (if a Gst::ITERATOR_ERROR is encountered or if a * concurrent update to the iterator occurs while it iterates). */ IteratorBasic operator++(int); }; /** A class used to retrieve multiple reference counted elements in a thread * safe way. * Gst::Iterator iterates specifically through elements that are reference * counted and therefore dereferencing the elements of the iterator yields a * Glib::RefPtr<> to the C++ element type. */ template class Iterator : public IteratorBasic { public: ///Default constructor. Iterator(); /** Creates a Gst::Iterator wrapper for a GstIterator object. The underlying * @a castitem will be freed with the Gst::Iterator destruction if a * take_ownership value of true is given. * * @param castitem The C instance to wrap. * @param take_ownership Whether to take over the underlying C object. If * true, C object is freed when wrapper is destroyed. */ explicit Iterator(GstIterator* castitem, bool take_ownership = true); /** Moves to the next iterator item. * * @return The result of the iteration. MT safe. */ IteratorResult next(); /** Dereferences the iterator and obtains the underlying Glib::RefPtr<>. */ Glib::RefPtr operator*() const; /** Accesses underlying object member through the RefPtr<>. */ CppType* operator->() const; /** Prefix auto-increment operator. It advances to the next item in the * iterator. It is faster than the postfix operator. * @throw std::runtime_error (if a Gst::ITERATOR_ERROR is encountered or if a * concurrent update to the iterator occurs while it iterates). */ Iterator& operator++(); /** Postfix auto-increment operator. It advances to the next item in the * iterator. * @throw std::runtime_error (if a Gst::ITERATOR_ERROR is encountered or if a * concurrent update to the iterator occurs while it iterates). */ Iterator operator++(int); }; #ifndef DOXYGEN_SHOULD_SKIP_THIS /***************** Gst::IteratorBase ************************/ template IteratorBase::IteratorBase() : current(G_VALUE_INIT), current_result(Gst::ITERATOR_OK), cobject_(nullptr), take_ownership(true) { } template IteratorBase::IteratorBase(const IteratorBase& other) : current(G_VALUE_INIT), current_result(other.current_result), take_ownership(other.take_ownership) { cobject_ = other.take_ownership ? gst_iterator_copy(other.cobject_) : other.cobject_; if (G_IS_VALUE (&other.current)) { g_value_init(¤t, G_VALUE_TYPE(&other.current)); g_value_copy(&other.current, ¤t); } } template IteratorBase::IteratorBase(GstIterator* castitem, bool take_ownership) : current(G_VALUE_INIT), current_result(Gst::ITERATOR_OK), cobject_(castitem), take_ownership(take_ownership) {} template IteratorBase& IteratorBase::operator=(const IteratorBase& other) { if (cobject_ && take_ownership) { gst_iterator_free(cobject_); } if (G_IS_VALUE(¤t)) { g_value_unset(¤t); } current_result = other.current_result; cobject_ = other.take_ownership ? gst_iterator_copy(other.cobject_) : other.cobject_; if (G_IS_VALUE (&other.current)) { g_value_init(¤t, G_VALUE_TYPE(&other.current)); g_value_copy(&other.current, ¤t); } return *this; } template IteratorResult IteratorBase::next() { // Unset current before calling gst_iterator_next() if(G_IS_VALUE(¤t)) g_value_unset(¤t); current_result = static_cast(gst_iterator_next(cobj(), ¤t)); // Reset current if iterator is done: if(current_result == Gst::ITERATOR_DONE) g_value_reset (¤t); return current_result; } template void IteratorBase::resync() { gst_iterator_resync(cobj()); g_value_reset (¤t); current_result = Gst::ITERATOR_OK; } template bool IteratorBase::is_start() const { return (G_VALUE_HOLDS_OBJECT(¤t) && current_result == Gst::ITERATOR_OK); } template bool IteratorBase::is_end() const { return (current_result == Gst::ITERATOR_DONE); } template IteratorBase::operator bool() const { return (! G_VALUE_HOLDS_OBJECT(¤t)); } //virtual template IteratorBase::~IteratorBase() { if(take_ownership && cobject_) { gst_iterator_free(cobject_); cobject_ = nullptr; } if(G_IS_VALUE(¤t)) g_value_unset(¤t); } /***************** Gst::IteratorBasic ***********************/ template IteratorBasic::IteratorBasic() : IteratorBase() {} template IteratorBasic::IteratorBasic(GstIterator* castitem, bool take_ownership) : IteratorBase(castitem, take_ownership) {} template void IteratorBasic::begin() { this->resync(); ++(*this); } template CppType IteratorBasic::operator*() const { typedef typename CppType::BaseObjectType CType; if(G_IS_VALUE(&this->current)) return CppType(static_cast(g_value_get_object(&this->current))); else return CppType(); } template CppType* IteratorBasic::operator->() const { static typename CppType::CppObjectType result; if(G_IS_VALUE(&this->current)) { result = this->operator*(); return &result; } else return static_cast(0); } template IteratorBasic& IteratorBasic::operator++() { const IteratorResult result = this->next(); if(result == Gst::ITERATOR_RESYNC) gstreamermm_handle_error("Concurrent update of iterator elements. " "Please resync."); else if(result == Gst::ITERATOR_ERROR) gstreamermm_handle_error("Iterator error while incrementing."); return *this; } template IteratorBasic IteratorBasic::operator++(int) { IteratorBasic original = *this; ++(*this); return original; } /******************* Gst::Iterator **************************/ template Iterator::Iterator() : IteratorBasic() {} template Iterator::Iterator(GstIterator* castitem, bool take_ownership) : IteratorBasic(castitem, take_ownership) {} template IteratorResult Iterator::next() { const IteratorResult result = IteratorBasic::next(); return result; } template Glib::RefPtr Iterator::operator*() const { typedef typename CppType::BaseObjectType CType; if(G_IS_VALUE(&this->current)) { //Take extra reference when dereferencing. The reference will disappear //when Glib::RefPtr<> is destroyed. return Glib::wrap(static_cast(g_value_get_object (&this->current)), true); } else return Glib::RefPtr(0); } template CppType* Iterator::operator->() const { typedef typename CppType::BaseObjectType CType; if(G_IS_VALUE(&this->current)) { //Take extra reference when dereferencing. The reference will disappear //when Glib::RefPtr<> is destroyed. return Glib::wrap(static_cast(g_value_get_object (&this->current)), true).operator->(); } else return static_cast(0); } template Iterator& Iterator::operator++() { IteratorBasic::operator++(); return *this; } template Iterator Iterator::operator++(int) { Iterator original = *this; ++(*this); return original; } #endif /* DOXYGEN_SHOULD_SKIP_THIS */ } //namespace Gst