From ffbe878fcb3e6b401f33cb1c075b86735763f681 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20M=C3=BCller?= Date: Fri, 15 Sep 2017 11:51:25 +0200 Subject: [PATCH] Add user data pointer to entities --- include/cppast/cpp_entity.hpp | 21 ++++++++++++++++++++- include/cppast/cpp_expression.hpp | 23 +++++++++++++++++++++-- include/cppast/cpp_type.hpp | 24 +++++++++++++++++++++++- 3 files changed, 64 insertions(+), 4 deletions(-) diff --git a/include/cppast/cpp_entity.hpp b/include/cppast/cpp_entity.hpp index 0fe192d..6fbee19 100644 --- a/include/cppast/cpp_entity.hpp +++ b/include/cppast/cpp_entity.hpp @@ -5,6 +5,7 @@ #ifndef CPPAST_CPP_ENTITY_HPP_INCLUDED #define CPPAST_CPP_ENTITY_HPP_INCLUDED +#include #include #include @@ -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 parent_; + mutable std::atomic user_data_; template friend struct detail::intrusive_list_access; diff --git a/include/cppast/cpp_expression.hpp b/include/cppast/cpp_expression.hpp index 9bcbb5c..5ea2503 100644 --- a/include/cppast/cpp_expression.hpp +++ b/include/cppast/cpp_expression.hpp @@ -5,6 +5,7 @@ #ifndef CPPAST_CPP_EXPRESSION_HPP_INCLUDED #define CPPAST_CPP_EXPRESSION_HPP_INCLUDED +#include #include #include @@ -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 type) : type_(std::move(type)) + cpp_expression(std::unique_ptr 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 type_; + std::unique_ptr type_; + mutable std::atomic user_data_; }; /// An unexposed [cppast::cpp_expression](). diff --git a/include/cppast/cpp_type.hpp b/include/cppast/cpp_type.hpp index 6db6695..8325baa 100644 --- a/include/cppast/cpp_type.hpp +++ b/include/cppast/cpp_type.hpp @@ -5,6 +5,7 @@ #ifndef CPPAST_CPP_TYPE_HPP_INCLUDED #define CPPAST_CPP_TYPE_HPP_INCLUDED +#include #include #include @@ -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 user_data_; + template friend struct detail::intrusive_list_access; friend detail::intrusive_list_node;