diff options
author | David Malcolm <dmalcolm@redhat.com> | 2020-01-09 01:39:45 +0000 |
---|---|---|
committer | David Malcolm <dmalcolm@gcc.gnu.org> | 2020-01-09 01:39:45 +0000 |
commit | e4d2203e31e7c017266e61dbf0d24ee85e59fa55 (patch) | |
tree | 280fe13e89a65db7d5867eceacee2ae31f26afda /gcc/vec.c | |
parent | b3de347f3296995eb2a648d6303ab66bf689e104 (diff) | |
download | gcc-e4d2203e31e7c017266e61dbf0d24ee85e59fa55.zip gcc-e4d2203e31e7c017266e61dbf0d24ee85e59fa55.tar.gz gcc-e4d2203e31e7c017266e61dbf0d24ee85e59fa55.tar.bz2 |
vec.h: add auto_delete_vec
This patch adds a class auto_delete_vec<T>, a subclass of auto_vec <T *>
that deletes all of its elements on destruction; it's used in many
places in the analyzer patch kit.
This is a crude way for a vec to "own" the objects it points to
and clean up automatically (essentially a workaround for not being able
to use unique_ptr, due to C++98).
gcc/ChangeLog:
* vec.c (class selftest::count_dtor): New class.
(selftest::test_auto_delete_vec): New test.
(selftest::vec_c_tests): Call it.
* vec.h (class auto_delete_vec): New class template.
(auto_delete_vec<T>::~auto_delete_vec): New dtor.
From-SVN: r280027
Diffstat (limited to 'gcc/vec.c')
-rw-r--r-- | gcc/vec.c | 27 |
1 files changed, 27 insertions, 0 deletions
@@ -516,6 +516,32 @@ test_reverse () } } +/* A test class that increments a counter every time its dtor is called. */ + +class count_dtor +{ + public: + count_dtor (int *counter) : m_counter (counter) {} + ~count_dtor () { (*m_counter)++; } + + private: + int *m_counter; +}; + +/* Verify that auto_delete_vec deletes the elements within it. */ + +static void +test_auto_delete_vec () +{ + int dtor_count = 0; + { + auto_delete_vec <count_dtor> v; + v.safe_push (new count_dtor (&dtor_count)); + v.safe_push (new count_dtor (&dtor_count)); + } + ASSERT_EQ (dtor_count, 2); +} + /* Run all of the selftests within this file. */ void @@ -533,6 +559,7 @@ vec_c_tests () test_block_remove (); test_qsort (); test_reverse (); + test_auto_delete_vec (); } } // namespace selftest |