-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
http: use OptRef helper to reduce some boilerplate #14361
Changes from 4 commits
1b28b0e
46b7941
41c424a
02329b1
d5c6a38
3d4a755
f52e870
fb8c9af
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,44 @@ template <class T> struct OptRef : public absl::optional<std::reference_wrapper< | |
const T& ref = **this; | ||
return &ref; | ||
} | ||
|
||
/** | ||
* Helper to convert a OptRef into a pointer. If the optional is not set, returns a nullptr. | ||
*/ | ||
T* ptr() { | ||
if (this->has_value()) { | ||
T& ref = **this; | ||
return &ref; | ||
} | ||
|
||
return nullptr; | ||
} | ||
|
||
/** | ||
* Helper to convert a OptRef into a pointer. If the optional is not set, returns a nullptr. | ||
*/ | ||
const T* ptr() const { | ||
if (this->has_value()) { | ||
const T& ref = **this; | ||
return &ref; | ||
} | ||
|
||
return nullptr; | ||
} | ||
|
||
T& ref() { return **this; } | ||
|
||
const T& ref() const { return **this; } | ||
}; | ||
|
||
template <class T> OptRef<T> makeOptRef(T& ref) { return {ref}; } | ||
|
||
template <class T> OptRef<T> makeOptRef(T* ptr) { | ||
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. this overload feels like it might result in ambiguity at some point in the future (thought it would have to be some kind of pathological case on optional reference to a pointer to get into that state, I think). Maybe call this one 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. Yeah good point, I'll make the name change |
||
if (ptr == nullptr) { | ||
return {}; | ||
} | ||
|
||
return {*ptr}; | ||
} | ||
|
||
} // namespace Envoy |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,7 @@ namespace Envoy { | |
#define DUMP_DETAILS(member) \ | ||
do { \ | ||
os << spaces << #member ": "; \ | ||
if ((member) != nullptr) { \ | ||
if ((member)) { \ | ||
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. do we need the double-parens? 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. Doesn't seem like it, removing |
||
os << "\n"; \ | ||
(member)->dumpState(os, indent_level + 1); \ | ||
} else { \ | ||
|
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.
doxygen doc for makeOptRef and makeOptRefFromPtr?