Flow123d  jenkins-Flow123d-windows32-release-multijob-51
lazy_dependecy.hh
Go to the documentation of this file.
1 /*
2  * lazy_dependecy.hh
3  *
4  * Created on: Sep 8, 2011
5  * Author: jb
6  */
7 
8 #ifndef LAZY_DEPENDECY_HH_
9 #define LAZY_DEPENDECY_HH_
10 
11 #include <boost/lambda/lambda.hpp>
12 #include <list>
13 #include <algorithm>
14 
15 using namespace boost;
16 
17 /**
18  * This class implements lazy dependency of objects that inherits from it.
19  *
20  * Consider you have equation E which depends on input vector fields A and B which are possibly results of another computations
21  * 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
22  * to input fields A and B. This is purpose of this class.
23  *
24  * How to use it:
25  * 1) make all classes in the dependency graph inherited form LazyDependency class. Multiple inheritance is OK in this case.
26  * 2) equation E should call add_dependency for A and B
27  * 3) every time you change A or B call update()
28  * 4) when you are asked for result of equation check needs_update() and update the result only if it returns true
29  * Possibly call also update() for possible objects that depends on E.
30  */
32 public:
33  /// Default constructor.
35  : change_set_(1)
36  {}
37 
38  /// Increase the change set and set actual values of change sets of objects we depend on.
39  void update() {
40  change_set_ ++;
41  std::for_each(dependencies_.begin(), dependencies_.end(),
42  _1.second() = _1.first().change_set_
43  );
44  }
45 
46  /**
47  * Adds new object into dependency list. This do not update this dependency!
48  * @param object - any instance of LazyDependency
49  */
51  dependencies_.push_back(std::pair<LazyDependency&, unsigned int>(object, 0));
52  }
53 
54  /**
55  * Returns true if there is at least one object in the dependency list that changed its change set
56  * since the last call of update().
57  */
58  bool needs_update() const {
59  bool no_chnage=true;
60  std::for_each(dependencies_.begin(), dependencies_.end(),
61  no_change = no_change && ( _1.first().change_set_ == _1.second() )
62  );
63  return ! no_change;
64  }
65 
66 private:
67  unsigned int change_set_;
69 };
70 
71 #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