update dev300-m58
[ooovba.git] / cppu / source / UnsafeBridge / UnsafeBridge.cxx
blob02cb73fc8bad1753a43fc47966a80dd89ff770f8
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: UnsafeBridge.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_cppu.hxx"
34 #include "osl/mutex.hxx"
35 #include "osl/thread.h"
37 #include "cppu/helper/purpenv/Environment.hxx"
38 #include "cppu/helper/purpenv/Mapping.hxx"
41 #ifdef debug
42 # define LOG_LIFECYCLE_UnsafeBridge
43 #endif
45 #ifdef LOG_LIFECYCLE_UnsafeBridge
46 # include <iostream>
47 # define LOG_LIFECYCLE_UnsafeBridge_emit(x) x
49 #else
50 # define LOG_LIFECYCLE_UnsafeBridge_emit(x)
52 #endif
55 class SAL_DLLPRIVATE UnsafeBridge : public cppu::Enterable
57 osl::Mutex m_mutex;
58 sal_Int32 m_count;
59 oslThreadIdentifier m_threadId;
61 virtual ~UnsafeBridge(void);
63 public:
64 explicit UnsafeBridge(void);
66 virtual void v_callInto_v(uno_EnvCallee * pCallee, va_list * pParam);
67 virtual void v_callOut_v (uno_EnvCallee * pCallee, va_list * pParam);
69 virtual void v_enter(void);
70 virtual void v_leave(void);
72 virtual int v_isValid(rtl::OUString * pReason);
75 UnsafeBridge::UnsafeBridge(void)
76 : m_count (0),
77 m_threadId(0)
79 LOG_LIFECYCLE_UnsafeBridge_emit(fprintf(stderr, "LIFE: %s -> %p\n", "UnsafeBridge::UnsafeBridge(uno_Environment * pEnv)", this));
82 UnsafeBridge::~UnsafeBridge(void)
84 LOG_LIFECYCLE_UnsafeBridge_emit(fprintf(stderr, "LIFE: %s -> %p\n", "UnsafeBridge::~UnsafeBridge(void)", this));
86 OSL_ASSERT(m_count >= 0);
89 void UnsafeBridge::v_callInto_v(uno_EnvCallee * pCallee, va_list * pParam)
91 enter();
92 pCallee(pParam);
93 leave();
96 void UnsafeBridge::v_callOut_v(uno_EnvCallee * pCallee, va_list * pParam)
98 OSL_ASSERT(m_count > 0);
100 -- m_count;
101 pCallee(pParam);
102 ++ m_count;
104 if (!m_threadId)
105 m_threadId = osl_getThreadIdentifier(NULL);
108 void UnsafeBridge::v_enter(void)
110 m_mutex.acquire();
112 OSL_ASSERT(m_count >= 0);
114 if (m_count == 0)
115 m_threadId = osl_getThreadIdentifier(NULL);
117 ++ m_count;
120 void UnsafeBridge::v_leave(void)
122 OSL_ASSERT(m_count > 0);
124 -- m_count;
125 if (!m_count)
126 m_threadId = 0;
129 m_mutex.release();
132 int UnsafeBridge::v_isValid(rtl::OUString * pReason)
134 int result = 1;
136 result = m_count > 0;
137 if (!result)
138 *pReason = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("not entered"));
140 else
142 result = m_threadId == osl_getThreadIdentifier(NULL);
144 if (!result)
145 *pReason = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("wrong thread"));
148 if (result)
149 *pReason = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("OK"));
151 return result;
154 extern "C" void SAL_DLLPUBLIC_EXPORT SAL_CALL uno_initEnvironment(uno_Environment * pEnv)
155 SAL_THROW_EXTERN_C()
157 cppu::helper::purpenv::Environment_initWithEnterable(pEnv, new UnsafeBridge());
160 extern "C" void SAL_DLLPUBLIC_EXPORT SAL_CALL uno_ext_getMapping(uno_Mapping ** ppMapping,
161 uno_Environment * pFrom,
162 uno_Environment * pTo )
164 cppu::helper::purpenv::createMapping(ppMapping, pFrom, pTo);