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 "cachemapobject1.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::Object1;
39 using ucb::cachemap::ObjectContainer1;
40 
41 inline
Object1(rtl::Reference<ObjectContainer1> const & rContainer)42 Object1::Object1(rtl::Reference< ObjectContainer1 > const & rContainer):
43     m_xContainer(rContainer),
44     m_nRefCount(0)
45 {
46     OSL_ASSERT(m_xContainer.is());
47 }
48 
~Object1()49 inline Object1::~Object1() SAL_THROW(())
50 {}
51 
releaseElement(Object1 * pElement)52 void ObjectContainer1::releaseElement(Object1 * pElement) SAL_THROW(())
53 {
54     OSL_ASSERT(pElement);
55     bool bDelete = false;
56     {
57         osl::MutexGuard aGuard(m_aMutex);
58         if (osl_decrementInterlockedCount(&pElement->m_nRefCount) == 0)
59         {
60             m_aMap.erase(pElement->m_aContainerIt);
61             bDelete = true;
62         }
63     }
64     if (bDelete)
65         delete pElement;
66 }
67 
ObjectContainer1()68 ObjectContainer1::ObjectContainer1()
69 {}
70 
~ObjectContainer1()71 ObjectContainer1::~ObjectContainer1() SAL_THROW(())
72 {}
73 
get(rtl::OUString const & rKey)74 rtl::Reference< Object1 > ObjectContainer1::get(rtl::OUString const & rKey)
75 {
76     osl::MutexGuard aGuard(m_aMutex);
77     Map::iterator aIt(m_aMap.find(rKey));
78     if (aIt == m_aMap.end())
79     {
80         std::auto_ptr< Object1 > xElement(new Object1(this));
81         aIt = m_aMap.insert(Map::value_type(rKey, xElement.get())).first;
82         aIt->second->m_aContainerIt = aIt;
83         xElement.release();
84     }
85     return aIt->second;
86 }
87