change permissions before fsync()
[libi2ncommon] / src / pointer_func.hpp
CommitLineData
0e23f538
TJ
1/*
2The software in this package is distributed under the GNU General
3Public License version 2 (with a special exception described below).
4
5A copy of GNU General Public License (GPL) is included in this distribution,
6in the file COPYING.GPL.
7
8As a special exception, if other files instantiate templates or use macros
9or inline functions from this file, or you compile this file and link it
10with other works to produce a work based on this file, this file
11does not by itself cause the resulting work to be covered
12by the GNU General Public License.
13
14However the source code for this file must still be made available
15in accordance with section (3) of the GNU General Public License.
16
17This exception does not invalidate any other reasons why a work based
18on this file might be covered by the GNU General Public License.
19*/
e525f445
RP
20/** @file
21 * @brief provides a little base class for classes wich are used in conjunction with shared pointer.
22 *
0e23f538 23 * @author Reinhard Pfau
e525f445
RP
24 *
25 * @copyright © Copyright 2008 Intra2Net AG
e525f445
RP
26 *
27 */
28
29#ifndef __I2N_POINTER_FUNC_HPP__
30#define __I2N_POINTER_FUNC_HPP__
31
32#include <boost/shared_ptr.hpp>
33#include <boost/enable_shared_from_this.hpp>
34
35namespace I2n
36{
37
38
39/**
40 * @brief base class for classes used as type with shared pointers.
41 *
42 * Must be virtually inherited by (base) classes which are used in
43 * conjunction with shared pointers.
44 *
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.
48 */
49class SharedBase
50: public boost::enable_shared_from_this< SharedBase >
51{
52 public:
53 typedef boost::shared_ptr< SharedBase > BasePtrType;
54 typedef boost::shared_ptr< SharedBase > PtrType;
55
56 public:
57 SharedBase();
58 virtual ~SharedBase();
59
60
61 /**
62 * @brief gets a shared pointer to itself if applicable.
63 * @return the shared pointer (to the base class); empty if not applicable.
64 *
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.
67 */
68 BasePtrType get_base_ptr()
69 {
70 BasePtrType result;
71 try {
72 result = shared_from_this();
73 }
74 catch (boost::bad_weak_ptr)
75 {
76 }
77 return result;
78 } // eo get_base_ptr
79
80
81 /**
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.
85 *
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.
88 */
89 template< class T >
90 boost::shared_ptr< T > get_ptr_as()
91 {
92 boost::shared_ptr< T > result;
93 BasePtrType ptr;
94 try {
95 ptr = shared_from_this();
96 }
97 catch (boost::bad_weak_ptr)
98 {
99 }
100 if (ptr)
101 {
0a4178cf 102 result= boost::dynamic_pointer_cast< T >(ptr);
e525f445
RP
103 }
104 return result;
105 } // eo get_ptr_as
106
107
108}; // eo SharedBase
109
110typedef SharedBase::PtrType SharedBasePtr;
111
112} // eo namespace I2n
113
114#endif