LibreOffice Module cli_ure (master) 1
cli_environment.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include "Cli_environment.h"
21
22#using <cli_ure.dll>
23#using <system.dll>
24#include "osl/diagnose.h"
25#include "cli_proxy.h"
26
27using namespace System::Runtime::Remoting;
28using namespace System::Runtime::Remoting::Proxies;
29using namespace System::Collections;
30using namespace System::Text;
31using namespace System::Diagnostics;
32using namespace System::Threading;
33
34namespace cli_uno
35{
36
37inline System::String^ Cli_environment::createKey(System::String^ oid, System::Type^ t)
38{
39 return System::String::Concat(oid, t->FullName);
40}
41
42
44{
45#if OSL_DEBUG_LEVEL >= 2
47#endif
48}
49
51{
52 this->!Cli_environment(); // call finalizer
53}
54
56{
57#if OSL_DEBUG_LEVEL >= 2
58 OSL_ENSURE(_numRegisteredObjects == 0,
59 "cli uno bridge: CLI environment contains unrevoked objects");
60#endif
61}
62
64 System::Object^ obj, System::String^ oid)
65{
66#if OSL_DEBUG_LEVEL >= 1
67 //obj must be a transparent proxy
68 OSL_ASSERT(RemotingServices::IsTransparentProxy(obj));
70#endif
71 OSL_ASSERT( ! m_objects->ContainsKey(oid));
72 m_objects->Add(oid, gcnew WeakReference(obj));
73 return obj;
74}
76 System::Object^ obj, System::String^ oid, System::Type^ type)
77{
78#if OSL_DEBUG_LEVEL >= 1
79 //obj must be a real cli object
80 OSL_ASSERT( ! RemotingServices::IsTransparentProxy(obj));
82#endif
83 System::String^ key = createKey(oid, type);
84 //see synchronization in map_uno2cli in cli_data.cxx
85 OSL_ASSERT( ! m_objects->ContainsKey(key));
86 m_objects->Add(key, gcnew WeakReference(obj));
87 return obj;
88}
89
90void Cli_environment::revokeInterface(System::String^ oid, System::Type^ type)
91{
92 System::String^ key = type != nullptr ? createKey(oid, type) : oid;
93#if OSL_DEBUG_LEVEL >= 1
95#endif
96#if OSL_DEBUG_LEVEL >= 2
97 int i = 1;
98 if (m_objects->ContainsKey(key) == false)
99 {
100 Trace::WriteLine("cli uno bridge: try to revoke unregistered interface");
101 Trace::WriteLine(oid);
102 i = 0;
103 }
104 Trace::WriteLine(System::String::Format(
105 gcnew System::String("cli uno bridge: {0} remaining registered interfaces"),
106 m_objects->Count - 1));
107#endif
108 m_objects->Remove(key);
109}
110
111inline void Cli_environment::revokeInterface(System::String^ oid)
112{
113 return revokeInterface(oid, nullptr);
114}
115
116System::Object^ Cli_environment::getRegisteredInterface(System::String^ oid,
117 System::Type^ type)
118{
119 //try if it is a UNO interface
120 System::Object^ ret = m_objects[oid];
121 if (! ret)
122 {
123 //try if it is a proxy for a cli object
124 oid = createKey(oid, type);
125 ret = m_objects[ oid ];
126 }
127 if (ret != nullptr)
128 {
129 System::WeakReference^ weakIface =
130 static_cast< System::WeakReference ^ >( ret );
131 ret = weakIface->Target;
132 }
133 if (ret == nullptr)
134 m_objects->Remove( oid );
135 return ret;
136}
137
138System::String^ Cli_environment::getObjectIdentifier(System::Object^ obj)
139{
140 System::String^ oId= nullptr;
141 RealProxy^ aProxy= RemotingServices::GetRealProxy(obj);
142 if (aProxy)
143 {
144 UnoInterfaceProxy^ proxyImpl= dynamic_cast<UnoInterfaceProxy^>(aProxy);
145 if (proxyImpl)
146 oId= proxyImpl->getOid();
147 }
148
149 if (oId == nullptr)
150 {
151 StringBuilder ^ buf= gcnew StringBuilder(256);
152 bool bFirst = new bool(false);
153 System::Threading::Monitor::Enter(Cli_environment::typeid);
154 try {
155 buf->Append(m_IDGen->GetId(obj, bFirst));
156 } finally
157 {
158 System::Threading::Monitor::Exit(Cli_environment::typeid);
159 }
160
161 buf->Append(sOidPart);
162 oId= buf->ToString();
163 }
164 return oId;
165}
166} //namespace cli_uno
167
168/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
XPropertyListType t
static Hashtable m_objects
static System::String createKey(System::String ^ oid, System::Type ^ t)
Object registerInterface(Object ^ obj, System::String ^ oid)
Registers a UNO object as being mapped by this bridge.
void revokeInterface(System::String ^ oid)
By revoking an interface it is declared that the respective interface has not been mapped.
static System::Runtime::Serialization::ObjectIDGenerator m_IDGen
Object getRegisteredInterface(System::String ^ oid, System::Type ^ type)
Retrieves an interface identified by its object id and type from this environment.
static System::String getObjectIdentifier(Object ^ obj)
Generates a worldwide unique object identifier (oid) for the given object.
static System::String sOidPart
System::String getOid()
Definition: cli_proxy.h:117
int i
ResultType type