Generalize basic_cpp_entity_ref to use predicate
This commit is contained in:
parent
9d60bd2992
commit
21c59afc71
4 changed files with 68 additions and 33 deletions
|
|
@ -6,12 +6,11 @@
|
||||||
#define CPPAST_CPP_ENTITY_REF_HPP_INCLUDED
|
#define CPPAST_CPP_ENTITY_REF_HPP_INCLUDED
|
||||||
|
|
||||||
#include <cppast/cpp_entity_index.hpp>
|
#include <cppast/cpp_entity_index.hpp>
|
||||||
#include <cppast/cpp_entity_type.hpp>
|
|
||||||
|
|
||||||
namespace cppast
|
namespace cppast
|
||||||
{
|
{
|
||||||
/// A basic reference to some kind of [cppast::cpp_entity]().
|
/// 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
|
class basic_cpp_entity_ref
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
@ -37,7 +36,10 @@ namespace cppast
|
||||||
/// \returns The [cppast::cpp_entity]() it refers to.
|
/// \returns The [cppast::cpp_entity]() it refers to.
|
||||||
const T& get(const cpp_entity_index& idx) const noexcept
|
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:
|
private:
|
||||||
|
|
@ -45,8 +47,20 @@ namespace cppast
|
||||||
std::string name_;
|
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]().
|
/// 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
|
} // namespace cppast
|
||||||
|
|
||||||
#endif // CPPAST_CPP_ENTITY_REF_HPP_INCLUDED
|
#endif // CPPAST_CPP_ENTITY_REF_HPP_INCLUDED
|
||||||
|
|
|
||||||
|
|
@ -24,18 +24,6 @@ namespace cppast
|
||||||
|
|
||||||
/// \returns Whether or not a given entity type is one derived from [cppast::cpp_scope]().
|
/// \returns Whether or not a given entity type is one derived from [cppast::cpp_scope]().
|
||||||
bool is_scope(cpp_entity_type type) noexcept;
|
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
|
} // namespace cppast
|
||||||
|
|
||||||
#endif // CPPAST_CPP_ENTITY_TYPE_HPP_INCLUDED
|
#endif // CPPAST_CPP_ENTITY_TYPE_HPP_INCLUDED
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@
|
||||||
|
|
||||||
#include <cppast/cpp_entity_index.hpp>
|
#include <cppast/cpp_entity_index.hpp>
|
||||||
#include <cppast/cpp_entity_ref.hpp>
|
#include <cppast/cpp_entity_ref.hpp>
|
||||||
|
#include <cppast/cpp_entity_type.hpp>
|
||||||
#include <cppast/cpp_scope.hpp>
|
#include <cppast/cpp_scope.hpp>
|
||||||
|
|
||||||
namespace cppast
|
namespace cppast
|
||||||
|
|
@ -57,16 +58,23 @@ namespace cppast
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
cpp_entity_type do_get_entity_type() const noexcept override
|
cpp_entity_type do_get_entity_type() const noexcept override;
|
||||||
{
|
|
||||||
return cpp_entity_type::namespace_t;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool inline_;
|
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]().
|
/// 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.
|
/// A [cppast::cpp_entity]() modelling a namespace alias.
|
||||||
class cpp_namespace_alias final : public cpp_entity
|
class cpp_namespace_alias final : public cpp_entity
|
||||||
|
|
@ -95,10 +103,7 @@ namespace cppast
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
cpp_entity_type do_get_entity_type() const noexcept override
|
cpp_entity_type do_get_entity_type() const noexcept override;
|
||||||
{
|
|
||||||
return cpp_entity_type::namespace_alias_t;
|
|
||||||
}
|
|
||||||
|
|
||||||
cpp_namespace_ref target_;
|
cpp_namespace_ref target_;
|
||||||
};
|
};
|
||||||
|
|
@ -130,10 +135,7 @@ namespace cppast
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
cpp_entity_type do_get_entity_type() const noexcept override
|
cpp_entity_type do_get_entity_type() const noexcept override;
|
||||||
{
|
|
||||||
return cpp_entity_type::using_directive_t;
|
|
||||||
}
|
|
||||||
|
|
||||||
cpp_entity_id target_;
|
cpp_entity_id target_;
|
||||||
};
|
};
|
||||||
|
|
@ -165,10 +167,7 @@ namespace cppast
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
cpp_entity_type do_get_entity_type() const noexcept override
|
cpp_entity_type do_get_entity_type() const noexcept override;
|
||||||
{
|
|
||||||
return cpp_entity_type::using_declaration_t;
|
|
||||||
}
|
|
||||||
|
|
||||||
cpp_entity_id target_;
|
cpp_entity_id target_;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
34
src/cpp_namespace.cpp
Normal file
34
src/cpp_namespace.cpp
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
// Copyright (C) 2017 Jonathan Müller <jonathanmueller.dev@gmail.com>
|
||||||
|
// This file is subject to the license terms in the LICENSE file
|
||||||
|
// found in the top-level directory of this distribution.
|
||||||
|
|
||||||
|
#include <cppast/cpp_namespace.hpp>
|
||||||
|
|
||||||
|
#include <cppast/cpp_entity_type.hpp>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue