Drake
vector_interface.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cstdint>
4 #include <memory>
5 
8 
9 #include <Eigen/Dense>
10 
11 namespace drake {
12 namespace systems {
13 
22 
23 // TODO(sherm1) Currently these must be contiguous. Is that reasonable?
24 template <typename T>
26  public:
27  virtual ~VectorInterface() {}
28 
31  virtual int size() const = 0;
32 
37  virtual void set_value(const Eigen::Ref<const VectorX<T>>& value) = 0;
38 
40  virtual Eigen::VectorBlock<const VectorX<T>> get_value() const = 0;
41 
44  virtual Eigen::VectorBlock<VectorX<T>> get_mutable_value() = 0;
45 
48  virtual std::unique_ptr<VectorInterface<T>> Clone() const = 0;
49 
50  protected:
52 
53  private:
54  // VectorInterface objects are neither copyable nor moveable.
55  VectorInterface(const VectorInterface<T>& other) = delete;
56  VectorInterface& operator=(const VectorInterface<T>& other) = delete;
57  VectorInterface(VectorInterface<T>&& other) = delete;
58  VectorInterface& operator=(VectorInterface<T>&& other) = delete;
59 };
60 
66 // TODO(sherm1) This wrapper would not be necessary if we had a smart
67 // pointer for adding object semantics to abstract objects that support Clone(),
68 // say copy_unique_ptr<T>. Consider adapting Simbody's ClonePtr<T>.
69 template <typename T>
70 class VectorObject {
71  public:
73  VectorObject() noexcept {}
74 
76  explicit VectorObject(std::unique_ptr<VectorInterface<T>> vector) noexcept
77  : vector_(std::move(vector)) {}
78 
81  VectorObject(const VectorObject& source) : VectorObject() {
82  if (!source.empty())
83  vector_ = source.get_vector().Clone();
84  }
85 
87  VectorObject(VectorObject&& source) noexcept
88  : vector_(std::move(source.vector_)) {}
89 
93  vector_.reset(); // Delete current vector.
94  if (!source.empty()) vector_ = source.get_vector().Clone();
95  return *this;
96  }
97 
99  VectorObject& operator=(VectorObject&& source) noexcept {
100  vector_ = std::move(source.vector_);
101  return *this;
102  }
103 
109  if (empty())
110  throw std::logic_error("VectorObject::get_vector(): object is empty.");
111  return *vector_;
112  }
113 
119  if (empty()) {
120  throw std::logic_error(
121  "VectorObject::get_mutable_vector(): object is empty.");
122  }
123  return vector_.get();
124  }
125 
129  bool empty() const noexcept { return !vector_; }
130 
131  private:
132  std::unique_ptr<VectorInterface<T>> vector_;
133 };
134 
136 template <typename T>
138  const AbstractValue& value) {
139  const VectorObject<T>& object = value.GetValue<VectorObject<T>>();
140  return object.get_vector();
141 }
142 
144 template <typename T>
146  VectorObject<T>* object = value->GetMutableValue<VectorObject<T>>();
147  return object->get_mutable_vector();
148 }
149 
150 } // namespace systems
151 } // namespace drake
VectorInterface is a pure abstract interface that real-valued signals between Systems must satisfy...
Definition: vector_interface.h:25
virtual void set_value(const Eigen::Ref< const VectorX< T >> &value)=0
Sets the vector to the given value.
This file contains abbreviated definitions for certain specializations of Eigen::Matrix that are comm...
Definition: constants.h:3
VectorInterface< T > * to_mutable_vector_interface(AbstractValue *value)
Strip off the VectorObject so we can see the VectorInterface.
Definition: vector_interface.h:145
VectorInterface()
Definition: vector_interface.h:51
VectorObject(const VectorObject &source)
Copy constructor uses the source object&#39;s Clone() method to make a deep copy which is then owned by t...
Definition: vector_interface.h:81
VectorObject & operator=(VectorObject &&source) noexcept
Move assignment leaves source empty.
Definition: vector_interface.h:99
STL namespace.
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > VectorX
A column vector of any size, templated on scalar type.
Definition: eigen_types.h:36
virtual Eigen::VectorBlock< VectorX< T > > get_mutable_value()=0
Returns a reference that allows mutation of the values in this vector, but does not allow resizing th...
virtual std::unique_ptr< VectorInterface< T > > Clone() const =0
Copies the entire vector to a new VectorInterface, with the same concrete implementation type...
VectorObject(std::unique_ptr< VectorInterface< T >> vector) noexcept
Takes over ownership of the provided VectorInterface object.
Definition: vector_interface.h:76
virtual Eigen::VectorBlock< const VectorX< T > > get_value() const =0
Returns a column vector containing the entire value of the signal.
bool empty() const noexcept
Returns true if this VectorObject does not own a VectorInterface object.
Definition: vector_interface.h:129
const T & GetValue() const
Returns the value wrapped in this AbstractValue, which must be of exactly type T. ...
Definition: value.h:35
virtual ~VectorInterface()
Definition: vector_interface.h:27
VectorObject & operator=(const VectorObject &source)
Copy assignment replaces the current contents of this VectorObject with a clone of the source object...
Definition: vector_interface.h:92
T * GetMutableValue()
Returns the value wrapped in this AbstractValue, which must be of exactly type T. ...
Definition: value.h:46
This concrete class provides object semantics to an abstract VectorInterface by implementing a copy c...
Definition: vector_interface.h:70
A fully type-erased container class.
Definition: value.h:22
const VectorInterface< T > & get_vector() const
Returns a const reference to the VectorInterface object owned by this VectorObject, if any.
Definition: vector_interface.h:108
VectorInterface< T > * get_mutable_vector()
Returns a mutable pointer to the VectorInterface object owned by this VectorObject, if any.
Definition: vector_interface.h:118
VectorObject(VectorObject &&source) noexcept
Move construction leaves source empty.
Definition: vector_interface.h:87
virtual int size() const =0
Returns the size of the vector, which must be equal to the number of rows in get_value().
VectorObject() noexcept
Constructs an empty VectorObject.
Definition: vector_interface.h:73
const VectorInterface< T > & to_vector_interface(const AbstractValue &value)
Strip off the VectorObject so we can see the VectorInterface.
Definition: vector_interface.h:137