summaryrefslogtreecommitdiffstats
path: root/third_party/cld/bar/common/scopedptr.h
blob: 813661543e989cb48ff45005c532d0c8d4f7401f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef BAR_COMMON_SCOPEDPTR_H_
#define BAR_COMMON_SCOPEDPTR_H_

#include <wininet.h>

//  Boxer for dumb types, allows you to associate cleanup code when the object
//  falls off the stack. Destructor implementation must be provided for each
//  type.
template < class T >
class ScopedObject {
 public:
  explicit ScopedObject(const T& v) : v_(v) { }
  ~ScopedObject();

  operator T() const { return v_; }
  T get() const { return v_; }

 private:
  T v_;

  DISALLOW_COPY_AND_ASSIGN(ScopedObject);
};

// A scoped object for the various HANDLE- and LPVOID-based types.
// destroy() implementation must be provided for each type.
// Added by Breen Hagan of Google.
template < class T, int DIFFERENTIATOR >
class ScopedHandle {
 public:
  explicit ScopedHandle(const T& v) : v_(v) {}
  ~ScopedHandle() {
    destroy();
  }

  operator T() const { return v_; }
  T get() const { return v_; }

  void reset(const T& v) {
    if (v_ != v) {
      destroy();
      v_ = v;
    }
  }

  // Swap two scoped handlers.
  void swap(ScopedHandle& h2) {
    T tmp = v_;
    v_ = h2.v_;
    h2.v_ = tmp;
  }

  T release() {
    T released_value(v_);
    v_ = 0;
    return released_value;
  }

 private:
  void destroy();

  T v_;

  DISALLOW_COPY_AND_ASSIGN(ScopedHandle);
};

// Free functions.
template <class T, int DIFFERENTIATOR>
inline void swap(ScopedHandle<T, DIFFERENTIATOR>& h1,
                 ScopedHandle<T, DIFFERENTIATOR>& h2) {
  h1.swap(h2);
}


// Uses ScopedHandle to automatically call CloseHandle().
typedef ScopedHandle< HANDLE, 1 > SAFE_HANDLE;

template <>
inline void ScopedHandle< HANDLE, 1 >::destroy() {
  if (v_)
    ::CloseHandle(v_);
}

// Uses ScopedHandle to automatically call CryptReleaseContext().
typedef ScopedHandle< HCRYPTPROV, 2 > SAFE_HCRYPTPROV;

template <>
inline void ScopedHandle< HCRYPTPROV, 2 >::destroy() {
  if (v_)
    ::CryptReleaseContext(v_, 0);
}

// Uses ScopedHandle to automatically call CryptDestroyKey().
typedef ScopedHandle< HCRYPTKEY, 3 > SAFE_HCRYPTKEY;

template <>
inline void ScopedHandle< HCRYPTKEY, 3 >::destroy() {
  if (v_)
    ::CryptDestroyKey(v_);
}

// Uses ScopedHandle to automatically call CryptDestroyHash().
typedef ScopedHandle< HCRYPTHASH, 4 > SAFE_HCRYPTHASH;

template <>
inline void ScopedHandle< HCRYPTHASH, 4 >::destroy() {
  if (v_)
    ::CryptDestroyHash(v_);
}

// Uses ScopedHandle to automatically call UnmapViewOfFile().
typedef ScopedHandle< LPVOID, 5 > SAFE_MAPPEDVIEW;

template <>
inline void ScopedHandle< LPVOID, 5 >::destroy() {
  if (v_)
    ::UnmapViewOfFile(v_);
}

//  SAFE_HINTERNET
//    Uses ScopedHandle to automatically call InternetCloseHandle().
typedef ScopedHandle< HINTERNET, 6 > SAFE_HINTERNET;

template <>
inline void ScopedHandle< HINTERNET, 6 >::destroy() {
  if (v_)
    ::InternetCloseHandle(v_);
}

// SAFE_HMODULE
//     Uses ScopedHandle to automatically call ::FreeLibrary().
typedef ScopedHandle< HMODULE, 7 > SAFE_HMODULE;

template <>
inline void ScopedHandle< HMODULE, 7 >::destroy() {
  if (v_)
    ::FreeLibrary(v_);
}

// SAFE_RESOURCE
//     Uses ScopedHandle to automatically call ::FreeResource().
//     The type is HGLOBAL for backward compatibility, see MSDN, LoadResource()
//     function for details.
typedef ScopedHandle< HGLOBAL, 8 > SAFE_RESOURCE;

template <>
inline void ScopedHandle< HGLOBAL, 8 >::destroy() {
  if (v_)
    ::FreeResource(v_);
}


// ScopedIntCounter is a class that will increment given integet on construction
// and decrement it when the class is destructed.
class ScopedIntCounter {
 public:
  ScopedIntCounter(int *counter):
    counter_(counter) {
    (*counter_)++;
  }

  ~ScopedIntCounter() {
    (*counter_)--;
  }

  int count() {
    return *counter_;
  }

 private:
  int* counter_;
};

#endif // BAR_COMMON_SCOPEDPTR_H_