Generalize basic_cpp_entity_ref to use predicate

This commit is contained in:
Jonathan Müller 2017-01-21 19:53:37 +01:00
commit 21c59afc71
4 changed files with 68 additions and 33 deletions

View file

@ -6,12 +6,11 @@
#define CPPAST_CPP_ENTITY_REF_HPP_INCLUDED
#include <cppast/cpp_entity_index.hpp>
#include <cppast/cpp_entity_type.hpp>
namespace cppast
{
/// A basic reference to some kind of [cppast::cpp_entity]().
template <typename T, cpp_entity_type Type>
template <typename T, typename Predicate>
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<const T&>(idx.lookup(target_).value(), Type);
auto entity = idx.lookup(target_);
DEBUG_ASSERT(Predicate{}(entity.value()), detail::precondition_error_handler{},
"invalid entity");
return static_cast<const T&>(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<cpp_entity, cpp_entity_type::count>;
using cpp_entity_ref = basic_cpp_entity_ref<cpp_entity, detail::cpp_entity_ref_predicate>;
} // namespace cppast
#endif // CPPAST_CPP_ENTITY_REF_HPP_INCLUDED

View file

@ -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 <typename T, typename Org>
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<T>(org);
}
} // namespace detail
} // namespace cppast
#endif // CPPAST_CPP_ENTITY_TYPE_HPP_INCLUDED

View file

@ -7,6 +7,7 @@
#include <cppast/cpp_entity_index.hpp>
#include <cppast/cpp_entity_ref.hpp>
#include <cppast/cpp_entity_type.hpp>
#include <cppast/cpp_scope.hpp>
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<cpp_namespace, cpp_entity_type::namespace_t>;
using cpp_namespace_ref =
basic_cpp_entity_ref<cpp_namespace, detail::cpp_namespace_ref_predicate>;
/// 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_;
};