Add RAII helper class to add/remove an element from an std::list#7037
Add RAII helper class to add/remove an element from an std::list#7037htuch merged 4 commits intoenvoyproxy:masterfrom
Conversation
Signed-off-by: Elisha Ziskind <eziskind@google.com>
|
/retest |
|
🔨 rebuilding |
htuch
left a comment
There was a problem hiding this comment.
Looks great, thanks for this cleanup.
/wait
| template <class T> class ListAddAndRemove { | ||
| public: | ||
| ListAddAndRemove(std::list<T>& container, T element) : container_(container) { | ||
| it_ = container.emplace(container.begin(), element); |
There was a problem hiding this comment.
Are there any operations on container that would invalidate the it_ reference? If so, maybe document them in the constructor docs, since these are useful to know.
There was a problem hiding this comment.
It can be invalidated by clearing the list or deleting the element some other way. I added a comment and also methods to allow explicitly deleting the element before the destructor, or canceling the delete in case the iterator was invalidated. This allowed be to convert over another use case (GrpcMuxWatchImpl).
source/common/common/cleanup.h
Outdated
| ListAddAndRemove(std::list<T>& container, T element) : container_(container) { | ||
| it_ = container.emplace(container.begin(), element); | ||
| } | ||
| virtual ~ListAddAndRemove() { container_.erase(it_); } |
There was a problem hiding this comment.
Should this be a virtual class or final concrete? No strong opinions here, just curious to get your thoughts.
There was a problem hiding this comment.
If we mark it as final I don't think other classes can inherit from it. Or am I misunderstanding?
Signed-off-by: Elisha Ziskind <eziskind@google.com>
Signed-off-by: Elisha Ziskind <eziskind@google.com>
Signed-off-by: Elisha Ziskind <eziskind@google.com>
| for (const auto& api_state : api_state_) { | ||
| for (auto watch : api_state.second.watches_) { | ||
| watch->inserted_ = false; | ||
| // watch->inserted_ = false; |
There was a problem hiding this comment.
@eziskind can you do a follow up to clean up this comment? Thank you.
There was a problem hiding this comment.
Woops - will clean that up. Thanks for catch!
Description: Add an RAII helper class that adds an element to a list on construction and removes it on destruction.
Risk Level: low
Testing: unit tests
Fixes: #7020
Signed-off-by: Elisha Ziskind eziskind@google.com