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 "cachemapobject3.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::Object3;
39cdf0e10cSrcweir using ucb::cachemap::ObjectContainer3;
40cdf0e10cSrcweir 
41cdf0e10cSrcweir inline
Object3(rtl::Reference<ObjectContainer3> const & rContainer)42cdf0e10cSrcweir Object3::Object3(rtl::Reference< ObjectContainer3 > const & rContainer):
43cdf0e10cSrcweir     m_xContainer(rContainer),
44cdf0e10cSrcweir     m_nRefCount(0)
45cdf0e10cSrcweir {
46cdf0e10cSrcweir     OSL_ASSERT(m_xContainer.is());
47cdf0e10cSrcweir }
48cdf0e10cSrcweir 
~Object3()49cdf0e10cSrcweir inline Object3::~Object3() SAL_THROW(())
50cdf0e10cSrcweir {}
51cdf0e10cSrcweir 
release()52cdf0e10cSrcweir void Object3::release() SAL_THROW(())
53cdf0e10cSrcweir {
54cdf0e10cSrcweir     if (osl_decrementInterlockedCount(&m_nRefCount) == 0)
55cdf0e10cSrcweir     {
56cdf0e10cSrcweir         m_xContainer->releaseElement(this);
57cdf0e10cSrcweir         delete this;
58cdf0e10cSrcweir     }
59cdf0e10cSrcweir }
60cdf0e10cSrcweir 
releaseElement(Object3 * pElement)61cdf0e10cSrcweir void ObjectContainer3::releaseElement(Object3 * pElement) SAL_THROW(())
62cdf0e10cSrcweir {
63cdf0e10cSrcweir     OSL_ASSERT(pElement);
64cdf0e10cSrcweir     osl::MutexGuard aGuard(m_aMutex);
65cdf0e10cSrcweir     if (pElement->m_aContainerIt != m_aMap.end())
66cdf0e10cSrcweir         m_aMap.erase(pElement->m_aContainerIt);
67cdf0e10cSrcweir }
68cdf0e10cSrcweir 
ObjectContainer3()69cdf0e10cSrcweir ObjectContainer3::ObjectContainer3()
70cdf0e10cSrcweir {}
71cdf0e10cSrcweir 
~ObjectContainer3()72cdf0e10cSrcweir ObjectContainer3::~ObjectContainer3() SAL_THROW(())
73cdf0e10cSrcweir {}
74cdf0e10cSrcweir 
get(rtl::OUString const & rKey)75cdf0e10cSrcweir rtl::Reference< Object3 > ObjectContainer3::get(rtl::OUString const & rKey)
76cdf0e10cSrcweir {
77cdf0e10cSrcweir     osl::MutexGuard aGuard(m_aMutex);
78cdf0e10cSrcweir     Map::iterator aIt(m_aMap.find(rKey));
79cdf0e10cSrcweir     if (aIt == m_aMap.end())
80cdf0e10cSrcweir     {
81cdf0e10cSrcweir         std::auto_ptr< Object3 > xElement(new Object3(this));
82cdf0e10cSrcweir         aIt = m_aMap.insert(Map::value_type(rKey, xElement.get())).first;
83cdf0e10cSrcweir         aIt->second->m_aContainerIt = aIt;
84cdf0e10cSrcweir         xElement.release();
85cdf0e10cSrcweir         return aIt->second;
86cdf0e10cSrcweir     }
87cdf0e10cSrcweir     else if (osl_incrementInterlockedCount(&aIt->second->m_nRefCount) > 1)
88cdf0e10cSrcweir     {
89cdf0e10cSrcweir         rtl::Reference< Object3 > xElement(aIt->second);
90cdf0e10cSrcweir         osl_decrementInterlockedCount(&aIt->second->m_nRefCount);
91cdf0e10cSrcweir         return xElement;
92cdf0e10cSrcweir     }
93cdf0e10cSrcweir     else
94cdf0e10cSrcweir     {
95cdf0e10cSrcweir         osl_decrementInterlockedCount(&aIt->second->m_nRefCount);
96cdf0e10cSrcweir         aIt->second->m_aContainerIt = m_aMap.end();
97cdf0e10cSrcweir         aIt->second = new Object3(this);
98cdf0e10cSrcweir         aIt->second->m_aContainerIt = aIt;
99cdf0e10cSrcweir         return aIt->second;
100cdf0e10cSrcweir     }
101cdf0e10cSrcweir }
102