1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 #ifndef INCLUDED_cppu_FreeReference_hxx 25 #define INCLUDED_cppu_FreeReference_hxx 26 27 #include "uno/environment.hxx" 28 #include "cppu/Map.hxx" 29 #include "com/sun/star/uno/Reference.h" 30 31 32 namespace cssuno = com::sun::star::uno; 33 34 35 namespace cppu 36 { 37 /** Freely (environment independent) usable Reference. 38 (https://wiki.openoffice.org/wiki/Uno/Cpp/Spec/FreeReference) 39 40 @since UDK 3.2.7 41 */ 42 template< class T > 43 class FreeReference 44 { 45 cssuno::Environment m_env; 46 T * m_pObject; 47 48 public: FreeReference()49 FreeReference() : m_pObject(NULL) {} 50 FreeReference(T * pObject,__sal_NoAcquire)51 FreeReference(T * pObject, __sal_NoAcquire) 52 : m_env(cssuno::Environment::getCurrent()), 53 m_pObject(pObject) 54 { 55 } 56 FreeReference(T * pObject)57 FreeReference(T * pObject) 58 : m_env(cssuno::Environment::getCurrent()), 59 m_pObject(pObject) 60 { 61 if (m_pObject) 62 m_env.get()->pExtEnv->acquireInterface(m_env.get()->pExtEnv, m_pObject); 63 } 64 FreeReference(cssuno::Reference<T> const & xRef)65 explicit FreeReference(cssuno::Reference<T> const & xRef) 66 : m_env(cssuno::Environment::getCurrent()), 67 m_pObject(xRef.get()) 68 { 69 if (m_pObject) 70 m_env.get()->pExtEnv->acquireInterface(m_env.get()->pExtEnv, m_pObject); 71 } 72 FreeReference(FreeReference<T> const & rOther)73 FreeReference(FreeReference<T> const & rOther) 74 : m_env (rOther.m_env), 75 m_pObject(rOther.m_pObject) 76 { 77 if (m_pObject) 78 m_env.get()->pExtEnv->acquireInterface(m_env.get()->pExtEnv, m_pObject); 79 } 80 81 ~FreeReference()82 ~FreeReference() 83 { 84 clear(); 85 } 86 getEnv() const87 cssuno::Environment getEnv() const throw (cssuno::RuntimeException) 88 { 89 return m_env; 90 } 91 get() const92 cssuno::Reference<T> get() const throw (cssuno::RuntimeException) 93 { 94 return cssuno::Reference<T>(cppu::mapIn(m_pObject, m_env), SAL_NO_ACQUIRE); 95 } 96 operator cssuno::Reference<T>() const97 operator cssuno::Reference<T> () const throw (cssuno::RuntimeException) 98 { 99 return get(); 100 } 101 operator ->() const102 cssuno::Reference<T> operator -> () const throw (cssuno::RuntimeException) 103 { 104 return get(); 105 } 106 is() const107 bool is() const throw (cssuno::RuntimeException) 108 { 109 return m_pObject != NULL; 110 } 111 clear()112 void clear() 113 { 114 if (m_pObject) 115 { 116 117 m_env.get()->pExtEnv->releaseInterface(m_env.get()->pExtEnv, m_pObject); 118 m_pObject = NULL; 119 m_env.clear(); 120 } 121 } 122 operator =(FreeReference<T> const & rOther)123 FreeReference<T> & operator = (FreeReference<T> const & rOther) 124 { 125 clear(); 126 127 m_pObject = rOther.m_pObject; 128 if (m_pObject) 129 { 130 m_env = rOther.m_env; 131 m_env.get()->pExtEnv->acquireInterface(m_env.get()->pExtEnv, m_pObject); 132 } 133 134 return *this; 135 } 136 set(cssuno::Reference<T> const & xRef)137 void set(cssuno::Reference<T> const & xRef) 138 { 139 clear(); 140 141 m_pObject = xRef.get(); 142 if (m_pObject) 143 { 144 m_env = cssuno::Environment::getCurrent(); 145 146 m_env.get()->pExtEnv->acquireInterface(m_env.get()->pExtEnv, m_pObject); 147 } 148 } 149 operator ==(FreeReference const & rOther) const150 bool operator == (FreeReference const & rOther) const 151 { 152 return get() == rOther.get(); 153 } 154 operator !=(FreeReference const & rOther) const155 bool operator != (FreeReference const & rOther) const 156 { 157 return !operator==(rOther); 158 } 159 }; 160 } 161 162 #endif 163