robot-rust 0.2.1

The robot-rust project, bindings for wpilib, ctre, and rev in rust.
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
// This file is part of Eigen, a lightweight C++ template library
// for linear algebra.
//
// Copyright (C) 2017 Gael Guennebaud <gael.guennebaud@inria.fr>
//
// This Source Code Form is subject to the terms of the Mozilla
// Public License v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.

#ifndef EIGEN_SYMBOLIC_INDEX_H
#define EIGEN_SYMBOLIC_INDEX_H

// IWYU pragma: private
#include "../InternalHeaderCheck.h"

namespace Eigen {

/** \namespace Eigen::symbolic
 * \ingroup Core_Module
 *
 * This namespace defines a set of classes and functions to build and evaluate symbolic expressions of scalar type
 * Index. Here is a simple example:
 *
 * \code
 * // First step, defines symbols:
 * struct x_tag {};  static const symbolic::SymbolExpr<x_tag> x;
 * struct y_tag {};  static const symbolic::SymbolExpr<y_tag> y;
 * struct z_tag {};  static const symbolic::SymbolExpr<z_tag> z;
 *
 * // Defines an expression:
 * auto expr = (x+3)/y+z;
 *
 * // And evaluate it: (c++14)
 * std::cout << expr.eval(x=6,y=3,z=-13) << "\n";
 *
 * \endcode
 *
 * It is currently only used internally to define and manipulate the
 * Eigen::placeholders::last and Eigen::placeholders::lastp1 symbols in
 * Eigen::seq and Eigen::seqN.
 *
 */
namespace symbolic {

template <typename Tag>
class Symbol;
template <typename Tag, typename Type>
class SymbolValue;
template <typename Arg0>
class NegateExpr;
template <typename Arg1, typename Arg2>
class AddExpr;
template <typename Arg1, typename Arg2>
class ProductExpr;
template <typename Arg1, typename Arg2>
class QuotientExpr;
template <typename IndexType = Index>
class ValueExpr;

/** \class BaseExpr
 * \ingroup Core_Module
 * Common base class of any symbolic expressions
 */
template <typename Derived_>
class BaseExpr {
 public:
  using Derived = Derived_;
  constexpr const Derived& derived() const { return *static_cast<const Derived*>(this); }

  /** Evaluate the expression given the \a values of the symbols.
   *
   * \param values defines the values of the symbols, as constructed by SymbolExpr::operator= operator.
   *
   */
  template <typename... Tags, typename... Types>
  constexpr Index eval(const SymbolValue<Tags, Types>&... values) const {
    return derived().eval_impl(values...);
  }

  /** Evaluate the expression at compile time given the \a values of the symbols.
   *
   * If a value is not known at compile-time, returns Eigen::Undefined.
   *
   */
  template <typename... Tags, typename... Types>
  static constexpr Index eval_at_compile_time(const SymbolValue<Tags, Types>&...) {
    return Derived::eval_at_compile_time_impl(SymbolValue<Tags, Types>{}...);
  }

  constexpr NegateExpr<Derived> operator-() const { return NegateExpr<Derived>(derived()); }

  constexpr AddExpr<Derived, ValueExpr<>> operator+(Index b) const {
    return AddExpr<Derived, ValueExpr<>>(derived(), b);
  }
  constexpr AddExpr<Derived, ValueExpr<>> operator-(Index a) const {
    return AddExpr<Derived, ValueExpr<>>(derived(), -a);
  }
  constexpr ProductExpr<Derived, ValueExpr<>> operator*(Index a) const {
    return ProductExpr<Derived, ValueExpr<>>(derived(), a);
  }
  constexpr QuotientExpr<Derived, ValueExpr<>> operator/(Index a) const {
    return QuotientExpr<Derived, ValueExpr<>>(derived(), a);
  }

