Skip to content

Commit 6d0d0f6

Browse files
finger563Copilot
andauthored
feat(joystick/vector): Update to return const ref where possible (#416)
* feat(joystick/vector): Update to return `const ref` where possible * Update `espp::Vector2d` and `espp::Joystick` to return const references where possible Reduces the amount of unnecessary copies in application code Build and run `math/example` and `joystick/example` * add missing const ref to vector * Update components/joystick/include/joystick.hpp Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * fix comments --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 14b5bf0 commit 6d0d0f6

File tree

3 files changed

+8
-6
lines changed

3 files changed

+8
-6
lines changed

components/joystick/include/joystick.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,9 @@ class Joystick : public BaseComponent {
202202
/**
203203
* @brief Get the most recently updated calibrated position.
204204
* @return The most recent position (from when update() was last called).
205+
* @note The returned reference is valid as long as the Joystick object is alive.
205206
*/
206-
espp::Vector2f position() const;
207+
const espp::Vector2f &position() const;
207208

208209
/**
209210
* @brief Get the most recently updated raw / uncalibrated readings. This
@@ -212,8 +213,9 @@ class Joystick : public BaseComponent {
212213
* structures.
213214
* @return The most recent raw measurements (from when update() was last
214215
* called).
216+
* @note The returned reference is valid as long as the Joystick object is alive.
215217
*/
216-
espp::Vector2f raw() const;
218+
const espp::Vector2f &raw() const;
217219

218220
friend struct fmt::formatter<Joystick>;
219221

components/joystick/src/joystick.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ float espp::Joystick::x() const { return position_.x(); }
7474

7575
float espp::Joystick::y() const { return position_.y(); }
7676

77-
espp::Vector2f espp::Joystick::position() const { return position_; }
77+
const espp::Vector2f &espp::Joystick::position() const { return position_; }
7878

79-
espp::Vector2f espp::Joystick::raw() const { return raw_; }
79+
const espp::Vector2f &espp::Joystick::raw() const { return raw_; }
8080

8181
void espp::Joystick::recalculate(float raw_x, float raw_y) {
8282
raw_.x(raw_x);

components/math/include/vector2d.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ template <typename T> class Vector2d {
7676
* @brief Getter for the x value.
7777
* @return The current x value.
7878
*/
79-
T x() const { return x_; }
79+
const T &x() const { return x_; }
8080

8181
/**
8282
* @brief Setter for the x value.
@@ -88,7 +88,7 @@ template <typename T> class Vector2d {
8888
* @brief Getter for the y value.
8989
* @return The current y value.
9090
*/
91-
T y() const { return y_; }
91+
const T &y() const { return y_; }
9292

9393
/**
9494
* @brief Setter for the y value.

0 commit comments

Comments
 (0)