- Update smartp to version 1.1: add support for dynamic casting
git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@501 3ecf2c5c-341e-0410-92b4-d18e462d057c
This commit is contained in:
parent
181164722b
commit
e0142149b0
|
@ -18,15 +18,17 @@
|
|||
#define SMARTP_HH 1
|
||||
|
||||
#include <new>
|
||||
#include <typeinfo>
|
||||
|
||||
namespace memory {
|
||||
|
||||
/** \brief A simple reference counted smart pointer
|
||||
*
|
||||
* \author Matteo Settenvini
|
||||
* \version 1.0
|
||||
* \date October 2005
|
||||
*
|
||||
*
|
||||
* \version \c 1.1, March 7th, 2006 - Add support for dynamic casting
|
||||
* \version \c 1.0, October 10th, 2006 - Initial stable release
|
||||
*
|
||||
* This template is a smart pointer which uses
|
||||
* a simple mechanism of reference count to
|
||||
* keep track of when and if a stored object
|
||||
|
@ -115,6 +117,35 @@ namespace memory {
|
|||
* \return The number of references */
|
||||
inline unsigned int alive_refs() const throw();
|
||||
|
||||
|
||||
/** \brief Dynamic cast the stored pointer
|
||||
* to another type
|
||||
*
|
||||
* This functions tries to cast the stored
|
||||
* object to the given type.
|
||||
*
|
||||
* \param U The type to cast to.
|
||||
* \return A reference of the casted type
|
||||
* \exception bad_alloc Raise this exception if
|
||||
* the cast isn't successful.
|
||||
*/
|
||||
template<typename U>
|
||||
inline U& cast_to() throw(std::bad_cast);
|
||||
|
||||
/** \brief Dynamic cast the stored pointer
|
||||
* to another type
|
||||
*
|
||||
* This functions tries to cast the stored
|
||||
* object to the given type.
|
||||
*
|
||||
* \param U The type to cast to.
|
||||
* \return A const reference of the casted type
|
||||
* \exception bad_alloc Raise this exception if
|
||||
* the cast isn't successful.
|
||||
*/
|
||||
template<typename U>
|
||||
inline const U& cast_to() const throw(std::bad_cast);
|
||||
|
||||
private:
|
||||
struct contents_type {
|
||||
T* ptr;
|
||||
|
|
|
@ -140,6 +140,30 @@ namespace memory {
|
|||
return _contents->ptr != 0 ? _contents->rc : 0;
|
||||
}
|
||||
|
||||
|
||||
template<typename T, bool isArray>
|
||||
template<typename U>
|
||||
U&
|
||||
smart_ptr<T,isArray>::cast_to() throw(std::bad_cast)
|
||||
{
|
||||
if(_contents->ptr != 0)
|
||||
return dynamic_cast<U&>(*_contents->ptr);
|
||||
else
|
||||
throw std::bad_cast();
|
||||
}
|
||||
|
||||
|
||||
template<typename T, bool isArray>
|
||||
template<typename U>
|
||||
const U&
|
||||
smart_ptr<T, isArray>::cast_to() const throw(std::bad_cast)
|
||||
{
|
||||
if(_contents->ptr != 0)
|
||||
return dynamic_cast<U&>(*_contents->ptr);
|
||||
else
|
||||
throw std::bad_cast();
|
||||
}
|
||||
|
||||
} //~ namespace memory
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue