Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / base / win / scoped_gdi_object.h
blob57b013e2fbaae8cf80fc7f32a452b204fc4b923c
1 // Copyright (c) 2010 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.
5 #ifndef BASE_WIN_SCOPED_GDI_OBJECT_H_
6 #define BASE_WIN_SCOPED_GDI_OBJECT_H_
8 #include <windows.h>
10 #include "base/basictypes.h"
11 #include "base/logging.h"
13 namespace base {
14 namespace win {
16 // Like ScopedHandle but for GDI objects.
17 template<class T>
18 class ScopedGDIObject {
19 public:
20 ScopedGDIObject() : object_(NULL) {}
21 explicit ScopedGDIObject(T object) : object_(object) {}
23 ~ScopedGDIObject() {
24 Close();
27 T Get() {
28 return object_;
31 void Set(T object) {
32 if (object_ && object != object_)
33 Close();
34 object_ = object;
37 ScopedGDIObject& operator=(T object) {
38 Set(object);
39 return *this;
42 T release() {
43 T object = object_;
44 object_ = NULL;
45 return object;
48 operator T() { return object_; }
50 private:
51 void Close() {
52 if (object_)
53 DeleteObject(object_);
56 T object_;
57 DISALLOW_COPY_AND_ASSIGN(ScopedGDIObject);
60 // An explicit specialization for HICON because we have to call DestroyIcon()
61 // instead of DeleteObject() for HICON.
62 template<>
63 void inline ScopedGDIObject<HICON>::Close() {
64 if (object_)
65 DestroyIcon(object_);
68 // Typedefs for some common use cases.
69 typedef ScopedGDIObject<HBITMAP> ScopedBitmap;
70 typedef ScopedGDIObject<HRGN> ScopedRegion;
71 typedef ScopedGDIObject<HFONT> ScopedHFONT;
72 typedef ScopedGDIObject<HICON> ScopedHICON;
74 } // namespace win
75 } // namespace base
77 #endif // BASE_WIN_SCOPED_GDI_OBJECT_H_