Drake
cache3.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <functional>
4 #include <memory>
5 #include <vector>
6 
10 
11 namespace drake {
12 namespace systems {
13 
26 // TODO(sherm1) Consider how to define "up to date" for a sampled quantity;
27 // seems like it should still be up to date if its next sample
28 // time hasn't arrived yet, even if prereqs have changed.
30  public:
33  using Calculator =
34  std::function<void(const class AbstractSystem3&,
35  const class AbstractContext3&, AbstractValue*)>;
36 
40 
42  explicit CacheEntry(const AbstractValue& value) : value_(value.Clone()) {}
43 
45  explicit CacheEntry(std::unique_ptr<AbstractValue> value) noexcept
46  : value_(std::move(value)) {}
47 
51  CacheEntry(const CacheEntry& source)
52  : CacheEntry(source.get_abstract_value()) {}
53 
57  CacheEntry(CacheEntry&& source) noexcept
58  : value_(std::move(source.value_)), is_current_(source.is_current_) {
59  source.set_is_current(false);
60  }
61 
64  void set_calculator(const Calculator& calculator) {
65  calculate_ = calculator;
66  }
67 
70  const Calculator& get_calculator() const { return calculate_; }
71 
75  void ResetAbstractValue(std::unique_ptr<AbstractValue> value) {
76  value_ = std::move(value);
77  set_is_current(false);
78  }
79 
82  void ResetAbstractValue(const AbstractValue& value) {
83  ResetAbstractValue(value.Clone());
84  }
85 
86  void RealizeCacheEntry(const AbstractSystem3& system,
87  const AbstractContext3& context) {
88  // TODO(sherm1) remove when invalidation logic is working
89  set_is_current(false);
91 
92  if (is_current()) return;
93  get_calculator()(system, context, get_mutable_abstract_value());
94  set_is_current(true);
95  }
96 
98  bool is_current() const { return is_current_; }
99 
101  void set_is_current(bool is_current) { is_current_ = is_current; }
102 
105  const AbstractValue& get_abstract_value() const { return *value_; }
106 
111  return value_.get();
112  }
113 
118  template <class Type>
119  const Type& get_value() const {
120  const AbstractValue& abstract_value = get_abstract_value();
121  return abstract_value.GetValue<Type>();
122  }
123 
128  template <class Type>
130  AbstractValue* abstract_value = get_mutable_abstract_value();
131  return abstract_value->GetMutableValue<Type>();
132  }
133 
134  template <typename T>
136  const VectorObject<T>& object = get_value<VectorObject<T>>();
137  return object.get_vector();
138  }
139 
140  template <typename T>
142  VectorObject<T>* object = get_mutable_value<VectorObject<T>>();
143  return object.get_mutable_vector();
144  }
145 
146  // Implement ValueListenerInterface
147 
150  void Invalidate() override {
151  set_is_current(false);
152  ++version_;
153  listeners_.NotifyListeners();
154  }
155 
156  private:
157  std::unique_ptr<AbstractValue> value_;
158 
159  Calculator calculate_;
160 
161  // TODO(sherm1) The rest of these members should be reset_on_copy<T> to allow
162  // default constructors.
163 
164  // Whether this entry is up to date with respect to its prerequisites.
165  bool is_current_{false};
166 
167  // Who needs to be notified when this entry's value changes?
168  ValueListenerList listeners_;
169 
170  // A counter that is bumped every time this entry's value changes.
171  long long version_{0};
172 };
173 //
175 //for computed results so that they do not have to be recomputed if already
176 //valid. It contains a set of CacheEntry objects, each representing a single
177 //value. The values may be of any type, however if they are numerical values
178 //then they must use scalar type `T`.
179 //
180 //@tparam T The type of numerical values in this %Cache. Must be a
181 // valid Eigen scalar. **/
182 //template <typename T>
183 //class Cache {
184 // public:
185 // /** Construct a `Cache` containing no cache entries. **/
186 // Cache() {}
187 // ~Cache() = default;
188 //
189 // /** Return the number of entries `n` currently in this %Cache. Entries are
190 // numbered in the order added from 0 to `n-1`. **/
191 // int get_num_entries() const { return (int)entries_.size(); }
192 //
193 // /** Add a cache entry by moving the contents of the given `entry` into the
194 // Cache. The source entry is left empty.
195 // @retval entry_num The assigned entry number for retrieval. **/
196 // int add_entry(CacheEntry&& entry) {
197 // entries_.push_back(std::move(entry));
198 // return (int)entries_.size() - 1;
199 // }
200 //
201 // /** Add a cache entry that is a copy of the given `entry`.
202 // @retval entry_num The assigned entry number for retrieval. **/
203 // int add_entry(const CacheEntry& entry) {
204 // return add_entry(CacheEntry(entry)); // use move signature
205 // }
206 //
207 // /** Retrieve a const reference to a cache entry using the `entry_num` that
208 // was returned when the entry was added. **/
209 // const CacheEntry& get_entry(int entry_num) const {
210 // return entries_[entry_num];
211 // }
212 //
213 // /** Retrieve a mutable pointer to a cache entry using the `entry_num` that
214 // was returned when the entry was added. **/
215 // CacheEntry* get_mutable_entry(int entry_num) { return &entries_[entry_num]; }
216 //
217 // // Cache objects are copyable with the default copy constructor.
218 // Cache(const Cache& other) = default;
219 // Cache& operator=(const Cache& other) = default;
220 //
221 // private:
222 // // Cache objects are not moveable.
223 // Cache(Cache&& other) = delete;
224 // Cache& operator=(Cache&& other) = delete;
225 //
226 // std::vector<CacheEntry> entries_;
227 //};
228 
229 } // namespace systems
230 } // namesapce drake
VectorInterface is a pure abstract interface that real-valued signals between Systems must satisfy...
Definition: vector_interface.h:25
const Type & get_value() const
Return a const reference to the value contained in this cache entry, which must be of type Type...
Definition: cache3.h:119
AbstractValue * get_mutable_abstract_value()
Return a mutable pointer to the AbstractValue contained in this cache entry, regardless of whether it...
Definition: cache3.h:110
An abstract superclass for the Context3 objects for dynamical systems, encapsulating functionality th...
Definition: context3.h:87
CacheEntry(CacheEntry &&source) noexcept
Move constructor moves the value and preserves the is_current status of the source in the destination...
Definition: cache3.h:57
const Calculator & get_calculator() const
Set the function to be used when we need to recompute this cache entry&#39;s value.
Definition: cache3.h:70
CacheEntry(const CacheEntry &source)
Copy constructor makes a new cache entry that has a copy of the value from source, but is marked "not current" regardless of whether source is current.
Definition: cache3.h:51
Definition: constants.h:3
virtual std::unique_ptr< AbstractValue > Clone() const =0
Returns a copy of this AbstractValue.
const AbstractValue & get_abstract_value() const
Return a const reference to the AbstractValue contained in this cache entry, regardless of whether it...
Definition: cache3.h:105
void Invalidate() override
Invalidate this cache entry and notify any downstream listeners.
Definition: cache3.h:150
A ValueListenerList object maintains a list of value listeners that have registered to receive notifi...
Definition: value_listener3.h:34
STL namespace.
const VectorInterface< T > * get_mutable_vector_value() const
Definition: cache3.h:141
ValueListenerInterface is an interface that dependent computations must implement so that they can re...
Definition: value_listener3.h:19
void NotifyListeners()
Notify all dependents of this value that the value has changed.
Definition: value_listener3.h:56
void RealizeCacheEntry(const AbstractSystem3 &system, const AbstractContext3 &context)
Definition: cache3.h:86
void set_is_current(bool is_current)
Mark this entry as current or not; up to caller to do this right.
Definition: cache3.h:101
CacheEntry()
Create an empty cache entry that is not yet committed to a particular kind of abstract value...
Definition: cache3.h:39
void ResetAbstractValue(const AbstractValue &value)
Set the AbstractValue type stored in this CacheEntry by replacing the existing one (if any) with a co...
Definition: cache3.h:82
const T & GetValue() const
Returns the value wrapped in this AbstractValue, which must be of exactly type T. ...
Definition: value.h:35
const VectorInterface< T > & get_vector_value() const
Definition: cache3.h:135
bool is_current() const
Return the value of the is_current flag for this cache entry.
Definition: cache3.h:98
An abstract superclass for dynamical systems, encapsulating functionality that is independent of the ...
Definition: system3.h:68
T * GetMutableValue()
Returns the value wrapped in this AbstractValue, which must be of exactly type T. ...
Definition: value.h:46
This concrete class provides object semantics to an abstract VectorInterface by implementing a copy c...
Definition: vector_interface.h:70
A fully type-erased container class.
Definition: value.h:22
const VectorInterface< T > & get_vector() const
Returns a const reference to the VectorInterface object owned by this VectorObject, if any.
Definition: vector_interface.h:108
VectorInterface< T > * get_mutable_vector()
Returns a mutable pointer to the VectorInterface object owned by this VectorObject, if any.
Definition: vector_interface.h:118
std::function< void(const class AbstractSystem3 &, const class AbstractContext3 &, AbstractValue *)> Calculator
This is the type of a function that unconditionally computes this cache entry&#39;s value with respect to...
Definition: cache3.h:35
void ResetAbstractValue(std::unique_ptr< AbstractValue > value)
Set the AbstractValue type stored in this CacheEntry by replacing the existing one (if any) with the ...
Definition: cache3.h:75
CacheEntry(std::unique_ptr< AbstractValue > value) noexcept
Create a cache entry that takes over ownership of the given value.
Definition: cache3.h:45
Type * get_mutable_value()
Return a mutable pointer to the value contained in this cache entry, which must be of type Type...
Definition: cache3.h:129
CacheEntry(const AbstractValue &value)
Create a cache entry initialized to a copy of the given value.
Definition: cache3.h:42
Each cache entry contains:
Definition: cache3.h:29
void set_calculator(const Calculator &calculator)
Set the function to be used when we need to recompute this cache entry&#39;s value.
Definition: cache3.h:64