forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput_port_base.h
102 lines (84 loc) · 3.7 KB
/
input_port_base.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#pragma once
#include <optional>
#include <string>
#include "drake/common/random.h"
#include "drake/systems/framework/context_base.h"
#include "drake/systems/framework/framework_common.h"
#include "drake/systems/framework/port_base.h"
namespace drake {
namespace systems {
/** An InputPort is a System resource that describes the kind of input a
System accepts, on a given port. It does not directly contain any runtime
input port data; that is always contained in a Context. The actual value will
be either the value of an OutputPort to which this is connected, or a fixed
value set in a Context.
%InputPortBase is the scalar type-independent part of an InputPort. */
class InputPortBase : public PortBase {
public:
DRAKE_NO_COPY_NO_MOVE_NO_ASSIGN(InputPortBase)
~InputPortBase() override;
/** Returns the index of this input port within the owning System. For a
Diagram, this will be the index within the Diagram, _not_ the index within
a LeafSystem whose input port was exported. */
InputPortIndex get_index() const { return InputPortIndex(get_int_index()); }
/** Returns true if this is a random port. */
bool is_random() const { return static_cast<bool>(random_type_); }
/** Returns the RandomDistribution if this is a random port. */
std::optional<RandomDistribution> get_random_type() const {
return random_type_;
}
// A using-declaration adds these methods into our class's Doxygen.
// (Placed in an order that makes sense for the class's table of contents.)
using PortBase::get_name;
using PortBase::GetFullDescription;
using PortBase::get_data_type;
using PortBase::size;
using PortBase::ticket;
protected:
/** Signature of a function suitable for returning the cached value of a
particular input port. Will return nullptr if the port is not connected. */
using EvalAbstractCallback =
std::function<const AbstractValue*(const ContextBase&)>;
/** Provides derived classes the ability to set the base class members at
construction.
@param owning_system
The System that owns this input port.
@param name
A name for the port. Input port names should be non-empty and unique
within a single System.
@param index
The index to be assigned to this InputPort.
@param ticket
The DependencyTicket to be assigned to this InputPort.
@param data_type
Whether the port described is vector- or abstract-valued.
@param size
If the port described is vector-valued, the number of elements. Ignored for
abstract-valued ports.
@param random_type
Input ports may optionally be labeled as random, if the port is intended to
model a random-source "noise" or "disturbance" input. */
InputPortBase(
internal::SystemMessageInterface* owning_system, std::string name,
InputPortIndex index, DependencyTicket ticket, PortDataType data_type,
int size, const std::optional<RandomDistribution>& random_type,
EvalAbstractCallback eval);
/** Evaluate this port; throws an exception if the port is not connected. */
const AbstractValue& DoEvalRequired(const ContextBase& context) const {
const AbstractValue* const result = eval_(context);
if (!result) { ThrowRequiredMissing(); }
return *result;
}
/** Evaluate this port; returns nullptr if the port is not connected. */
const AbstractValue* DoEvalOptional(const ContextBase& context) const {
return eval_(context);
}
/** Throws an exception that this port is not connected, but was expected to
be connected (i.e., an Eval caller expected that it was always connected). */
[[noreturn]] void ThrowRequiredMissing() const;
private:
const EvalAbstractCallback eval_;
const std::optional<RandomDistribution> random_type_;
};
} // namespace systems
} // namespace drake