forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalue_checker.h
72 lines (66 loc) · 2.8 KB
/
value_checker.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
#pragma once
#include <memory>
#include <stdexcept>
#include <string>
#include "drake/common/drake_throw.h"
#include "drake/common/nice_type_name.h"
#include "drake/common/value.h"
#include "drake/systems/framework/basic_vector.h"
namespace drake {
namespace systems {
namespace internal {
/// Checks some BasicVector invariants on @p basic_vector.
///
/// Because this function uses shady implementation tricks, it should ONLY be
/// called from within DRAKE_ASSERT_VOID or unit test code.
///
/// This function is likely to be expensive (on the order of a full copy), so
/// should be used sparingly. In particular, only a few select locations
/// within the Systems Framework itself should likely call this function.
///
/// @throws std::exception if invariants are violated or basic_vector is nullptr
template <typename T>
void CheckBasicVectorInvariants(const BasicVector<T>* basic_vector) {
DRAKE_THROW_UNLESS(basic_vector != nullptr);
std::unique_ptr<BasicVector<T>> cloned_base = basic_vector->Clone();
const BasicVector<T>* const cloned_vector = cloned_base.get();
DRAKE_THROW_UNLESS(cloned_vector != nullptr);
const auto& original_type = typeid(*basic_vector);
const auto& cloned_type = typeid(*cloned_vector);
if (original_type != cloned_type) {
const std::string original_name = NiceTypeName::Get(*basic_vector);
const std::string cloned_name = NiceTypeName::Get(*cloned_vector);
throw std::runtime_error(
"CheckBasicVectorInvariants failed: " + original_name + "::Clone "
"produced a " + cloned_name + " object instead of the same type");
}
}
/// If @p abstract_value is a Value<BasicVector<T>>, then checks some
/// BasicVector invariants. Otherwise, does nothing.
///
/// Because this function uses shady implementation tricks, it should ONLY be
/// called from within DRAKE_ASSERT_VOID or unit test code.
///
/// This function is likely to be expensive (on the order of a full copy), so
/// should be used sparingly. In particular, only a few select locations
/// within the Systems Framework itself should likely call this function.
///
/// @tparam T the supposed element type of the Value<BasicVector<T>> that has
/// been erased into an AbstractValue
///
/// @throws std::exception if invariants are violated or abstract_value is
/// nullptr
template <typename T>
void CheckVectorValueInvariants(const AbstractValue* abstract_value) {
DRAKE_THROW_UNLESS(abstract_value != nullptr);
const Value<BasicVector<T>>* const vector_value =
dynamic_cast<const Value<BasicVector<T>>*>(abstract_value);
if (vector_value != nullptr) {
// We are a Value<BasicVector<T>>, so check the invariants.
const BasicVector<T>& basic_vector = vector_value->get_value();
CheckBasicVectorInvariants<T>(&basic_vector);
}
}
} // namespace internal
} // namespace systems
} // namespace drake