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 #ifndef CHART2_DISPOSEHELPER_HXX 24 #define CHART2_DISPOSEHELPER_HXX 25 26 #include <com/sun/star/uno/Reference.hxx> 27 #include <com/sun/star/lang/XComponent.hpp> 28 29 #include <functional> 30 #include <algorithm> 31 #include <utility> 32 33 namespace chart 34 { 35 namespace DisposeHelper 36 { 37 38 template< class T > Dispose(const T & xIntf)39 void Dispose( const T & xIntf ) 40 { 41 ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent > xComp( 42 xIntf, ::com::sun::star::uno::UNO_QUERY ); 43 if( xComp.is()) 44 xComp->dispose(); 45 } 46 47 template< class Intf > DisposeAndClear(::com::sun::star::uno::Reference<Intf> & rIntf)48 void DisposeAndClear( ::com::sun::star::uno::Reference< Intf > & rIntf ) 49 { 50 Dispose< ::com::sun::star::uno::Reference< Intf > >( rIntf ); 51 rIntf.set( 0 ); 52 } 53 54 template< class T > 55 struct DisposeFunctor 56 { 57 typedef T argument_type; 58 typedef void result_type; operator ()chart::DisposeHelper::DisposeFunctor59 void operator() ( const T & xIntf ) 60 { 61 Dispose< T >( xIntf ); 62 } 63 }; 64 65 template< typename T > 66 struct DisposeFirstOfPairFunctor 67 { 68 typedef T argument_type; 69 typedef void result_type; operator ()chart::DisposeHelper::DisposeFirstOfPairFunctor70 void operator() ( const T & rElem ) 71 { 72 Dispose< typename T::first_type >( rElem.first ); 73 } 74 }; 75 76 template< typename T > 77 struct DisposeSecondOfPairFunctor 78 { 79 typedef T argument_type; 80 typedef void result_type; operator ()chart::DisposeHelper::DisposeSecondOfPairFunctor81 void operator() ( const T & rElem ) 82 { 83 Dispose< typename T::second_type >( rElem.second ); 84 } 85 }; 86 87 template< class Container > DisposeAllElements(Container & rContainer)88 void DisposeAllElements( Container & rContainer ) 89 { 90 ::std::for_each( rContainer.begin(), rContainer.end(), DisposeFunctor< typename Container::value_type >()); 91 } 92 93 template< class Map > DisposeAllMapElements(Map & rContainer)94 void DisposeAllMapElements( Map & rContainer ) 95 { 96 ::std::for_each( rContainer.begin(), rContainer.end(), DisposeSecondOfPairFunctor< typename Map::value_type >()); 97 } 98 99 } // namespace DisposeHelper 100 } // namespace chart 101 102 // CHART2_DISPOSEHELPER_HXX 103 #endif 104