Drake
value.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <memory>
4 #include <stdexcept>
5 #include <string>
6 #include <typeinfo>
7 
9 #include "drake/drakeSystemFramework_export.h"
10 
11 namespace drake {
12 namespace systems {
13 
14 template <typename T>
15 class Value;
16 
22 class DRAKESYSTEMFRAMEWORK_EXPORT AbstractValue {
23  public:
25  virtual ~AbstractValue() {}
26 
28  virtual std::unique_ptr<AbstractValue> Clone() const = 0;
29 
34  template <typename T>
35  const T& GetValue() const {
36  return DownCastOrMaybeThrow<T>()->get_value();
37  }
38 
45  template <typename T>
47  return DownCastMutableOrMaybeThrow<T>()->get_mutable_value();
48  }
49 
55  template <typename T>
56  void SetValue(const T& value_to_set) {
57  DownCastMutableOrMaybeThrow<T>()->set_value(value_to_set);
58  }
59 
60  private:
61  // Casts this class to a Value<T>*. In Debug builds, throws
62  // std::bad_cast if the cast fails.
63  template <typename T>
64  Value<T>* DownCastMutableOrMaybeThrow() {
65  // We cast away const in this private non-const method so that we can reuse
66  // DownCastOrMaybeThrow. This is equivalent to duplicating the logic of
67  // DownCastOrMaybeThrow with a non-const target type.
68  return const_cast<Value<T>*>(DownCastOrMaybeThrow<T>());
69  }
70 
71  // Casts this class to a const Value<T>*. In Debug builds, throws
72  // std::logic_error if the cast fails.
73  // TODO(david-german-tri): Use static_cast in Release builds for speed.
74  template <typename T>
75  const Value<T>* DownCastOrMaybeThrow() const {
76  const Value<T>* value = dynamic_cast<const Value<T>*>(this);
77  if (value == nullptr) {
78  throw std::logic_error(
79  "AbstractValue::DownCastOrMaybeThrow(): Can't downcast to type " +
80  NiceTypeName::Get<T>() + ".");
81  }
82  return value;
83  }
84 };
85 
90 template <typename T>
91 class Value : public AbstractValue {
92  public:
93  explicit Value(const T& v) : value_(v) {}
94 
95  // Values are copyable but not moveable.
96  Value(const Value<T>& other) = default;
97  Value& operator=(const Value<T>& other) = default;
98  Value(Value<T>&& other) = delete;
99  Value& operator=(Value<T>&& other) = delete;
100 
101  std::unique_ptr<AbstractValue> Clone() const override {
102  return std::make_unique<Value<T>>(*this);
103  }
104 
106  const T& get_value() const { return value_; }
107 
109  T* get_mutable_value() { return &value_; }
110 
112  void set_value(const T& v) { value_ = v; }
113 
114  private:
115  T value_;
116 };
117 
118 } // namespace systems
119 } // namespace drake
void SetValue(const T &value_to_set)
Sets the value wrapped in this AbstractValue, which must be of exactly type T.
Definition: value.h:56
std::unique_ptr< AbstractValue > Clone() const override
Returns a copy of this AbstractValue.
Definition: value.h:101
Definition: constants.h:3
Value(const T &v)
Definition: value.h:93
virtual ~AbstractValue()
Definition: value.h:25
const T & GetValue() const
Returns the value wrapped in this AbstractValue, which must be of exactly type T. ...
Definition: value.h:35
AbstractValue()
Definition: value.h:24
T * get_mutable_value()
Returns a mutable pointer to the stored value (never null).
Definition: value.h:109
T * GetMutableValue()
Returns the value wrapped in this AbstractValue, which must be of exactly type T. ...
Definition: value.h:46
void set_value(const T &v)
Replaces the stored value with a new one.
Definition: value.h:112
A fully type-erased container class.
Definition: value.h:22
A container class for an arbitrary type T.
Definition: value.h:15
const T & get_value() const
Returns a const reference to the stored value.
Definition: value.h:106