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