Add basic entity
This commit is contained in:
commit
9a944cd500
4 changed files with 287 additions and 0 deletions
61
include/cppast/cpp_entity.hpp
Normal file
61
include/cppast/cpp_entity.hpp
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
// 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.
|
||||
|
||||
#ifndef CPPAST_CPP_ENTITY_HPP_INCLUDED
|
||||
#define CPPAST_CPP_ENTITY_HPP_INCLUDED
|
||||
|
||||
#include <type_safe/optional_ref.hpp>
|
||||
|
||||
#include <cppast/detail/intrusive_list.hpp>
|
||||
|
||||
namespace cppast
|
||||
{
|
||||
enum class cpp_entity_type;
|
||||
|
||||
/// The base class for all entities in the C++ AST.
|
||||
class cpp_entity : detail::intrusive_list_node<cpp_entity>
|
||||
{
|
||||
public:
|
||||
cpp_entity(const cpp_entity&) = delete;
|
||||
cpp_entity& operator=(const cpp_entity&) = delete;
|
||||
|
||||
virtual ~cpp_entity() noexcept = default;
|
||||
|
||||
/// \returns The type of the entity.
|
||||
cpp_entity_type type() const noexcept
|
||||
{
|
||||
return do_get_entity_type();
|
||||
}
|
||||
|
||||
/// \returns The name of the entity.
|
||||
/// The name is the string associated with the entity's declaration.
|
||||
const std::string& name() const noexcept
|
||||
{
|
||||
return name_;
|
||||
}
|
||||
|
||||
/// \returns A [ts::optional_ref]() to the parent entity in the AST.
|
||||
type_safe::optional_ref<cpp_entity> parent() const noexcept
|
||||
{
|
||||
return parent_;
|
||||
}
|
||||
|
||||
protected:
|
||||
/// \effects Creates it giving it the parent entity and the name.
|
||||
cpp_entity(type_safe::optional_ref<cpp_entity> parent, std::string name)
|
||||
: parent_(std::move(parent)), name_(std::move(name))
|
||||
{
|
||||
}
|
||||
|
||||
private:
|
||||
virtual cpp_entity_type do_get_entity_type() const noexcept = 0;
|
||||
|
||||
type_safe::optional_ref<cpp_entity> parent_;
|
||||
std::string name_;
|
||||
|
||||
friend detail::intrusive_list_access<cpp_entity>;
|
||||
};
|
||||
} // namespace cppast
|
||||
|
||||
#endif // CPPAST_CPP_ENTITY_HPP_INCLUDED
|
||||
17
include/cppast/cpp_entity_type.hpp
Normal file
17
include/cppast/cpp_entity_type.hpp
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
// 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.
|
||||
|
||||
#ifndef CPPAST_CPP_ENTITY_TYPE_HPP_INCLUDED
|
||||
#define CPPAST_CPP_ENTITY_TYPE_HPP_INCLUDED
|
||||
|
||||
namespace cppast
|
||||
{
|
||||
/// All possible types of C++ entities.
|
||||
enum class cpp_entity_type
|
||||
{
|
||||
|
||||
};
|
||||
} // namespace cppast
|
||||
|
||||
#endif // CPPAST_CPP_ENTITY_TYPE_HPP_INCLUDED
|
||||
20
include/cppast/detail/assert.hpp
Normal file
20
include/cppast/detail/assert.hpp
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
// 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.
|
||||
|
||||
#ifndef CPPAST_ASSERT_HPP_INCLUDED
|
||||
#define CPPAST_ASSERT_HPP_INCLUDED
|
||||
|
||||
#include <debug_assert.hpp>
|
||||
|
||||
namespace cppast
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
struct assert_handler : debug_assert::set_level<1>, debug_assert::default_handler
|
||||
{
|
||||
};
|
||||
}
|
||||
} // namespace cppast::detail
|
||||
|
||||
#endif // CPPAST_ASSERT_HPP_INCLUDED
|
||||
189
include/cppast/detail/intrusive_list.hpp
Normal file
189
include/cppast/detail/intrusive_list.hpp
Normal file
|
|
@ -0,0 +1,189 @@
|
|||
// 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.
|
||||
|
||||
#ifndef CPPAST_INTRUSIVE_LIST_HPP_INCLUDED
|
||||
#define CPPAST_INTRUSIVE_LIST_HPP_INCLUDED
|
||||
|
||||
#include <memory>
|
||||
#include <iterator>
|
||||
|
||||
#include <type_safe/optional_ref.hpp>
|
||||
|
||||
#include <cppast/detail/assert.hpp>
|
||||
|
||||
namespace cppast
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
template <typename T>
|
||||
class intrusive_list;
|
||||
|
||||
template <typename T>
|
||||
class intrusive_list_node
|
||||
{
|
||||
std::unique_ptr<T> next_;
|
||||
|
||||
template <typename U>
|
||||
friend struct intrusive_list_access;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct intrusive_list_access
|
||||
{
|
||||
template <typename U>
|
||||
static T* get_next(const intrusive_list_node<U>& obj)
|
||||
{
|
||||
static_assert(std::is_base_of<U, T>::value, "must be a base");
|
||||
return static_cast<T*>(obj.next_.get());
|
||||
}
|
||||
|
||||
template <typename U>
|
||||
static T* set_next(intrusive_list_node<U>& obj, std::unique_ptr<T> node)
|
||||
{
|
||||
static_assert(std::is_base_of<U, T>::value, "must be a base");
|
||||
obj.next_ = std::move(node);
|
||||
return obj.next_.get();
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class intrusive_list_iterator
|
||||
{
|
||||
public:
|
||||
using value_type = T;
|
||||
using reference = T&;
|
||||
using pointer = T*;
|
||||
using difference_type = std::ptrdiff_t;
|
||||
using iterator_category = std::forward_iterator_tag;
|
||||
|
||||
intrusive_list_iterator() noexcept : cur_(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
reference operator*() const noexcept
|
||||
{
|
||||
return *cur_;
|
||||
}
|
||||
|
||||
pointer operator->() const noexcept
|
||||
{
|
||||
return cur_;
|
||||
}
|
||||
|
||||
intrusive_list_iterator& operator++() noexcept
|
||||
{
|
||||
cur_ = intrusive_list_access<T>::get_next(*cur_);
|
||||
return *this;
|
||||
}
|
||||
|
||||
intrusive_list_iterator operator++(int)noexcept
|
||||
{
|
||||
auto tmp = *this;
|
||||
++(*this);
|
||||
return tmp;
|
||||
}
|
||||
|
||||
friend bool operator==(const intrusive_list_iterator& a,
|
||||
const intrusive_list_iterator& b) noexcept
|
||||
{
|
||||
return a.cur_ == b.cur_;
|
||||
}
|
||||
|
||||
friend bool operator!=(const intrusive_list_iterator& a,
|
||||
const intrusive_list_iterator& b) noexcept
|
||||
{
|
||||
return !(a == b);
|
||||
}
|
||||
|
||||
private:
|
||||
intrusive_list_iterator(T* ptr) : cur_(ptr)
|
||||
{
|
||||
}
|
||||
|
||||
T* cur_;
|
||||
|
||||
friend intrusive_list<T>;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class intrusive_list
|
||||
{
|
||||
public:
|
||||
intrusive_list() = default;
|
||||
|
||||
//=== modifiers ===//
|
||||
void push_back(std::unique_ptr<T> obj) noexcept
|
||||
{
|
||||
DEBUG_ASSERT(obj, detail::assert_handler{});
|
||||
|
||||
if (last_)
|
||||
{
|
||||
auto ptr = intrusive_list_access<T>::set_next(last_.value(), std::move(obj));
|
||||
last_ = type_safe::opt_cref(ptr);
|
||||
}
|
||||
else
|
||||
{
|
||||
first_ = std::move(obj);
|
||||
last_ = type_safe::opt_ref(first_.get());
|
||||
}
|
||||
}
|
||||
|
||||
//=== accesors ===//
|
||||
bool empty() const noexcept
|
||||
{
|
||||
return first_ == nullptr;
|
||||
}
|
||||
|
||||
type_safe::optional_ref<T> front() noexcept
|
||||
{
|
||||
return type_safe::opt_ref(first_.get());
|
||||
}
|
||||
|
||||
type_safe::optional_ref<const T> front() const noexcept
|
||||
{
|
||||
return type_safe::opt_cref(first_.get());
|
||||
}
|
||||
|
||||
type_safe::optional_ref<T> back() noexcept
|
||||
{
|
||||
return last_;
|
||||
}
|
||||
|
||||
type_safe::optional_ref<const T> back() const noexcept
|
||||
{
|
||||
return last_;
|
||||
}
|
||||
|
||||
//=== iterators ===//
|
||||
using iterator = intrusive_list_iterator<T>;
|
||||
using const_iterator = intrusive_list_iterator<const T>;
|
||||
|
||||
iterator begin() noexcept
|
||||
{
|
||||
return iterator(first_.get());
|
||||
}
|
||||
|
||||
iterator end() noexcept
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
const_iterator begin() const noexcept
|
||||
{
|
||||
return const_iterator(first_.get());
|
||||
}
|
||||
|
||||
const_iterator end() const noexcept
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
private:
|
||||
std::unique_ptr<T> first_;
|
||||
type_safe::optional_ref<T> last_;
|
||||
};
|
||||
}
|
||||
} // namespace cppast::detail
|
||||
|
||||
#endif // CPPAST_INTRUSIVE_LIST_HPP_INCLUDED
|
||||
Loading…
Add table
Add a link
Reference in a new issue