From 32ab3a9fbcc1ec090250a489ee3ad93f37e2cffb Mon Sep 17 00:00:00 2001 From: Fernando Pereira Date: Tue, 10 Mar 2020 11:06:48 +0100 Subject: [PATCH 1/2] Normalize use of inline in header functions --- binds/python/CMakeLists.txt | 2 +- binds/python/bindings_utils.h | 2 +- include/morphio/mito_section.h | 4 +- include/morphio/section.h | 3 +- include/morphio/section_base.h | 22 ++------ include/morphio/section_base.tpp | 33 ++++++++---- include/morphio/section_iterators.hpp | 78 ++++++++++++++------------- include/morphio/shared_utils.tpp | 12 ++--- src/CMakeLists.txt | 4 +- 9 files changed, 82 insertions(+), 78 deletions(-) diff --git a/binds/python/CMakeLists.txt b/binds/python/CMakeLists.txt index cab467e7e..3994497ae 100644 --- a/binds/python/CMakeLists.txt +++ b/binds/python/CMakeLists.txt @@ -10,7 +10,7 @@ pybind11_add_module(_morphio target_include_directories(_morphio PRIVATE - ${CMAKE_CURRENT_SOURCE_DIR}/../../include + ${CMAKE_SOURCE_DIR}/include ) target_link_libraries(_morphio diff --git a/binds/python/bindings_utils.h b/binds/python/bindings_utils.h index 940e7bec3..ab18674b0 100644 --- a/binds/python/bindings_utils.h +++ b/binds/python/bindings_utils.h @@ -11,7 +11,7 @@ morphio::Points array_to_points(py::array_t& buf); py::array_t span_array_to_ndarray(const morphio::range& span); template -py::array_t span_to_ndarray(const morphio::range& span) { +inline py::array_t span_to_ndarray(const morphio::range& span) { const void* ptr = static_cast(span.data()); const auto buffer_info = py::buffer_info( // Cast from (const void*) to (void*) for function signature matching diff --git a/include/morphio/mito_section.h b/include/morphio/mito_section.h index c0e021087..9c9d5a864 100644 --- a/include/morphio/mito_section.h +++ b/include/morphio/mito_section.h @@ -55,8 +55,8 @@ class MitoSection: public SectionBase range relativePathLengths() const; protected: - MitoSection(uint32_t id_, const std::shared_ptr& morphology) - : SectionBase(id_, morphology) {} + using SectionBase::SectionBase; + friend MitoSection Mitochondria::section(uint32_t) const; friend class SectionBase; friend class mut::MitoSection; diff --git a/include/morphio/section.h b/include/morphio/section.h index d1d07b346..264f16892 100644 --- a/include/morphio/section.h +++ b/include/morphio/section.h @@ -87,8 +87,7 @@ class Section: public SectionBase
friend class SectionBase
; protected: - Section(uint32_t id_, const std::shared_ptr& properties) - : SectionBase(id_, properties) {} + using SectionBase
::SectionBase; }; // explicit instanciation diff --git a/include/morphio/section_base.h b/include/morphio/section_base.h index cfe384af7..6e31d76ed 100644 --- a/include/morphio/section_base.h +++ b/include/morphio/section_base.h @@ -24,15 +24,15 @@ template class SectionBase { public: - SectionBase() + inline SectionBase() : _id(0) {} SectionBase(const SectionBase& section); SectionBase& operator=(const SectionBase& other); - inline bool operator==(const SectionBase& other) const noexcept; - inline bool operator!=(const SectionBase& other) const noexcept; + bool operator==(const SectionBase& other) const noexcept; + bool operator!=(const SectionBase& other) const noexcept; /** * Return true if this section is a root section (parent ID == -1) @@ -52,7 +52,7 @@ class SectionBase std::vector children() const; /** Return the ID of this section. */ - inline uint32_t id() const noexcept; + uint32_t id() const noexcept; protected: SectionBase(uint32_t id, const std::shared_ptr& properties); @@ -64,20 +64,6 @@ class SectionBase std::shared_ptr _properties; }; -template -inline bool SectionBase::operator==(const SectionBase& other) const noexcept { - return other._id == _id && other._properties == _properties; -} - -template -inline bool SectionBase::operator!=(const SectionBase& other) const noexcept { - return !(*this == other); -} - -template -inline uint32_t SectionBase::id() const noexcept { - return _id; -} } // namespace morphio diff --git a/include/morphio/section_base.tpp b/include/morphio/section_base.tpp index 51b44accf..8b6b17aa2 100644 --- a/include/morphio/section_base.tpp +++ b/include/morphio/section_base.tpp @@ -4,8 +4,8 @@ namespace morphio { template -SectionBase::SectionBase(const uint32_t id_, - const std::shared_ptr& properties) +inline SectionBase::SectionBase(const uint32_t id_, + const std::shared_ptr& properties) : _id(id_) , _properties(properties) { const auto& sections = properties->get(); @@ -27,13 +27,13 @@ SectionBase::SectionBase(const uint32_t id_, } template -SectionBase::SectionBase(const SectionBase& other) +inline SectionBase::SectionBase(const SectionBase& other) : _id(other._id) , _range(other._range) , _properties(other._properties) {} template -SectionBase& SectionBase::operator=(const SectionBase& section) { +inline SectionBase& SectionBase::operator=(const SectionBase& section) { if (§ion == this) return *this; _id = section._id; @@ -44,7 +44,7 @@ SectionBase& SectionBase::operator=(const SectionBase& section) { template template -range SectionBase::get() const { +inline range SectionBase::get() const { const auto& data = _properties->get(); if (data.empty()) return {}; @@ -54,15 +54,15 @@ range SectionBase::get() const { } template -bool SectionBase::isRoot() const { +inline bool SectionBase::isRoot() const { return _properties->get()[_id][1] == -1; } template -T SectionBase::parent() const { +inline T SectionBase::parent() const { if (isRoot()) throw MissingParentError("Cannot call Section::parent() on a root node (section id=" + - std::to_string(_id) + ")."); + std::to_string(_id) + ")."); const auto _parent = static_cast( _properties->get()[_id][1]); @@ -70,7 +70,7 @@ T SectionBase::parent() const { } template -std::vector SectionBase::children() const { +inline std::vector SectionBase::children() const { std::vector result; try { const std::vector& _children = _properties->children().at( @@ -85,4 +85,19 @@ std::vector SectionBase::children() const { } } +template +inline bool SectionBase::operator==(const SectionBase& other) const noexcept { + return other._id == _id && other._properties == _properties; +} + +template +inline bool SectionBase::operator!=(const SectionBase& other) const noexcept { + return !(*this == other); +} + +template +inline uint32_t SectionBase::id() const noexcept { + return _id; +} + } // namespace morphio diff --git a/include/morphio/section_iterators.hpp b/include/morphio/section_iterators.hpp index d8f5e1fff..49b049602 100644 --- a/include/morphio/section_iterators.hpp +++ b/include/morphio/section_iterators.hpp @@ -9,43 +9,47 @@ #include #include +namespace morphio { namespace detail { template -std::vector getChildren(const MorphologyT& morphology) noexcept { +inline std::vector getChildren(const MorphologyT& morphology) noexcept { return morphology.rootSections(); } template -std::vector getChildren(const SectionT& section) { +inline std::vector getChildren(const SectionT& section) { return section.children(); } template -std::vector> getChildren(const std::shared_ptr& section) { +inline std::vector> getChildren( + const std::shared_ptr& section) { return section->children(); } template -bool isRoot(const SectionT& current) { +inline bool isRoot(const SectionT& current) { return current.isRoot(); } template -bool isRoot(const std::shared_ptr& current) { +inline bool isRoot(const std::shared_ptr& current) { return current->isRoot(); } template -SectionT getParent(const SectionT& current) { +inline SectionT getParent(const SectionT& current) { return current.parent(); } template -std::shared_ptr getParent(const std::shared_ptr& current) { +inline std::shared_ptr getParent(const std::shared_ptr& current) { return current->parent(); } } // namespace detail +} // namespace morphio + namespace morphio { @@ -61,16 +65,16 @@ class breadth_iterator_t breadth_iterator_t() = default; - inline explicit breadth_iterator_t(const SectionT& section); - inline explicit breadth_iterator_t(const MorphologyT& morphology); - inline breadth_iterator_t(const breadth_iterator_t& other); + explicit breadth_iterator_t(const SectionT& section); + explicit breadth_iterator_t(const MorphologyT& morphology); + breadth_iterator_t(const breadth_iterator_t& other); - inline SectionT operator*() const; + SectionT operator*() const; - inline breadth_iterator_t& operator++(); - inline breadth_iterator_t operator++(int); + breadth_iterator_t& operator++(); + breadth_iterator_t operator++(int); - inline bool operator==(const breadth_iterator_t& other) const; + bool operator==(const breadth_iterator_t& other) const; bool operator!=(const breadth_iterator_t& other) const; private: @@ -89,17 +93,17 @@ class depth_iterator_t depth_iterator_t() = default; - inline explicit depth_iterator_t(const SectionT& section); - inline explicit depth_iterator_t(const MorphologyT& morphology); - inline depth_iterator_t(const depth_iterator_t& other); + explicit depth_iterator_t(const SectionT& section); + explicit depth_iterator_t(const MorphologyT& morphology); + depth_iterator_t(const depth_iterator_t& other); - inline SectionT operator*() const; + SectionT operator*() const; - inline depth_iterator_t& operator++(); - inline depth_iterator_t operator++(int); + depth_iterator_t& operator++(); + depth_iterator_t operator++(int); - inline bool operator==(const depth_iterator_t& other) const; - inline bool operator!=(const depth_iterator_t& other) const; + bool operator==(const depth_iterator_t& other) const; + bool operator!=(const depth_iterator_t& other) const; private: std::deque deque_; @@ -115,18 +119,18 @@ class upstream_iterator_t using pointer = SectionT*; using reference = SectionT&; - inline upstream_iterator_t(); - inline explicit upstream_iterator_t(const SectionT& section); - inline upstream_iterator_t(const upstream_iterator_t& other); - inline ~upstream_iterator_t(); + upstream_iterator_t(); + explicit upstream_iterator_t(const SectionT& section); + upstream_iterator_t(const upstream_iterator_t& other); + ~upstream_iterator_t(); - inline SectionT operator*() const; + SectionT operator*() const; - inline upstream_iterator_t& operator++(); - inline upstream_iterator_t operator++(int); + upstream_iterator_t& operator++(); + upstream_iterator_t operator++(int); - inline bool operator==(const upstream_iterator_t& other) const; - inline bool operator!=(const upstream_iterator_t& other) const; + bool operator==(const upstream_iterator_t& other) const; + bool operator!=(const upstream_iterator_t& other) const; private: // This is a workaround for not having std::optional until c++17. @@ -166,8 +170,8 @@ inline SectionT breadth_iterator_t::operator*() const { } template -inline breadth_iterator_t& -breadth_iterator_t::operator++() { +inline breadth_iterator_t& breadth_iterator_t:: +operator++() { if (deque_.empty()) { throw MorphioError("Can't iterate past the end"); } @@ -180,8 +184,8 @@ breadth_iterator_t::operator++() { } template -inline breadth_iterator_t -breadth_iterator_t::operator++(int) { +inline breadth_iterator_t breadth_iterator_t:: +operator++(int) { breadth_iterator_t ret(*this); ++(*this); return ret; @@ -222,8 +226,8 @@ inline SectionT depth_iterator_t::operator*() const { } template -inline depth_iterator_t& -depth_iterator_t::operator++() { +inline depth_iterator_t& depth_iterator_t:: +operator++() { if (deque_.empty()) { throw MorphioError("Can't iterate past the end"); } diff --git a/include/morphio/shared_utils.tpp b/include/morphio/shared_utils.tpp index a710c6500..f3b258f9e 100644 --- a/include/morphio/shared_utils.tpp +++ b/include/morphio/shared_utils.tpp @@ -6,9 +6,9 @@ namespace morphio { template -float _somaSurface(const SomaType type, - const ContainerDiameters& diameters, - const ContainerPoints& points) { +inline float _somaSurface(const SomaType type, + const ContainerDiameters& diameters, + const ContainerPoints& points) { size_t size = points.size(); if (size == 0) return 0.; @@ -48,13 +48,13 @@ float _somaSurface(const SomaType type, } } template -void _appendVector(std::vector& to, const std::vector& from, int offset) { +inline void _appendVector(std::vector& to, const std::vector& from, int offset) { to.insert(to.end(), from.begin() + offset, from.end()); } template -std::vector copySpan(const std::vector& data, - SectionRange range) { +inline std::vector copySpan(const std::vector& data, + SectionRange range) { if (data.empty()) return {}; diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 44897c187..df9267401 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -50,8 +50,8 @@ add_library(morphio_obj OBJECT ${MORPHIO_SOURCES}) target_include_directories(morphio_obj PUBLIC - ${CMAKE_SOURCE_DIR} - ${CMAKE_CURRENT_SOURCE_DIR}/../include + ${CMAKE_CURRENT_SOURCE_DIR} + ${CMAKE_SOURCE_DIR}/include ) target_include_directories(morphio_obj From ffb6c1ff67cee283a295d0c23ad82bdb2ca259b2 Mon Sep 17 00:00:00 2001 From: Fernando Pereira Date: Tue, 10 Mar 2020 11:18:21 +0100 Subject: [PATCH 2/2] Clang format v9 --- include/morphio/section_iterators.hpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/include/morphio/section_iterators.hpp b/include/morphio/section_iterators.hpp index 49b049602..619bf6e47 100644 --- a/include/morphio/section_iterators.hpp +++ b/include/morphio/section_iterators.hpp @@ -170,8 +170,8 @@ inline SectionT breadth_iterator_t::operator*() const { } template -inline breadth_iterator_t& breadth_iterator_t:: -operator++() { +inline breadth_iterator_t& +breadth_iterator_t::operator++() { if (deque_.empty()) { throw MorphioError("Can't iterate past the end"); } @@ -184,8 +184,8 @@ operator++() { } template -inline breadth_iterator_t breadth_iterator_t:: -operator++(int) { +inline breadth_iterator_t +breadth_iterator_t::operator++(int) { breadth_iterator_t ret(*this); ++(*this); return ret; @@ -226,8 +226,8 @@ inline SectionT depth_iterator_t::operator*() const { } template -inline depth_iterator_t& depth_iterator_t:: -operator++() { +inline depth_iterator_t& +depth_iterator_t::operator++() { if (deque_.empty()) { throw MorphioError("Can't iterate past the end"); }