Migrate libasyncio from boost.signal to signals2 (#8756)
[libasyncio] / utils / asyncio_pointer_func.hpp
1 /*
2 The software in this package is distributed under the GNU General
3 Public License version 2 (with a special exception described below).
4
5 A copy of GNU General Public License (GPL) is included in this distribution,
6 in the file COPYING.GPL.
7
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.
13
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.
16
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.
19 */
20 /**
21  * @file
22  * @brief some functions to deal with (smart) pointer
23  *
24  * @author Reinhard Pfau \<Reinhard.Pfau@gmx.de\>
25  * @copyright &copy; Copyright 2009 by Intra2net AG
26  */
27
28 #ifndef _ASYNCIO_POINTER_FUNC_HPP_
29 #define _ASYNCIO_POINTER_FUNC_HPP_
30
31 #include <asyncio_config.hpp>
32 #include <boost/shared_ptr.hpp>
33 #include <boost/enable_shared_from_this.hpp>
34 #ifdef HAVE_LIBI2NCOMMON
35 #  include <pointer_func.hpp>
36 #endif
37
38 namespace AsyncIo
39 {
40 namespace Utils
41 {
42
43 #ifdef HAVE_LIBI2NCOMMON
44
45 // NOTE: this typedef is needed by I2N code; please keep it!
46
47 typedef I2n::SharedBase SharedBase;
48
49 #else
50
51 class SharedBase
52 : public boost::enable_shared_from_this< SharedBase >
53 {
54   public:
55     typedef boost::shared_ptr< SharedBase > PtrType;
56
57   public:
58     SharedBase() {}
59     virtual ~SharedBase() {}
60
61     template< class D >
62     boost::shared_ptr< D > get_ptr_as()
63     {
64       try
65       {
66         return boost::dynamic_pointer_cast< D >( shared_from_this() );
67       }
68       catch( boost::bad_weak_ptr& e )
69       {
70         // we ignore this error.
71       }
72       return boost::shared_ptr< D >();
73     }
74     
75 }; // end of SharedBase
76
77 #endif
78
79
80 } // end of namespace Utils
81 } // end of namespace AsyncIo
82
83
84 #endif