mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 13:32:45 +00:00 
			
		
		
		
	AK: Improve smart pointer ergonomics a bit.
This commit is contained in:
		
							parent
							
								
									d5dec1922b
								
							
						
					
					
						commit
						3f6408919f
					
				
					 17 changed files with 49 additions and 31 deletions
				
			
		|  | @ -66,9 +66,6 @@ public: | |||
| 
 | ||||
|     bool operator!() const { return !m_ptr; } | ||||
| 
 | ||||
|     typedef T* OwnPtr::*UnspecifiedBoolType; | ||||
|     operator UnspecifiedBoolType() const { return m_ptr ? &OwnPtr::m_ptr : nullptr; } | ||||
| 
 | ||||
|     T* leak_ptr() | ||||
|     { | ||||
|         T* leakedPtr = m_ptr; | ||||
|  | @ -85,6 +82,9 @@ public: | |||
|     T& operator*() { return *m_ptr; } | ||||
|     const T& operator*() const { return *m_ptr; } | ||||
| 
 | ||||
|     operator const T*() const { return m_ptr; } | ||||
|     operator T*() { return m_ptr; } | ||||
| 
 | ||||
|     operator bool() { return !!m_ptr; } | ||||
| 
 | ||||
| private: | ||||
|  |  | |||
|  | @ -53,6 +53,14 @@ public: | |||
|         return *this; | ||||
|     } | ||||
| 
 | ||||
|     template<typename U> | ||||
|     RetainPtr& operator=(Retained<U>&& other) | ||||
|     { | ||||
|         release_if_not_null(m_ptr); | ||||
|         m_ptr = &other.leak_ref(); | ||||
|         return *this; | ||||
|     } | ||||
| 
 | ||||
|     RetainPtr& operator=(T* ptr) | ||||
|     { | ||||
|         if (m_ptr != ptr) | ||||
|  | @ -90,9 +98,6 @@ public: | |||
| 
 | ||||
|     bool operator!() const { return !m_ptr; } | ||||
| 
 | ||||
|     typedef T* RetainPtr::*UnspecifiedBoolType; | ||||
|     operator UnspecifiedBoolType() const { return m_ptr ? &RetainPtr::m_ptr : nullptr; } | ||||
| 
 | ||||
|     T* leak_ref() | ||||
|     { | ||||
|         T* leakedPtr = m_ptr; | ||||
|  | @ -109,8 +114,20 @@ public: | |||
|     T& operator*() { return *m_ptr; } | ||||
|     const T& operator*() const { return *m_ptr; } | ||||
| 
 | ||||
|     operator const T*() const { return m_ptr; } | ||||
|     operator T*() { return m_ptr; } | ||||
| 
 | ||||
|     operator bool() { return !!m_ptr; } | ||||
| 
 | ||||
|     bool operator==(std::nullptr_t) const { return !m_ptr; } | ||||
|     bool operator!=(std::nullptr_t) const { return m_ptr; } | ||||
| 
 | ||||
|     bool operator==(const RetainPtr& other) const { return m_ptr == other.m_ptr; } | ||||
|     bool operator!=(const RetainPtr& other) const { return m_ptr != other.m_ptr; } | ||||
| 
 | ||||
|     bool operator==(const T* other) const { return m_ptr == other; } | ||||
|     bool operator!=(const T* other) const { return m_ptr != other; } | ||||
| 
 | ||||
|     bool is_null() const { return !m_ptr; } | ||||
| 
 | ||||
| private: | ||||
|  |  | |||
|  | @ -107,6 +107,9 @@ public: | |||
|     CALLABLE_WHEN(unconsumed) T& operator*() { ASSERT(m_ptr); return *m_ptr; } | ||||
|     CALLABLE_WHEN(unconsumed) const T& operator*() const { ASSERT(m_ptr); return *m_ptr; } | ||||
| 
 | ||||
|     CALLABLE_WHEN(unconsumed) operator T*() { ASSERT(m_ptr); return m_ptr; } | ||||
|     CALLABLE_WHEN(unconsumed) operator const T*() const { ASSERT(m_ptr); return m_ptr; } | ||||
| 
 | ||||
| private: | ||||
|     Retained() { } | ||||
| 
 | ||||
|  |  | |||
|  | @ -81,7 +81,7 @@ void VBForm::second_paint_event(GPaintEvent& event) | |||
| 
 | ||||
| bool VBForm::is_selected(const VBWidget& widget) const | ||||
| { | ||||
|     return &widget == m_selected_widget.ptr(); | ||||
|     return &widget == m_selected_widget; | ||||
| } | ||||
| 
 | ||||
| VBWidget* VBForm::widget_at(const Point& position) | ||||
|  |  | |||
|  | @ -336,7 +336,7 @@ KResult VFS::rename(const String& old_path, const String& new_path, Inode& base) | |||
|     if (!new_inode_or_error.is_error()) { | ||||
|         auto new_inode = new_inode_or_error.value(); | ||||
|         // FIXME: Is this really correct? Check what other systems do.
 | ||||
|         if (new_inode.ptr() == old_inode.ptr()) | ||||
|         if (new_inode == old_inode) | ||||
|             return KSuccess; | ||||
|         if (new_inode->is_directory() && !old_inode->is_directory()) | ||||
|             return KResult(-EISDIR); | ||||
|  |  | |||
|  | @ -121,7 +121,7 @@ bool Process::deallocate_region(Region& region) | |||
| { | ||||
|     InterruptDisabler disabler; | ||||
|     for (int i = 0; i < m_regions.size(); ++i) { | ||||
|         if (m_regions[i].ptr() == ®ion) { | ||||
|         if (m_regions[i] == ®ion) { | ||||
|             MM.unmap_region(region); | ||||
|             m_regions.remove(i); | ||||
|             return true; | ||||
|  |  | |||
|  | @ -142,7 +142,7 @@ auto MemoryManager::ensure_pte(PageDirectory& page_directory, LinearAddress ladd | |||
|         dbgprintf("MM: PDE %u not present (requested for L%x), allocating\n", page_directory_index, laddr.get()); | ||||
| #endif | ||||
|         if (page_directory_index == 0) { | ||||
|             ASSERT(&page_directory == m_kernel_page_directory.ptr()); | ||||
|             ASSERT(&page_directory == m_kernel_page_directory); | ||||
|             pde.set_page_table_base((dword)m_page_table_zero); | ||||
|             pde.set_user_allowed(false); | ||||
|             pde.set_present(true); | ||||
|  | @ -153,7 +153,7 @@ auto MemoryManager::ensure_pte(PageDirectory& page_directory, LinearAddress ladd | |||
| #ifdef MM_DEBUG | ||||
|             dbgprintf("MM: PD K%x (%s) at P%x allocated page table #%u (for L%x) at P%x\n", | ||||
|                 &page_directory, | ||||
|                 &page_directory == m_kernel_page_directory.ptr() ? "Kernel" : "User", | ||||
|                 &page_directory == m_kernel_page_directory ? "Kernel" : "User", | ||||
|                 page_directory.cr3(), | ||||
|                 page_directory_index, | ||||
|                 laddr.get(), | ||||
|  |  | |||
|  | @ -68,7 +68,7 @@ public: | |||
| 
 | ||||
|     void set_page_directory(PageDirectory& page_directory) | ||||
|     { | ||||
|         ASSERT(!m_page_directory || m_page_directory.ptr() == &page_directory); | ||||
|         ASSERT(!m_page_directory || m_page_directory == &page_directory); | ||||
|         m_page_directory = page_directory; | ||||
|     } | ||||
| 
 | ||||
|  |  | |||
|  | @ -15,7 +15,7 @@ GAbstractView::~GAbstractView() | |||
| 
 | ||||
| void GAbstractView::set_model(RetainPtr<GModel>&& model) | ||||
| { | ||||
|     if (model.ptr() == m_model.ptr()) | ||||
|     if (model == m_model) | ||||
|         return; | ||||
|     if (m_model) | ||||
|         m_model->unregister_view(Badge<GAbstractView>(), *this); | ||||
|  |  | |||
|  | @ -137,7 +137,7 @@ void GButton::set_action(GAction& action) | |||
| 
 | ||||
| void GButton::set_icon(RetainPtr<GraphicsBitmap>&& icon) | ||||
| { | ||||
|     if (m_icon.ptr() == icon.ptr()) | ||||
|     if (m_icon == icon) | ||||
|         return; | ||||
|     m_icon = move(icon); | ||||
|     update(); | ||||
|  |  | |||
|  | @ -11,14 +11,14 @@ GLayout::~GLayout() | |||
| 
 | ||||
| void GLayout::notify_adopted(Badge<GWidget>, GWidget& widget) | ||||
| { | ||||
|     if (m_owner.ptr() == &widget) | ||||
|     if (m_owner == &widget) | ||||
|         return; | ||||
|     m_owner = widget.make_weak_ptr(); | ||||
| } | ||||
| 
 | ||||
| void GLayout::notify_disowned(Badge<GWidget>, GWidget& widget) | ||||
| { | ||||
|     ASSERT(m_owner.ptr() == &widget); | ||||
|     ASSERT(m_owner == &widget); | ||||
|     m_owner.clear(); | ||||
| } | ||||
| 
 | ||||
|  | @ -43,7 +43,7 @@ void GLayout::add_widget(GWidget& widget) | |||
| void GLayout::remove_widget(GWidget& widget) | ||||
| { | ||||
|     m_entries.remove_first_matching([&] (auto& entry) { | ||||
|         return entry.widget.ptr() == &widget; | ||||
|         return entry.widget == &widget; | ||||
|     }); | ||||
|     if (m_owner) | ||||
|         m_owner->notify_layout_changed(Badge<GLayout>()); | ||||
|  |  | |||
|  | @ -120,8 +120,6 @@ void GWidget::handle_paint_event(GPaintEvent& event) | |||
| 
 | ||||
| void GWidget::set_layout(OwnPtr<GLayout>&& layout) | ||||
| { | ||||
|     if (m_layout.ptr() == layout.ptr()) | ||||
|         return; | ||||
|     if (m_layout) | ||||
|         m_layout->notify_disowned(Badge<GWidget>(), *this); | ||||
|     m_layout = move(layout); | ||||
|  |  | |||
|  | @ -346,14 +346,14 @@ void GWindow::set_focused_widget(GWidget* widget) | |||
| 
 | ||||
| void GWindow::set_global_cursor_tracking_widget(GWidget* widget) | ||||
| { | ||||
|     if (widget == m_global_cursor_tracking_widget.ptr()) | ||||
|     if (widget == m_global_cursor_tracking_widget) | ||||
|         return; | ||||
|     m_global_cursor_tracking_widget = widget ? widget->make_weak_ptr() : nullptr; | ||||
| } | ||||
| 
 | ||||
| void GWindow::set_automatic_cursor_tracking_widget(GWidget* widget) | ||||
| { | ||||
|     if (widget == m_automatic_cursor_tracking_widget.ptr()) | ||||
|     if (widget == m_automatic_cursor_tracking_widget) | ||||
|         return; | ||||
|     m_automatic_cursor_tracking_widget = widget ? widget->make_weak_ptr() : nullptr; | ||||
| } | ||||
|  | @ -385,7 +385,7 @@ void GWindow::set_opacity(float opacity) | |||
| 
 | ||||
| void GWindow::set_hovered_widget(GWidget* widget) | ||||
| { | ||||
|     if (widget == m_hovered_widget.ptr()) | ||||
|     if (widget == m_hovered_widget) | ||||
|         return; | ||||
| 
 | ||||
|     if (m_hovered_widget) | ||||
|  |  | |||
|  | @ -92,7 +92,7 @@ void WSMenu::draw() | |||
|     for (auto& item : m_items) { | ||||
|         if (item->type() == WSMenuItem::Text) { | ||||
|             Color text_color = Color::Black; | ||||
|             if (item.ptr() == m_hovered_item) { | ||||
|             if (item == m_hovered_item) { | ||||
|                 painter.fill_rect(item->rect(), WSWindowManager::the().menu_selection_color()); | ||||
|                 text_color = Color::White; | ||||
|             } | ||||
|  |  | |||
|  | @ -146,17 +146,17 @@ void WSWindowFrame::paint(Painter& painter) | |||
| 
 | ||||
|     auto& wm = WSWindowManager::the(); | ||||
| 
 | ||||
|     if (&window == wm.m_highlight_window.ptr()) { | ||||
|     if (&window == wm.m_highlight_window) { | ||||
|         border_color = wm.m_highlight_window_border_color; | ||||
|         border_color2 = wm.m_highlight_window_border_color2; | ||||
|         title_color = wm.m_highlight_window_title_color; | ||||
|         middle_border_color = Color::White; | ||||
|     } else if (&window == wm.m_drag_window.ptr()) { | ||||
|     } else if (&window == wm.m_drag_window) { | ||||
|         border_color = wm.m_dragging_window_border_color; | ||||
|         border_color2 = wm.m_dragging_window_border_color2; | ||||
|         title_color = wm.m_dragging_window_title_color; | ||||
|         middle_border_color = Color::from_rgb(0xf9b36a); | ||||
|     } else if (&window == wm.m_active_window.ptr()) { | ||||
|     } else if (&window == wm.m_active_window) { | ||||
|         border_color = wm.m_active_window_border_color; | ||||
|         border_color2 = wm.m_active_window_border_color2; | ||||
|         title_color = wm.m_active_window_title_color; | ||||
|  |  | |||
|  | @ -1010,7 +1010,7 @@ void WSWindowManager::on_message(const WSMessage& message) | |||
| 
 | ||||
| void WSWindowManager::set_highlight_window(WSWindow* window) | ||||
| { | ||||
|     if (window == m_highlight_window.ptr()) | ||||
|     if (window == m_highlight_window) | ||||
|         return; | ||||
|     if (auto* previous_highlight_window = m_highlight_window.ptr()) | ||||
|         invalidate(*previous_highlight_window); | ||||
|  | @ -1029,7 +1029,7 @@ void WSWindowManager::set_active_window(WSWindow* window) | |||
|         return; | ||||
|     } | ||||
| 
 | ||||
|     if (window == m_active_window.ptr()) | ||||
|     if (window == m_active_window) | ||||
|         return; | ||||
| 
 | ||||
|     auto* previously_active_window = m_active_window.ptr(); | ||||
|  | @ -1053,7 +1053,7 @@ void WSWindowManager::set_active_window(WSWindow* window) | |||
| 
 | ||||
| void WSWindowManager::set_hovered_window(WSWindow* window) | ||||
| { | ||||
|     if (m_hovered_window.ptr() == window) | ||||
|     if (m_hovered_window == window) | ||||
|         return; | ||||
| 
 | ||||
|     if (m_hovered_window) | ||||
|  |  | |||
|  | @ -234,7 +234,7 @@ IterationDecision WSWindowManager::for_each_visible_window_of_type_from_back_to_ | |||
|             continue; | ||||
|         if (window->type() != type) | ||||
|             continue; | ||||
|         if (m_highlight_window.ptr() == window) { | ||||
|         if (m_highlight_window == window) { | ||||
|             do_highlight_window_at_end = true; | ||||
|             continue; | ||||
|         } | ||||
|  | @ -277,7 +277,7 @@ IterationDecision WSWindowManager::for_each_visible_window_of_type_from_front_to | |||
|             continue; | ||||
|         if (window->type() != type) | ||||
|             continue; | ||||
|         if (window == m_highlight_window.ptr()) | ||||
|         if (window == m_highlight_window) | ||||
|             continue; | ||||
|         if (callback(*window) == IterationDecision::Abort) | ||||
|             return IterationDecision::Abort; | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Andreas Kling
						Andreas Kling