Extract cpp_entity_container CRTP class
This commit is contained in:
parent
2812c7bd0a
commit
079320cd01
3 changed files with 37 additions and 38 deletions
|
|
@ -6,13 +6,14 @@
|
||||||
#define CPPAST_CPP_FILE_HPP_INCLUDED
|
#define CPPAST_CPP_FILE_HPP_INCLUDED
|
||||||
|
|
||||||
#include <cppast/cpp_entity.hpp>
|
#include <cppast/cpp_entity.hpp>
|
||||||
|
#include <cppast/cpp_scope.hpp>
|
||||||
|
|
||||||
namespace cppast
|
namespace cppast
|
||||||
{
|
{
|
||||||
/// A [cppast::cpp_entity]() modelling a file.
|
/// A [cppast::cpp_entity]() modelling a file.
|
||||||
///
|
///
|
||||||
/// This is the top-level entity of the AST.
|
/// This is the top-level entity of the AST.
|
||||||
class cpp_file final : public cpp_entity
|
class cpp_file final : public cpp_entity, public cpp_entity_container<cpp_file, cpp_entity>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
/// Builds a [cppast::cpp_file]().
|
/// Builds a [cppast::cpp_file]().
|
||||||
|
|
@ -40,20 +41,6 @@ namespace cppast
|
||||||
std::unique_ptr<cpp_file> file_;
|
std::unique_ptr<cpp_file> file_;
|
||||||
};
|
};
|
||||||
|
|
||||||
using iterator = detail::intrusive_list<cpp_entity>::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();
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
cpp_file(std::string name) : cpp_entity(std::move(name))
|
cpp_file(std::string name) : cpp_entity(std::move(name))
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@
|
||||||
namespace cppast
|
namespace cppast
|
||||||
{
|
{
|
||||||
/// A [cppast::cpp_entity]() modelling a namespace.
|
/// A [cppast::cpp_entity]() modelling a namespace.
|
||||||
class cpp_namespace : public cpp_scope
|
class cpp_namespace final : public cpp_scope
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
/// Builds a [cppast::cpp_namespace]().
|
/// Builds a [cppast::cpp_namespace]().
|
||||||
|
|
|
||||||
|
|
@ -9,22 +9,14 @@
|
||||||
|
|
||||||
namespace cppast
|
namespace cppast
|
||||||
{
|
{
|
||||||
/// Base class for all entities that add a scope.
|
/// Helper class for entities that are containers.
|
||||||
///
|
///
|
||||||
/// Examples are namespaces and classes,
|
/// Inherit from it to generate container access.
|
||||||
/// or anything else that can appear followed by `::`.
|
template <class Derived, typename T>
|
||||||
class cpp_scope : public cpp_entity
|
class cpp_entity_container
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
/// \returns The name of the scope.
|
using iterator = typename detail::intrusive_list<T>::const_iterator;
|
||||||
/// 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<cpp_entity>::const_iterator;
|
|
||||||
|
|
||||||
/// \returns A const iterator to the first child.
|
/// \returns A const iterator to the first child.
|
||||||
iterator begin() const noexcept
|
iterator begin() const noexcept
|
||||||
|
|
@ -39,26 +31,48 @@ namespace cppast
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
using cpp_entity::cpp_entity;
|
/// \effects Adds a new child to the container.
|
||||||
|
void add_child(std::unique_ptr<T> ptr) noexcept
|
||||||
/// \effects Adds a new child to the scope.
|
|
||||||
void add_child(std::unique_ptr<cpp_entity> ptr) noexcept
|
|
||||||
{
|
{
|
||||||
children_.push_back(*this, std::move(ptr));
|
children_.push_back(static_cast<Derived&>(*this), std::move(ptr));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// \returns A non-const iterator to the first child.
|
/// \returns A non-const iterator to the first child.
|
||||||
detail::intrusive_list<cpp_entity>::iterator mutable_begin() noexcept
|
typename detail::intrusive_list<T>::iterator mutable_begin() noexcept
|
||||||
{
|
{
|
||||||
return children_.begin();
|
return children_.begin();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// \returns A non-const iterator one past the last child.
|
/// \returns A non-const iterator one past the last child.
|
||||||
detail::intrusive_list<cpp_entity>::iterator mutable_end() noexcept
|
typename detail::intrusive_list<T>::iterator mutable_end() noexcept
|
||||||
{
|
{
|
||||||
return children_.begin();
|
return children_.begin();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
~cpp_entity_container() noexcept = default;
|
||||||
|
|
||||||
|
private:
|
||||||
|
detail::intrusive_list<T> children_;
|
||||||
|
};
|
||||||
|
|
||||||
|
/// 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 cpp_entity_container<cpp_scope, 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();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
using cpp_entity::cpp_entity;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/// \returns The name of the new scope,
|
/// \returns The name of the new scope,
|
||||||
/// defaults to the name of the entity.
|
/// defaults to the name of the entity.
|
||||||
|
|
@ -66,8 +80,6 @@ namespace cppast
|
||||||
{
|
{
|
||||||
return name();
|
return name();
|
||||||
}
|
}
|
||||||
|
|
||||||
detail::intrusive_list<cpp_entity> children_;
|
|
||||||
};
|
};
|
||||||
} // namespace cppast
|
} // namespace cppast
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue