-
Notifications
You must be signed in to change notification settings - Fork 7
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
AlexanderTonn
wants to merge
1
commit into
janelia-arduino:master
Choose a base branch
from
AlexanderTonn:at-outOfRange
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* @file at.ino | ||
* @author Alexander Tonn (https://github.com/AlexanderTonn) | ||
* @brief This example shows the usage of ::at Operator | ||
* ! If the inserted index is out of range, the program will return the value of the last index | ||
* ! Code tested with Arduino MEGA2560 | ||
* @date 2024-01-05 | ||
*/ | ||
|
||
#include "Array.h" | ||
|
||
Array<uint32_t,12> test1 = {{34,32,66,34,35,23,44,78,56,240,2323,234}}; | ||
const Array<uint32_t,10> test2 = {{1,7,3,7,35,23,44,78,56,240}}; | ||
Array<float,5> test3 = {{1.45,425.0,234.9,234.4,567.4}}; | ||
Array<float,8> test4 = {{false,false,true,true,false,true,true,true}}; | ||
void setup() | ||
{ | ||
Serial.begin(9600); | ||
|
||
// Try to Access an out of range index | ||
Serial.println(test1.at(16)); | ||
Serial.println(test2.at(14)); | ||
Serial.println(test3.at(10)); | ||
Serial.println(test4.at(20)); | ||
|
||
Serial.println("########"); | ||
|
||
//Try to Access an in range index | ||
Serial.println(test1.at(5)); | ||
Serial.println(test2.at(3)); | ||
Serial.println(test3.at(2)); | ||
Serial.println(test4.at(6)); | ||
|
||
} | ||
|
||
void loop() | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,14 +66,32 @@ template <typename T, | |
size_t MAX_SIZE> | ||
const T & Array<T,MAX_SIZE>::at(size_t index) const | ||
{ | ||
return values_[index]; | ||
auto arraySize = sizeof(values_)/sizeof(values_[0]); | ||
|
||
if(index < 0 || index >= arraySize) | ||
{ | ||
Serial.println(" Array Index out of Range! Last valid Index will be returned."); | ||
|
||
auto outOfRangeIndex = arraySize-1; | ||
return values_[outOfRangeIndex]; | ||
}else | ||
return values_[index]; | ||
|
||
} | ||
|
||
template <typename T, | ||
size_t MAX_SIZE> | ||
T & Array<T,MAX_SIZE>::at(size_t index) | ||
{ | ||
return values_[index]; | ||
auto arraySize = sizeof(values_)/sizeof(values_[0]); | ||
if(index < 0 || index >= arraySize) | ||
{ | ||
Serial.println(" Array Index out of Range! Last valid Index will be returned."); | ||
|
||
auto outOfRangeIndex = arraySize-1; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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];
|
||
return values_[outOfRangeIndex]; | ||
}else | ||
return values_[index]; | ||
} | ||
|
||
template <typename T, | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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