Add cpp_variable_base

This commit is contained in:
Jonathan Müller 2017-01-22 12:53:49 +01:00
commit 4b69e823c8
4 changed files with 48 additions and 2 deletions

View file

@ -0,0 +1,46 @@
// 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_VARIABLE_BASE_HPP_INCLUDED
#define CPPAST_CPP_VARIABLE_BASE_HPP_INCLUDED
#include <cppast/cpp_entity.hpp>
#include <cppast/cpp_expression.hpp>
#include <cppast/cpp_type.hpp>
namespace cppast
{
/// Base class for all [cppast::cpp_entity]() modelling some kind of variable.
///
/// Examples are [cppast::cpp_variable]() or [cppast::cpp_function_parameter](),
/// or anything that is name/type/default-value triple.
class cpp_variable_base : public cpp_entity
{
public:
/// \returns A reference to the [cppast::cpp_type]() of the variable.
const cpp_type& type() const noexcept
{
return *type_;
}
/// \returns A [ts::optional_ref]() to the [cppast::cpp_expression]() that is the default value.
type_safe::optional_ref<const cpp_expression> default_value() const noexcept
{
return *default_;
}
protected:
cpp_variable_base(std::string name, std::unique_ptr<cpp_type> type,
std::unique_ptr<cpp_expression> def)
: cpp_entity(std::move(name)), type_(std::move(type)), default_(std::move(def))
{
}
private:
std::unique_ptr<cpp_type> type_;
std::unique_ptr<cpp_expression> default_;
};
} // namespace cppast
#endif // CPPAST_CPP_VARIABLE_BASE_HPP_INCLUDED

View file

@ -12,7 +12,7 @@ namespace cppast
/// Information about the state of a visit operation.
enum class visitor_info
{
leave_entity, //< Callback called for a leave entity without children.
leaf_entity, //< Callback called for a leaf entity without children.
container_entity_enter, //< Callback called for a container entity before the children.
container_entity_exit, //< Callback called for a container entity after the children.

View file

@ -47,7 +47,7 @@ bool detail::visit(const cpp_entity& e, detail::visitor_callback_t cb, void* fun
case cpp_entity_kind::using_declaration_t:
case cpp_entity_kind::type_alias_t:
case cpp_entity_kind::enum_value_t:
return cb(functor, e, visitor_info::leave_entity);
return cb(functor, e, visitor_info::leaf_entity);
case cpp_entity_kind::count:
break;