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