Add cpp_entity_index
This commit is contained in:
parent
079320cd01
commit
6d666a4d9f
3 changed files with 88 additions and 6 deletions
76
include/cppast/cpp_entity_index.hpp
Normal file
76
include/cppast/cpp_entity_index.hpp
Normal file
|
|
@ -0,0 +1,76 @@
|
||||||
|
// 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_INDEX_HPP_INCLUDED
|
||||||
|
#define CPPAST_CPP_ENTITY_INDEX_HPP_INCLUDED
|
||||||
|
|
||||||
|
#include <map>
|
||||||
|
#include <mutex>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include <type_safe/optional_ref.hpp>
|
||||||
|
#include <type_safe/reference.hpp>
|
||||||
|
#include <type_safe/strong_typedef.hpp>
|
||||||
|
|
||||||
|
#include <cppast/detail/assert.hpp>
|
||||||
|
|
||||||
|
namespace cppast
|
||||||
|
{
|
||||||
|
class cpp_entity;
|
||||||
|
|
||||||
|
/// A [ts::strong_typedef]() representing the unique id of a [cppast::cpp_entity]().
|
||||||
|
///
|
||||||
|
/// It is fully comparable.
|
||||||
|
struct cpp_entity_id : type_safe::strong_typedef<cpp_entity_id, std::string>,
|
||||||
|
type_safe::strong_typedef_op::equality_comparison<cpp_entity_id, bool>,
|
||||||
|
type_safe::strong_typedef_op::relational_comparison<cpp_entity_id, bool>
|
||||||
|
{
|
||||||
|
using strong_typedef::strong_typedef;
|
||||||
|
};
|
||||||
|
|
||||||
|
inline namespace literals
|
||||||
|
{
|
||||||
|
/// \returns A new [cppast::cpp_entity_id]() created from the given string.
|
||||||
|
inline cpp_entity_id operator""_id(const char* str, std::size_t size)
|
||||||
|
{
|
||||||
|
return cpp_entity_id(std::string(str, size));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// An index of all [cppast::cpp_entity]() objects created.
|
||||||
|
///
|
||||||
|
/// It maps [cppast::cpp_entity_id]() to references to the [cppast::cpp_entity]() objects.
|
||||||
|
class cpp_entity_index
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/// \effects Registers a new [cppast::cpp_entity]() also giving its [cppast::cpp_entity_id]().
|
||||||
|
/// \requires The entity must not have been registered before,
|
||||||
|
/// and it must live as long as the index lives.
|
||||||
|
/// \notes This operation is thread safe.
|
||||||
|
void register_entity(cpp_entity_id id, type_safe::object_ref<const cpp_entity> entity) const
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(mutex_);
|
||||||
|
auto result = map_.emplace(std::move(id), std::move(entity));
|
||||||
|
DEBUG_ASSERT(result.second, detail::precondition_error_handler{},
|
||||||
|
"duplicate index registration");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// \returns A [ts::optional_ref]() corresponding to the entity of the given [cppast::cpp_entity_id]().
|
||||||
|
/// \notes This operation is thread safe.
|
||||||
|
type_safe::optional_ref<const cpp_entity> lookup(const cpp_entity_id& id) const noexcept
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(mutex_);
|
||||||
|
auto iter = map_.find(id);
|
||||||
|
if (iter == map_.end())
|
||||||
|
return {};
|
||||||
|
return iter->second.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
mutable std::mutex mutex_;
|
||||||
|
mutable std::map<cpp_entity_id, type_safe::object_ref<const cpp_entity>> map_;
|
||||||
|
};
|
||||||
|
} // namespace cppast
|
||||||
|
|
||||||
|
#endif // CPPAST_CPP_ENTITY_INDEX_HPP_INCLUDED
|
||||||
|
|
@ -5,7 +5,7 @@
|
||||||
#ifndef CPPAST_CPP_FILE_HPP_INCLUDED
|
#ifndef CPPAST_CPP_FILE_HPP_INCLUDED
|
||||||
#define CPPAST_CPP_FILE_HPP_INCLUDED
|
#define CPPAST_CPP_FILE_HPP_INCLUDED
|
||||||
|
|
||||||
#include <cppast/cpp_entity.hpp>
|
#include <cppast/cpp_entity_index.hpp>
|
||||||
#include <cppast/cpp_scope.hpp>
|
#include <cppast/cpp_scope.hpp>
|
||||||
|
|
||||||
namespace cppast
|
namespace cppast
|
||||||
|
|
@ -28,12 +28,15 @@ namespace cppast
|
||||||
/// \effects Adds an entity.
|
/// \effects Adds an entity.
|
||||||
void add_child(std::unique_ptr<cpp_entity> child) noexcept
|
void add_child(std::unique_ptr<cpp_entity> child) noexcept
|
||||||
{
|
{
|
||||||
file_->children_.push_back(*file_, std::move(child));
|
file_->add_child(std::move(child));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// \effects Registers the file in the [cppast::cpp_entity_index]().
|
||||||
|
/// It will use the file name as identifier.
|
||||||
/// \returns The finished file.
|
/// \returns The finished file.
|
||||||
std::unique_ptr<cpp_file> finish() noexcept
|
std::unique_ptr<cpp_file> finish(const cpp_entity_index& idx) noexcept
|
||||||
{
|
{
|
||||||
|
idx.register_entity(cpp_entity_id(file_->name()), type_safe::ref(*file_));
|
||||||
return std::move(file_);
|
return std::move(file_);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -48,8 +51,6 @@ namespace cppast
|
||||||
|
|
||||||
/// \returns [cpp_entity_type::file_t]().
|
/// \returns [cpp_entity_type::file_t]().
|
||||||
cpp_entity_type do_get_entity_type() const noexcept override;
|
cpp_entity_type do_get_entity_type() const noexcept override;
|
||||||
|
|
||||||
detail::intrusive_list<cpp_entity> children_;
|
|
||||||
};
|
};
|
||||||
} // namespace cppast
|
} // namespace cppast
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@
|
||||||
#ifndef CPPAST_CPP_NAMESPACE_HPP_INCLUDED
|
#ifndef CPPAST_CPP_NAMESPACE_HPP_INCLUDED
|
||||||
#define CPPAST_CPP_NAMESPACE_HPP_INCLUDED
|
#define CPPAST_CPP_NAMESPACE_HPP_INCLUDED
|
||||||
|
|
||||||
|
#include <cppast/cpp_entity_index.hpp>
|
||||||
#include <cppast/cpp_scope.hpp>
|
#include <cppast/cpp_scope.hpp>
|
||||||
|
|
||||||
namespace cppast
|
namespace cppast
|
||||||
|
|
@ -29,9 +30,13 @@ namespace cppast
|
||||||
namespace_->add_child(std::move(child));
|
namespace_->add_child(std::move(child));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// \effects Registers the file in the [cppast::cpp_entity_index](),
|
||||||
|
/// using the given [cppast::cpp_entity_id]().
|
||||||
/// \returns The finished namespace.
|
/// \returns The finished namespace.
|
||||||
std::unique_ptr<cpp_namespace> finish() noexcept
|
std::unique_ptr<cpp_namespace> finish(const cpp_entity_index& idx,
|
||||||
|
cpp_entity_id id) noexcept
|
||||||
{
|
{
|
||||||
|
idx.register_entity(std::move(id), type_safe::ref(*namespace_));
|
||||||
return std::move(namespace_);
|
return std::move(namespace_);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue