Bump for 4.0-11
[LibreOffice.git] / cppu / source / UnsafeBridge / UnsafeBridge.cxx
blob96a531068123300e2ac3155973077f88f6c214c6
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include "osl/mutex.hxx"
22 #include "osl/thread.h"
24 #include "cppu/helper/purpenv/Environment.hxx"
25 #include "cppu/helper/purpenv/Mapping.hxx"
28 #ifdef debug
29 # define LOG_LIFECYCLE_UnsafeBridge
30 #endif
32 #ifdef LOG_LIFECYCLE_UnsafeBridge
33 # include <iostream>
34 # define LOG_LIFECYCLE_UnsafeBridge_emit(x) x
36 #else
37 # define LOG_LIFECYCLE_UnsafeBridge_emit(x)
39 #endif
42 class SAL_DLLPRIVATE UnsafeBridge : public cppu::Enterable
44 osl::Mutex m_mutex;
45 sal_Int32 m_count;
46 oslThreadIdentifier m_threadId;
48 virtual ~UnsafeBridge(void);
50 public:
51 explicit UnsafeBridge(void);
53 virtual void v_callInto_v(uno_EnvCallee * pCallee, va_list * pParam);
54 virtual void v_callOut_v (uno_EnvCallee * pCallee, va_list * pParam);
56 virtual void v_enter(void);
57 virtual void v_leave(void);
59 virtual int v_isValid(rtl::OUString * pReason);
62 UnsafeBridge::UnsafeBridge(void)
63 : m_count (0),
64 m_threadId(0)
66 LOG_LIFECYCLE_UnsafeBridge_emit(fprintf(stderr, "LIFE: %s -> %p\n", "UnsafeBridge::UnsafeBridge(uno_Environment * pEnv)", this));
69 UnsafeBridge::~UnsafeBridge(void)
71 LOG_LIFECYCLE_UnsafeBridge_emit(fprintf(stderr, "LIFE: %s -> %p\n", "UnsafeBridge::~UnsafeBridge(void)", this));
73 OSL_ASSERT(m_count >= 0);
76 void UnsafeBridge::v_callInto_v(uno_EnvCallee * pCallee, va_list * pParam)
78 enter();
79 pCallee(pParam);
80 leave();
83 void UnsafeBridge::v_callOut_v(uno_EnvCallee * pCallee, va_list * pParam)
85 OSL_ASSERT(m_count > 0);
87 -- m_count;
88 pCallee(pParam);
89 ++ m_count;
91 if (!m_threadId)
92 m_threadId = osl_getThreadIdentifier(NULL);
95 void UnsafeBridge::v_enter(void)
97 m_mutex.acquire();
99 OSL_ASSERT(m_count >= 0);
101 if (m_count == 0)
102 m_threadId = osl_getThreadIdentifier(NULL);
104 ++ m_count;
107 void UnsafeBridge::v_leave(void)
109 OSL_ASSERT(m_count > 0);
111 -- m_count;
112 if (!m_count)
113 m_threadId = 0;
116 m_mutex.release();
119 int UnsafeBridge::v_isValid(rtl::OUString * pReason)
121 int result = 1;
123 result = m_count > 0;
124 if (!result)
125 *pReason = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("not entered"));
127 else
129 result = m_threadId == osl_getThreadIdentifier(NULL);
131 if (!result)
132 *pReason = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("wrong thread"));
135 if (result)
136 *pReason = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("OK"));
138 return result;
141 extern "C" void SAL_DLLPUBLIC_EXPORT SAL_CALL uno_initEnvironment(uno_Environment * pEnv)
142 SAL_THROW_EXTERN_C()
144 cppu::helper::purpenv::Environment_initWithEnterable(pEnv, new UnsafeBridge());
147 extern "C" void SAL_DLLPUBLIC_EXPORT SAL_CALL uno_ext_getMapping(uno_Mapping ** ppMapping,
148 uno_Environment * pFrom,
149 uno_Environment * pTo )
151 cppu::helper::purpenv::createMapping(ppMapping, pFrom, pTo);
154 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */