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 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_ucb.hxx"
26 #include "cachemapobject3.hxx"
27 #include "osl/diagnose.h"
28 #include "osl/interlck.h"
29 #include "osl/mutex.hxx"
30 #include "rtl/ref.hxx"
31 #include "rtl/ustring.hxx"
32 
33 #ifndef INCLUDED_MEMORY
34 #include <memory>
35 #define INCLUDED_MEMORY
36 #endif
37 
38 using ucb::cachemap::Object3;
39 using ucb::cachemap::ObjectContainer3;
40 
41 inline
Object3(rtl::Reference<ObjectContainer3> const & rContainer)42 Object3::Object3(rtl::Reference< ObjectContainer3 > const & rContainer):
43     m_xContainer(rContainer),
44     m_nRefCount(0)
45 {
46     OSL_ASSERT(m_xContainer.is());
47 }
48 
~Object3()49 inline Object3::~Object3() SAL_THROW(())
50 {}
51 
release()52 void Object3::release() SAL_THROW(())
53 {
54     if (osl_decrementInterlockedCount(&m_nRefCount) == 0)
55     {
56         m_xContainer->releaseElement(this);
57         delete this;
58     }
59 }
60 
releaseElement(Object3 * pElement)61 void ObjectContainer3::releaseElement(Object3 * pElement) SAL_THROW(())
62 {
63     OSL_ASSERT(pElement);
64     osl::MutexGuard aGuard(m_aMutex);
65     if (pElement->m_aContainerIt != m_aMap.end())
66         m_aMap.erase(pElement->m_aContainerIt);
67 }
68 
ObjectContainer3()69 ObjectContainer3::ObjectContainer3()
70 {}
71 
~ObjectContainer3()72 ObjectContainer3::~ObjectContainer3() SAL_THROW(())
73 {}
74 
get(rtl::OUString const & rKey)75 rtl::Reference< Object3 > ObjectContainer3::get(rtl::OUString const & rKey)
76 {
77     osl::MutexGuard aGuard(m_aMutex);
78     Map::iterator aIt(m_aMap.find(rKey));
79     if (aIt == m_aMap.end())
80     {
81         std::auto_ptr< Object3 > xElement(new Object3(this));
82         aIt = m_aMap.insert(Map::value_type(rKey, xElement.get())).first;
83         aIt->second->m_aContainerIt = aIt;
84         xElement.release();
85         return aIt->second;
86     }
87     else if (osl_incrementInterlockedCount(&aIt->second->m_nRefCount) > 1)
88     {
89         rtl::Reference< Object3 > xElement(aIt->second);
90         osl_decrementInterlockedCount(&aIt->second->m_nRefCount);
91         return xElement;
92     }
93     else
94     {
95         osl_decrementInterlockedCount(&aIt->second->m_nRefCount);
96         aIt->second->m_aContainerIt = m_aMap.end();
97         aIt->second = new Object3(this);
98         aIt->second->m_aContainerIt = aIt;
99         return aIt->second;
100     }
101 }
102