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 #include "IdentityMapping.hxx"
29 
30 #include "uno/mapping.h"
31 #include "uno/environment.hxx"
32 
33 #include "osl/interlck.h"
34 
35 
36 using namespace ::com::sun::star;
37 
38 struct IdentityMapping : public uno_Mapping
39 {
40 	sal_Int32	     m_nRef;
41 	uno::Environment m_env;
42 
43 	IdentityMapping(uno::Environment const & rEnv);
44 };
45 
46 extern "C"
47 {
48 
49 static void SAL_CALL s_free(uno_Mapping * pMapping) SAL_THROW(())
50 {
51 	delete static_cast<IdentityMapping *>(pMapping);
52 }
53 
54 static void SAL_CALL s_acquire(uno_Mapping * pMapping) SAL_THROW(())
55 {
56 	static rtl::OUString s_purpose;
57 
58 	if (1 == ::osl_incrementInterlockedCount(&static_cast<IdentityMapping *>(pMapping)->m_nRef))
59 	{
60 		uno_registerMapping(
61 			&pMapping,
62 			s_free,
63 			static_cast<IdentityMapping *>(pMapping)->m_env.get(),
64 			static_cast<IdentityMapping *>(pMapping)->m_env.get(),
65 			s_purpose.pData);
66 	}
67 }
68 
69 static void SAL_CALL s_release(uno_Mapping * pMapping) SAL_THROW(())
70 {
71 	if (!::osl_decrementInterlockedCount(&static_cast<IdentityMapping *>(pMapping )->m_nRef))
72 		uno_revokeMapping(pMapping);
73 }
74 
75 static void SAL_CALL s_mapInterface(uno_Mapping                       * pMapping,
76 									void                             ** ppOut,
77 									void                              * pInterface,
78 									struct _typelib_InterfaceTypeDescription * /*pInterfaceTypeDescr*/)
79 	SAL_THROW(())
80 {
81 	*ppOut = pInterface;
82 
83 	if (pInterface)
84 	{
85 		IdentityMapping * that = static_cast<IdentityMapping *>(pMapping);
86 
87 		(that->m_env.get()->pExtEnv->acquireInterface)(that->m_env.get()->pExtEnv, pInterface);
88 	}
89 }
90 }
91 
92 
93 IdentityMapping::IdentityMapping(uno::Environment const & rEnv)
94 	: m_nRef(0),
95 	  m_env(rEnv)
96 {
97 	uno_Mapping::acquire		= s_acquire;
98 	uno_Mapping::release		= s_release;
99 	uno_Mapping::mapInterface	= s_mapInterface;
100 }
101 
102 
103 uno_Mapping * createIdentityMapping(uno::Environment const & rEnv) SAL_THROW(())
104 {
105 	return new IdentityMapping(rEnv);
106 }
107