LibreOffice Module canvas (master) 1
vclwrapper.hxx
Go to the documentation of this file.
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#pragma once
21
22#include <vcl/svapp.hxx>
23#include <memory>
24
25namespace canvas
26{
27 namespace vcltools
28 {
53 template< class Wrappee_ > class VCLObject
54 {
55 public:
56 typedef Wrappee_ Wrappee;
57
59 mpWrappee( new Wrappee() )
60 {
61 }
62
63 // no explicit here. VCLObjects should be freely
64 // constructible with Wrappees, and AFAIK there is no other
65 // implicit conversion path that could cause harm here
66 VCLObject( std::unique_ptr<Wrappee> pWrappee ) :
67 mpWrappee( pWrappee )
68 {
69 }
70
71 // This object has value semantics, thus, forward copy
72 // to wrappee
73 VCLObject( const VCLObject& rOrig )
74 {
75 if( rOrig.mpWrappee )
76 mpWrappee = new Wrappee( *rOrig.mpWrappee );
77 else
78 mpWrappee = nullptr;
79 }
80
81 VCLObject(VCLObject&& rOrig) noexcept
82 : mpWrappee(std::move(rOrig.mpWrappee))
83 {
84 }
85
86 // This object has value semantics, thus, forward copy
87 // to wrappee
88 VCLObject( const Wrappee& rOrig ) :
89 mpWrappee( new Wrappee( rOrig ) )
90 {
91 }
92
93 // This object has value semantics, thus, forward
94 // assignment to wrappee
96 {
97 if (this != &rhs)
98 {
99 if( mpWrappee )
100 {
101 if( rhs.mpWrappee )
102 *mpWrappee = *rhs.mpWrappee;
103 }
104 else
105 {
106 if( rhs.mpWrappee )
107 mpWrappee = new Wrappee( *rhs.mpWrappee );
108 }
109 }
110 return *this;
111 }
112
114 {
115 std::swap(mpWrappee, rhs.mpWrappee);
116
117 return *this;
118 }
119
121 {
122 // This here is the whole purpose of the template:
123 // protecting object deletion with the solar mutex
124 SolarMutexGuard aGuard;
125
126 mpWrappee.reset();
127 }
128
129 Wrappee* operator->() { return mpWrappee.get(); }
130 const Wrappee* operator->() const { return mpWrappee.get(); }
131
133 const Wrappee& operator*() const { return *mpWrappee; }
134
135 Wrappee& get() { return *mpWrappee; }
136 const Wrappee& get() const { return *mpWrappee; }
137
138 void swap( VCLObject& rOther )
139 {
140 ::std::swap( mpWrappee, rOther.mpWrappee );
141 }
142
143 private:
144
145 std::unique_ptr<Wrappee> mpWrappee;
146 };
147
148 }
149}
150
151/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
This helper template wraps VCL objects, and protects object deletion with the Solar mutex.
Definition: vclwrapper.hxx:54
const Wrappee * operator->() const
Definition: vclwrapper.hxx:130
VCLObject & operator=(const VCLObject &rhs)
Definition: vclwrapper.hxx:95
VCLObject(VCLObject &&rOrig) noexcept
Definition: vclwrapper.hxx:81
VCLObject(std::unique_ptr< Wrappee > pWrappee)
Definition: vclwrapper.hxx:66
VCLObject(const VCLObject &rOrig)
Definition: vclwrapper.hxx:73
VCLObject(const Wrappee &rOrig)
Definition: vclwrapper.hxx:88
const Wrappee & get() const
Definition: vclwrapper.hxx:136
std::unique_ptr< Wrappee > mpWrappee
Definition: vclwrapper.hxx:145
VCLObject & operator=(VCLObject &&rhs) noexcept
Definition: vclwrapper.hxx:113
void swap(VCLObject &rOther)
Definition: vclwrapper.hxx:138
const Wrappee & operator*() const
Definition: vclwrapper.hxx:133