From 2812c7bd0ac4cf182e5733f73a46e2801f7cb70f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20M=C3=BCller?= Date: Sat, 21 Jan 2017 12:25:58 +0100 Subject: [PATCH] Set parent when adding automatically --- include/cppast/cpp_entity.hpp | 16 ++++++++-------- include/cppast/cpp_file.hpp | 10 ++-------- include/cppast/cpp_namespace.hpp | 14 ++++---------- include/cppast/cpp_scope.hpp | 4 +--- include/cppast/detail/intrusive_list.hpp | 16 +++++++++++++++- 5 files changed, 30 insertions(+), 30 deletions(-) diff --git a/include/cppast/cpp_entity.hpp b/include/cppast/cpp_entity.hpp index 47b5fa0..bb005ac 100644 --- a/include/cppast/cpp_entity.hpp +++ b/include/cppast/cpp_entity.hpp @@ -42,14 +42,8 @@ namespace cppast } protected: - /// \effects Creates it giving it the parent entity and the name. - cpp_entity(const cpp_entity& parent, std::string name) - : parent_(parent), name_(std::move(name)) - { - } - - /// \effects Creates it giving it no parent and the name. - cpp_entity(std::nullptr_t, std::string name) : name_(std::move(name)) + /// \effects Creates it giving it the the name. + cpp_entity(std::string name) : name_(std::move(name)) { } @@ -57,11 +51,17 @@ namespace cppast /// \returns The type of the entity. virtual cpp_entity_type do_get_entity_type() const noexcept = 0; + void on_insert(const cpp_entity& parent) noexcept + { + parent_ = parent; + } + type_safe::optional_ref parent_; std::string name_; template friend struct detail::intrusive_list_access; + friend detail::intrusive_list_node; }; } // namespace cppast diff --git a/include/cppast/cpp_file.hpp b/include/cppast/cpp_file.hpp index c809a1f..ddfaf36 100644 --- a/include/cppast/cpp_file.hpp +++ b/include/cppast/cpp_file.hpp @@ -27,7 +27,7 @@ namespace cppast /// \effects Adds an entity. void add_child(std::unique_ptr child) noexcept { - file_->children_.push_back(std::move(child)); + file_->children_.push_back(*file_, std::move(child)); } /// \returns The finished file. @@ -36,12 +36,6 @@ namespace cppast return std::move(file_); } - /// \returns A reference to (not yet finished) file. - const cpp_file& get() const noexcept - { - return *file_; - } - private: std::unique_ptr file_; }; @@ -61,7 +55,7 @@ namespace cppast } private: - cpp_file(std::string name) : cpp_entity(nullptr, std::move(name)) + cpp_file(std::string name) : cpp_entity(std::move(name)) { } diff --git a/include/cppast/cpp_namespace.hpp b/include/cppast/cpp_namespace.hpp index 54f7b49..0ac73aa 100644 --- a/include/cppast/cpp_namespace.hpp +++ b/include/cppast/cpp_namespace.hpp @@ -18,8 +18,8 @@ namespace cppast { public: /// \effects Sets the namespace name and whether it is inline. - explicit builder(const cpp_entity& parent, std::string name, bool is_inline) - : namespace_(new cpp_namespace(parent, std::move(name), is_inline)) + explicit builder(std::string name, bool is_inline) + : namespace_(new cpp_namespace(std::move(name), is_inline)) { } @@ -35,12 +35,6 @@ namespace cppast return std::move(namespace_); } - /// \returns A reference to (not yet finished) namespace. - const cpp_namespace& get() const noexcept - { - return *namespace_; - } - private: std::unique_ptr namespace_; }; @@ -52,8 +46,8 @@ namespace cppast } private: - cpp_namespace(const cpp_entity& parent, std::string name, bool is_inline) - : cpp_scope(parent, std::move(name)), inline_(is_inline) + cpp_namespace(std::string name, bool is_inline) + : cpp_scope(std::move(name)), inline_(is_inline) { } diff --git a/include/cppast/cpp_scope.hpp b/include/cppast/cpp_scope.hpp index cae376a..ea26ef9 100644 --- a/include/cppast/cpp_scope.hpp +++ b/include/cppast/cpp_scope.hpp @@ -44,9 +44,7 @@ namespace cppast /// \effects Adds a new child to the scope. void add_child(std::unique_ptr ptr) noexcept { - DEBUG_ASSERT(ptr->parent() == *this, detail::precondition_error_handler{}, - "parent not set properly"); - children_.push_back(std::move(ptr)); + children_.push_back(*this, std::move(ptr)); } /// \returns A non-const iterator to the first child. diff --git a/include/cppast/detail/intrusive_list.hpp b/include/cppast/detail/intrusive_list.hpp index 94bd3ec..20068d9 100644 --- a/include/cppast/detail/intrusive_list.hpp +++ b/include/cppast/detail/intrusive_list.hpp @@ -21,6 +21,11 @@ namespace cppast { std::unique_ptr next_; + void do_on_insert(const T& parent) noexcept + { + static_cast(*this).on_insert(parent); + } + template friend struct intrusive_list_access; }; @@ -42,6 +47,12 @@ namespace cppast obj.next_ = std::move(node); return obj.next_.get(); } + + template + static void on_insert(U& obj, const V& parent) + { + obj.do_on_insert(parent); + } }; template @@ -111,7 +122,8 @@ namespace cppast intrusive_list() = default; //=== modifiers ===// - void push_back(std::unique_ptr obj) noexcept + template + void push_back(const U& parent, std::unique_ptr obj) noexcept { DEBUG_ASSERT(obj != nullptr, detail::assert_handler{}); @@ -125,6 +137,8 @@ namespace cppast first_ = std::move(obj); last_ = type_safe::opt_ref(first_.get()); } + + intrusive_list_access::on_insert(last_.value(), parent); } //=== accesors ===//