1 // Copyright 2020 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef UI_VIEWS_WIDGET_UNIQUE_WIDGET_PTR_H_
6 #define UI_VIEWS_WIDGET_UNIQUE_WIDGET_PTR_H_
7 
8 #include <memory>
9 
10 #include "ui/views/views_export.h"
11 
12 namespace views {
13 
14 class Widget;
15 
16 // Ensures the Widget is properly closed when this special
17 // auto pointer goes out of scope.
18 
19 class VIEWS_EXPORT UniqueWidgetPtr {
20  public:
21   UniqueWidgetPtr();
22   // This class acts like a std::unique_ptr<Widget>, so this constructor is
23   // deliberately implicit.
24   UniqueWidgetPtr(std::unique_ptr<Widget> widget);  // NOLINT
25   UniqueWidgetPtr(UniqueWidgetPtr&&);
26   UniqueWidgetPtr& operator=(UniqueWidgetPtr&&);
27   ~UniqueWidgetPtr();
28 
29   explicit operator bool() const;
30   Widget& operator*() const;
31   Widget* operator->() const;
32   void reset();
33   Widget* get() const;
34 
35  private:
36   class UniqueWidgetPtrImpl;
37 
38   std::unique_ptr<UniqueWidgetPtrImpl> unique_widget_ptr_impl_;
39 };
40 
41 }  // namespace views
42 
43 #endif  // UI_VIEWS_WIDGET_UNIQUE_WIDGET_PTR_H_
44