schwehr / libais

C++ decoder for Automatic Identification System for tracking ships and decoding maritime information
Other
215 stars 94 forks source link

Use uniqueptr and dynamic cast #221

Closed lsix closed 2 years ago

lsix commented 2 years ago

This PR is the first part of the resubmitted #209 .

This PR includes a first patch that proposes to use std::unique_ptr to manage subareas in Ais8_1_22 and Ais8_366_22. The current implementation uses raw pointers and relies on destructors in order to do clean-up. The std::unique_ptr implementation is usually considered safer and less error prone.

The second patch changes some reinterpred_cast into dynamic_cast when casting from a pointer to parent class into a pointer to a child class.

Both commits are kept together because they inpact common lines.

schwehr commented 2 years ago

Starting to take a look at this PR. It will take a bit for me to verify everything. Sorry I'm going slow.

lsix commented 2 years ago

Starting to take a look at this PR. It will take a bit for me to verify everything. Sorry I'm going slow.

That is fine, take your time! If you have any question please come back to me (everything is not fresh in my mind but I can dig back into it, no problem!)

schwehr commented 2 years ago

Sadly, the current CI setup doesn't build src/tests/..., so this PR breaks those. I will fix them up.

schwehr commented 2 years ago

Oh, I see them failing in the 3.9 build. I will still fix those.

ais8_1_22_test.cc:126:55: error: cannot dynamic_cast ‘msg.libais::Ais8_1_22::sub_areas.std::vector<_Tp, _Alloc>::operator[]<std::unique_ptr<libais::Ais8_1_22_SubArea>, std::allocator<std::unique_ptr<libais::Ais8_1_22_SubArea> > >(1ul)’ (of type ‘__gnu_cxx::__alloc_traits<std::allocator<std::unique_ptr<libais::Ais8_1_22_SubArea> > >::value_type {aka class std::unique_ptr<libais::Ais8_1_22_SubArea>}’) to type ‘class libais::Ais8_1_22_Polygon*’ (source is not a pointer)
     dynamic_cast<Ais8_1_22_Polygon *>(msg.sub_areas[1]);