cppast/include/cppast/cpp_forward_declarable.hpp
2017-04-11 18:33:58 +02:00

95 lines
3.7 KiB
C++

// 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_FORWARD_DECLARABLE_HPP_INCLUDED
#define CPPAST_CPP_FORWARD_DECLARABLE_HPP_INCLUDED
#include <type_traits>
#include <type_safe/optional.hpp>
#include <cppast/cpp_entity.hpp>
#include <cppast/cpp_entity_ref.hpp>
namespace cppast
{
/// Mixin base class for all entities that can have a forward declaration.
///
/// Examples are [cppast::cpp_enum]() or [cppast::cpp_class](),
/// but also [cppast::cpp_function_base]().
/// Those entities can have multiple declarations and one definition.
class cpp_forward_declarable
{
public:
/// \returns Whether or not the entity is the definition.
bool is_definition() const noexcept
{
return !definition_.has_value();
}
/// \returns Whether or not the entity is "just" a declaration.
bool is_declaration() const noexcept
{
return definition_.has_value();
}
/// \returns The [cppast::cpp_entity_id]() of the definition,
/// if the current entity is not the definition.
const type_safe::optional<cpp_entity_id>& definition() const noexcept
{
return definition_;
}
protected:
/// \effects Marks the entity as definition.
/// \notes If it is not a definition,
/// [*set_definition]() must be called.
cpp_forward_declarable() noexcept = default;
~cpp_forward_declarable() noexcept = default;
/// \effects Sets the definition of the entity,
/// marking it as a forward declaration.
void set_definition(cpp_entity_id def) noexcept
{
definition_ = std::move(def);
}
private:
type_safe::optional<cpp_entity_id> definition_;
};
/// \returns Whether or not the given entity is a definition.
bool is_definition(const cpp_entity& e) noexcept;
class cpp_enum;
class cpp_class;
class cpp_variable;
class cpp_function_base;
/// Gets the definition of an entity.
/// \returns A [ts::optional_ref]() to the entity that is the definition.
/// If the entity is a definition or not derived from [cppast::cpp_forward_declarable]() (only valid for the generic entity overload),
/// returns a reference to the entity itself.
/// Otherwise lookups the definition id and returns it.
/// \notes The return value will only be `nullptr`, if the definition is not registered.
/// \group get_definition
type_safe::optional_ref<const cpp_entity> get_definition(const cpp_entity_index& idx,
const cpp_entity& e);
/// \group get_definition
type_safe::optional_ref<const cpp_enum> get_definition(const cpp_entity_index& idx,
const cpp_enum& e);
/// \group get_definition
type_safe::optional_ref<const cpp_class> get_definition(const cpp_entity_index& idx,
const cpp_class& e);
/// \group get_definition
type_safe::optional_ref<const cpp_variable> get_definition(const cpp_entity_index& idx,
const cpp_variable& e);
/// \group get_definition
type_safe::optional_ref<const cpp_function_base> get_definition(const cpp_entity_index& idx,
const cpp_function_base& e);
} // namespace cppast
#endif // CPPAST_CPP_FORWARD_DECLARABLE_HPP_INCLUDED