2 The software in this package is distributed under the GNU General
3 Public License version 2 (with a special exception described below).
5 A copy of GNU General Public License (GPL) is included in this distribution,
6 in the file COPYING.GPL.
8 As a special exception, if other files instantiate templates or use macros
9 or inline functions from this file, or you compile this file and link it
10 with other works to produce a work based on this file, this file
11 does not by itself cause the resulting work to be covered
12 by the GNU General Public License.
14 However the source code for this file must still be made available
15 in accordance with section (3) of the GNU General Public License.
17 This exception does not invalidate any other reasons why a work based
18 on this file might be covered by the GNU General Public License.
21 * @brief provides a little base class for classes wich are used in conjunction with shared pointer.
23 * @author Reinhard Pfau
25 * @copyright © Copyright 2008 Intra2Net AG
29 #ifndef __I2N_POINTER_FUNC_HPP__
30 #define __I2N_POINTER_FUNC_HPP__
32 #include <boost/shared_ptr.hpp>
33 #include <boost/enable_shared_from_this.hpp>
40 * @brief base class for classes used as type with shared pointers.
42 * Must be virtually inherited by (base) classes which are used in
43 * conjunction with shared pointers.
45 * Allows these classes (and its derived classes) to protect their methods
46 * against preliminary deletion by giving them the possibility to obtain
47 * a shared pointer to themself.
50 : public boost::enable_shared_from_this< SharedBase >
53 typedef boost::shared_ptr< SharedBase > BasePtrType;
54 typedef boost::shared_ptr< SharedBase > PtrType;
58 virtual ~SharedBase();
62 * @brief gets a shared pointer to itself if applicable.
63 * @return the shared pointer (to the base class); empty if not applicable.
65 * The method catches the @a boost::bad_weak_ptr exception and ignores it.
66 * So this method doesn't throw if the instance is not held by a shared pointer.
68 BasePtrType get_base_ptr()
72 result = shared_from_this();
74 catch (boost::bad_weak_ptr)
82 * @brief gets a shared pointer to itself with a specific (derived) type.
83 * @tparam T the desired type for the shared pointer.
84 * @return the shared pointer (to the desired type); empty if not applicable.
86 * The method catches the @a boost::bad_weak_ptr exception and ignores it.
87 * So this method doesn't throw if the instance is not held by a shared pointer.
90 boost::shared_ptr< T > get_ptr_as()
92 boost::shared_ptr< T > result;
95 ptr = shared_from_this();
97 catch (boost::bad_weak_ptr)
102 result= boost::dynamic_pointer_cast< T >(ptr);
110 typedef SharedBase::PtrType SharedBasePtr;
112 } // eo namespace I2n