Add user data pointer to entities

This commit is contained in:
Jonathan Müller 2017-09-15 11:51:25 +02:00
commit ffbe878fcb
3 changed files with 64 additions and 4 deletions

View file

@ -5,6 +5,7 @@
#ifndef CPPAST_CPP_ENTITY_HPP_INCLUDED
#define CPPAST_CPP_ENTITY_HPP_INCLUDED
#include <atomic>
#include <string>
#include <type_safe/optional_ref.hpp>
@ -121,9 +122,26 @@ namespace cppast
comment_ = comment.value_or("");
}
/// \returns The specified user data.
void* user_data() const noexcept
{
return user_data_.load();
}
/// \effects Sets some kind of user data.
///
/// User data is just some kind of pointer, there are no requirements.
/// The class will do no lifetime management.
///
/// User data is useful if you need to store additional data for an entity without the need to maintain a registry.
void set_user_data(void* data) const noexcept
{
user_data_ = data;
}
protected:
/// \effects Creates it giving it the the name.
cpp_entity(std::string name) : name_(std::move(name))
cpp_entity(std::string name) : name_(std::move(name)), user_data_(nullptr)
{
}
@ -146,6 +164,7 @@ namespace cppast
std::string name_;
std::string comment_;
type_safe::optional_ref<const cpp_entity> parent_;
mutable std::atomic<void*> user_data_;
template <typename T>
friend struct detail::intrusive_list_access;

View file

@ -5,6 +5,7 @@
#ifndef CPPAST_CPP_EXPRESSION_HPP_INCLUDED
#define CPPAST_CPP_EXPRESSION_HPP_INCLUDED
#include <atomic>
#include <memory>
#include <cppast/cpp_type.hpp>
@ -40,10 +41,27 @@ namespace cppast
return *type_;
}
/// \returns The specified user data.
void* user_data() const noexcept
{
return user_data_.load();
}
/// \effects Sets some kind of user data.
///
/// User data is just some kind of pointer, there are no requirements.
/// The class will do no lifetime management.
///
/// User data is useful if you need to store additional data for an entity without the need to maintain a registry.
void set_user_data(void* data) const noexcept
{
user_data_ = data;
}
protected:
/// \effects Creates it given the type.
/// \requires The type must not be `nullptr`.
cpp_expression(std::unique_ptr<cpp_type> type) : type_(std::move(type))
cpp_expression(std::unique_ptr<cpp_type> type) : type_(std::move(type)), user_data_(nullptr)
{
DEBUG_ASSERT(type_ != nullptr, detail::precondition_error_handler{});
}
@ -52,7 +70,8 @@ namespace cppast
/// \returns The [cppast::cpp_expression_kind]().
virtual cpp_expression_kind do_get_kind() const noexcept = 0;
std::unique_ptr<cpp_type> type_;
std::unique_ptr<cpp_type> type_;
mutable std::atomic<void*> user_data_;
};
/// An unexposed [cppast::cpp_expression]().

View file

@ -5,6 +5,7 @@
#ifndef CPPAST_CPP_TYPE_HPP_INCLUDED
#define CPPAST_CPP_TYPE_HPP_INCLUDED
#include <atomic>
#include <memory>
#include <cppast/detail/intrusive_list.hpp>
@ -55,8 +56,27 @@ namespace cppast
return do_get_kind();
}
/// \returns The specified user data.
void* user_data() const noexcept
{
return user_data_.load();
}
/// \effects Sets some kind of user data.
///
/// User data is just some kind of pointer, there are no requirements.
/// The class will do no lifetime management.
///
/// User data is useful if you need to store additional data for an entity without the need to maintain a registry.
void set_user_data(void* data) const noexcept
{
user_data_ = data;
}
protected:
cpp_type() noexcept = default;
cpp_type() noexcept : user_data_(nullptr)
{
}
private:
/// \returns The [cppast::cpp_type_kind]().
@ -66,6 +86,8 @@ namespace cppast
{
}
mutable std::atomic<void*> user_data_;
template <typename T>
friend struct detail::intrusive_list_access;
friend detail::intrusive_list_node<cpp_type>;