update dev300-m58
[ooovba.git] / cppu / test / cascade_mapping / TestMapping.cxx
blob27a1b9db431e5950c6422a8436a390c181c9fa63
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: TestMapping.cxx,v $
10 * $Revision: 1.3 $
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 #include "osl/interlck.h"
32 #include "uno/dispatcher.h"
33 #include "uno/mapping.hxx"
35 #include "TestProxy.hxx"
38 #ifdef LOG_LIFECYCLE
39 #define LOG_LIFECYCLE_TestMapping
40 #endif
42 #define LOG_LIFECYCLE_TestMapping
43 #ifdef LOG_LIFECYCLE_TestMapping
44 # include <iostream>
45 # define LOG_LIFECYCLE_TestMapping_emit(x) x
47 #else
48 # define LOG_LIFECYCLE_TestMapping_emit(x)
50 #endif
53 class SAL_DLLPRIVATE TestMapping : public uno_Mapping
55 private:
56 oslInterlockedCount m_nCount;
57 uno_ExtEnvironment * m_pFrom;
58 uno_ExtEnvironment * m_pTo;
60 public:
61 explicit TestMapping(uno_Environment * pFrom, uno_Environment * pTo);
62 ~TestMapping(void);
64 void acquire() SAL_THROW(());
65 void release() SAL_THROW(());
67 void SAL_CALL mapInterface(uno_Interface ** ppOut,
68 uno_Interface * pUnoI,
69 typelib_InterfaceTypeDescription * pTypeDescr
71 SAL_THROW_EXTERN_C();
75 extern "C" {
76 static void SAL_CALL s_mapInterface(
77 uno_Mapping * pMapping,
78 void ** ppOut,
79 void * pUnoI,
80 typelib_InterfaceTypeDescription * pTypeDescr )
81 SAL_THROW_EXTERN_C()
83 TestMapping * pTestMapping = static_cast<TestMapping *>(pMapping);
84 pTestMapping->mapInterface((uno_Interface **)ppOut, (uno_Interface *)pUnoI, pTypeDescr);
88 static void SAL_CALL s_acquire(uno_Mapping * pMapping) SAL_THROW_EXTERN_C()
90 TestMapping * pTestMapping = static_cast<TestMapping *>(pMapping);
91 pTestMapping->acquire();
95 static void SAL_CALL s_release(uno_Mapping * pMapping) SAL_THROW_EXTERN_C()
97 TestMapping * pTestMapping = static_cast<TestMapping * >(pMapping);
98 pTestMapping->release();
101 static void SAL_CALL s_free(uno_Mapping * pMapping) SAL_THROW_EXTERN_C()
103 TestMapping * pTestMapping = static_cast<TestMapping * >(pMapping);
104 delete pTestMapping;
108 TestMapping::TestMapping(uno_Environment * pFrom, uno_Environment * pTo)
109 SAL_THROW( () )
110 : m_nCount(1),
111 m_pFrom(reinterpret_cast<uno_ExtEnvironment *>(pFrom)),
112 m_pTo (reinterpret_cast<uno_ExtEnvironment *>(pTo))
114 LOG_LIFECYCLE_TestMapping_emit(fprintf(stderr, "LIFE: %s -> %p\n", "TestMapping::TestMapping", this));
116 m_pFrom->aBase.acquire(&m_pFrom->aBase);
117 m_pTo ->aBase.acquire(&m_pTo ->aBase);
119 uno_Mapping::acquire = s_acquire;
120 uno_Mapping::release = s_release;
121 uno_Mapping::mapInterface = s_mapInterface;
124 TestMapping::~TestMapping(void)
126 LOG_LIFECYCLE_TestMapping_emit(fprintf(stderr, "LIFE: %s -> %p\n", "TestMapping::~TestMapping", this));
128 m_pFrom->aBase.release(&m_pFrom->aBase);
129 m_pTo ->aBase.release(&m_pTo ->aBase);
133 void TestMapping::acquire() SAL_THROW(())
135 if (osl_incrementInterlockedCount(&m_nCount) == 1)
137 uno_Mapping * pMapping = this;
139 ::uno_registerMapping(&pMapping, s_free, &m_pFrom->aBase, &m_pTo->aBase, NULL);
143 void TestMapping::release() SAL_THROW(())
145 if (osl_decrementInterlockedCount(&m_nCount) == 0)
146 ::uno_revokeMapping(this);
150 void SAL_CALL TestMapping::mapInterface(
151 uno_Interface ** ppOut,
152 uno_Interface * pUnoI,
153 typelib_InterfaceTypeDescription * pTypeDescr )
154 SAL_THROW_EXTERN_C()
156 // get object id of uno interface to be wrapped
157 rtl_uString * pOId = 0;
158 m_pFrom->getObjectIdentifier(m_pFrom, &pOId, pUnoI);
160 OSL_ASSERT(pOId);
162 if (*ppOut)
164 (*ppOut)->release(*ppOut);
165 *ppOut = 0;
168 // try to get any known interface from target environment
169 m_pTo->getRegisteredInterface(m_pTo, (void **)ppOut, pOId, pTypeDescr);
170 if (!*ppOut) // not yet there, register new proxy interface
172 // try to publish a new proxy (ref count initially 1)
173 TestProxy * pTestProxy = new TestProxy(pUnoI, pOId, pTypeDescr, m_pTo, m_pFrom);
175 // proxy may be exchanged during registration
176 m_pTo->registerProxyInterface(m_pTo,(void **)&pTestProxy, TestProxy_free, pOId, pTypeDescr);
178 *ppOut = pTestProxy;
181 rtl_uString_release(pOId);
185 extern "C" void SAL_DLLPUBLIC_EXPORT SAL_CALL uno_initEnvironment(uno_Environment * /*pEnv*/)
186 SAL_THROW_EXTERN_C()
190 extern "C" void uno_ext_getMapping(uno_Mapping ** ppMapping,
191 uno_Environment * pFrom,
192 uno_Environment * pTo )
194 *ppMapping = new TestMapping(pFrom, pTo);
196 ::uno_registerMapping(ppMapping, s_free, pFrom, pTo, NULL);