  friend constexpr AddExpr<Derived, ValueExpr<>> operator+(Index a, const BaseExpr& b) {
    return AddExpr<Derived, ValueExpr<>>(b.derived(), a);
  }
  friend constexpr AddExpr<NegateExpr<Derived>, ValueExpr<>> operator-(Index a, const BaseExpr& b) {
    return AddExpr<NegateExpr<Derived>, ValueExpr<>>(-b.derived(), a);
  }
  friend constexpr ProductExpr<ValueExpr<>, Derived> operator*(Index a, const BaseExpr& b) {
    return ProductExpr<ValueExpr<>, Derived>(a, b.derived());
  }
  friend constexpr QuotientExpr<ValueExpr<>, Derived> operator/(Index a, const BaseExpr& b) {
    return QuotientExpr<ValueExpr<>, Derived>(a, b.derived());
  }

  template <int N>
  constexpr AddExpr<Derived, ValueExpr<internal::FixedInt<N>>> operator+(internal::FixedInt<N>) const {
    return AddExpr<Derived, ValueExpr<internal::FixedInt<N>>>(derived(), ValueExpr<internal::FixedInt<N>>());
  }
  template <int N>
  constexpr AddExpr<Derived, ValueExpr<internal::FixedInt<-N>>> operator-(internal::FixedInt<N>) const {
    return AddExpr<Derived, ValueExpr<internal::FixedInt<-N>>>(derived(), ValueExpr<internal::FixedInt<-N>>());
  }
  template <int N>
  constexpr ProductExpr<Derived, ValueExpr<internal::FixedInt<N>>> operator*(internal::FixedInt<N>) const {
    return ProductExpr<Derived, ValueExpr<internal::FixedInt<N>>>(derived(), ValueExpr<internal::FixedInt<N>>());
  }
  template <int N>
  constexpr QuotientExpr<Derived, ValueExpr<internal::FixedInt<N>>> operator/(internal::FixedInt<N>) const {
    return QuotientExpr<Derived, ValueExpr<internal::FixedInt<N>>>(derived(), ValueExpr<internal::FixedInt<N>>());
  }

  template <int N>
  friend constexpr AddExpr<Derived, ValueExpr<internal::FixedInt<N>>> operator+(internal::FixedInt<N>,
                                                                                const BaseExpr& b) {
    return AddExpr<Derived, ValueExpr<internal::FixedInt<N>>>(b.derived(), ValueExpr<internal::FixedInt<N>>());
  }
  template <int N>
  friend constexpr AddExpr<NegateExpr<Derived>, ValueExpr<internal::FixedInt<N>>> operator-(internal::FixedInt<N>,
                                                                                            const BaseExpr& b) {
    return AddExpr<NegateExpr<Derived>, ValueExpr<internal::FixedInt<N>>>(-b.derived(),
                                                                          ValueExpr<internal::FixedInt<N>>());
  }
  template <int N>
  friend constexpr ProductExpr<ValueExpr<internal::FixedInt<N>>, Derived> operator*(internal::FixedInt<N>,
                                                                                    const BaseExpr& b) {
    return ProductExpr<ValueExpr<internal::FixedInt<N>>, Derived>(ValueExpr<internal::FixedInt<N>>(), b.derived());
  }
  template <int N>
  friend constexpr QuotientExpr<ValueExpr<internal::FixedInt<N>>, Derived> operator/(internal::FixedInt<N>,
                                                                                     const BaseExpr& b) {
    return QuotientExpr<ValueExpr<internal::FixedInt<N>>, Derived>(ValueExpr<internal::FixedInt<N>>(), b.derived());
  }

  template <typename OtherDerived>
  constexpr AddExpr<Derived, OtherDerived> operator+(const BaseExpr<OtherDerived>& b) const {
    return AddExpr<Derived, OtherDerived>(derived(), b.derived());
  }

  template <typename OtherDerived>
  constexpr AddExpr<Derived, NegateExpr<OtherDerived>> operator-(const BaseExpr<OtherDerived>& b) const {
    return AddExpr<Derived, NegateExpr<OtherDerived>>(derived(), -b.derived());
  }

  template <typename OtherDerived>
  constexpr ProductExpr<Derived, OtherDerived> operator*(const BaseExpr<OtherDerived>& b) const {
    return ProductExpr<Derived, OtherDerived>(derived(), b.derived());
  }

  template <typename OtherDerived>
  constexpr QuotientExpr<Derived, OtherDerived> operator/(const BaseExpr<OtherDerived>& b) const {
    return QuotientExpr<Derived, OtherDerived>(derived(), b.derived());
  }
};

template <typename T>
struct is_symbolic {
  // BaseExpr has no conversion ctor, so we only have to check whether T can be statically cast to its base class
  // BaseExpr<T>.
  enum { value = internal::is_convertible<T, BaseExpr<T>>::value };
};

// A simple wrapper around an integral value to provide the eval method.
// We could also use a free-function symbolic_eval...
template <typename IndexType>
class ValueExpr : BaseExpr<ValueExpr<IndexType>> {
 public:
  constexpr ValueExpr() = default;
  constexpr ValueExpr(IndexType val) : value_(val) {}
  template <typename... Tags, typename... Types>
  constexpr IndexType eval_impl(const SymbolValue<Tags, Types>&...) const {
    return value_;
  }
  template <typename... Tags, typename... Types>
  static constexpr IndexType eval_at_compile_time_impl(const SymbolValue<Tags, Types>&...) {
    return IndexType(Undefined);
  }

 protected:
  IndexType value_;
};

// Specialization for compile-time value,
// It is similar to ValueExpr(N) but this version helps the compiler to generate better code.
template <int N>
class ValueExpr<internal::FixedInt<N>> : public BaseExpr<ValueExpr<internal::FixedInt<N>>> {
 public:
  constexpr ValueExpr() = default;
  constexpr ValueExpr(internal::FixedInt<N>) {}
  template <typename... Tags, typename... Types>
  constexpr Index eval_impl(const SymbolValue<Tags, Types>&...) const {
    return Index(N);
  }
  template <typename... Tags, typename... Types>
  static constexpr Index eval_at_compile_time_impl(const SymbolValue<Tags, Types>&...) {
    return Index(N);
  }
};

/** Represents the actual value of a symbol identified by its tag
 *
 * It is the return type of SymbolValue::operator=, and most of the time this is only way it is used.
 */
template <typename Tag, typename Type>
class SymbolValue : public BaseExpr<SymbolValue<Tag, Type>> {};

template <typename Tag>
class SymbolValue<Tag, Index> : public BaseExpr<SymbolValue<Tag, Index>> {
 public:
  constexpr SymbolValue() = default;

  /** Default constructor from the value \a val */
  constexpr SymbolValue(Index val) : value_(val) {}

  /** \returns the stored value of the symbol */
  constexpr Index value() const { return value_; }

  /** \returns the stored value of the symbol at compile time, or Undefined if not known. */
  static constexpr Index value_at_compile_time() { return Index(Undefined); }

  template <typename... Tags, typename... Types>
  constexpr Index eval_impl(const SymbolValue<Tags, Types>&...) const {
    return value();
  }

  template <typename... Tags, typename... Types>
  static constexpr Index eval_at_compile_time_impl(const SymbolValue<Tags, Types>&...) {
    return value_at_compile_time();
  }

 protected:
  Index value_;
};

template <typename Tag, int N>
class SymbolValue<Tag, internal::FixedInt<N>> : public BaseExpr<SymbolValue<Tag, internal::FixedInt<N>>> {
 public:
  constexpr SymbolValue() = default;

  /** Default constructor from the value \a val */
  constexpr SymbolValue(internal::FixedInt<N>) {}

  /** \returns the stored value of the symbol */
  constexpr Index value() const { return static_cast<Index>(N); }

