Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Arduino compliant out of Range handling for the at() function #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

AlexanderTonn
Copy link

Beforehand, I have to say that I appreciate it that you've created this library! 👍

The big advantage of at() in the std::array lib is the detection of out of ranges indices. The function is throwing an exception if the inserted index is out of range of the array.

  • I added a small handling if an invalid index is passed to at(). If the UART Interface is started, the user will get an error message.
  • however in case of out of range the function will return the value of the last valid index
  • I also added an examples .ino file for showing the at() function with different data types.
  • tested with arduino MEGA 2560 R3

{
Serial.println(" Array Index out of Range! Last valid Index will be returned.");

auto outOfRangeIndex = arraySize-1;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove the local (not good named) variable and inserting the subtraction directly in the bracket operator

- auto outOfRangeIndex = arraySize-1;
- return values_[outOfRangeIndex]; 
+ return values_[arraySize-1]; 

{
Serial.println(" Array Index out of Range! Last valid Index will be returned.");

auto outOfRangeIndex = arraySize-1;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove the local (not good named) variable and inserting the subtraction directly in the bracket operator

- auto outOfRangeIndex = arraySize-1;
- return values_[outOfRangeIndex]; 
+ return values_[arraySize-1]; 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant