GetFEM  5.4.3
getfem_context.cc
1 /*===========================================================================
2 
3  Copyright (C) 2004-2020 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, write to the Free Software Foundation,
18  Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19 
20 ===========================================================================*/
21 
22 
23 
24 #include "getfem/getfem_context.h"
25 
26 namespace getfem {
27 
28  context_dependencies::context_dependencies(const context_dependencies &cd)
29  : state(cd.state),
30  touched(static_cast<bool>(cd.touched)),
31  dependencies(cd.dependencies),
32  dependent(cd.dependent),
33  locks_( )
34  {}
35 
36  context_dependencies&
37  context_dependencies::operator=(const context_dependencies &cd) {
38  state = cd.state;
39  touched = static_cast<bool>(cd.touched);
40  dependencies = cd.dependencies;
41  dependent = cd.dependent;
42  return *this;
43  }
44 
45  void context_dependencies::sup_dependent_
46  (const context_dependencies &cd) const {
47  getfem::local_guard lock = locks_.get_lock();
48  size_type s = dependent.size();
49  iterator_list it1 = dependent.begin(), it2 = it1, ite = dependent.end();
50  for (; it1 != ite; ++it1) {
51  *it2 = *it1;
52  if (*it2 != &cd)
53  ++it2;
54  else
55  --s;
56  }
57  dependent.resize(s);
58  }
59 
60  void context_dependencies::sup_dependency_
61  (const context_dependencies &cd) const {
62  getfem::local_guard lock = locks_.get_lock();
63  size_type s = dependencies.size();
64  iterator_list it1=dependencies.begin(), it2=it1, ite=dependencies.end();
65  for (; it1 != ite; ++it1) {
66  *it2 = *it1;
67  if (*it2 != &cd)
68  ++it2;
69  else
70  --s;
71  }
72  dependencies.resize(s);
73  }
74 
75  void context_dependencies::invalid_context() const {
76  if (state != CONTEXT_INVALID) {
77  for (auto &it : dependent)
78  it->invalid_context();
79  getfem::local_guard lock = locks_.get_lock();
80  state = CONTEXT_INVALID;
81  }
82  }
83 
84  void context_dependencies::add_dependency(const context_dependencies &cd) {
85  cd.context_check(); cd.touched = false;
86  {
87  getfem::local_guard lock = locks_.get_lock();
88  for (auto &it : dependencies)
89  if (it == &cd) return;
90  dependencies.push_back(&cd);
91  }
92  getfem::local_guard lock = cd.locks_.get_lock();
93  cd.dependent.push_back(this);
94  }
95 
96  bool context_dependencies::go_check() const {
97  if (state == CONTEXT_CHANGED) {
98  for (auto &it : dependencies) {
99  it->context_check();
100  it->touched = false;
101  }
102  getfem::local_guard lock = locks_.get_lock();
103  state = CONTEXT_NORMAL;
104  update_from_context();
105  return true;
106  }
107  GMM_ASSERT1(state != CONTEXT_INVALID, "Invalid context");
108  return false;
109  }
110 
111  void context_dependencies::touch() const {
112  if (!touched) {
113  for (auto &it : dependent)
114  it->change_context();
115  touched = true;
116  }
117  }
118 
119  void context_dependencies::clear_dependencies() {
120  for (auto &it : dependencies)
121  it->sup_dependent_(*this);
122  dependencies.clear();
123  }
124 
125  context_dependencies::~context_dependencies() {
126  invalid_context();
127  for (auto &it : dependencies) it->sup_dependent_(*this);
128  for (auto &it : dependent) it->sup_dependency_(*this);
129  }
130 
131 }
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:49
GEneric Tool for Finite Element Methods.