-
Notifications
You must be signed in to change notification settings - Fork 76
feat: 🧑💻 Check device literal port numbers at compile time #689
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
base: develop-pros-4
Are you sure you want to change the base?
feat: 🧑💻 Check device literal port numbers at compile time #689
Conversation
/azp run |
Azure Pipelines successfully started running 1 pipeline(s). |
Please fix this MR to only include relevant commits so its reviewable. |
793eac0
to
9a792f0
Compare
/azp run |
Azure Pipelines successfully started running 1 pipeline(s). |
// from_chars doesn't seem to handle hex/binary/octal prefixes, | ||
// so we handle them ourselves |
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.
This sucks lmao, good catch though
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.
Turns out it does, I failed to read the docs lol. Fixed in bea9c44.
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.
oh thank god
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.
Turns out I failed to read the docs doubly lol: from_chars does indeed NOT auto detect base womp womp
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.
lol? so what, we need to add back in the 0b
and 0x
detection? that sucks bruh
// hex literal | ||
base = 16; |
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.
Does std::from_chars
handle both uppercase and lowercase hex literals?
include/pros/distance.hpp
Outdated
const pros::Distance operator"" _dist(const unsigned long long int d); | ||
template <char... Cs> | ||
const pros::Distance operator"" _dist() { | ||
static_assert(pros::detail::is_valid_port<Cs...>(), "Port is out of range!"); |
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.
Was originally unsure if this is better than requires-clause, but it probably is because it allows giving a message (and benefits of requires-clause, ie SFINAE, arent really useful here).
The message you have is not great though, because a failed from_chars
will give the same error. I imagine that this happens if you try to give it a non-integer? I would probably prefer "invalid port number" or something
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.
Same goes for other declarations
@@ -2419,7 +2426,12 @@ const pros::Motor operator"" _mtr(const unsigned long long int m); | |||
* } | |||
* \endcode | |||
*/ | |||
const pros::Motor operator"" _rmtr(const unsigned long long int m); | |||
template <char... Cs> | |||
const pros::Motor operator"" _rmtr() { |
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 wonder if theres a single pros project in the whole world that uses _rmtr
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.
/** | ||
* Returns a rotation sesnor that has the same port as the original, but reversed. | ||
* | ||
* This can only be used on a temporary object (such as a device literal). | ||
* | ||
* \b Example | ||
* \code | ||
* using namespace pros::literals; | ||
* | ||
* void opcontrol() { | ||
* pros::Rotation rotation_sensor = -1_rot; | ||
* printf("Reversed: %d \n", rotation_sensor.get_reversed()); | ||
* } | ||
* \endcode | ||
*/ | ||
Rotation operator-() && { return Rotation(-this->get_port()); } |
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.
why should this exist? can you not create negatives with the other user-defined literal operator? If you can, then this probably shouldnt exist
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.
Oh I see that its rvalue-this-qualified. still a little weird
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.
No, not for rotation sensors. Both 10_rrot
and 10_rotr
seem a bit confusing compared to -10_rot
(negative port numbers also match how the ctors work)
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.
If this is necessary for rotation, its surely necessary for motors too?
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.
hopefully its not necessary for either though lol
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.
im just confused why this doesnt work with the user-defined literal directly.
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.
Because -10_rot
is -(operator""_rot<'1','0'>())
, not operator""_rot<'-','1','0'>()
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.
Motors have _rmtr so its not needed for motors, although adding this to motors might be a good idea for consistency
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.
oh i see. yeah that sucks lmao. i think this should be added to motors as well if it has to exist.
/azp run |
Azure Pipelines successfully started running 1 pipeline(s). |
@Rocky14683 can you rerun the pipeline please? |
/azp run |
Azure Pipelines successfully started running 1 pipeline(s). |
@Rocky14683 It should compile now, can you rerun the pipeline please? |
/azp run |
Azure Pipelines successfully started running 1 pipeline(s). |
Summary:
Checks port numbers for device literals at compile time rather than at runtime. (Thanks to @djava for giving me suggestions on refactoring my mess of templates into actually readable code)
Motivation:
If something can be checked at compile time, it should be checked at compile time.
Test Plan:
Note:
This is an ABI breaking change and therefore should be a minor version bump.