Filtered visitor (#18)
New `visit()` overload taking a filter. Closes #17.
This commit is contained in:
parent
2af22083a6
commit
2be20f60a2
4 changed files with 140 additions and 4 deletions
|
|
@ -5,10 +5,11 @@
|
|||
#ifndef CPPAST_VISITOR_HPP_INCLUDED
|
||||
#define CPPAST_VISITOR_HPP_INCLUDED
|
||||
|
||||
#include <cppast/cpp_entity.hpp>
|
||||
#include <cppast/cpp_entity_kind.hpp>
|
||||
|
||||
namespace cppast
|
||||
{
|
||||
class cpp_entity;
|
||||
|
||||
/// Information about the state of a visit operation.
|
||||
struct visitor_info
|
||||
{
|
||||
|
|
@ -31,7 +32,8 @@ namespace cppast
|
|||
/// \exclude
|
||||
namespace detail
|
||||
{
|
||||
using visitor_callback_t = bool (*)(void* mem, const cpp_entity&, visitor_info info);
|
||||
using visitor_callback_t = bool (*)(void* mem, const cpp_entity&, visitor_info info);
|
||||
using visitor_predicate_t = bool (*)(const cpp_entity&);
|
||||
|
||||
template <typename Func>
|
||||
bool visitor_callback(void* mem, const cpp_entity& e, visitor_info info)
|
||||
|
|
@ -54,6 +56,52 @@ namespace cppast
|
|||
{
|
||||
detail::visit(e, &detail::visitor_callback<Func>, &f, false);
|
||||
}
|
||||
|
||||
/// Visits a [cppast::cpp_entity]().
|
||||
/// \effects Invokes the callback for the current entity,
|
||||
/// and child entities that match the given predicate.
|
||||
/// It will pass a reference to the current entity and the [cppast::visitor_info]().
|
||||
/// The return value of the callback controls the visit operation,
|
||||
/// the semantic depend on the [cppast::visitor_info::event_type]().
|
||||
template <typename Func, typename Predicate>
|
||||
void visit(const cpp_entity& e, Predicate pred, Func f)
|
||||
{
|
||||
visit(e, [&](const cpp_entity& e, visitor_info& info) {
|
||||
if (pred(e))
|
||||
return f(e, info);
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
/// Generates, at compile-time, a predicate that returns true iff the
|
||||
/// given entity holds one of the [cppast::cpp_entity_kind]()s specified
|
||||
/// in paramaters of the blacklist template.
|
||||
template <cpp_entity_kind... K>
|
||||
detail::visitor_predicate_t whitelist()
|
||||
{
|
||||
static_assert(sizeof...(K) > 0, "At least one entity kind should be specified");
|
||||
return [](const cpp_entity& e) {
|
||||
bool result = false;
|
||||
// this ugliness avoids recursive expansion which would be required in C++11,
|
||||
// otherwise known as poor man's fold expression.
|
||||
(void)std::initializer_list<int>{(result = result || (K == e.kind()), 0)...};
|
||||
return result;
|
||||
};
|
||||
}
|
||||
|
||||
/// Generates, at compile-time, a predicate that returns true iff the
|
||||
/// given entity holds a [cppast::cpp_entity_kind]() that's different
|
||||
/// from all of those specified in the paramaters of the blacklist template.
|
||||
template <cpp_entity_kind... K>
|
||||
detail::visitor_predicate_t blacklist()
|
||||
{
|
||||
static_assert(sizeof...(K) > 0, "At least one entity kind should be specified");
|
||||
return [](const cpp_entity& e) {
|
||||
bool result = true;
|
||||
(void)std::initializer_list<int>{(result = result && (K != e.kind()), 0)...};
|
||||
return result;
|
||||
};
|
||||
}
|
||||
} // namespace cppast
|
||||
|
||||
#endif // CPPAST_VISITOR_HPP_INCLUDED
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue