From 865217a1902f3b62658df7f84a009cbe811e996e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20M=C3=BCller?= Date: Fri, 20 Jan 2017 23:10:04 +0100 Subject: [PATCH] Add cpp_scope base --- include/cppast/cpp_entity.hpp | 1 + include/cppast/cpp_entity_type.hpp | 3 ++ include/cppast/cpp_scope.hpp | 74 ++++++++++++++++++++++++++++++ src/cpp_entity_type.cpp | 16 +++++++ 4 files changed, 94 insertions(+) create mode 100644 include/cppast/cpp_scope.hpp create mode 100644 src/cpp_entity_type.cpp diff --git a/include/cppast/cpp_entity.hpp b/include/cppast/cpp_entity.hpp index 0276d62..98c3085 100644 --- a/include/cppast/cpp_entity.hpp +++ b/include/cppast/cpp_entity.hpp @@ -49,6 +49,7 @@ namespace cppast } private: + /// \returns The type of the entity. virtual cpp_entity_type do_get_entity_type() const noexcept = 0; type_safe::optional_ref parent_; diff --git a/include/cppast/cpp_entity_type.hpp b/include/cppast/cpp_entity_type.hpp index d79627c..13a23b0 100644 --- a/include/cppast/cpp_entity_type.hpp +++ b/include/cppast/cpp_entity_type.hpp @@ -12,6 +12,9 @@ namespace cppast { }; + + /// \returns Whether or not a given entity type is one derived from [cppast::cpp_scope](). + bool is_scope(cpp_entity_type type) noexcept; } // namespace cppast #endif // CPPAST_CPP_ENTITY_TYPE_HPP_INCLUDED diff --git a/include/cppast/cpp_scope.hpp b/include/cppast/cpp_scope.hpp new file mode 100644 index 0000000..da3593a --- /dev/null +++ b/include/cppast/cpp_scope.hpp @@ -0,0 +1,74 @@ +// Copyright (C) 2017 Jonathan Müller +// This file is subject to the license terms in the LICENSE file +// found in the top-level directory of this distribution. + +#ifndef CPPAST_CPP_SCOPE_HPP_INCLUDED +#define CPPAST_CPP_SCOPE_HPP_INCLUDED + +#include + +namespace cppast +{ + /// 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: + /// \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; + + /// \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(); + } + + protected: + using cpp_entity::cpp_entity; + + /// \effects Adds a new child to the scope. + void add_child(std::unique_ptr ptr) noexcept + { + children_.push_back(std::move(ptr)); + } + + /// \returns A non-const iterator to the first child. + 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 + { + return children_.begin(); + } + + private: + /// \returns The name of the new scope, + /// defaults to the name of the entity. + virtual std::string do_get_scope_name() const + { + return name(); + } + + detail::intrusive_list children_; + }; +} // namespace cppast + +#endif // CPPAST_CPP_SCOPE_HPP_INCLUDED diff --git a/src/cpp_entity_type.cpp b/src/cpp_entity_type.cpp new file mode 100644 index 0000000..20b530d --- /dev/null +++ b/src/cpp_entity_type.cpp @@ -0,0 +1,16 @@ +// Copyright (C) 2017 Jonathan Müller +// This file is subject to the license terms in the LICENSE file +// found in the top-level directory of this distribution. + +#include + +using namespace cppast; + +bool cppast::is_scope(cpp_entity_type type) noexcept +{ + switch (type) + { + } + + return false; +}