/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set ts=8 sts=2 et sw=2 tw=80: */ // Copyright (c) 2006-2008 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.
// Used so we always remember to close the handle. // The class interface matches that of ScopedStdioHandle in addition to an // IsValid() method since invalid handles on windows can be either NULL or // INVALID_HANDLE_VALUE (-1). // // Example: // ScopedHandle hfile(CreateFile(...)); // if (!hfile.Get()) // ...process error // ReadFile(hfile.Get(), ...); // // To sqirrel the handle away somewhere else: // secret_handle_ = hfile.Take(); // // To explicitly close the handle: // hfile.Close(); class ScopedHandle { public:
ScopedHandle() : handle_(NULL) {}
// Like ScopedHandle, but for HANDLEs returned from FindFile(). class ScopedFindFileHandle { public: explicit ScopedFindFileHandle(HANDLE handle) : handle_(handle) { // Windows is inconsistent about invalid handles, so we always use NULL if (handle_ == INVALID_HANDLE_VALUE) handle_ = NULL;
}
~ScopedFindFileHandle() { if (handle_) FindClose(handle_);
}
// Use this instead of comparing to INVALID_HANDLE_VALUE to pick up our NULL // usage for errors. bool IsValid() const { return handle_ != NULL; }
// Like ScopedHandle but for HDC. Only use this on HDCs returned from // CreateCompatibleDC. For an HDC returned by GetDC, use ReleaseDC instead. class ScopedHDC { public:
ScopedHDC() : hdc_(NULL) {} explicit ScopedHDC(HDC h) : hdc_(h) {}
private: void Close() { if (object_) DeleteObject(object_);
}
T object_;
DISALLOW_COPY_AND_ASSIGN(ScopedGDIObject);
};
// Typedefs for some common use cases. typedef ScopedGDIObject<HBITMAP> ScopedBitmap; typedef ScopedGDIObject<HRGN> ScopedHRGN; typedef ScopedGDIObject<HFONT> ScopedHFONT;
// Like ScopedHandle except for HGLOBAL. template <class T> class ScopedHGlobal { public: explicit ScopedHGlobal(HGLOBAL glob) : glob_(glob) {
data_ = static_cast<T*>(GlobalLock(glob_));
}
~ScopedHGlobal() { GlobalUnlock(glob_); }
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.