• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef BASE_WIN_SCOPED_GDI_OBJECT_H_
6 #define BASE_WIN_SCOPED_GDI_OBJECT_H_
7 
8 #include <windows.h>
9 
10 #include "base/scoped_generic.h"
11 
12 namespace base {
13 namespace win {
14 
15 namespace internal {
16 
17 template <class T>
18 struct ScopedGDIObjectTraits {
InvalidValueScopedGDIObjectTraits19   static T InvalidValue() { return nullptr; }
FreeScopedGDIObjectTraits20   static void Free(T object) { DeleteObject(object); }
21 };
22 
23 // An explicit specialization for HICON because we have to call DestroyIcon()
24 // instead of DeleteObject() for HICON.
25 template <>
Free(HICON icon)26 void inline ScopedGDIObjectTraits<HICON>::Free(HICON icon) {
27   DestroyIcon(icon);
28 }
29 
30 }  // namespace internal
31 
32 // Like ScopedHandle but for GDI objects.
33 template <class T>
34 using ScopedGDIObject = ScopedGeneric<T, internal::ScopedGDIObjectTraits<T>>;
35 
36 // Typedefs for some common use cases.
37 typedef ScopedGDIObject<HBITMAP> ScopedBitmap;
38 typedef ScopedGDIObject<HRGN> ScopedRegion;
39 typedef ScopedGDIObject<HFONT> ScopedHFONT;
40 typedef ScopedGDIObject<HICON> ScopedHICON;
41 
42 }  // namespace win
43 }  // namespace base
44 
45 #endif  // BASE_WIN_SCOPED_GDI_OBJECT_H_
46