-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
value_producer.cc
68 lines (54 loc) · 1.83 KB
/
value_producer.cc
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
#include "drake/systems/framework/value_producer.h"
#include <stdexcept>
#include <fmt/format.h>
#include "drake/common/nice_type_name.h"
namespace drake {
namespace systems {
ValueProducer::ValueProducer() = default;
ValueProducer::ValueProducer(
AllocateCallback allocate, CalcCallback calc)
: allocate_(std::move(allocate)), calc_(std::move(calc)) {
if (allocate_ == nullptr) {
throw std::logic_error(
"Cannot create a ValueProducer with a null AllocateCallback");
}
if (calc_ == nullptr) {
throw std::logic_error(
"Cannot create a ValueProducer with a null Calc");
}
}
ValueProducer::~ValueProducer() = default;
bool ValueProducer::is_valid() const {
return (allocate_ != nullptr) && (calc_ != nullptr);
}
void ValueProducer::NoopCalc(const ContextBase&, AbstractValue*) {}
std::unique_ptr<AbstractValue> ValueProducer::Allocate() const {
if (allocate_ == nullptr) {
throw std::logic_error(
"ValueProducer cannot invoke a null AllocateCallback");
}
return allocate_();
}
void ValueProducer::Calc(const ContextBase& context,
AbstractValue* output) const {
if (output == nullptr) {
throw std::logic_error(
"ValueProducer output was nullptr");
}
if (calc_ == nullptr) {
throw std::logic_error(
"ValueProducer cannot invoke a null CalcCallback");
}
return calc_(context, output);
}
void ValueProducer::ThrowBadNull() {
throw std::logic_error("ValueProducer was given a null callback pointer");
}
void ValueProducer::ThrowBadCast(const std::type_info& actual_type,
const std::type_info& desired_type) {
throw std::logic_error(fmt::format(
"ValueProducer cannot cast a {} to a {}",
NiceTypeName::Get(actual_type), NiceTypeName::Get(desired_type)));
}
} // namespace systems
} // namespace drake