GetFEM  5.4.2
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& context_dependencies::operator=(const context_dependencies& cd)
37  {
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; if (*it2 != &cd) ++it2; else --s; }
52  dependent.resize(s);
53  }
54 
55  void context_dependencies::sup_dependency_
56  (const context_dependencies &cd) const {
57  getfem::local_guard lock = locks_.get_lock();
58  size_type s = dependencies.size();
59  iterator_list it1=dependencies.begin(), it2=it1, ite=dependencies.end();
60  for (; it1 != ite; ++it1)
61  { *it2 = *it1; if (*it2 != &cd) ++it2; else --s; }
62  dependencies.resize(s);
63  }
64 
65  void context_dependencies::invalid_context() const {
66  if (state != CONTEXT_INVALID)
67  {
68  iterator_list it = dependent.begin(), ite = dependent.end();
69  for (; it != ite; ++it) (*it)->invalid_context();
70  getfem::local_guard lock = locks_.get_lock();
71  state = CONTEXT_INVALID;
72  }
73  }
74 
75  void context_dependencies::add_dependency(const context_dependencies &cd) {
76  cd.context_check(); cd.touched = false;
77  {
78  getfem::local_guard lock = locks_.get_lock();
79  iterator_list it = dependencies.begin(), ite = dependencies.end();
80  for (; it != ite; ++it) if ((*it) == &cd) return;
81  dependencies.push_back(&cd);
82  }
83  getfem::local_guard lock = cd.locks_.get_lock();
84  cd.dependent.push_back(this);
85  }
86 
87  bool context_dependencies::go_check() const
88  {
89  if (state == CONTEXT_CHANGED)
90  {
91  iterator_list it = dependencies.begin(), ite = dependencies.end();
92  for (; it != ite; ++it)
93  {
94  (*it)->context_check();
95  (*it)->touched = false;
96  }
97  getfem::local_guard lock = locks_.get_lock();
98  state = CONTEXT_NORMAL;
99  update_from_context();
100  return true;
101  }
102  GMM_ASSERT1(state != CONTEXT_INVALID, "Invalid context");
103  return false;
104  }
105 
106  void context_dependencies::touch() const
107  {
108  if (!touched)
109  {
110  iterator_list it = dependent.begin(), ite = dependent.end();
111  for (; it != ite; ++it) (*it)->change_context();
112  touched = true;
113  }
114  }
115 
116  void context_dependencies::clear_dependencies() {
117  iterator_list it = dependencies.begin(), ite = dependencies.end();
118  for (; it != ite; ++it) (*it)->sup_dependent_(*this);
119  dependencies.clear();
120  }
121 
122  context_dependencies::~context_dependencies() {
123  invalid_context();
124  iterator_list it = dependencies.begin(), ite = dependencies.end();
125  for (; it != ite; ++it) (*it)->sup_dependent_(*this);
126  it = dependent.begin(), ite = dependent.end();
127  for (; it != ite; ++it) (*it)->sup_dependency_(*this);
128  }
129 
130 }
bgeot::size_type
size_t size_type
used as the common size type in the library
Definition: bgeot_poly.h:49
getfem
GEneric Tool for Finite Element Methods.
Definition: getfem_accumulated_distro.h:46
getfem_context.h
Deal with interdependencies of objects (getfem::context_dependencies).