-
Notifications
You must be signed in to change notification settings - Fork 8.5k
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
Use til::some<T,N> to replace the SomeViewports class #4174
Conversation
src/types/viewport.cpp
Outdated
@@ -6,6 +6,11 @@ | |||
|
|||
using namespace Microsoft::Console::Types; | |||
|
|||
Viewport::Viewport() noexcept : |
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 didn't intend to add this. But the compiler complains about there is no default ctor when initializing til::some<Viewport, 4>result
.
Oops forget some files. Will look into it later. |
src/types/inc/viewport.hpp
Outdated
struct SomeViewports; | ||
class Viewport; | ||
|
||
typedef til::some<Viewport, 4> SomeViewports; |
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.
nit: prefer using
for new code
src/types/inc/viewport.hpp
Outdated
|
||
class Viewport final | ||
{ | ||
public: | ||
~Viewport() {} | ||
Viewport() noexcept; |
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.
interesting! I'm surprised this became necessary; perhaps it's something we could improve in some
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've tried bypass it by using SomeViewports result{Viewport::Empty x 4}
but that would change the meaning of some
, which I assume _used
represents the number of element that's actually used. So I'm stucked here.
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.
Unless Dustin has an idea of how to avoid this, I'm OK with the default constructor making an empty one. We're going to hopefully improve Viewport
into a til::rect
or something in the future anyway and a default constructor will almost certainly be warranted at that point in time.
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. No, yeah, having a default constructor that does the same thing as Viewport::Empty
is 100% the correct thing to do- to the point where I'm wondering why we ended up doing it not-that-way :P
We should do two or three things here.
- make the constructor constexpr
- make it initialize to the same values as
Empty()
(0, 0, -1, -1
) - make it live in the header
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.
4? make Empty
just use the default ctor?
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 will take a look at it. But I don’t really get it why a default is needed for the array. Perhaps I just don’t get C++...
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.
Ah! Okay, so, std::array<T, 4>
requires that storage be allocated for four things. Since array doesn't have a use counter, there must be a valid value of type T
in every addressable index. Without a default constructor, the compiler cannot emit anything that would truly and safely initialize the values.
This is largely the same as Viewports v[4]
-- each of the four viewports will be created and default-constructed.
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.
Vector doesn't need to make this guarantee because accessing the objects beyond the end of the "used" region is undefined behavior.
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.
Thanks Dustin. My admiration for you remains the same. My love for Java & C# just got a lot deeper.
@msftbot make sure @miniksa signs off on this |
Hello @zadjii-msft! Because you've given me some instructions on how to help merge this pull request, I'll be modifying my merge approach. Here's how I understand your requirements for merging this pull request:
If this doesn't seem right to you, you can tell me to cancel these instructions and use the auto-merge policy that has been configured for this repository. Try telling me "forget everything I just told you". |
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'm OK with this. I'd like the Dustin comments driven to conclusion before merge though.
@@ -907,7 +912,7 @@ Viewport Viewport::ToOrigin() const noexcept | |||
if (!intersection.IsValid()) | |||
{ | |||
// Just put the original rectangle into the results and return early. | |||
result.viewports.at(result.used++) = original; | |||
result.push_back(original); |
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.
The compiler also complains about this throwing exception. But logically this method does not throw anything. And I'm surprised that the Substract
is not used anywhere. Should I make it non-noexcept
or just catch the exception inside it?
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, so this is an interesting one. push_back
on some
can throw an exception if it's full, so technically this one can throw an exception.
We know through static analysis (like, with our eyes) that it will never throw...
@miniksa what do you think about cases like this? This will never except until 4 becomes less than 3...
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.
Nnnnn. A few ideas all of which I don't really like....
- Make it
except
: That then pollutes all callers. Yuck. - Catch it: Seems obnoxious to write a whole try/CATCH_LOG or try/CATCH_FAIL_FAST around it.
- Suppress it: A
noexcept
that throws technically goes straight tostd::terminate
which is more or less the same as try/CATCH_FAIL_FAST. - Add another accessor onto
til::some
. I can't think of one that wouldn't also suffer from this. If you do it with just unchecked array access, now you have a different warning and needtil::at
. If you do it withsome.at()
, that would presumably throw out of bounds too. If we add any other method, then it doesn't look quite like the STL collections it is mocking.
I'm leaning toward 2. I hate 1 and 4. I really don't like 3 as it's implicit instead of explicit like 2.
Doing 2 is also generally what we do around the codebase for this problem.
Also, this applies to .ToOrigin()
below as well, right?
0d8f35d
to
7842f3a
Compare
src/types/inc/viewport.hpp
Outdated
@@ -13,15 +13,20 @@ Author(s): | |||
|
|||
#pragma once | |||
#include "../../inc/operators.hpp" | |||
#include "../../inc/til.h" |
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 i recall, this one is included by default now!
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.
It should be. This shouldn't be needed.
@@ -907,7 +912,7 @@ Viewport Viewport::ToOrigin() const noexcept | |||
if (!intersection.IsValid()) | |||
{ | |||
// Just put the original rectangle into the results and return early. | |||
result.viewports.at(result.used++) = original; | |||
result.push_back(original); |
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, so this is an interesting one. push_back
on some
can throw an exception if it's full, so technically this one can throw an exception.
We know through static analysis (like, with our eyes) that it will never throw...
@miniksa what do you think about cases like this? This will never except until 4 becomes less than 3...
(this comment represents me recognizing that there's 2 green checks on this PR already, so I'm not going to review it personally. I'll let @miniksa and @DHowett-MSFT help resolve the static analysis build issues to merge this) |
OK, this one is now waiting for @skyline75489 to come back past one more time and probably try/catch guard the thing so it can pass static analysis. @DHowett-MSFT, sound good enough (out of the options I listed?) |
7842f3a
to
187422d
Compare
Updated the PR. @DHowett-MSFT @miniksa |
Nnnnn the static analysis check. Can you create src/inc/til/at.h and move the definition of EDIT: And probably also Sorry, this is new stuff and I hadn't worked out the kinks yet. I'd edit the branch for you, but I don't seem to have permission. |
187422d
to
f8480b8
Compare
Yeah it helps. The static analysis is good now. |
/azp run |
Azure Pipelines successfully started running 1 pipeline(s). |
Hello @zadjii-msft! Because this pull request has the p.s. you can customize the way I help with merging this pull request, such as holding this pull request until a specific person approves. Simply @mention me (
|
🎉 Once again, thanks for the contribution! This pull request was included in a set of conhost changes that was just |
Summary of the Pull Request
Let's give it a test drive.
References
PR Checklist
Detailed Description of the Pull Request / Additional comments
Validation Steps Performed
Build and run it.