  /** \returns the stored value of the symbol at compile time, or Undefined if not known. */
  static constexpr Index value_at_compile_time() { return static_cast<Index>(N); }

  template <typename... Tags, typename... Types>
  constexpr Index eval_impl(const SymbolValue<Tags, Types>&...) const {
    return value();
  }

  template <typename... Tags, typename... Types>
  static constexpr Index eval_at_compile_time_impl(const SymbolValue<Tags, Types>&...) {
    return value_at_compile_time();
  }
};

// Find and return a symbol value based on the tag.
template <typename Tag, typename... Types>
struct EvalSymbolValueHelper;

// Empty base case, symbol not found.
template <typename Tag>
struct EvalSymbolValueHelper<Tag> {
  static constexpr Index eval_impl() {
    eigen_assert(false && "Symbol not found.");
    return Index(Undefined);
  }
  static constexpr Index eval_at_compile_time_impl() { return Index(Undefined); }
};

// We found a symbol value matching the provided Tag!
template <typename Tag, typename Type, typename... OtherTypes>
struct EvalSymbolValueHelper<Tag, SymbolValue<Tag, Type>, OtherTypes...> {
  static constexpr Index eval_impl(const SymbolValue<Tag, Type>& symbol, const OtherTypes&...) {
    return symbol.value();
  }
  static constexpr Index eval_at_compile_time_impl(const SymbolValue<Tag, Type>& symbol, const OtherTypes&...) {
    return symbol.value_at_compile_time();
  }
};

// No symbol value in first value, recursive search starting with next.
template <typename Tag, typename T1, typename... OtherTypes>
struct EvalSymbolValueHelper<Tag, T1, OtherTypes...> {
  static constexpr Index eval_impl(const T1&, const OtherTypes&... values) {
    return EvalSymbolValueHelper<Tag, OtherTypes...>::eval_impl(values...);
  }
  static constexpr Index eval_at_compile_time_impl(const T1&, const OtherTypes&...) {
    return EvalSymbolValueHelper<Tag, OtherTypes...>::eval_at_compile_time_impl(OtherTypes{}...);
  }
};

/** Expression of a symbol uniquely identified by the template parameter type \c tag */
template <typename tag>
class SymbolExpr : public BaseExpr<SymbolExpr<tag>> {
 public:
  /** Alias to the template parameter \c tag */
  typedef tag Tag;

  constexpr SymbolExpr() = default;

  /** Associate the value \a val to the given symbol \c *this, uniquely identified by its \c Tag.
   *
   * The returned object should be passed to ExprBase::eval() to evaluate a given expression with this specified
   * runtime-time value.
   */
  constexpr SymbolValue<Tag, Index> operator=(Index val) const { return SymbolValue<Tag, Index>(val); }

  template <int N>
  constexpr SymbolValue<Tag, internal::FixedInt<N>> operator=(internal::FixedInt<N>) const {
    return SymbolValue<Tag, internal::FixedInt<N>>{internal::FixedInt<N>{}};
  }

  template <typename... Tags, typename... Types>
  constexpr Index eval_impl(const SymbolValue<Tags, Types>&... values) const {
    return EvalSymbolValueHelper<Tag, SymbolValue<Tags, Types>...>::eval_impl(values...);
  }

  template <typename... Tags, typename... Types>
  static constexpr Index eval_at_compile_time_impl(const SymbolValue<Tags, Types>&...) {
    return EvalSymbolValueHelper<Tag, SymbolValue<Tags, Types>...>::eval_at_compile_time_impl(
        SymbolValue<Tags, Types>{}...);
  }
};

template <typename Arg0>
class NegateExpr : public BaseExpr<NegateExpr<Arg0>> {
 public:
  constexpr NegateExpr() = default;
  constexpr NegateExpr(const Arg0& arg0) : m_arg0(arg0) {}

  template <typename... Tags, typename... Types>
  constexpr Index eval_impl(const SymbolValue<Tags, Types>&... values) const {
    return -m_arg0.eval_impl(values...);
  }

