Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(278)

Unified Diff: third_party/cld/bar/common/scopedlibrary.h

Issue 122007: [chromium-reviews] Add Compact Language Detection (CLD) library to Chrome. This works in Windows... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/chrome.gyp ('k') | third_party/cld/bar/common/scopedptr.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: third_party/cld/bar/common/scopedlibrary.h
===================================================================
--- third_party/cld/bar/common/scopedlibrary.h (revision 0)
+++ third_party/cld/bar/common/scopedlibrary.h (revision 0)
@@ -0,0 +1,68 @@
+// Copyright (c) 2006-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_SCOPEDLIBRARY_H_
+#define BAR_COMMON_SCOPEDLIBRARY_H_
+
+
+// A scoped object to safely load and free a DLL referenced by name.
+// Provides an access to a handle to loaded library (HMODULE type).
+//
+// Example:
+// ScopedLibrary library(LIBRARY_NAME);
+// ... = ::GetProcAddress(library.handle(), FUNCTION_NAME);
+class ScopedLibrary {
+ public:
+ // Always creates initialized ScopedLibrary.
+ // [in] file_name - library's file name.
+ explicit ScopedLibrary(const TCHAR *file_name)
+ : library_(::LoadLibrary(file_name)) {}
+ // Unloads owned library, if any.
+ ~ScopedLibrary() {
+ if (library_ != NULL)
+ ::FreeLibrary(library_);
+ }
+ inline HMODULE handle() const { return library_; }
+ // Returns true if library was loaded successfully.
+ bool IsValid() const { return library_ != NULL; }
+
+ private:
+ // Handle to loaded library.
+ const HMODULE library_;
+ DISALLOW_COPY_AND_ASSIGN(ScopedLibrary);
+};
+
+
+// A class representing a pointer to a function retrieved from DLL.
+// FunctionPrototype is a regular C-style pointer-to-function type
+// definition. For example, type of WinAPI IsValidSid function:
+// BOOL (WINAPI*)(PSID)
+//
+// Example:
+// FunctionFromDll<BOOL (WINAPI*)(PSID)> is_valid_sid;
+// ... = is_valid_sid.function()(...);
+template<typename FunctionPrototype>
+class FunctionFromDll {
+ public:
+ FunctionFromDll() : function_(NULL) {}
+ // Binds this object to a function from DLL.
+ // [in] library - handle to a library containing a function.
+ // Must not be NULL.
+ // [in] name - name of the function.
+ void Bind(HMODULE library, const char *name) {
+ function_ =
+ reinterpret_cast<FunctionPrototype>(::GetProcAddress(library, name));
+ }
+ inline FunctionPrototype function() const { return function_; }
+ // Returns true if function was bound successfully.
+ bool IsValid() const { return function_ != NULL; }
+
+ private:
+ // Pointer to the function.
+ FunctionPrototype function_;
+ DISALLOW_COPY_AND_ASSIGN(FunctionFromDll);
+};
+
+
+#endif // BAR_COMMON_SCOPEDLIBRARY_H_
Property changes on: third_party\cld\bar\common\scopedlibrary.h
___________________________________________________________________
Added: svn:eol-style
+ LF
« no previous file with comments | « chrome/chrome.gyp ('k') | third_party/cld/bar/common/scopedptr.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698