From 6c45cf78a552376c1e3f59dba17a7434de2bcc44 Mon Sep 17 00:00:00 2001 From: Hicham Azimani Date: Thu, 15 Jul 2021 07:34:25 +0200 Subject: [PATCH] clang_tidy modernize --- coord_parser.h | 2 +- deadline.h | 2 +- exception.h | 9 +++++---- flat_enum_map.h | 30 +++++++++++++++--------------- functions.h | 4 ++-- idx_map.h | 22 +++++++++++----------- lotus.h | 2 +- lru.h | 30 +++++++++++++++--------------- map_find.h | 6 +++--- multi_obj_pool.h | 9 +++++---- tests/lru_test.cpp | 2 +- tests/map_find_test.cpp | 5 +++-- tests/utils_test.cpp | 4 ++-- threadbuf.h | 12 ++++++------ 14 files changed, 71 insertions(+), 68 deletions(-) diff --git a/coord_parser.h b/coord_parser.h index 4d2bb91..da6a027 100644 --- a/coord_parser.h +++ b/coord_parser.h @@ -38,7 +38,7 @@ class wrong_coordinate : public std::runtime_error { public: wrong_coordinate(const std::string& what) : std::runtime_error(what) {} wrong_coordinate(const wrong_coordinate&) = default; - virtual ~wrong_coordinate() noexcept; + ~wrong_coordinate() noexcept override; }; extern const boost::regex coord_regex; diff --git a/deadline.h b/deadline.h index 7e4dcf3..cdfca61 100644 --- a/deadline.h +++ b/deadline.h @@ -41,7 +41,7 @@ class DeadlineExpired : public recoverable_exception { public: DeadlineExpired(const DeadlineExpired& o) = default; - virtual ~DeadlineExpired(); + ~DeadlineExpired() override; }; class Deadline { diff --git a/exception.h b/exception.h index 6fad9f0..ad68bbe 100644 --- a/exception.h +++ b/exception.h @@ -33,6 +33,7 @@ www.navitia.io #include #include +#include namespace navitia { class exception : public std::exception { @@ -41,13 +42,13 @@ class exception : public std::exception { std::string _backtrace; public: - exception(const std::string& msg) : msg(msg), _backtrace(get_backtrace()) {} + exception(std::string msg) : msg(std::move(msg)), _backtrace(get_backtrace()) {} exception() = default; exception(const exception&) = default; exception& operator=(const exception&) = default; - virtual ~exception() noexcept; + ~exception() noexcept override; - const char* what() const noexcept { return msg.c_str(); } + const char* what() const noexcept override { return msg.c_str(); } const std::string& backtrace() const noexcept { return _backtrace; } }; @@ -62,6 +63,6 @@ struct recoverable_exception : public exception { recoverable_exception() = default; recoverable_exception(const recoverable_exception&) = default; recoverable_exception& operator=(const recoverable_exception&) = default; - virtual ~recoverable_exception() noexcept; + ~recoverable_exception() noexcept override; }; } // namespace navitia diff --git a/flat_enum_map.h b/flat_enum_map.h index dca09b4..cb627f5 100644 --- a/flat_enum_map.h +++ b/flat_enum_map.h @@ -50,7 +50,7 @@ struct get_enum_type { #else template struct get_enum_type { - typedef int type; + using type = int; }; #endif @@ -92,9 +92,9 @@ class flat_enum_map_iterator; */ template struct flat_enum_map { - typedef std::array::size()> underlying_container; - typedef flat_enum_map_iterator iterator; - typedef flat_enum_map_iterator const_iterator; + using underlying_container = std::array::size()>; + using iterator = flat_enum_map_iterator; + using const_iterator = flat_enum_map_iterator; underlying_container array; @@ -136,11 +136,11 @@ struct flat_enum_map { template class enum_iterator : public boost::iterator_facade, Enum, boost::random_access_traversal_tag, Enum> { - typedef typename get_enum_type::type underlying_type; + using underlying_type = typename get_enum_type::type; underlying_type _it; public: - typedef std::make_signed::type> difference_type; + using difference_type = std::make_signed::type>; enum_iterator() : _it(enum_size_trait::size()) {} enum_iterator(const Enum& e) : _it(static_cast(e)) {} enum_iterator(const underlying_type& i) : _it(i) {} @@ -156,27 +156,27 @@ class enum_iterator template boost::iterator_range> enum_range() { - typedef enum_iterator it; + using it = enum_iterator; return boost::make_iterator_range(it(get_first_elt()), it()); } template boost::iterator_range>> reverse_enum_range() { - typedef enum_iterator it; - typedef boost::reverse_iterator rit; + using it = enum_iterator; + using rit = boost::reverse_iterator; return boost::make_iterator_range(rit(it(enum_size_trait::size())), rit(it(0))); } template boost::iterator_range> enum_range_from(Enum e) { - typedef enum_iterator it; + using it = enum_iterator; return boost::make_iterator_range(it(e), it()); } template boost::iterator_range>> reverse_enum_range_from(Enum e) { - typedef enum_iterator it; - typedef boost::reverse_iterator rit; + using it = enum_iterator; + using rit = boost::reverse_iterator; using e_type = typename get_enum_type::type; return boost::make_iterator_range(rit(it(static_cast(static_cast(e) + 1))), rit(it(0))); } @@ -186,13 +186,13 @@ class flat_enum_map_iterator : public boost::iterator_facade, boost::random_access_traversal_tag, std::pair> { - typedef flat_enum_map enum_map; - typedef typename flat_enum_map_iterator::difference_type difference_type; + using enum_map = flat_enum_map; + using difference_type = typename flat_enum_map_iterator::difference_type; typename enum_map::underlying_container::iterator _iterator; enum_iterator _enum_iterator; public: - flat_enum_map_iterator() {} + flat_enum_map_iterator() = default; flat_enum_map_iterator(typename enum_map::underlying_container::iterator it) : _iterator(it) {} flat_enum_map_iterator(typename enum_map::underlying_container::iterator it, EnumKey e) : _iterator(it), _enum_iterator(e) {} diff --git a/functions.h b/functions.h index 3e92ab8..d7ef717 100644 --- a/functions.h +++ b/functions.h @@ -164,7 +164,7 @@ void sort_and_truncate(Vector& input, size_t nbmax, Cmp cmp) { */ template void sort_and_truncate(typename google::protobuf::RepeatedPtrField& input, size_t nbmax, Cmp cmp) { - typedef typename google::protobuf::RepeatedPtrField Vector; + using Vector = typename google::protobuf::RepeatedPtrField; typename Vector::iterator middle_iterator; if (nbmax < size_t(input.size())) middle_iterator = input.begin() + nbmax; @@ -182,7 +182,7 @@ void sort_and_truncate(typename google::protobuf::RepeatedPtrField& input, */ template void sort_and_truncate(typename std::vector& input, size_t nbmax, Cmp cmp) { - typedef typename std::vector Vector; + using Vector = typename std::vector; typename Vector::iterator middle_iterator; if (nbmax < size_t(input.size())) { middle_iterator = input.begin() + nbmax; diff --git a/idx_map.h b/idx_map.h index e295083..972c303 100644 --- a/idx_map.h +++ b/idx_map.h @@ -36,7 +36,7 @@ www.navitia.io namespace navitia { -typedef uint32_t idx_t; +using idx_t = uint32_t; const idx_t invalid_idx = std::numeric_limits::max(); // Strong typing of index with a phantom type! @@ -60,10 +60,10 @@ class IdxMapIterator : public boost::iterator_facade, boost::random_access_traversal_tag, std::pair::reference> > { public: - typedef typename std::iterator_traits traits; - typedef typename traits::difference_type difference_type; + using traits = typename std::iterator_traits; + using difference_type = typename traits::difference_type; - inline IdxMapIterator() {} + inline IdxMapIterator() = default; inline IdxMapIterator(const idx_t& i, I it) : idx(i), iterator(it) {} private: @@ -92,13 +92,13 @@ class IdxMapIterator : public boost::iterator_facade, // A HashMap with optimal hash! template struct IdxMap { - typedef Idx key_type; - typedef V mapped_type; - typedef std::vector container; - typedef IdxMapIterator iterator; - typedef IdxMapIterator const_iterator; - typedef boost::iterator_range::iterator> range; - typedef boost::iterator_range::const_iterator> const_range; + using key_type = Idx; + using mapped_type = V; + using container = std::vector; + using iterator = IdxMapIterator; + using const_iterator = IdxMapIterator; + using range = boost::iterator_range::iterator>; + using const_range = boost::iterator_range::const_iterator>; inline IdxMap() = default; inline IdxMap(const std::vector& c, const V& val = V()) : map(c.size(), val) {} diff --git a/lotus.h b/lotus.h index effa04d..dbdf39f 100644 --- a/lotus.h +++ b/lotus.h @@ -40,7 +40,7 @@ www.navitia.io struct LotusException : public std::exception { std::string const message; LotusException(std::string message) : message(std::move(message)) {} - virtual const char* what() const noexcept override; + const char* what() const noexcept override; }; struct Lotus { diff --git a/lru.h b/lru.h index 638a288..e4b5964 100644 --- a/lru.h +++ b/lru.h @@ -54,15 +54,15 @@ template class Lru { private: typedef typename boost::remove_cv::type>::type key_type; - typedef - typename boost::remove_cv::type>::type mapped_type; - typedef std::pair value_type; - typedef boost::multi_index_container< + using mapped_type = + typename boost::remove_cv::type>::type; + using value_type = std::pair; + using Cache = boost::multi_index_container< value_type, - boost::multi_index::indexed_by, - boost::multi_index::ordered_unique< - boost::multi_index::member>>> - Cache; + boost::multi_index::indexed_by< + boost::multi_index::sequenced<>, + boost::multi_index::ordered_unique< + boost::multi_index::member>>>; // the encapsulate function F f; @@ -89,8 +89,8 @@ class Lru { friend struct ConcurrentLru; public: - typedef mapped_type const& result_type; - typedef typename F::argument_type argument_type; + using result_type = const mapped_type&; + using argument_type = typename F::argument_type; Lru(F fun, size_t max = 10) : f(std::move(fun)), max_cache(max) { if (max < 1) { @@ -134,8 +134,8 @@ class Lru { } }; template -inline Lru make_lru(F fun, size_t max = 10) { - return Lru(std::move(fun), max); +inline Lru make_lru(F&& fun, size_t max = 10) { + return Lru(std::forward(fun), max); } template @@ -167,7 +167,7 @@ struct ConcurrentLru { using argument_type = typename SharedPtrF::argument_type; ConcurrentLru(F fun, size_t max = 10) : lru(SharedPtrF{std::move(fun)}, max) {} - ConcurrentLru(ConcurrentLru&&) = default; // needed by old version of gcc + ConcurrentLru(ConcurrentLru&&) = default; // NOLINT // needed by old version of gcc result_type operator()(argument_type arg) const { typename SharedPtrF::result_type future; @@ -195,8 +195,8 @@ struct ConcurrentLru { } }; template -inline ConcurrentLru make_concurrent_lru(F fun, size_t max = 10) { - return ConcurrentLru(std::move(fun), max); +inline ConcurrentLru make_concurrent_lru(F&& fun, size_t max = 10) { + return ConcurrentLru(std::forward(fun), max); } } // namespace navitia diff --git a/map_find.h b/map_find.h index b1924b4..6f736b1 100644 --- a/map_find.h +++ b/map_find.h @@ -45,9 +45,9 @@ namespace utils { template class MapFind { public: - typedef typename Map::key_type KeyType; - typedef typename Map::mapped_type ValueType; - typedef ValueType const* ConstPtrValueType; + using KeyType = typename Map::key_type; + using ValueType = typename Map::mapped_type; + using ConstPtrValueType = const ValueType*; /** * @brief Create a MapFind object on a map, no find is performed until find() is called. diff --git a/multi_obj_pool.h b/multi_obj_pool.h index fe3bb84..0e2273c 100644 --- a/multi_obj_pool.h +++ b/multi_obj_pool.h @@ -34,6 +34,7 @@ www.navitia.io #include #include +#include template struct DefaultParetoFrontVisitor { @@ -53,14 +54,14 @@ struct DefaultParetoFrontVisitor { template > class ParetoFront { public: - typedef std::list Pool; - typedef typename Pool::value_type value_type; - typedef typename Pool::const_iterator const_iterator; + using Pool = std::list; + using value_type = typename Pool::value_type; + using const_iterator = typename Pool::const_iterator; Visitor visitor; ParetoFront() = default; - explicit ParetoFront(Dominator x) : dominate(x) {} + explicit ParetoFront(Dominator x) : dominate(std::move(x)) {} explicit ParetoFront(Dominator x, Visitor v) : dominate(x), visitor(v) {} bool add(const Obj& obj); diff --git a/tests/lru_test.cpp b/tests/lru_test.cpp index 3952a2b..363db50 100644 --- a/tests/lru_test.cpp +++ b/tests/lru_test.cpp @@ -36,7 +36,7 @@ www.navitia.io struct Fun { typedef int const& argument_type; - typedef int result_type; + using result_type = int; size_t& nb_call; Fun(size_t& nb): nb_call(nb) {} int operator()(const int& i) const { ++nb_call; return i * 2; } diff --git a/tests/map_find_test.cpp b/tests/map_find_test.cpp index 975c013..640e715 100644 --- a/tests/map_find_test.cpp +++ b/tests/map_find_test.cpp @@ -37,6 +37,7 @@ www.navitia.io #include #include #include +#include using namespace navitia::utils; using std::string; @@ -105,14 +106,14 @@ BOOST_FIXTURE_TEST_CASE(should_not_copy_value_object_when_queried, MapFindFixtur struct NonCopyable { const string str; - NonCopyable(const string & str): str(str) {} + NonCopyable(string str) : str(std::move(str)) {} /// We remove the copy semantic to make sure objects are not duplicated along the way NonCopyable(const NonCopyable &) = delete; NonCopyable& operator=(const NonCopyable &) = delete; /// we only keep the move construction semantic to emplace into the map - NonCopyable(NonCopyable && rhs): str(std::move(rhs.str)) {} + NonCopyable(NonCopyable&& rhs) noexcept : str(std::move(rhs.str)) {} NonCopyable& operator=(NonCopyable && rhs) = delete; }; diff --git a/tests/utils_test.cpp b/tests/utils_test.cpp index 4f5cc3a..3121074 100644 --- a/tests/utils_test.cpp +++ b/tests/utils_test.cpp @@ -264,11 +264,11 @@ BOOST_AUTO_TEST_CASE(natural_sort_test2) { struct MockedContainerWithFind { struct iterator{}; - iterator end() const{return iterator();} + iterator end() const { return {}; } bool mutable find_is_called{false}; iterator find(int) const { find_is_called = true; - return iterator(); + return {}; } }; diff --git a/threadbuf.h b/threadbuf.h index 691f079..1e2b63e 100644 --- a/threadbuf.h +++ b/threadbuf.h @@ -34,8 +34,8 @@ class threadbuf : public std::streambuf { private: - typedef std::streambuf::traits_type traits_type; - typedef std::string::size_type string_size_t; + using traits_type = std::streambuf::traits_type; + using string_size_t = std::string::size_type; std::mutex d_mutex; std::condition_variable d_condition; @@ -55,7 +55,7 @@ class threadbuf : public std::streambuf { this->setp(&this->d_out[0], &this->d_out[0] + this->d_out.size() - 1); this->setg(&this->d_in[0], &this->d_in[0], &this->d_in[0]); } - virtual ~threadbuf(); + ~threadbuf() override; void close() { { std::unique_lock lock(this->d_mutex); @@ -68,7 +68,7 @@ class threadbuf : public std::streambuf { } private: - int_type underflow() { + int_type underflow() override { if (this->gptr() == this->egptr()) { std::unique_lock lock(this->d_mutex); while (&this->d_tmp[0] == this->d_current && !this->d_closed) { @@ -84,7 +84,7 @@ class threadbuf : public std::streambuf { } return this->gptr() == this->egptr() ? traits_type::eof() : traits_type::to_int_type(*this->gptr()); } - int_type overflow(int_type c) { + int_type overflow(int_type c) override { std::unique_lock lock(this->d_mutex); if (!traits_type::eq_int_type(c, traits_type::eof())) { *this->pptr() = traits_type::to_char_type(c); @@ -92,7 +92,7 @@ class threadbuf : public std::streambuf { } return this->internal_sync(lock) ? traits_type::eof() : traits_type::not_eof(c); } - int sync() { + int sync() override { std::unique_lock lock(this->d_mutex); return this->internal_sync(lock); }