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 24 #ifndef _NULLCANVAS_USAGECOUNTER_HXX 25 #define _NULLCANVAS_USAGECOUNTER_HXX 26 27 #include <osl/interlck.h> 28 #include <boost/current_function.hpp> 29 30 namespace nullcanvas 31 { 32 /** Little resource tracking counter. 33 34 When using this object, a global use counter, specific to the 35 given type is incremented on object construction, and 36 decremented on object destruction. 37 */ 38 template< class Type > class UsageCounter 39 { 40 public: UsageCounter()41 UsageCounter() 42 { 43 OSL_TRACE( "%s, %d objects currently in use.\n", 44 BOOST_CURRENT_FUNCTION, 45 osl_incrementInterlockedCount( &s_nCount ) ); 46 } 47 ~UsageCounter()48 ~UsageCounter() 49 { 50 const sal_Int32 nCount( osl_decrementInterlockedCount( &s_nCount ) ); 51 52 if( !nCount ) 53 { 54 OSL_TRACE( "%s, last instance deleted.\n", 55 BOOST_CURRENT_FUNCTION ); 56 } 57 else 58 { 59 OSL_TRACE( "%s, %d instances left.\n", 60 BOOST_CURRENT_FUNCTION, 61 nCount ); 62 } 63 } 64 65 private: 66 static oslInterlockedCount s_nCount; 67 }; 68 69 template< class Type > oslInterlockedCount UsageCounter<Type>::s_nCount = 0; 70 } 71 72 #endif /* _NULLCANVAS_USAGECOUNTER_HXX */ 73