//
// The LLVM Compiler Infrastructure
//
-// This file was developed by Jeff Cohen and is distributed under the
-// University of Illinois Open Source License. See LICENSE.TXT for details.
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
#include <cassert>
#include <string>
-inline bool GetError(const std::string &Prefix, std::string *Dest) {
- if (Dest == 0) return true;
+inline bool MakeErrMsg(std::string* ErrMsg, const std::string& prefix) {
+ if (!ErrMsg)
+ return true;
char *buffer = NULL;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
NULL, GetLastError(), 0, (LPSTR)&buffer, 1, NULL);
- *Dest = Prefix + buffer;
+ *ErrMsg = prefix + buffer;
LocalFree(buffer);
return true;
}
-inline void ThrowError(const std::string& msg) {
- char *buffer = NULL;
- FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
- NULL, GetLastError(), 0, (LPSTR)&buffer, 1, NULL);
- std::string s(msg);
- s += buffer;
- LocalFree(buffer);
- throw s;
-}
+class AutoHandle {
+ HANDLE handle;
-inline void ThrowErrno(const std::string& prefix) {
- ThrowError(prefix + ": " + strerror(errno));
-}
+public:
+ AutoHandle(HANDLE h) : handle(h) {}
+
+ ~AutoHandle() {
+ if (handle)
+ CloseHandle(handle);
+ }
+
+ operator HANDLE() {
+ return handle;
+ }
+
+ AutoHandle &operator=(HANDLE h) {
+ handle = h;
+ return *this;
+ }
+};