Add cpp_enum and cpp_enum_value
This commit is contained in:
parent
023ce3e87c
commit
a2c54165bd
6 changed files with 142 additions and 2 deletions
|
|
@ -19,6 +19,9 @@ namespace cppast
|
|||
using_directive_t,
|
||||
using_declaration_t,
|
||||
|
||||
enum_t,
|
||||
enum_value_t,
|
||||
|
||||
count,
|
||||
};
|
||||
|
||||
|
|
|
|||
114
include/cppast/cpp_enum.hpp
Normal file
114
include/cppast/cpp_enum.hpp
Normal file
|
|
@ -0,0 +1,114 @@
|
|||
// 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_ENUM_HPP_INCLUDED
|
||||
#define CPPAST_CPP_ENUM_HPP_INCLUDED
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include <type_safe/optional_ref.hpp>
|
||||
|
||||
#include <cppast/cpp_entity_container.hpp>
|
||||
#include <cppast/cpp_entity_index.hpp>
|
||||
#include <cppast/cpp_entity.hpp>
|
||||
#include <cppast/cpp_expression.hpp>
|
||||
#include <cppast/cpp_type.hpp>
|
||||
|
||||
namespace cppast
|
||||
{
|
||||
/// A [cppast::cpp_entity]() modelling the value of an [cppast::cpp_enum]().
|
||||
class cpp_enum_value final : public cpp_entity
|
||||
{
|
||||
public:
|
||||
/// \returns A newly created and registered enum value.
|
||||
/// \notes `value` may be `nullptr`, in which case the enum has an implicit value.
|
||||
static std::unique_ptr<cpp_enum_value> build(
|
||||
const cpp_entity_index& idx, cpp_entity_id id, std::string name,
|
||||
std::unique_ptr<cpp_expression> value = nullptr)
|
||||
{
|
||||
auto result = std::unique_ptr<cpp_enum_value>(
|
||||
new cpp_enum_value(std::move(name), std::move(value)));
|
||||
idx.register_entity(std::move(id), type_safe::ref(*result));
|
||||
return result;
|
||||
}
|
||||
|
||||
/// \returns A [ts::optional_ref]() to the [cppast::cpp_expression]() that is the enum value.
|
||||
/// \notes It only has an associated expression if the value is explictly given.
|
||||
type_safe::optional_ref<const cpp_expression> value() const noexcept
|
||||
{
|
||||
return type_safe::opt_cref(value_.get());
|
||||
}
|
||||
|
||||
private:
|
||||
cpp_enum_value(std::string name, std::unique_ptr<cpp_expression> value)
|
||||
: cpp_entity(std::move(name)), value_(std::move(value))
|
||||
{
|
||||
}
|
||||
|
||||
cpp_entity_type do_get_entity_type() const noexcept override;
|
||||
|
||||
std::unique_ptr<cpp_expression> value_;
|
||||
};
|
||||
|
||||
/// A [cppast::cpp_entity]() modelling a C++ enumeration.
|
||||
class cpp_enum final : public cpp_entity, public cpp_entity_container<cpp_enum, cpp_enum_value>
|
||||
{
|
||||
public:
|
||||
/// Builds a [cppast::cpp_enum]().
|
||||
class builder
|
||||
{
|
||||
public:
|
||||
/// \effects Sets the name, underlying type and whether it is scoped.
|
||||
/// \notes The underlying type may be `nullptr` if it is not explictly given.
|
||||
builder(std::string name, bool scoped, std::unique_ptr<cpp_type> type = nullptr)
|
||||
: enum_(new cpp_enum(std::move(name), std::move(type), scoped))
|
||||
{
|
||||
}
|
||||
|
||||
/// \effects Adds a [cppast::cpp_enum_value]().
|
||||
void add_value(std::unique_ptr<cpp_enum_value> value)
|
||||
{
|
||||
enum_->add_child(std::move(value));
|
||||
}
|
||||
|
||||
/// \effects Registers the enum in the [cppast::cpp_entity_index](),
|
||||
/// using the given [cppast::cpp_entity_id]().
|
||||
/// \returns The finished enum.
|
||||
std::unique_ptr<cpp_enum> finish(const cpp_entity_index& idx, cpp_entity_id id) noexcept
|
||||
{
|
||||
idx.register_entity(std::move(id), type_safe::ref(*enum_));
|
||||
return std::move(enum_);
|
||||
}
|
||||
|
||||
private:
|
||||
std::unique_ptr<cpp_enum> enum_;
|
||||
};
|
||||
|
||||
/// \returns A [ts::optional_ref]() to the underlying [cppast::cpp_type]() of the enum.
|
||||
/// \notes It only has an associated underlying type if it is not explictly given.
|
||||
type_safe::optional_ref<const cpp_type> underlying_type() const noexcept
|
||||
{
|
||||
return type_safe::opt_cref(type_.get());
|
||||
}
|
||||
|
||||
/// \returns Whether or not it is a scoped enumeration (i.e. an `enum class`).
|
||||
bool is_scoped() const noexcept
|
||||
{
|
||||
return scoped_;
|
||||
}
|
||||
|
||||
private:
|
||||
cpp_enum(std::string name, std::unique_ptr<cpp_type> type, bool scoped)
|
||||
: cpp_entity(std::move(name)), type_(std::move(type)), scoped_(scoped)
|
||||
{
|
||||
}
|
||||
|
||||
cpp_entity_type do_get_entity_type() const noexcept override;
|
||||
|
||||
std::unique_ptr<cpp_type> type_;
|
||||
bool scoped_;
|
||||
};
|
||||
} // namespace cppast
|
||||
|
||||
#endif // CPPAST_CPP_ENUM_HPP_INCLUDED
|
||||
|
|
@ -33,7 +33,7 @@ namespace cppast
|
|||
namespace_->add_child(std::move(child));
|
||||
}
|
||||
|
||||
/// \effects Registers the file in the [cppast::cpp_entity_index](),
|
||||
/// \effects Registers the namespace in the [cppast::cpp_entity_index](),
|
||||
/// using the given [cppast::cpp_entity_id]().
|
||||
/// \returns The finished namespace.
|
||||
std::unique_ptr<cpp_namespace> finish(const cpp_entity_index& idx,
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@ namespace cppast
|
|||
{
|
||||
static_assert(std::is_base_of<U, T>::value, "must be a base");
|
||||
obj.next_ = std::move(node);
|
||||
return obj.next_.get();
|
||||
return static_cast<T*>(obj.next_.get());
|
||||
}
|
||||
|
||||
template <typename U, typename V>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue