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