From 079320cd015eb8d94c9f9aa9511d5a100603f9ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20M=C3=BCller?= Date: Sat, 21 Jan 2017 12:35:11 +0100 Subject: [PATCH] Extract cpp_entity_container CRTP class --- include/cppast/cpp_file.hpp | 17 ++-------- include/cppast/cpp_namespace.hpp | 2 +- include/cppast/cpp_scope.hpp | 56 +++++++++++++++++++------------- 3 files changed, 37 insertions(+), 38 deletions(-) diff --git a/include/cppast/cpp_file.hpp b/include/cppast/cpp_file.hpp index ddfaf36..a78ab4c 100644 --- a/include/cppast/cpp_file.hpp +++ b/include/cppast/cpp_file.hpp @@ -6,13 +6,14 @@ #define CPPAST_CPP_FILE_HPP_INCLUDED #include +#include namespace cppast { /// A [cppast::cpp_entity]() modelling a file. /// /// This is the top-level entity of the AST. - class cpp_file final : public cpp_entity + class cpp_file final : public cpp_entity, public cpp_entity_container { public: /// Builds a [cppast::cpp_file](). @@ -40,20 +41,6 @@ namespace cppast std::unique_ptr file_; }; - using iterator = detail::intrusive_list::const_iterator; - - /// \returns A const iterator to the first child. - iterator begin() const noexcept - { - return children_.begin(); - } - - /// \returns A const iterator to the last child. - iterator end() const noexcept - { - return children_.end(); - } - private: cpp_file(std::string name) : cpp_entity(std::move(name)) { diff --git a/include/cppast/cpp_namespace.hpp b/include/cppast/cpp_namespace.hpp index 0ac73aa..d5dc486 100644 --- a/include/cppast/cpp_namespace.hpp +++ b/include/cppast/cpp_namespace.hpp @@ -10,7 +10,7 @@ namespace cppast { /// A [cppast::cpp_entity]() modelling a namespace. - class cpp_namespace : public cpp_scope + class cpp_namespace final : public cpp_scope { public: /// Builds a [cppast::cpp_namespace](). diff --git a/include/cppast/cpp_scope.hpp b/include/cppast/cpp_scope.hpp index ea26ef9..9f5b7b4 100644 --- a/include/cppast/cpp_scope.hpp +++ b/include/cppast/cpp_scope.hpp @@ -9,22 +9,14 @@ namespace cppast { - /// Base class for all entities that add a scope. + /// Helper class for entities that are containers. /// - /// Examples are namespaces and classes, - /// or anything else that can appear followed by `::`. - class cpp_scope : public cpp_entity + /// Inherit from it to generate container access. + template + class cpp_entity_container { public: - /// \returns The name of the scope. - /// By default, this is the same name as the entity, - /// but derived classes can override it. - std::string scope_name() const - { - return do_get_scope_name(); - } - - using iterator = detail::intrusive_list::const_iterator; + using iterator = typename detail::intrusive_list::const_iterator; /// \returns A const iterator to the first child. iterator begin() const noexcept @@ -39,26 +31,48 @@ namespace cppast } protected: - using cpp_entity::cpp_entity; - - /// \effects Adds a new child to the scope. - void add_child(std::unique_ptr ptr) noexcept + /// \effects Adds a new child to the container. + void add_child(std::unique_ptr ptr) noexcept { - children_.push_back(*this, std::move(ptr)); + children_.push_back(static_cast(*this), std::move(ptr)); } /// \returns A non-const iterator to the first child. - detail::intrusive_list::iterator mutable_begin() noexcept + typename detail::intrusive_list::iterator mutable_begin() noexcept { return children_.begin(); } /// \returns A non-const iterator one past the last child. - detail::intrusive_list::iterator mutable_end() noexcept + typename detail::intrusive_list::iterator mutable_end() noexcept { return children_.begin(); } + ~cpp_entity_container() noexcept = default; + + private: + detail::intrusive_list children_; + }; + + /// Base class for all entities that add a scope. + /// + /// Examples are namespaces and classes, + /// or anything else that can appear followed by `::`. + class cpp_scope : public cpp_entity, public cpp_entity_container + { + public: + /// \returns The name of the scope. + /// By default, this is the same name as the entity, + /// but derived classes can override it. + std::string scope_name() const + { + return do_get_scope_name(); + } + + protected: + using cpp_entity::cpp_entity; + private: /// \returns The name of the new scope, /// defaults to the name of the entity. @@ -66,8 +80,6 @@ namespace cppast { return name(); } - - detail::intrusive_list children_; }; } // namespace cppast