From 21c59afc71d6ebac71e7d43aba4693bd1edfe092 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20M=C3=BCller?= Date: Sat, 21 Jan 2017 19:53:37 +0100 Subject: [PATCH] Generalize basic_cpp_entity_ref to use predicate --- include/cppast/cpp_entity_ref.hpp | 22 +++++++++++++++---- include/cppast/cpp_entity_type.hpp | 12 ----------- include/cppast/cpp_namespace.hpp | 33 ++++++++++++++--------------- src/cpp_namespace.cpp | 34 ++++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 33 deletions(-) create mode 100644 src/cpp_namespace.cpp diff --git a/include/cppast/cpp_entity_ref.hpp b/include/cppast/cpp_entity_ref.hpp index bcfdda5..bf97f0c 100644 --- a/include/cppast/cpp_entity_ref.hpp +++ b/include/cppast/cpp_entity_ref.hpp @@ -6,12 +6,11 @@ #define CPPAST_CPP_ENTITY_REF_HPP_INCLUDED #include -#include namespace cppast { /// A basic reference to some kind of [cppast::cpp_entity](). - template + template class basic_cpp_entity_ref { public: @@ -37,7 +36,10 @@ namespace cppast /// \returns The [cppast::cpp_entity]() it refers to. const T& get(const cpp_entity_index& idx) const noexcept { - return detail::downcast_entity(idx.lookup(target_).value(), Type); + auto entity = idx.lookup(target_); + DEBUG_ASSERT(Predicate{}(entity.value()), detail::precondition_error_handler{}, + "invalid entity"); + return static_cast(entity.value()); } private: @@ -45,8 +47,20 @@ namespace cppast std::string name_; }; + /// \exclude + namespace detail + { + struct cpp_entity_ref_predicate + { + bool operator()(const cpp_entity&) + { + return true; + } + }; + } + /// A [cppast::basic_cpp_entity_ref]() to any [cppast::cpp_entity](). - using cpp_entity_ref = basic_cpp_entity_ref; + using cpp_entity_ref = basic_cpp_entity_ref; } // namespace cppast #endif // CPPAST_CPP_ENTITY_REF_HPP_INCLUDED diff --git a/include/cppast/cpp_entity_type.hpp b/include/cppast/cpp_entity_type.hpp index 684e9bf..05c25bc 100644 --- a/include/cppast/cpp_entity_type.hpp +++ b/include/cppast/cpp_entity_type.hpp @@ -24,18 +24,6 @@ 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; - - /// \exclude - namespace detail - { - template - T downcast_entity(Org& org, cpp_entity_type dest_type) noexcept - { - DEBUG_ASSERT(dest_type == cpp_entity_type::count || org.type() == dest_type, - detail::precondition_error_handler{}, "invalid downcast"); - return static_cast(org); - } - } // namespace detail } // namespace cppast #endif // CPPAST_CPP_ENTITY_TYPE_HPP_INCLUDED diff --git a/include/cppast/cpp_namespace.hpp b/include/cppast/cpp_namespace.hpp index 94ad7ea..302e202 100644 --- a/include/cppast/cpp_namespace.hpp +++ b/include/cppast/cpp_namespace.hpp @@ -7,6 +7,7 @@ #include #include +#include #include namespace cppast @@ -57,16 +58,23 @@ namespace cppast { } - cpp_entity_type do_get_entity_type() const noexcept override - { - return cpp_entity_type::namespace_t; - } + cpp_entity_type do_get_entity_type() const noexcept override; bool inline_; }; + /// \exclude + namespace detail + { + struct cpp_namespace_ref_predicate + { + bool operator()(const cpp_entity& e); + }; + } // namespace detail + /// A reference to a [cppast::cpp_namespace](). - using cpp_namespace_ref = basic_cpp_entity_ref; + using cpp_namespace_ref = + basic_cpp_entity_ref; /// A [cppast::cpp_entity]() modelling a namespace alias. class cpp_namespace_alias final : public cpp_entity @@ -95,10 +103,7 @@ namespace cppast { } - cpp_entity_type do_get_entity_type() const noexcept override - { - return cpp_entity_type::namespace_alias_t; - } + cpp_entity_type do_get_entity_type() const noexcept override; cpp_namespace_ref target_; }; @@ -130,10 +135,7 @@ namespace cppast { } - cpp_entity_type do_get_entity_type() const noexcept override - { - return cpp_entity_type::using_directive_t; - } + cpp_entity_type do_get_entity_type() const noexcept override; cpp_entity_id target_; }; @@ -165,10 +167,7 @@ namespace cppast { } - cpp_entity_type do_get_entity_type() const noexcept override - { - return cpp_entity_type::using_declaration_t; - } + cpp_entity_type do_get_entity_type() const noexcept override; cpp_entity_id target_; }; diff --git a/src/cpp_namespace.cpp b/src/cpp_namespace.cpp new file mode 100644 index 0000000..3476bf3 --- /dev/null +++ b/src/cpp_namespace.cpp @@ -0,0 +1,34 @@ +// 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 + +#include + +using namespace cppast; + +cpp_entity_type cpp_namespace::do_get_entity_type() const noexcept +{ + return cpp_entity_type::namespace_t; +} + +bool detail::cpp_namespace_ref_predicate::operator()(const cpp_entity& e) +{ + return e.type() == cpp_entity_type::namespace_t; +} + +cpp_entity_type cpp_namespace_alias::do_get_entity_type() const noexcept +{ + return cpp_entity_type::namespace_alias_t; +} + +cpp_entity_type cpp_using_directive::do_get_entity_type() const noexcept +{ + return cpp_entity_type::using_directive_t; +} + +cpp_entity_type cpp_using_declaration::do_get_entity_type() const noexcept +{ + return cpp_entity_type::using_declaration_t; +}