Arcane  4.1.12.0
Developer documentation
Loading...
Searching...
No Matches
AnyItemLinkVariable.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/* AnyItemLinkVariable.h (C) 2000-2025 */
9/* */
10/* Link variable for items of arbitrary types. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_CORE_ANYITEM_ANYITEMLINKVARIABLE_H
13#define ARCANE_CORE_ANYITEM_ANYITEMLINKVARIABLE_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arcane/core/anyitem/AnyItemGlobal.h"
18
19/*---------------------------------------------------------------------------*/
20/*---------------------------------------------------------------------------*/
21
22namespace Arcane::AnyItem
23{
24
25/*---------------------------------------------------------------------------*/
26/*---------------------------------------------------------------------------*/
27
28/*
29 * \brief Link variable for items of arbitrary types.
30 *
31 * For example:
32 *
33 * AnyItem::Variable<Real> variable(family); // Filled
34 * AnyItem::LinkVariable<Real> link_variable(link_family); // Filled
35 *
36 * Real value = 0;
37 * ENUMERATE_ANY_ITEM_LINK(ilink, link_family) {
38 * if(ilink.index() < 10) {
39 * info() << "back item = [uid=" << family.concreteItem(ilink.back()).uniqueId()
40 * << ",lid=" << family.concreteItem(ilink.back()).localId() << ",kind="
41 * << family.concreteItem(ilink.back()).kind() << "]";
42 * info() << "front item = [uid=" << family.concreteItem(ilink.front()).uniqueId()
43 * << ",lid=" << family.concreteItem(ilink.front()).localId() << ",kind="
44 * << family.concreteItem(ilink.front()).kind() << "]";
45 * }
46 * value += link_variable[ilink] + variable[ilink.back()] + variable[ilink.front()];
47 * }
48 *
49 */
50template <typename DataType>
51class LinkVariable
53{
54 public:
55
56 LinkVariable(const LinkFamily& family)
57 : m_family(family)
58 , m_values(m_family.capacity())
59 {
60 m_family.registerObserver(*this);
61 }
62
63 LinkVariable(const LinkVariable& v)
66 {
67 m_family.registerObserver(*this);
68 }
69
70 ~LinkVariable()
71 {
72 arcaneCallFunctionAndTerminateIfThrow([&]() { m_family.removeObserver(*this); });
73 }
74
76 inline DataType& operator[](const LinkFamily::LinkIndex& item)
77 {
78 return m_values[item.index()];
79 }
80
82 inline DataType operator[](const LinkFamily::LinkIndex& item) const
83 {
84 return m_values[item.index()];
85 }
86
89 {
90 // If the family changes, we resize
91 m_values.resize(m_family.capacity());
92 }
93
96 {
97 // If the family is reserved, we simply resize
98 m_values.resize(m_family.capacity());
99 }
100
101 private:
102
105
108};
109
110/*---------------------------------------------------------------------------*/
111/*---------------------------------------------------------------------------*/
112
113} // End namespace Arcane::AnyItem
114
115/*---------------------------------------------------------------------------*/
116/*---------------------------------------------------------------------------*/
117
118#endif
AnyItem link family observer interface.
AnyItem link family (flyweight pattern).
Arcane::UniqueArray< DataType > m_values
Values.
DataType & operator[](const LinkFamily::LinkIndex &item)
Accessor.
void notifyFamilyIsInvalidate()
Action if the family is invalidated: we resize.
DataType operator[](const LinkFamily::LinkIndex &item) const
Accessor.
const LinkFamily m_family
Link family.
void notifyFamilyIsReserved()
Action if the family is reserved: we resize.
1D data vector with value semantics (STL style).
void arcaneCallFunctionAndTerminateIfThrow(std::function< void()> function)
Calls the function function and calls std::terminate() if an exception occurs.