Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
UserDataList.cc
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/* UserDataList.cc (C) 2000-2012 */
9/* */
10/* Manages a list of user data. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/utils/ArcanePrecomp.h"
15
16#include "arcane/utils/UserDataList.h"
17#include "arcane/utils/String.h"
18#include "arcane/utils/TraceInfo.h"
19#include "arcane/utils/ArgumentException.h"
20#include "arcane/utils/IUserData.h"
21
22#include <map>
23
24/*---------------------------------------------------------------------------*/
25/*---------------------------------------------------------------------------*/
26
27namespace Arcane
28{
29
30/*---------------------------------------------------------------------------*/
31/*---------------------------------------------------------------------------*/
32
34{
35 public:
36
37 typedef std::map<String, IUserData*> MapType;
38 MapType m_list;
39};
40
41/*---------------------------------------------------------------------------*/
42/*---------------------------------------------------------------------------*/
43
44UserDataList::
45UserDataList()
46: m_p(new Impl())
47{
48}
49
50/*---------------------------------------------------------------------------*/
51/*---------------------------------------------------------------------------*/
52
55{
56 clear();
57 delete m_p;
58}
59
60/*---------------------------------------------------------------------------*/
61/*---------------------------------------------------------------------------*/
62
64clear()
65{
66 Impl::MapType::const_iterator begin = m_p->m_list.begin();
67 Impl::MapType::const_iterator end = m_p->m_list.end();
68 for (; begin != end; ++begin)
69 begin->second->notifyDetach();
70 m_p->m_list.clear();
71}
72
73/*---------------------------------------------------------------------------*/
74/*---------------------------------------------------------------------------*/
75
77setData(const String& name, IUserData* ud)
78{
79 Impl::MapType::const_iterator i = m_p->m_list.find(name);
80 if (i != m_p->m_list.end())
81 throw ArgumentException(A_FUNCINFO, String::format("key '{0}' already exists", name));
82 m_p->m_list.insert(std::make_pair(name, ud));
83 ud->notifyAttach();
84}
85
86/*---------------------------------------------------------------------------*/
87/*---------------------------------------------------------------------------*/
88
90data(const String& name, bool allow_null) const
91{
92 Impl::MapType::const_iterator i = m_p->m_list.find(name);
93 if (i == m_p->m_list.end()) {
94 if (allow_null)
95 return 0;
96 throw ArgumentException(A_FUNCINFO, String::format("key '{0}' not found", name));
97 }
98 return i->second;
99}
100
101/*---------------------------------------------------------------------------*/
102/*---------------------------------------------------------------------------*/
103
105removeData(const String& name, bool allow_null)
106{
107 Impl::MapType::iterator i = m_p->m_list.find(name);
108 if (i == m_p->m_list.end()) {
109 if (allow_null)
110 return;
111 throw ArgumentException(A_FUNCINFO, String::format("key '{0}' not found", name));
112 }
113 i->second->notifyDetach();
114 m_p->m_list.erase(i);
115}
116
117/*---------------------------------------------------------------------------*/
118/*---------------------------------------------------------------------------*/
119
120} // namespace Arcane
121
122/*---------------------------------------------------------------------------*/
123/*---------------------------------------------------------------------------*/
Interface for user data attached to another object.
Definition IUserData.h:33
virtual void notifyAttach()=0
Method executed when the instance is attached.
virtual void notifyDetach()=0
Method executed when the instance is detached.
virtual void clear()
Removes all user data.
virtual void removeData(const String &name, bool allow_null=false)
Removes the data associated with the name name.
virtual IUserData * data(const String &name, bool allow_null=false) const
Data associated with name.
virtual void setData(const String &name, IUserData *ud)
Sets the user data associated with the name name.
~UserDataList()
Frees resources.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --