Set parent when adding automatically

This commit is contained in:
Jonathan Müller 2017-01-21 12:25:58 +01:00
commit 2812c7bd0a
5 changed files with 30 additions and 30 deletions

View file

@ -42,14 +42,8 @@ namespace cppast
} }
protected: protected:
/// \effects Creates it giving it the parent entity and the name. /// \effects Creates it giving it the the name.
cpp_entity(const cpp_entity& parent, std::string name) cpp_entity(std::string name) : name_(std::move(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))
{ {
} }
@ -57,11 +51,17 @@ namespace cppast
/// \returns The type of the entity. /// \returns The type of the entity.
virtual cpp_entity_type do_get_entity_type() const noexcept = 0; 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<const cpp_entity> parent_; type_safe::optional_ref<const cpp_entity> parent_;
std::string name_; std::string name_;
template <typename T> template <typename T>
friend struct detail::intrusive_list_access; friend struct detail::intrusive_list_access;
friend detail::intrusive_list_node<cpp_entity>;
}; };
} // namespace cppast } // namespace cppast

View file

@ -27,7 +27,7 @@ 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(std::move(child)); file_->children_.push_back(*file_, std::move(child));
} }
/// \returns The finished file. /// \returns The finished file.
@ -36,12 +36,6 @@ namespace cppast
return std::move(file_); return std::move(file_);
} }
/// \returns A reference to (not yet finished) file.
const cpp_file& get() const noexcept
{
return *file_;
}
private: private:
std::unique_ptr<cpp_file> file_; std::unique_ptr<cpp_file> file_;
}; };
@ -61,7 +55,7 @@ namespace cppast
} }
private: private:
cpp_file(std::string name) : cpp_entity(nullptr, std::move(name)) cpp_file(std::string name) : cpp_entity(std::move(name))
{ {
} }

View file

@ -18,8 +18,8 @@ namespace cppast
{ {
public: public:
/// \effects Sets the namespace name and whether it is inline. /// \effects Sets the namespace name and whether it is inline.
explicit builder(const cpp_entity& parent, std::string name, bool is_inline) explicit builder(std::string name, bool is_inline)
: namespace_(new cpp_namespace(parent, std::move(name), is_inline)) : namespace_(new cpp_namespace(std::move(name), is_inline))
{ {
} }
@ -35,12 +35,6 @@ namespace cppast
return std::move(namespace_); return std::move(namespace_);
} }
/// \returns A reference to (not yet finished) namespace.
const cpp_namespace& get() const noexcept
{
return *namespace_;
}
private: private:
std::unique_ptr<cpp_namespace> namespace_; std::unique_ptr<cpp_namespace> namespace_;
}; };
@ -52,8 +46,8 @@ namespace cppast
} }
private: private:
cpp_namespace(const cpp_entity& parent, std::string name, bool is_inline) cpp_namespace(std::string name, bool is_inline)
: cpp_scope(parent, std::move(name)), inline_(is_inline) : cpp_scope(std::move(name)), inline_(is_inline)
{ {
} }

View file

@ -44,9 +44,7 @@ namespace cppast
/// \effects Adds a new child to the scope. /// \effects Adds a new child to the scope.
void add_child(std::unique_ptr<cpp_entity> ptr) noexcept void add_child(std::unique_ptr<cpp_entity> ptr) noexcept
{ {
DEBUG_ASSERT(ptr->parent() == *this, detail::precondition_error_handler{}, children_.push_back(*this, std::move(ptr));
"parent not set properly");
children_.push_back(std::move(ptr));
} }
/// \returns A non-const iterator to the first child. /// \returns A non-const iterator to the first child.

View file

@ -21,6 +21,11 @@ namespace cppast
{ {
std::unique_ptr<T> next_; std::unique_ptr<T> next_;
void do_on_insert(const T& parent) noexcept
{
static_cast<T&>(*this).on_insert(parent);
}
template <typename U> template <typename U>
friend struct intrusive_list_access; friend struct intrusive_list_access;
}; };
@ -42,6 +47,12 @@ namespace cppast
obj.next_ = std::move(node); obj.next_ = std::move(node);
return obj.next_.get(); return obj.next_.get();
} }
template <typename U, typename V>
static void on_insert(U& obj, const V& parent)
{
obj.do_on_insert(parent);
}
}; };
template <typename T> template <typename T>
@ -111,7 +122,8 @@ namespace cppast
intrusive_list() = default; intrusive_list() = default;
//=== modifiers ===// //=== modifiers ===//
void push_back(std::unique_ptr<T> obj) noexcept template <typename U>
void push_back(const U& parent, std::unique_ptr<T> obj) noexcept
{ {
DEBUG_ASSERT(obj != nullptr, detail::assert_handler{}); DEBUG_ASSERT(obj != nullptr, detail::assert_handler{});
@ -125,6 +137,8 @@ namespace cppast
first_ = std::move(obj); first_ = std::move(obj);
last_ = type_safe::opt_ref(first_.get()); last_ = type_safe::opt_ref(first_.get());
} }
intrusive_list_access<T>::on_insert(last_.value(), parent);
} }
//=== accesors ===// //=== accesors ===//