Skip to content

Add RAII helper class to add/remove an element from an std::list#7037

Merged
htuch merged 4 commits intoenvoyproxy:masterfrom
eziskind:eraser
May 29, 2019
Merged

Add RAII helper class to add/remove an element from an std::list#7037
htuch merged 4 commits intoenvoyproxy:masterfrom
eziskind:eraser

Conversation

@eziskind
Copy link
Contributor

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

Signed-off-by: Elisha Ziskind <eziskind@google.com>
@eziskind
Copy link
Contributor Author

/retest

@repokitteh-read-only
Copy link

🔨 rebuilding ci/circleci: tsan (failed build)

🐱

Caused by: a #7037 (comment) was created by @eziskind.

see: more, trace.

Copy link
Member

@htuch htuch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

ListAddAndRemove(std::list<T>& container, T element) : container_(container) {
it_ = container.emplace(container.begin(), element);
}
virtual ~ListAddAndRemove() { container_.erase(it_); }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be a virtual class or final concrete? No strong opinions here, just curious to get your thoughts.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
@htuch htuch added the waiting label May 28, 2019
Signed-off-by: Elisha Ziskind <eziskind@google.com>
Signed-off-by: Elisha Ziskind <eziskind@google.com>
Copy link
Member

@htuch htuch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@htuch htuch merged commit 89d2b1b into envoyproxy:master May 29, 2019
@eziskind eziskind deleted the eraser branch May 29, 2019 12:54
for (const auto& api_state : api_state_) {
for (auto watch : api_state.second.watches_) {
watch->inserted_ = false;
// watch->inserted_ = false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eziskind can you do a follow up to clean up this comment? Thank you.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woops - will clean that up. Thanks for catch!

mattklein123 pushed a commit that referenced this pull request May 29, 2019
Signed-off-by: Elisha Ziskind <eziskind@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Create utility mixin for self-destructing handles

3 participants