Flow123d  release_3.0.0-966-gb002c76
lazy_dependecy.hh
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License version 3 as published by the
7  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  *
14  * @file lazy_dependecy.hh
15  * @brief
16  */
17 
18 #ifndef LAZY_DEPENDECY_HH_
19 #define LAZY_DEPENDECY_HH_
20 
21 #include <boost/lambda/lambda.hpp>
22 #include <list>
23 #include <algorithm>
24 
25 using namespace boost;
26 
27 /**
28  * This class implements lazy dependency of objects that inherits from it.
29  *
30  * Consider you have equation E which depends on input vector fields A and B which are possibly results of another computations
31  * and can change over time. When someone ask you for result of equation E you have to look if your current result is up to date according
32  * to input fields A and B. This is purpose of this class.
33  *
34  * How to use it:
35  * 1) make all classes in the dependency graph inherited form LazyDependency class. Multiple inheritance is OK in this case.
36  * 2) equation E should call add_dependency for A and B
37  * 3) every time you change A or B call update()
38  * 4) when you are asked for result of equation check needs_update() and update the result only if it returns true
39  * Possibly call also update() for possible objects that depends on E.
40  */
42 public:
43  /// Default constructor.
45  : change_set_(1)
46  {}
47 
48  /// Increase the change set and set actual values of change sets of objects we depend on.
49  void update() {
50  change_set_ ++;
51  std::for_each(dependencies_.begin(), dependencies_.end(),
52  _1.second() = _1.first().change_set_
53  );
54  }
55 
56  /**
57  * Adds new object into dependency list. This do not update this dependency!
58  * @param object - any instance of LazyDependency
59  */
61  dependencies_.push_back(std::pair<LazyDependency&, unsigned int>(object, 0));
62  }
63 
64  /**
65  * Returns true if there is at least one object in the dependency list that changed its change set
66  * since the last call of update().
67  */
68  bool needs_update() const {
69  bool no_chnage=true;
70  std::for_each(dependencies_.begin(), dependencies_.end(),
71  no_change = no_change && ( _1.first().change_set_ == _1.second() )
72  );
73  return ! no_change;
74  }
75 
76 private:
77  unsigned int change_set_;
79 };
80 
81 #endif /* LAZY_DEPENDECY_HH_ */
void update()
Increase the change set and set actual values of change sets of objects we depend on...
LazyDependency()
Default constructor.
void add_dependency(LazyDependency &object)
unsigned int change_set_
std::list< std::pair< LazyDependency &, unsigned int > > dependencies_
bool needs_update() const