Line data Source code
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 : #ifndef INCLUDED_CANVAS_VCLWRAPPER_HXX
21 : #define INCLUDED_CANVAS_VCLWRAPPER_HXX
22 :
23 : #include <osl/mutex.hxx>
24 : #include <vcl/svapp.hxx>
25 :
26 : namespace canvas
27 : {
28 : namespace vcltools
29 : {
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.
33 :
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.
38 :
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.
43 :
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.".
53 : */
54 : template< class _Wrappee > class VCLObject
55 : {
56 : public:
57 : typedef _Wrappee Wrappee;
58 :
59 0 : VCLObject() :
60 0 : mpWrappee( new Wrappee() )
61 : {
62 0 : }
63 :
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 )
69 : {
70 : }
71 :
72 : // This object has value semantics, thus, forward copy
73 : // to wrappee
74 : VCLObject( const VCLObject& rOrig )
75 : {
76 : if( rOrig.mpWrappee )
77 : mpWrappee = new Wrappee( *rOrig.mpWrappee );
78 : else
79 : mpWrappee = NULL;
80 : }
81 :
82 : // This object has value semantics, thus, forward copy
83 : // to wrappee
84 0 : VCLObject( const Wrappee& rOrig ) :
85 0 : mpWrappee( new Wrappee( rOrig ) )
86 : {
87 0 : }
88 :
89 : // This object has value semantics, thus, forward
90 : // assignment to wrappee
91 0 : VCLObject& operator=( const VCLObject& rhs )
92 : {
93 0 : if( mpWrappee )
94 : {
95 0 : if( rhs.mpWrappee )
96 0 : *mpWrappee = *rhs.mpWrappee;
97 : }
98 : else
99 : {
100 0 : if( rhs.mpWrappee )
101 0 : mpWrappee = new Wrappee( *rhs.mpWrappee );
102 : }
103 :
104 0 : return *this;
105 : }
106 :
107 0 : ~VCLObject()
108 : {
109 : // This here is the whole purpose of the template:
110 : // protecting object deletion with the solar mutex
111 0 : SolarMutexGuard aGuard;
112 :
113 0 : if( mpWrappee )
114 0 : delete mpWrappee;
115 0 : }
116 :
117 0 : Wrappee* operator->() { return mpWrappee; }
118 0 : const Wrappee* operator->() const { return mpWrappee; }
119 :
120 0 : Wrappee& operator*() { return *mpWrappee; }
121 0 : const Wrappee& operator*() const { return *mpWrappee; }
122 :
123 : Wrappee& get() { return *mpWrappee; }
124 : const Wrappee& get() const { return *mpWrappee; }
125 :
126 : void swap( VCLObject& rOther )
127 : {
128 : ::std::swap( mpWrappee, rOther.mpWrappee );
129 : }
130 :
131 : private:
132 :
133 : Wrappee* mpWrappee;
134 : };
135 :
136 : }
137 : }
138 :
139 : #endif /* INCLUDED_CANVAS_VCLWRAPPER_HXX */
140 :
141 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|