UDK 3.2.7 C/C++ API Reference
|
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 00002 /************************************************************************* 00003 * 00004 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 00005 * 00006 * Copyright 2000, 2010 Oracle and/or its affiliates. 00007 * 00008 * OpenOffice.org - a multi-platform office productivity suite 00009 * 00010 * This file is part of OpenOffice.org. 00011 * 00012 * OpenOffice.org is free software: you can redistribute it and/or modify 00013 * it under the terms of the GNU Lesser General Public License version 3 00014 * only, as published by the Free Software Foundation. 00015 * 00016 * OpenOffice.org is distributed in the hope that it will be useful, 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 * GNU Lesser General Public License version 3 for more details 00020 * (a copy is included in the LICENSE file that accompanied this code). 00021 * 00022 * You should have received a copy of the GNU Lesser General Public License 00023 * version 3 along with OpenOffice.org. If not, see 00024 * <http://www.openoffice.org/license.html> 00025 * for a copy of the LGPLv3 License. 00026 * 00027 ************************************************************************/ 00028 00029 #ifndef INCLUDED_cppu_FreeReference_hxx 00030 #define INCLUDED_cppu_FreeReference_hxx 00031 00032 #include "uno/environment.hxx" 00033 #include "cppu/Map.hxx" 00034 #include "com/sun/star/uno/Reference.h" 00035 00036 00037 namespace cssuno = com::sun::star::uno; 00038 00039 00040 namespace cppu 00041 { 00047 template< class T > 00048 class FreeReference 00049 { 00050 cssuno::Environment m_env; 00051 T * m_pObject; 00052 00053 public: 00054 FreeReference() : m_pObject(NULL) {} 00055 00056 FreeReference(T * pObject, __sal_NoAcquire) 00057 : m_env(cssuno::Environment::getCurrent()), 00058 m_pObject(pObject) 00059 { 00060 } 00061 00062 FreeReference(T * pObject) 00063 : m_env(cssuno::Environment::getCurrent()), 00064 m_pObject(pObject) 00065 { 00066 if (m_pObject) 00067 m_env.get()->pExtEnv->acquireInterface(m_env.get()->pExtEnv, m_pObject); 00068 } 00069 00070 explicit FreeReference(cssuno::Reference<T> const & xRef) 00071 : m_env(cssuno::Environment::getCurrent()), 00072 m_pObject(xRef.get()) 00073 { 00074 if (m_pObject) 00075 m_env.get()->pExtEnv->acquireInterface(m_env.get()->pExtEnv, m_pObject); 00076 } 00077 00078 FreeReference(FreeReference<T> const & rOther) 00079 : m_env (rOther.m_env), 00080 m_pObject(rOther.m_pObject) 00081 { 00082 if (m_pObject) 00083 m_env.get()->pExtEnv->acquireInterface(m_env.get()->pExtEnv, m_pObject); 00084 } 00085 00086 00087 ~FreeReference() 00088 { 00089 clear(); 00090 } 00091 00092 cssuno::Environment getEnv() const throw (cssuno::RuntimeException) 00093 { 00094 return m_env; 00095 } 00096 00097 cssuno::Reference<T> get() const throw (cssuno::RuntimeException) 00098 { 00099 return cssuno::Reference<T>(cppu::mapIn(m_pObject, m_env), SAL_NO_ACQUIRE); 00100 } 00101 00102 operator cssuno::Reference<T> () const throw (cssuno::RuntimeException) 00103 { 00104 return get(); 00105 } 00106 00107 cssuno::Reference<T> operator -> () const throw (cssuno::RuntimeException) 00108 { 00109 return get(); 00110 } 00111 00112 bool is() const throw (cssuno::RuntimeException) 00113 { 00114 return m_pObject != NULL; 00115 } 00116 00117 void clear() 00118 { 00119 if (m_pObject) 00120 { 00121 00122 m_env.get()->pExtEnv->releaseInterface(m_env.get()->pExtEnv, m_pObject); 00123 m_pObject = NULL; 00124 m_env.clear(); 00125 } 00126 } 00127 00128 FreeReference<T> & operator = (FreeReference<T> const & rOther) 00129 { 00130 clear(); 00131 00132 m_pObject = rOther.m_pObject; 00133 if (m_pObject) 00134 { 00135 m_env = rOther.m_env; 00136 m_env.get()->pExtEnv->acquireInterface(m_env.get()->pExtEnv, m_pObject); 00137 } 00138 00139 return *this; 00140 } 00141 00142 void set(cssuno::Reference<T> const & xRef) 00143 { 00144 clear(); 00145 00146 m_pObject = xRef.get(); 00147 if (m_pObject) 00148 { 00149 m_env = cssuno::Environment::getCurrent(); 00150 00151 m_env.get()->pExtEnv->acquireInterface(m_env.get()->pExtEnv, m_pObject); 00152 } 00153 } 00154 00155 bool operator == (FreeReference const & rOther) const 00156 { 00157 return get() == rOther.get(); 00158 } 00159 00160 bool operator != (FreeReference const & rOther) const 00161 { 00162 return !operator==(rOther); 00163 } 00164 }; 00165 } 00166 00167 #endif 00168 00169 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */