-
-
Notifications
You must be signed in to change notification settings - Fork 680
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
Add Deref(T *)
, to ease dereferencing a pointer safely
#4278
Conversation
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.
Adding a follow-up commit with uses of this new functionality are highly welcome!
{ | ||
if (ptr == nullptr) | ||
{ | ||
throw DerefError(__FILE__, |
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.
Is this file reported in the exception message, or the line where it is invoked?
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.
Yes indeed, both of them (source file and line number) are reported in the exception message.
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 meant, is file == ".../Modules/Core/Common/include/itkDeref.h", or file == ".../my/file/in/my/code/which/tried/to/deref/null.cpp"?
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 itk::DerefError
exception message proposed by this PR will have something like "Modules/Core/Common/include/itkDeref.h" in there. Of course, it would have been nicer to have something like "my/file/in/my/code/which/tried/to/deref/null.cpp". I think that can be implemented most elegantly by C++20 std::source_location 😃. Until then, I think it's still nice for users to know the location where the DerefError
is thrown, in "itkDeref.h", so that they can put a breakpoint at that location, when they are debugging the problem.
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.
Did you consider using the "itkSpecializedMessageExceptionMacro?
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 @blowekamp, please check my force-push!
/azp run ITK.Windows |
791f0bc
to
263dc98
Compare
Update: I just removed
On the other hand, |
@dave3d Can you please explain how to fix the Spell Checking failures?
At https://github.com/InsightSoftwareConsortium/ITK/actions/runs/6803791308/job/18499888354?pr=4278 |
DerefError is a new keyword that should be added to the spelling dictionary. Add it to the file .github/workflows/itk_dict.txt. |
Or perhaps use something which is "kosher", such as |
Thanks for your feedback, @dzenanz, @dave3d Personally I find "Deref" clear enough. When I google on "deref", it's all about dereferencing: https://www.google.com/search?q=deref And as "DerefError" is the error that may occur specifically when calling "Deref", I would prefer to stick with "DerefError". Hope that's acceptable to you both 🙏 @dave3d would it be sufficient to add "Deref" to .github/workflows/itk_dict.txt in order to use "DerefError"? I guess it knows "Error" already, right? |
Yes, you can just add 'deref'. |
`Deref(ptr)` throws an exception when its argument (`ptr`) is null.
Replaced the raw pointers in the VTKPolyDataMeshIO GoogleTest by C++ references, as returned by `itk::Deref`. Removed the `ASSERT_NE(ptr, nullptr)` calls for those pointers. GoogleTest unit tests take care of catching exceptions, including those from `itk::Deref`.
Deref(const TPointer &)
to ease dereferencing pointers safelyDeref(T *)
, to ease dereferencing a pointer safely
ASSERT_NE(outputMesh, nullptr); | ||
const auto outputPoints = outputMesh->GetPoints(); | ||
ASSERT_NE(outputPoints, nullptr); | ||
const auto & outputMesh = itk::Deref(reader->GetOutput()); |
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 is a good demonstration of how this is supposed to be used. I would like to see some more instances of use, better demonstrating a need for a new function and exception class.
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 @dzenanz
I use elastix::Deref
extensively within elastix, e.g. (source):
elastix::Deref(MersenneTwisterRandomVariateGenerator::GetInstance()).SetSeed(...);
and (source)
MultiThreaderBase & multiThreader = elastix::Deref(this->ProcessObject::GetMultiThreader());
multiThreader.SetSingleMethod(...);
multiThreader.SingleMethodExecute();
Once itk::Deref
is available, I would like to replace those elastix::Deref
calls with itk::Deref
. I feel that this function isn't really elastix specific. It could be useful for other ITK users as well. Does that answer your question?
It appears somewhat risky to allow a smart pointer as `Deref` argument, as the original "smart" lifetime management may then be lost, in some cases. Adjusted `Deref` calls in `AdvancedTransformAdapter` accordingly. Following InsightSoftwareConsortium/ITK#4278 commit InsightSoftwareConsortium/ITK@042d423 "Add `Deref(T *)`, to ease dereferencing a pointer safely"
It appears somewhat risky to allow a smart pointer as `Deref` argument, as the original "smart" lifetime management may then be lost, in some cases. Adjusted `Deref` calls in `AdvancedTransformAdapter` accordingly. Following InsightSoftwareConsortium/ITK#4278 commit InsightSoftwareConsortium/ITK@042d423 "Add `Deref(T *)`, to ease dereferencing a pointer safely"
Following ITK pull request InsightSoftwareConsortium/ITK#4278 commit InsightSoftwareConsortium/ITK@042d423 "ENH: Add `Deref(T *)`, to ease dereferencing a pointer safely".
Following ITK pull request InsightSoftwareConsortium/ITK#4278 commit InsightSoftwareConsortium/ITK@042d423 "ENH: Add `Deref(T *)`, to ease dereferencing a pointer safely".
Following ITK pull request InsightSoftwareConsortium/ITK#4278 commit InsightSoftwareConsortium/ITK@042d423 "ENH: Add `Deref(T *)`, to ease dereferencing a pointer safely".
Following ITK pull request InsightSoftwareConsortium/ITK#4278 commit InsightSoftwareConsortium/ITK@042d423 "ENH: Add `Deref(T *)`, to ease dereferencing a pointer safely".
Following ITK pull request InsightSoftwareConsortium/ITK#4278 commit InsightSoftwareConsortium/ITK@042d423 "ENH: Add `Deref(T *)`, to ease dereferencing a pointer safely".
Following ITK pull request InsightSoftwareConsortium/ITK#4278 commit InsightSoftwareConsortium/ITK@042d423 "ENH: Add `Deref(T *)`, to ease dereferencing a pointer safely".
Deref(ptr)
throws an exception when its argument (ptr
) is null.A similar function template is being used internally in elastix for quite a while: https://github.com/SuperElastix/elastix/blob/f64965f09eb0903e7bf1da4a112d914c7ac2ce22/Common/elxDeref.h