GetFEM  5.5
getfem_context.cc
1 /*===========================================================================
2 
3  Copyright (C) 2004-2026 Yves Renard
4 
5  This file is a part of GetFEM
6 
7  GetFEM is free software; you can redistribute it and/or modify it
8  under the terms of the GNU Lesser General Public License as published
9  by the Free Software Foundation; either version 3 of the License, or
10  (at your option) any later version along with the GCC Runtime Library
11  Exception either version 3.1 or (at your option) any later version.
12  This program is distributed in the hope that it will be useful, but
13  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15  License and GCC Runtime Library Exception for more details.
16  You should have received a copy of the GNU Lesser General Public License
17  along with this program. If not, see https://www.gnu.org/licenses/.
18 
19 ===========================================================================*/
20 
21 
22 
23 #include "getfem/getfem_context.h"
24 
25 namespace getfem {
26 
27  context_dependencies::context_dependencies(const context_dependencies &cd)
28  : state(cd.state),
29  touched(static_cast<bool>(cd.touched)),
30  dependencies(cd.dependencies),
31  dependent(cd.dependent),
32  locks_( )
33  {}
34 
35  context_dependencies&
36  context_dependencies::operator=(const context_dependencies &cd) {
37  state = cd.state;
38  touched = static_cast<bool>(cd.touched);
39  dependencies = cd.dependencies;
40  dependent = cd.dependent;
41  return *this;
42  }
43 
44  void context_dependencies::sup_dependent_
45  (const context_dependencies &cd) const {
46  getfem::local_guard lock = locks_.get_lock();
47  size_type s = dependent.size();
48  iterator_list it1 = dependent.begin(), it2 = it1, ite = dependent.end();
49  for (; it1 != ite; ++it1) {
50  *it2 = *it1;
51  if (*it2 != &cd)
52  ++it2;
53  else
54  --s;
55  }
56  dependent.resize(s);
57  }
58 
59  void context_dependencies::sup_dependency_
60  (const context_dependencies &cd) const {
61  getfem::local_guard lock = locks_.get_lock();
62  size_type s = dependencies.size();
63  iterator_list it1=dependencies.begin(), it2=it1, ite=dependencies.end();
64  for (; it1 != ite; ++it1) {
65  *it2 = *it1;
66  if (*it2 != &cd)
67  ++it2;
68  else
69  --s;
70  }
71  dependencies.resize(s);
72  }
73 
74  void context_dependencies::invalid_context() const {
75  if (state != CONTEXT_INVALID) {
76  for (auto &it : dependent)
77  it->invalid_context();
78  getfem::local_guard lock = locks_.get_lock();
79  state = CONTEXT_INVALID;
80  }
81  }
82 
83  void context_dependencies::add_dependency(const context_dependencies &cd) {
84  cd.context_check(); cd.touched = false;
85  {
86  getfem::local_guard lock = locks_.get_lock();
87  for (auto &it : dependencies)
88  if (it == &cd) return;
89  dependencies.push_back(&cd);
90  }
91  getfem::local_guard lock = cd.locks_.get_lock();
92  cd.dependent.push_back(this);
93  }
94 
95  bool context_dependencies::go_check() const {
96  if (state == CONTEXT_CHANGED) {
97  for (auto &it : dependencies) {
98  it->context_check();
99  it->touched = false;
100  }
101  getfem::local_guard lock = locks_.get_lock();
102  state = CONTEXT_NORMAL;
103  update_from_context();
104  return true;
105  }
106  GMM_ASSERT1(state != CONTEXT_INVALID, "Invalid context");
107  return false;
108  }
109 
110  void context_dependencies::touch() const {
111  if (!touched) {
112  for (auto &it : dependent)
113  it->change_context();
114  touched = true;
115  }
116  }
117 
118  void context_dependencies::clear_dependencies() {
119  for (auto &it : dependencies)
120  it->sup_dependent_(*this);
121  dependencies.clear();
122  }
123 
124  context_dependencies::~context_dependencies() {
125  invalid_context();
126  for (auto &it : dependencies) it->sup_dependent_(*this);
127  for (auto &it : dependent) it->sup_dependency_(*this);
128  }
129 
130 }
Deal with interdependencies of objects (getfem::context_dependencies).
size_t size_type
used as the common size type in the library
Definition: bgeot_poly.h:48
GEneric Tool for Finite Element Methods.