Drake
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
system_input.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cstddef>
4 #include <functional>
5 #include <memory>
6 #include <vector>
7 
10 
11 namespace drake {
12 namespace systems {
13 
19 template <typename T>
21  public:
22  ~InputPort() override {}
23 
27  virtual int64_t get_version() const = 0;
28 
31  virtual double get_sample_time_sec() const = 0;
32 
36  virtual const VectorInterface<T>* get_vector_data() const = 0;
37 
41  void set_invalidation_callback(std::function<void()> callback) {
42  invalidation_callback_ = callback;
43  }
44 
47  void Invalidate() override {
48  if (invalidation_callback_ != nullptr) {
49  invalidation_callback_();
50  }
51  }
52 
53  protected:
54  InputPort() {}
55 
56  private:
57  std::function<void()> invalidation_callback_ = nullptr;
58 };
59 
65 template <typename T>
66 class DependentInputPort : public InputPort<T> {
67  public:
71  DependentInputPort(VectorOutputPort<T>* output_port, double sample_time_sec)
72  : output_port_(output_port), sample_time_sec_(sample_time_sec) {
73  output_port_->add_dependent(this);
74  }
75 
77  ~DependentInputPort() override {
78  output_port_->remove_dependent(this);
79  }
80 
82  int64_t get_version() const override {
83  return output_port_->get_version();
84  }
85 
86  double get_sample_time_sec() const override {
87  return sample_time_sec_;
88  }
89 
90  const VectorInterface<T>* get_vector_data() const override {
91  return output_port_->get_vector_data();
92  }
93 
94  private:
95  // DependentInputPort objects are neither copyable nor moveable.
96  DependentInputPort(const DependentInputPort& other) = delete;
97  DependentInputPort& operator=(const DependentInputPort& other) = delete;
98  DependentInputPort(DependentInputPort&& other) = delete;
99  DependentInputPort& operator=(DependentInputPort&& other) = delete;
100 
101  VectorOutputPort<T>* output_port_;
102  double sample_time_sec_;
103 };
104 
109 template <typename T>
110 class FreestandingInputPort : public InputPort<T> {
111  public:
115  std::unique_ptr<VectorInterface<T>> vector_data)
116  : FreestandingInputPort(std::move(vector_data), 0.0 /* continuous */) {}
117 
121  FreestandingInputPort(std::unique_ptr<VectorInterface<T>> vector_data,
122  double sample_time_sec)
123  : output_port_(std::move(vector_data)),
124  sample_time_sec_(sample_time_sec) {
125  output_port_.add_dependent(this);
126  }
127 
129  output_port_.remove_dependent(this);
130  }
131 
134  int64_t get_version() const override {
135  return output_port_.get_version();
136  }
137 
138  double get_sample_time_sec() const override {
139  return sample_time_sec_;
140  }
141 
142  const VectorInterface<T>* get_vector_data() const override {
143  return output_port_.get_vector_data();
144  }
145 
156  return output_port_.GetMutableVectorData();
157  }
158 
159  private:
160  // FreestandingInputPort objects are neither copyable nor moveable.
161  FreestandingInputPort(const FreestandingInputPort& other) = delete;
162  FreestandingInputPort& operator=(const FreestandingInputPort& other) = delete;
164  FreestandingInputPort& operator=(FreestandingInputPort&& other) = delete;
165 
166  VectorOutputPort<T> output_port_;
167  double sample_time_sec_ = 0.0;
168 };
169 
170 } // namespace systems
171 } // namespace drake
VectorInterface is a pure abstract interface that real-valued signals between Systems must satisfy...
Definition: vector_interface.h:25
void set_invalidation_callback(std::function< void()> callback)
Registers callback to be called whenever the value of get_version changes.
Definition: system_input.h:41
double get_sample_time_sec() const override
Returns the sampling interval of this port in seconds, or zero if this port is continuous.
Definition: system_input.h:86
~FreestandingInputPort() override
Definition: system_input.h:128
virtual const VectorInterface< T > * get_vector_data() const =0
Returns the vector data on this port, or nullptr if this port is not vector-valued or not connected...
Definition: constants.h:3
STL namespace.
VectorInterface< T > * GetMutableVectorData()
Returns a pointer to the data inside this InputPort, and updates the version so that Contexts dependi...
Definition: system_input.h:155
~InputPort() override
Definition: system_input.h:22
int64_t get_version() const override
Returns a positive and monotonically increasing number that is guaranteed to change whenever GetMutab...
Definition: system_input.h:134
The FreestandingInputPort encapsulates a vector of data for use as an input to a System.
Definition: system_input.h:110
~DependentInputPort() override
Disconnects from the output port.
Definition: system_input.h:77
void Invalidate() override
Receives notification that the output port on which this InputPort depends has changed, and calls the invalidation_callback_.
Definition: system_input.h:47
DependentInputPort(VectorOutputPort< T > *output_port, double sample_time_sec)
Creates an input port with the given sample_time_sec, connected to the given output_port, which must not be nullptr.
Definition: system_input.h:71
OutputPortListenerInterface is an interface that consumers of an output port must satisfy to receive ...
Definition: system_output.h:22
FreestandingInputPort(std::unique_ptr< VectorInterface< T >> vector_data, double sample_time_sec)
Constructs a FreestandingInputPort with the given sample rate sample_time_sec, which should be zero f...
Definition: system_input.h:121
int64_t get_version() const override
Returns the value version of the connected output port.
Definition: system_input.h:82
virtual int64_t get_version() const =0
Returns a positive number that increases monotonically, and changes whenever the data on this port ch...
double get_sample_time_sec() const override
Returns the sampling interval of this port in seconds, or zero if this port is continuous.
Definition: system_input.h:138
The DependentInputPort wraps a pointer to the OutputPort of a System for use as an input to another S...
Definition: system_input.h:66
FreestandingInputPort(std::unique_ptr< VectorInterface< T >> vector_data)
Constructs a continuous FreestandingInputPort.
Definition: system_input.h:114
const VectorInterface< T > * get_vector_data() const override
Returns the vector data on this port, or nullptr if this port is not vector-valued or not connected...
Definition: system_input.h:90
virtual double get_sample_time_sec() const =0
Returns the sampling interval of this port in seconds, or zero if this port is continuous.
The OutputPort represents a data output from a System.
Definition: system_output.h:95
const VectorInterface< T > * get_vector_data() const override
Returns the vector data on this port, or nullptr if this port is not vector-valued or not connected...
Definition: system_input.h:142
The InputPort describes a single input to a System, from another System or from an external driver...
Definition: system_input.h:20
InputPort()
Definition: system_input.h:54