1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
30 /** Dyn owns an object on the heap, which will be automatically
33 Dyn's main purpose is for class members on the heap:
34 You can't forget to delete them in the D'tor. Constness will be transfered
37 Dyn forbids the CopyC'tor and operator=(). So you can't incidentally
38 run into problems with compiler defined CopyC'tor or operator=() of the
39 owning class. If you need those, you have to define them explicitly - as
40 you should do anyway with all classes, that own members on the heap.
42 Dyn also works with incomplete types.
43 You only need to write
45 but needn't include #include <DX>.hxx.
46 This is a difference to std::auto_ptr, where it is not absolutely clear
47 if it is allowed to use it with incomplete types.
49 You can also use Dyn within function bodies, to make them exception safe.
52 If you use Dyn with an incomplete type, the owning class needs to
53 define a non-inline D'tor. Else the compiler will complain.
60 /// From now on, let_dpObject is owned by this Dyn-object.
62 DX
* let_dpObject
= 0);
65 /** This deletes a prevoiusly existing dpObject!
66 From now on, let_dpObject is owned by this Dyn-object.
70 /// @return true, if any valid object is hold, false else.
71 operator bool() const;
73 const DX
* operator->() const;
76 const DX
& operator*() const;
80 /** @return The hold object on the heap.
83 The caller of the function is responsible to delete
92 /// Shorthand for operator->(), if implicit overloading of -> can not be used.
93 const DX
* Ptr() const;
96 /// Shorthand for operator->(), if implicit overloading of -> can not be used.
98 /// So const objects can return mutable pointers to the owned object.
99 DX
* MutablePtr() const;
102 /* Does NOT set dpObject to zero! Because it is only used
103 internally in situations where dpObject is set immediately
108 /** Forbidden function!
110 Help ensure, that classes with
111 dynamic pointers use a selfdefined copy constructor
112 and operator=(). If the default versions of these
113 functions are used, the compiler will throw an error.
115 Dyn( const Dyn
<DX
> & );
116 /** Forbidden function!
118 Help ensure, that classes with
119 dynamic pointers use a selfdefined copy constructor
120 and operator=(). If the default versions of these
121 functions are used, the compiler will throw an error.
123 Dyn
<DX
> & operator=( const Dyn
<DX
> & );
126 /// An owned heap object. Needs to be deleted by this class.
144 Dyn
<DX
>::Dyn( DX
* let_dpObject
)
145 : dpObject(let_dpObject
) {}
155 Dyn
<DX
>::operator=( DX
* let_dpObject
)
157 if ( dpObject
== let_dpObject
)
161 dpObject
= let_dpObject
;
167 Dyn
<DX
>::operator bool() const
168 { return dpObject
!= 0; }
173 Dyn
<DX
>::operator->() const
178 Dyn
<DX
>::operator->()
183 Dyn
<DX
>::operator*() const
184 { csv_assert(dpObject
!= 0);
191 { csv_assert(dpObject
!= 0);
198 { DX
* ret
= dpObject
;
215 Dyn
<DX
>::MutablePtr() const
224 #define Dyn ::csv::Dyn
232 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */