1*2f86921cSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*2f86921cSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*2f86921cSAndrew Rist * or more contributor license agreements. See the NOTICE file 5*2f86921cSAndrew Rist * distributed with this work for additional information 6*2f86921cSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*2f86921cSAndrew Rist * to you under the Apache License, Version 2.0 (the 8*2f86921cSAndrew Rist * "License"); you may not use this file except in compliance 9*2f86921cSAndrew Rist * with the License. You may obtain a copy of the License at 10*2f86921cSAndrew Rist * 11*2f86921cSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*2f86921cSAndrew Rist * 13*2f86921cSAndrew Rist * Unless required by applicable law or agreed to in writing, 14*2f86921cSAndrew Rist * software distributed under the License is distributed on an 15*2f86921cSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*2f86921cSAndrew Rist * KIND, either express or implied. See the License for the 17*2f86921cSAndrew Rist * specific language governing permissions and limitations 18*2f86921cSAndrew Rist * under the License. 19*2f86921cSAndrew Rist * 20*2f86921cSAndrew Rist *************************************************************/ 21*2f86921cSAndrew Rist 22*2f86921cSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_ucb.hxx" 26cdf0e10cSrcweir #include "cachemapobject1.hxx" 27cdf0e10cSrcweir #include "osl/diagnose.h" 28cdf0e10cSrcweir #include "osl/interlck.h" 29cdf0e10cSrcweir #include "osl/mutex.hxx" 30cdf0e10cSrcweir #include "rtl/ref.hxx" 31cdf0e10cSrcweir #include "rtl/ustring.hxx" 32cdf0e10cSrcweir 33cdf0e10cSrcweir #ifndef INCLUDED_MEMORY 34cdf0e10cSrcweir #include <memory> 35cdf0e10cSrcweir #define INCLUDED_MEMORY 36cdf0e10cSrcweir #endif 37cdf0e10cSrcweir 38cdf0e10cSrcweir using ucb::cachemap::Object1; 39cdf0e10cSrcweir using ucb::cachemap::ObjectContainer1; 40cdf0e10cSrcweir 41cdf0e10cSrcweir inline Object1(rtl::Reference<ObjectContainer1> const & rContainer)42cdf0e10cSrcweirObject1::Object1(rtl::Reference< ObjectContainer1 > const & rContainer): 43cdf0e10cSrcweir m_xContainer(rContainer), 44cdf0e10cSrcweir m_nRefCount(0) 45cdf0e10cSrcweir { 46cdf0e10cSrcweir OSL_ASSERT(m_xContainer.is()); 47cdf0e10cSrcweir } 48cdf0e10cSrcweir ~Object1()49cdf0e10cSrcweirinline Object1::~Object1() SAL_THROW(()) 50cdf0e10cSrcweir {} 51cdf0e10cSrcweir releaseElement(Object1 * pElement)52cdf0e10cSrcweirvoid ObjectContainer1::releaseElement(Object1 * pElement) SAL_THROW(()) 53cdf0e10cSrcweir { 54cdf0e10cSrcweir OSL_ASSERT(pElement); 55cdf0e10cSrcweir bool bDelete = false; 56cdf0e10cSrcweir { 57cdf0e10cSrcweir osl::MutexGuard aGuard(m_aMutex); 58cdf0e10cSrcweir if (osl_decrementInterlockedCount(&pElement->m_nRefCount) == 0) 59cdf0e10cSrcweir { 60cdf0e10cSrcweir m_aMap.erase(pElement->m_aContainerIt); 61cdf0e10cSrcweir bDelete = true; 62cdf0e10cSrcweir } 63cdf0e10cSrcweir } 64cdf0e10cSrcweir if (bDelete) 65cdf0e10cSrcweir delete pElement; 66cdf0e10cSrcweir } 67cdf0e10cSrcweir ObjectContainer1()68cdf0e10cSrcweirObjectContainer1::ObjectContainer1() 69cdf0e10cSrcweir {} 70cdf0e10cSrcweir ~ObjectContainer1()71cdf0e10cSrcweirObjectContainer1::~ObjectContainer1() SAL_THROW(()) 72cdf0e10cSrcweir {} 73cdf0e10cSrcweir get(rtl::OUString const & rKey)74cdf0e10cSrcweirrtl::Reference< Object1 > ObjectContainer1::get(rtl::OUString const & rKey) 75cdf0e10cSrcweir { 76cdf0e10cSrcweir osl::MutexGuard aGuard(m_aMutex); 77cdf0e10cSrcweir Map::iterator aIt(m_aMap.find(rKey)); 78cdf0e10cSrcweir if (aIt == m_aMap.end()) 79cdf0e10cSrcweir { 80cdf0e10cSrcweir std::auto_ptr< Object1 > xElement(new Object1(this)); 81cdf0e10cSrcweir aIt = m_aMap.insert(Map::value_type(rKey, xElement.get())).first; 82cdf0e10cSrcweir aIt->second->m_aContainerIt = aIt; 83cdf0e10cSrcweir xElement.release(); 84cdf0e10cSrcweir } 85cdf0e10cSrcweir return aIt->second; 86cdf0e10cSrcweir } 87