1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_ucb.hxx"
30 #include "cachemapobject3.hxx"
31 #include "osl/diagnose.h"
32 #include "osl/interlck.h"
33 #include "osl/mutex.hxx"
34 #include "rtl/ref.hxx"
35 #include "rtl/ustring.hxx"
36 
37 #ifndef INCLUDED_MEMORY
38 #include <memory>
39 #define INCLUDED_MEMORY
40 #endif
41 
42 using ucb::cachemap::Object3;
43 using ucb::cachemap::ObjectContainer3;
44 
45 inline
46 Object3::Object3(rtl::Reference< ObjectContainer3 > const & rContainer):
47     m_xContainer(rContainer),
48     m_nRefCount(0)
49 {
50     OSL_ASSERT(m_xContainer.is());
51 }
52 
53 inline Object3::~Object3() SAL_THROW(())
54 {}
55 
56 void Object3::release() SAL_THROW(())
57 {
58     if (osl_decrementInterlockedCount(&m_nRefCount) == 0)
59     {
60         m_xContainer->releaseElement(this);
61         delete this;
62     }
63 }
64 
65 void ObjectContainer3::releaseElement(Object3 * pElement) SAL_THROW(())
66 {
67     OSL_ASSERT(pElement);
68     osl::MutexGuard aGuard(m_aMutex);
69     if (pElement->m_aContainerIt != m_aMap.end())
70         m_aMap.erase(pElement->m_aContainerIt);
71 }
72 
73 ObjectContainer3::ObjectContainer3()
74 {}
75 
76 ObjectContainer3::~ObjectContainer3() SAL_THROW(())
77 {}
78 
79 rtl::Reference< Object3 > ObjectContainer3::get(rtl::OUString const & rKey)
80 {
81     osl::MutexGuard aGuard(m_aMutex);
82     Map::iterator aIt(m_aMap.find(rKey));
83     if (aIt == m_aMap.end())
84     {
85         std::auto_ptr< Object3 > xElement(new Object3(this));
86         aIt = m_aMap.insert(Map::value_type(rKey, xElement.get())).first;
87         aIt->second->m_aContainerIt = aIt;
88         xElement.release();
89         return aIt->second;
90     }
91     else if (osl_incrementInterlockedCount(&aIt->second->m_nRefCount) > 1)
92     {
93         rtl::Reference< Object3 > xElement(aIt->second);
94         osl_decrementInterlockedCount(&aIt->second->m_nRefCount);
95         return xElement;
96     }
97     else
98     {
99         osl_decrementInterlockedCount(&aIt->second->m_nRefCount);
100         aIt->second->m_aContainerIt = m_aMap.end();
101         aIt->second = new Object3(this);
102         aIt->second->m_aContainerIt = aIt;
103         return aIt->second;
104     }
105 }
106