1*91c99ff4SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*91c99ff4SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*91c99ff4SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*91c99ff4SAndrew Rist * distributed with this work for additional information 6*91c99ff4SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*91c99ff4SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*91c99ff4SAndrew Rist * "License"); you may not use this file except in compliance 9*91c99ff4SAndrew Rist * with the License. You may obtain a copy of the License at 10*91c99ff4SAndrew Rist * 11*91c99ff4SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*91c99ff4SAndrew Rist * 13*91c99ff4SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*91c99ff4SAndrew Rist * software distributed under the License is distributed on an 15*91c99ff4SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*91c99ff4SAndrew Rist * KIND, either express or implied. See the License for the 17*91c99ff4SAndrew Rist * specific language governing permissions and limitations 18*91c99ff4SAndrew Rist * under the License. 19*91c99ff4SAndrew Rist * 20*91c99ff4SAndrew Rist *************************************************************/ 21*91c99ff4SAndrew Rist 22*91c99ff4SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir #ifndef INCLUDED_CANVAS_VCLWRAPPER_HXX 25cdf0e10cSrcweir #define INCLUDED_CANVAS_VCLWRAPPER_HXX 26cdf0e10cSrcweir 27cdf0e10cSrcweir #include <osl/mutex.hxx> 28cdf0e10cSrcweir #include <vos/mutex.hxx> 29cdf0e10cSrcweir #include <vcl/svapp.hxx> 30cdf0e10cSrcweir 31cdf0e10cSrcweir 32cdf0e10cSrcweir namespace canvas 33cdf0e10cSrcweir { 34cdf0e10cSrcweir namespace vcltools 35cdf0e10cSrcweir { 36cdf0e10cSrcweir /** This helper template wraps VCL objects, and protects 37cdf0e10cSrcweir object deletion with the Solar mutex. All other operations 38cdf0e10cSrcweir are unprotected, this must be handled by client code. 39cdf0e10cSrcweir 40cdf0e10cSrcweir The reason for this template is the fact that VCL objects 41cdf0e10cSrcweir hold by value in uno::Reference-handled classes are 42cdf0e10cSrcweir deleted without having the chance to get inbetween and 43cdf0e10cSrcweir lock the solar mutex. 44cdf0e10cSrcweir 45cdf0e10cSrcweir This template handles that problem transparently, the only 46cdf0e10cSrcweir inconvenience is the fact that object member access now 47cdf0e10cSrcweir has to be performed via operator->, since . is not 48cdf0e10cSrcweir overloadable. 49cdf0e10cSrcweir 50cdf0e10cSrcweir Otherwise, the template preserves the value semantics of 51cdf0e10cSrcweir the wrapped object, that is, copy operations are performed 52cdf0e10cSrcweir not by copying pointers, but by copying the underlying 53cdf0e10cSrcweir object. This includes constness, i.e. on a const 54cdf0e10cSrcweir VCLObject, only const methods of the wrapped object can be 55cdf0e10cSrcweir called. Simply imagine this to be a value object of type 56cdf0e10cSrcweir "template argument", with the only peculiarity that 57cdf0e10cSrcweir member/method access is performed by operator-> instead of 58cdf0e10cSrcweir the non-existing "operator.". 59cdf0e10cSrcweir */ 60cdf0e10cSrcweir template< class _Wrappee > class VCLObject 61cdf0e10cSrcweir { 62cdf0e10cSrcweir public: 63cdf0e10cSrcweir typedef _Wrappee Wrappee; 64cdf0e10cSrcweir VCLObject()65cdf0e10cSrcweir VCLObject() : 66cdf0e10cSrcweir mpWrappee( new Wrappee() ) 67cdf0e10cSrcweir { 68cdf0e10cSrcweir } 69cdf0e10cSrcweir 70cdf0e10cSrcweir // no explicit here. VCLObjects should be freely 71cdf0e10cSrcweir // constructible with Wrappees, and AFAIK there is no other 72cdf0e10cSrcweir // implicit conversion path that could cause harm here VCLObject(Wrappee * pWrappee)73cdf0e10cSrcweir VCLObject( Wrappee* pWrappee ) : 74cdf0e10cSrcweir mpWrappee( pWrappee ) 75cdf0e10cSrcweir { 76cdf0e10cSrcweir } 77cdf0e10cSrcweir 78cdf0e10cSrcweir // This object has value semantics, thus, forward copy 79cdf0e10cSrcweir // to wrappee VCLObject(const VCLObject & rOrig)80cdf0e10cSrcweir VCLObject( const VCLObject& rOrig ) 81cdf0e10cSrcweir { 82cdf0e10cSrcweir if( rOrig.mpWrappee ) 83cdf0e10cSrcweir mpWrappee = new Wrappee( *rOrig.mpWrappee ); 84cdf0e10cSrcweir else 85cdf0e10cSrcweir mpWrappee = NULL; 86cdf0e10cSrcweir } 87cdf0e10cSrcweir 88cdf0e10cSrcweir // This object has value semantics, thus, forward copy 89cdf0e10cSrcweir // to wrappee VCLObject(const Wrappee & rOrig)90cdf0e10cSrcweir VCLObject( const Wrappee& rOrig ) : 91cdf0e10cSrcweir mpWrappee( new Wrappee( rOrig ) ) 92cdf0e10cSrcweir { 93cdf0e10cSrcweir } 94cdf0e10cSrcweir 95cdf0e10cSrcweir // This object has value semantics, thus, forward 96cdf0e10cSrcweir // assignment to wrappee operator =(const VCLObject & rhs)97cdf0e10cSrcweir VCLObject& operator=( const VCLObject& rhs ) 98cdf0e10cSrcweir { 99cdf0e10cSrcweir if( mpWrappee ) 100cdf0e10cSrcweir { 101cdf0e10cSrcweir if( rhs.mpWrappee ) 102cdf0e10cSrcweir *mpWrappee = *rhs.mpWrappee; 103cdf0e10cSrcweir } 104cdf0e10cSrcweir else 105cdf0e10cSrcweir { 106cdf0e10cSrcweir if( rhs.mpWrappee ) 107cdf0e10cSrcweir mpWrappee = new Wrappee( *rhs.mpWrappee ); 108cdf0e10cSrcweir } 109cdf0e10cSrcweir 110cdf0e10cSrcweir return *this; 111cdf0e10cSrcweir } 112cdf0e10cSrcweir ~VCLObject()113cdf0e10cSrcweir ~VCLObject() 114cdf0e10cSrcweir { 115cdf0e10cSrcweir // This here is the whole purpose of the template: 116cdf0e10cSrcweir // protecting object deletion with the solar mutex 117cdf0e10cSrcweir ::vos::OGuard aGuard( Application::GetSolarMutex() ); 118cdf0e10cSrcweir 119cdf0e10cSrcweir if( mpWrappee ) 120cdf0e10cSrcweir delete mpWrappee; 121cdf0e10cSrcweir } 122cdf0e10cSrcweir operator ->()123cdf0e10cSrcweir Wrappee* operator->() { return mpWrappee; } operator ->() const124cdf0e10cSrcweir const Wrappee* operator->() const { return mpWrappee; } 125cdf0e10cSrcweir operator *()126cdf0e10cSrcweir Wrappee& operator*() { return *mpWrappee; } operator *() const127cdf0e10cSrcweir const Wrappee& operator*() const { return *mpWrappee; } 128cdf0e10cSrcweir get()129cdf0e10cSrcweir Wrappee& get() { return *mpWrappee; } get() const130cdf0e10cSrcweir const Wrappee& get() const { return *mpWrappee; } 131cdf0e10cSrcweir swap(VCLObject & rOther)132cdf0e10cSrcweir void swap( VCLObject& rOther ) 133cdf0e10cSrcweir { 134cdf0e10cSrcweir ::std::swap( mpWrappee, rOther.mpWrappee ); 135cdf0e10cSrcweir } 136cdf0e10cSrcweir 137cdf0e10cSrcweir private: 138cdf0e10cSrcweir 139cdf0e10cSrcweir Wrappee* mpWrappee; 140cdf0e10cSrcweir }; 141cdf0e10cSrcweir 142cdf0e10cSrcweir } 143cdf0e10cSrcweir } 144cdf0e10cSrcweir 145cdf0e10cSrcweir #endif /* INCLUDED_CANVAS_VCLWRAPPER_HXX */ 146