Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
ScopedPtr.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* ScopedPtr.h (C) 2000-2006 */
9/* */
10/* Encapsulation of a pointer that is automatically destroyed. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_UTILS_SCOPEDPTR_H
13#define ARCANE_UTILS_SCOPEDPTR_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arcane/utils/Ptr.h"
18
19/*---------------------------------------------------------------------------*/
20/*---------------------------------------------------------------------------*/
21
22namespace Arcane
23{
24
25/*---------------------------------------------------------------------------*/
26/*---------------------------------------------------------------------------*/
27
28/*!
29 * \ingroup Core
30 * \brief Encapsulation of an automatically destructing pointer.
31 *
32 This class encapsulates a pointer to an object that will be destroyed (via
33 the delete operator) when the instance of this class goes out of scope.
34
35 This class is useful to ensure that an object is deallocated even if an exception occurs.
36
37 \since 0.4.40
38 \author Gilles Grospellier
39 \date 16/07/2001
40 */
41template <class T>
43: public PtrT<T>
44{
45 public:
46
47 //! Base class type
49
50 public:
51
52 //! Constructs an instance without a reference
54 : BaseClass(0)
55 {}
56
57 //! Constructs an instance referencing t
58 explicit ScopedPtrT(T* t)
59 : BaseClass(t)
60 {}
61
62 //! Destroys the referenced object.
63 ~ScopedPtrT() { delete this->m_value; }
64
65 public:
66
67 //! Copy operator
69 {
70 if (this != &from) {
71 delete this->m_value;
73 }
74 return (*this);
75 }
76
77 //! Assigns the value new_value to the instance
78 const ScopedPtrT<T>& operator=(T* new_value)
79 {
80 if (this->m_value != new_value) {
81 delete this->m_value;
82 this->m_value = new_value;
83 }
84 return (*this);
85 }
86};
87
88/*---------------------------------------------------------------------------*/
89/*---------------------------------------------------------------------------*/
90
91} // namespace Arcane
92
93/*---------------------------------------------------------------------------*/
94/*---------------------------------------------------------------------------*/
95
96#endif
T * m_value
Pointer to the referenced object.
Definition Ptr.h:128
PtrT< T > & operator=(const PtrT< T > &from)
Copy operator.
Definition Ptr.h:50
PtrT(const PtrT< T > &from)
Constructs a reference referring to from.
Definition Ptr.h:71
const ScopedPtrT< T > & operator=(const ScopedPtrT< T > &from)
Copy operator.
Definition ScopedPtr.h:68
ScopedPtrT()
Constructs an instance without a reference.
Definition ScopedPtr.h:53
~ScopedPtrT()
Destroys the referenced object.
Definition ScopedPtr.h:63
const ScopedPtrT< T > & operator=(T *new_value)
Assigns the value new_value to the instance.
Definition ScopedPtr.h:78
PtrT< T > BaseClass
Base class type.
Definition ScopedPtr.h:48
ScopedPtrT(T *t)
Constructs an instance referencing t.
Definition ScopedPtr.h:58
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --