xref: /aosp_15_r20/external/cronet/base/win/scoped_hglobal.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2010 The Chromium Authors
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_HGLOBAL_H_
6 #define BASE_WIN_SCOPED_HGLOBAL_H_
7 
8 #include <windows.h>
9 
10 #include <stddef.h>
11 
12 #include <utility>
13 
14 namespace base {
15 namespace win {
16 
17 // Like ScopedHandle except for HGLOBAL.
18 template <class Ptr>
19 class ScopedHGlobal {
20  public:
ScopedHGlobal(HGLOBAL glob)21   explicit ScopedHGlobal(HGLOBAL glob)
22       : glob_(glob), data_(static_cast<Ptr>(GlobalLock(glob_))) {}
23 
24   ScopedHGlobal(const ScopedHGlobal&) = delete;
25   ScopedHGlobal& operator=(const ScopedHGlobal&) = delete;
26 
~ScopedHGlobal()27   ~ScopedHGlobal() { GlobalUnlock(glob_); }
28 
data()29   Ptr data() { return data_; }
size()30   size_t size() const { return GlobalSize(glob_); }
31 
32   Ptr operator->() const {
33     assert(data_ != 0);
34     return data_;
35   }
36 
release()37   Ptr release() { return std::exchange(data_, nullptr); }
38 
begin()39   Ptr begin() { return data(); }
end()40   Ptr end() { return data() + size(); }
41 
42  private:
43   HGLOBAL glob_;
44 
45   Ptr data_;
46 };
47 
48 }  // namespace win
49 }  // namespace base
50 
51 #endif  // BASE_WIN_SCOPED_HGLOBAL_H_
52