Branch libreoffice-5-0-4
[LibreOffice.git] / include / canvas / vclwrapper.hxx
blob330e88cb88ad97706051754cd487374776813ce6
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 .
20 #ifndef INCLUDED_CANVAS_VCLWRAPPER_HXX
21 #define INCLUDED_CANVAS_VCLWRAPPER_HXX
23 #include <osl/mutex.hxx>
24 #include <vcl/svapp.hxx>
26 namespace canvas
28 namespace vcltools
30 /** This helper template wraps VCL objects, and protects
31 object deletion with the Solar mutex. All other operations
32 are unprotected, this must be handled by client code.
34 The reason for this template is the fact that VCL objects
35 hold by value in uno::Reference-handled classes are
36 deleted without having the chance to get inbetween and
37 lock the solar mutex.
39 This template handles that problem transparently, the only
40 inconvenience is the fact that object member access now
41 has to be performed via operator->, since . is not
42 overloadable.
44 Otherwise, the template preserves the value semantics of
45 the wrapped object, that is, copy operations are performed
46 not by copying pointers, but by copying the underlying
47 object. This includes constness, i.e. on a const
48 VCLObject, only const methods of the wrapped object can be
49 called. Simply imagine this to be a value object of type
50 "template argument", with the only peculiarity that
51 member/method access is performed by operator-> instead of
52 the non-existing "operator.".
54 template< class _Wrappee > class VCLObject
56 public:
57 typedef _Wrappee Wrappee;
59 VCLObject() :
60 mpWrappee( new Wrappee() )
64 // no explicit here. VCLObjects should be freely
65 // constructible with Wrappees, and AFAIK there is no other
66 // implicit conversion path that could cause harm here
67 VCLObject( Wrappee* pWrappee ) :
68 mpWrappee( pWrappee )
72 // This object has value semantics, thus, forward copy
73 // to wrappee
74 VCLObject( const VCLObject& rOrig )
76 if( rOrig.mpWrappee )
77 mpWrappee = new Wrappee( *rOrig.mpWrappee );
78 else
79 mpWrappee = NULL;
82 // This object has value semantics, thus, forward copy
83 // to wrappee
84 VCLObject( const Wrappee& rOrig ) :
85 mpWrappee( new Wrappee( rOrig ) )
89 // This object has value semantics, thus, forward
90 // assignment to wrappee
91 VCLObject& operator=( const VCLObject& rhs )
93 if( mpWrappee )
95 if( rhs.mpWrappee )
96 *mpWrappee = *rhs.mpWrappee;
98 else
100 if( rhs.mpWrappee )
101 mpWrappee = new Wrappee( *rhs.mpWrappee );
104 return *this;
107 ~VCLObject()
109 // This here is the whole purpose of the template:
110 // protecting object deletion with the solar mutex
111 SolarMutexGuard aGuard;
113 if( mpWrappee )
114 delete mpWrappee;
117 Wrappee* operator->() { return mpWrappee; }
118 const Wrappee* operator->() const { return mpWrappee; }
120 Wrappee& operator*() { return *mpWrappee; }
121 const Wrappee& operator*() const { return *mpWrappee; }
123 Wrappee& get() { return *mpWrappee; }
124 const Wrappee& get() const { return *mpWrappee; }
126 void swap( VCLObject& rOther )
128 ::std::swap( mpWrappee, rOther.mpWrappee );
131 private:
133 Wrappee* mpWrappee;
139 #endif /* INCLUDED_CANVAS_VCLWRAPPER_HXX */
141 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */