00276c879a6179ed0d06ec29207ab0340a8b9054
[libasyncio] / utils / asyncio_pointer_func.hpp
1 /**
2  * @file
3  * @brief some functions to deal with (smart) pointer
4  *
5  * @author Reinhard Pfau \<Reinhard.Pfau@gmx.de\>
6  * @copyright &copy; Copyright 2009 by Intra2net AG
7  * @license LGPL
8  * @contact opensource@intra2net.com
9  *
10  */
11
12 #ifndef _ASYNCIO_POINTER_FUNC_HPP_
13 #define _ASYNCIO_POINTER_FUNC_HPP_
14
15 #include <asyncio_config.hpp>
16 #include <boost/shared_ptr.hpp>
17 #include <boost/enable_shared_from_this.hpp>
18 #ifdef HAVE_LIBI2NCOMMON
19 #  include <pointer_func.hpp>
20 #endif
21
22 namespace AsyncIo
23 {
24 namespace Utils
25 {
26
27 #ifdef HAVE_LIBI2NCOMMON
28
29 // NOTE: this typedef is needed by I2N code; please keep it!
30
31 typedef I2n::SharedBase SharedBase
32   
33 #else
34
35 class SharedBase
36 : public boost::enable_shared_from_this< SharedBase >
37 {
38   public:
39     typedef boost::shared_ptr< SharedBase > PtrType;
40
41   public:
42     SharedBase() {}
43     virtual ~SharedBase() {}
44
45     template< class D >
46     boost::shared_ptr< D > get_ptr_as()
47     {
48       try
49       {
50         return boost::shared_dynamic_cast< D >( shared_from_this() );
51       }
52       catch( boost::bad_weak_ptr& e )
53       {
54         // we ignore this error.
55       }
56       return boost::shared_ptr< D >();
57     }
58     
59 }; // end of SharedBase
60
61 #endif
62
63
64 } // end of namespace Utils
65 } // end of namespace AsyncIo
66
67
68 #endif