  template <typename... Tags, typename... Types>
  static constexpr Index eval_at_compile_time_impl(const SymbolValue<Tags, Types>&...) {
    constexpr Index v = Arg0::eval_at_compile_time_impl(SymbolValue<Tags, Types>{}...);
    return (v == Undefined) ? Undefined : -v;
  }

 protected:
  Arg0 m_arg0;
};

template <typename Arg0, typename Arg1>
class AddExpr : public BaseExpr<AddExpr<Arg0, Arg1>> {
 public:
  constexpr AddExpr() = default;
  constexpr AddExpr(const Arg0& arg0, const Arg1& arg1) : m_arg0(arg0), m_arg1(arg1) {}

  template <typename... Tags, typename... Types>
  constexpr Index eval_impl(const SymbolValue<Tags, Types>&... values) const {
    return m_arg0.eval_impl(values...) + m_arg1.eval_impl(values...);
  }

  template <typename... Tags, typename... Types>
  static constexpr Index eval_at_compile_time_impl(const SymbolValue<Tags, Types>&...) {
    constexpr Index v0 = Arg0::eval_at_compile_time_impl(SymbolValue<Tags, Types>{}...);
    constexpr Index v1 = Arg1::eval_at_compile_time_impl(SymbolValue<Tags, Types>{}...);
    return (v0 == Undefined || v1 == Undefined) ? Undefined : v0 + v1;
  }

 protected:
  Arg0 m_arg0;
  Arg1 m_arg1;
};

template <typename Arg0, typename Arg1>
class ProductExpr : public BaseExpr<ProductExpr<Arg0, Arg1>> {
 public:
  constexpr ProductExpr() = default;
  constexpr ProductExpr(const Arg0& arg0, const Arg1& arg1) : m_arg0(arg0), m_arg1(arg1) {}

  template <typename... Tags, typename... Types>
  constexpr Index eval_impl(const SymbolValue<Tags, Types>&... values) const {
    return m_arg0.eval_impl(values...) * m_arg1.eval_impl(values...);
  }

  template <typename... Tags, typename... Types>
  static constexpr Index eval_at_compile_time_impl(const SymbolValue<Tags, Types>&...) {
    constexpr Index v0 = Arg0::eval_at_compile_time_impl(SymbolValue<Tags, Types>{}...);
    constexpr Index v1 = Arg1::eval_at_compile_time_impl(SymbolValue<Tags, Types>{}...);
    return (v0 == Undefined || v1 == Undefined) ? Undefined : v0 * v1;
  }

 protected:
  Arg0 m_arg0;
  Arg1 m_arg1;
};

template <typename Arg0, typename Arg1>
class QuotientExpr : public BaseExpr<QuotientExpr<Arg0, Arg1>> {
 public:
  constexpr QuotientExpr() = default;
  constexpr QuotientExpr(const Arg0& arg0, const Arg1& arg1) : m_arg0(arg0), m_arg1(arg1) {}

  template <typename... Tags, typename... Types>
  constexpr Index eval_impl(const SymbolValue<Tags, Types>&... values) const {
    return m_arg0.eval_impl(values...) / m_arg1.eval_impl(values...);
  }

  template <typename... Tags, typename... Types>
  static constexpr Index eval_at_compile_time_impl(const SymbolValue<Tags, Types>&...) {
    constexpr Index v0 = Arg0::eval_at_compile_time_impl(SymbolValue<Tags, Types>{}...);
    constexpr Index v1 = Arg1::eval_at_compile_time_impl(SymbolValue<Tags, Types>{}...);
    return (v0 == Undefined || v1 == Undefined) ? Undefined : v0 / v1;
  }

 protected:
  Arg0 m_arg0;
  Arg1 m_arg1;
};

}  // end namespace symbolic

}  // end namespace Eigen

#endif  // EIGEN_SYMBOLIC_INDEX_H