xref: /AOO42X/main/comphelper/inc/comphelper/make_shared_from_uno.hxx (revision 9bce9b0d387299c68bd81d539e1478357a103de5)
1*9877b273SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*9877b273SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*9877b273SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*9877b273SAndrew Rist  * distributed with this work for additional information
6*9877b273SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*9877b273SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*9877b273SAndrew Rist  * "License"); you may not use this file except in compliance
9*9877b273SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*9877b273SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*9877b273SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*9877b273SAndrew Rist  * software distributed under the License is distributed on an
15*9877b273SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*9877b273SAndrew Rist  * KIND, either express or implied.  See the License for the
17*9877b273SAndrew Rist  * specific language governing permissions and limitations
18*9877b273SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*9877b273SAndrew Rist  *************************************************************/
21*9877b273SAndrew Rist 
22*9877b273SAndrew Rist 
23cdf0e10cSrcweir #if ! defined(INCLUDED_COMPHELPER_MAKE_SHARED_FROM_UNO_HXX)
24cdf0e10cSrcweir #define INCLUDED_COMPHELPER_MAKE_SHARED_FROM_UNO_HXX
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include "boost/shared_ptr.hpp"
27cdf0e10cSrcweir #include <functional>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir namespace comphelper {
30cdf0e10cSrcweir 
31cdf0e10cSrcweir /// @internal
32cdf0e10cSrcweir namespace detail {
33cdf0e10cSrcweir /// @internal
34cdf0e10cSrcweir template <typename T> struct ReleaseFunc : ::std::unary_function<T *, void> {
operator ()comphelper::detail::ReleaseFunc35cdf0e10cSrcweir     void operator()( T * p ) const { p->release(); }
36cdf0e10cSrcweir };
37cdf0e10cSrcweir } // namespace detail
38cdf0e10cSrcweir 
39cdf0e10cSrcweir /** Makes a boost::shared_ptr from a ref-counted UNO object pointer.
40cdf0e10cSrcweir     This makes sense if the object is used via UNO (implementing some X
41cdf0e10cSrcweir     interface) and also internally using its implementation class, e.g.
42cdf0e10cSrcweir 
43cdf0e10cSrcweir     <pre>
44cdf0e10cSrcweir         boost::shared_ptr<MyUnoImpl> const ptr(
45cdf0e10cSrcweir             comphelper::make_shared_from_UNO( new MyUnoImpl ) );
46cdf0e10cSrcweir         ...
47cdf0e10cSrcweir         xUno->callingUno( uno::Reference<XSomeInterface>( ptr.get() ) );
48cdf0e10cSrcweir         ...
49cdf0e10cSrcweir         takeSharedPtr( ptr );
50cdf0e10cSrcweir         ...
51cdf0e10cSrcweir     </pre>
52cdf0e10cSrcweir 
53cdf0e10cSrcweir     @attention The shared_ptr operates on a separate reference counter, so
54cdf0e10cSrcweir                weak pointers (boost::weak_ptr) are invalidated when the last
55cdf0e10cSrcweir                shared_ptr is destroyed, although the UNO object may still be
56cdf0e10cSrcweir                alive.
57cdf0e10cSrcweir 
58cdf0e10cSrcweir     @param p object pointer
59cdf0e10cSrcweir     @return shared_ptr to object
60cdf0e10cSrcweir */
61cdf0e10cSrcweir template <typename T>
make_shared_from_UNO(T * p)62cdf0e10cSrcweir inline ::boost::shared_ptr<T> make_shared_from_UNO( T * p )
63cdf0e10cSrcweir {
64cdf0e10cSrcweir     p->acquire();
65cdf0e10cSrcweir     return ::boost::shared_ptr<T>( p, detail::ReleaseFunc<T>() );
66cdf0e10cSrcweir }
67cdf0e10cSrcweir 
68cdf0e10cSrcweir } // namespace comphelper
69cdf0e10cSrcweir 
70cdf0e10cSrcweir #endif // ! defined(INCLUDED_COMPHELPER_MAKE_SHARED_FROM_UNO_HXX)
71