merged tag ooo/DEV300_m102
[LibreOffice.git] / cppu / source / UnsafeBridge / UnsafeBridge.cxx
blob5a531de47182be6de622644c4864bd2a2133a54e
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_cppu.hxx"
31 #include "osl/mutex.hxx"
32 #include "osl/thread.h"
34 #include "cppu/helper/purpenv/Environment.hxx"
35 #include "cppu/helper/purpenv/Mapping.hxx"
38 #ifdef debug
39 # define LOG_LIFECYCLE_UnsafeBridge
40 #endif
42 #ifdef LOG_LIFECYCLE_UnsafeBridge
43 # include <iostream>
44 # define LOG_LIFECYCLE_UnsafeBridge_emit(x) x
46 #else
47 # define LOG_LIFECYCLE_UnsafeBridge_emit(x)
49 #endif
52 class SAL_DLLPRIVATE UnsafeBridge : public cppu::Enterable
54 osl::Mutex m_mutex;
55 sal_Int32 m_count;
56 oslThreadIdentifier m_threadId;
58 virtual ~UnsafeBridge(void);
60 public:
61 explicit UnsafeBridge(void);
63 virtual void v_callInto_v(uno_EnvCallee * pCallee, va_list * pParam);
64 virtual void v_callOut_v (uno_EnvCallee * pCallee, va_list * pParam);
66 virtual void v_enter(void);
67 virtual void v_leave(void);
69 virtual int v_isValid(rtl::OUString * pReason);
72 UnsafeBridge::UnsafeBridge(void)
73 : m_count (0),
74 m_threadId(0)
76 LOG_LIFECYCLE_UnsafeBridge_emit(fprintf(stderr, "LIFE: %s -> %p\n", "UnsafeBridge::UnsafeBridge(uno_Environment * pEnv)", this));
79 UnsafeBridge::~UnsafeBridge(void)
81 LOG_LIFECYCLE_UnsafeBridge_emit(fprintf(stderr, "LIFE: %s -> %p\n", "UnsafeBridge::~UnsafeBridge(void)", this));
83 OSL_ASSERT(m_count >= 0);
86 void UnsafeBridge::v_callInto_v(uno_EnvCallee * pCallee, va_list * pParam)
88 enter();
89 pCallee(pParam);
90 leave();
93 void UnsafeBridge::v_callOut_v(uno_EnvCallee * pCallee, va_list * pParam)
95 OSL_ASSERT(m_count > 0);
97 -- m_count;
98 pCallee(pParam);
99 ++ m_count;
101 if (!m_threadId)
102 m_threadId = osl_getThreadIdentifier(NULL);
105 void UnsafeBridge::v_enter(void)
107 m_mutex.acquire();
109 OSL_ASSERT(m_count >= 0);
111 if (m_count == 0)
112 m_threadId = osl_getThreadIdentifier(NULL);
114 ++ m_count;
117 void UnsafeBridge::v_leave(void)
119 OSL_ASSERT(m_count > 0);
121 -- m_count;
122 if (!m_count)
123 m_threadId = 0;
126 m_mutex.release();
129 int UnsafeBridge::v_isValid(rtl::OUString * pReason)
131 int result = 1;
133 result = m_count > 0;
134 if (!result)
135 *pReason = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("not entered"));
137 else
139 result = m_threadId == osl_getThreadIdentifier(NULL);
141 if (!result)
142 *pReason = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("wrong thread"));
145 if (result)
146 *pReason = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("OK"));
148 return result;
151 extern "C" void SAL_DLLPUBLIC_EXPORT SAL_CALL uno_initEnvironment(uno_Environment * pEnv)
152 SAL_THROW_EXTERN_C()
154 cppu::helper::purpenv::Environment_initWithEnterable(pEnv, new UnsafeBridge());
157 extern "C" void SAL_DLLPUBLIC_EXPORT SAL_CALL uno_ext_getMapping(uno_Mapping ** ppMapping,
158 uno_Environment * pFrom,
159 uno_Environment * pTo )
161 cppu::helper::purpenv::createMapping(ppMapping, pFrom, pTo);