Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
AutoDestroyUserData.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/* AutoDestroyUserData.h (C) 2000-2012 */
9/* */
10/* UserData that self-destructs once detached. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_UTILS_AUTODESTROYUSERDATA_H
13#define ARCANE_UTILS_AUTODESTROYUSERDATA_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arcane/utils/IUserData.h"
18
19/*---------------------------------------------------------------------------*/
20/*---------------------------------------------------------------------------*/
21
22namespace Arcane
23{
24
25/*---------------------------------------------------------------------------*/
26/*---------------------------------------------------------------------------*/
27
28template <typename T>
30{
31 public:
32
33 static void destroy(T* t) { delete t; }
34};
35
36/*---------------------------------------------------------------------------*/
37/*---------------------------------------------------------------------------*/
38
39/*!
40 * \brief UserData that self-destructs once detached.
41 * \ingroup Core
42 *
43 * An instance of this class must be allocated via new()
44 * and is automatically destroyed along with its associated data when it
45 * is detached from an IUserDataList via IUserDataList::removeData().
46 *
47 * By default, it calls the delete operator for its data
48 * but it is possible to change its behavior via the
49 * DestroyBehaviour template parameter.
50 */
51template <typename T, typename DestroyBehaviour = DeleteOnDestroyBehaviour<T>>
52class AutoDestroyUserData
53: public IUserData
54{
55 public:
56
57 AutoDestroyUserData(T* adata)
58 : m_data(adata)
59 {}
60
61 private:
62
63 ~AutoDestroyUserData()
64 {
65 }
66
67 public:
68
69 virtual void notifyAttach() {}
70
71 virtual void notifyDetach()
72 {
73 DestroyBehaviour::destroy(m_data);
74 m_data = 0;
75 delete this;
76 }
77
78 T* data() { return m_data; }
79
80 private:
81
82 T* m_data;
83};
84
85/*---------------------------------------------------------------------------*/
86/*---------------------------------------------------------------------------*/
87
88} // namespace Arcane
89
90/*---------------------------------------------------------------------------*/
91/*---------------------------------------------------------------------------*/
92
93#endif
virtual void notifyAttach()
Method executed when the instance is attached.
virtual void notifyDetach()
Method executed when the instance is detached.
Interface for user data attached to another object.
Definition IUserData.h:33
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --