Drake
my_vector.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <memory>
4 #include <utility>
5 
11 
12 namespace drake {
13 namespace systems {
14 
15 /// A simple subclass of BasicVector<T> for testing, particularly for cases
16 /// where BasicVector subtyping must be preserved through the framework.
17 template <int N, typename T>
18 class MyVector : public BasicVector<T> {
19  public:
21 
22  /// Constructs an uninitialized N-vector.
23  MyVector() : BasicVector<T>(N) {}
24 
25  /// Constructs from a variable-length vector whose length must be N.
26  explicit MyVector(const VectorX<T>& data) : BasicVector<T>(data) {
27  DRAKE_DEMAND(data.size() == N);
28  }
29 
30  /// Constructs from a fixed-size Eigen VectorN.
31  explicit MyVector(const Eigen::Matrix<T, N, 1>& data)
32  : BasicVector<T>(data) {}
33 
34  /// Constructs a MyVector where each element is constructed using the
35  /// placewise-corresponding member of @p args as the sole constructor
36  /// argument. For instance: `MyVector<2, double>::Make(1.1, 2.2)`.
37  template<typename... Fargs>
38  static std::unique_ptr<MyVector> Make(Fargs&&... args) {
39  static_assert(sizeof...(args) == N,
40  "The number of arguments must match the MyVector size");
41  auto data = std::make_unique<MyVector>();
42  BasicVector<T>::MakeRecursive(data.get(), 0, args...);
43  return data;
44  }
45 
46  /// Shadows the base class Clone() method to change the return type, so that
47  /// this can be used in `copyable_unique_ptr<MyVector>` and `Value<MyVector>`.
48 
49  // TODO(jwnimmer-tri) This is extremely dangerous -- the return type of Clone
50  // determines template argument for the Value<> that is type-erased into an
51  // AbstractValue; we should not pun away from BasicVector, since many methods
52  // in the leaf system and context code assumes that BasicVector is what gets
53  // type-erased!
54  std::unique_ptr<MyVector<N, T>> Clone() const {
55  return dynamic_pointer_cast_or_throw<MyVector<N, T>>(
57  }
58 
59  private:
60  // BasicVector's Clone() method handles copying the values; DoClone() is
61  // only supposed to allocate a vector of the right concrete type and size.
62  MyVector* DoClone() const override {
63  return new MyVector();
64  }
65 };
66 
71 
72 } // namespace systems
73 } // namespace drake
MyVector()
Constructs an uninitialized N-vector.
Definition: my_vector.h:23
This file contains abbreviated definitions for certain specializations of Eigen::Matrix that are comm...
Definition: bullet_model.cc:22
MyVector(const VectorX< T > &data)
Constructs from a variable-length vector whose length must be N.
Definition: my_vector.h:26
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > VectorX
A column vector of any size, templated on scalar type.
Definition: eigen_types.h:46
static std::unique_ptr< MyVector > Make(Fargs &&...args)
Constructs a MyVector where each element is constructed using the placewise-corresponding member of a...
Definition: my_vector.h:38
std::unique_ptr< BasicVector< T > > Clone() const
Copies the entire vector to a new BasicVector, with the same concrete implementation type...
Definition: basic_vector.h:130
Provides Drake&#39;s assertion implementation.
MyVector(const Eigen::Matrix< T, N, 1 > &data)
Constructs from a fixed-size Eigen VectorN.
Definition: my_vector.h:31
#define DRAKE_DEMAND(condition)
Evaluates condition and iff the value is false will trigger an assertion failure with a message showi...
Definition: drake_assert.h:45
BasicVector is a semantics-free wrapper around an Eigen vector that satisfies VectorBase.
Definition: basic_vector.h:25
A simple subclass of BasicVector<T> for testing, particularly for cases where BasicVector subtyping m...
Definition: my_vector.h:18
std::unique_ptr< MyVector< N, T > > Clone() const
Shadows the base class Clone() method to change the return type, so that this can be used in copyable...
Definition: my_vector.h:54
boolean< T > T
Definition: drake_bool_deprecated.h:26
#define DRAKE_NO_COPY_NO_MOVE_NO_ASSIGN(Classname)
DRAKE_NO_COPY_NO_MOVE_NO_ASSIGN deletes the special member functions for copy-construction, copy-assignment, move-construction, and move-assignment.
Definition: drake_copyable.h:33
static void MakeRecursive(BasicVector< T > *data, int index, F constructor_arg, Fargs &&...recursive_args)
Sets data at index to an object of type T, which must have a single-argument constructor invoked via ...
Definition: basic_vector.h:152
Provides careful macros to selectively enable or disable the special member functions for copy-constr...
int data
Definition: value_test.cc:20