Last active
October 18, 2016 03:48
-
-
Save piaoger/affeac34823dd42f9eb2e666172f3747 to your computer and use it in GitHub Desktop.
raii style resource management
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# raii (c++) | |
before: | |
void UseFile(char const* fn) | |
{ | |
FILE* f = fopen(fn, "r"); // 获取资源 | |
// 使用资源 | |
try { | |
if (!g()) { fclose(f); return; } | |
// ... | |
if (!h()) { fclose(f); return; } | |
// ... | |
} | |
catch (...) { | |
fclose(f); // 释放资源 | |
throw; | |
} | |
fclose(f); // 释放资源 | |
} | |
after: | |
class FileHandle { | |
public: | |
FileHandle(char const* n, char const* a) { p = fopen(n, a); } | |
~FileHandle() { fclose(p); } | |
FILE* handle() const { return p}; | |
private: | |
// 禁止拷贝操作 | |
FileHandle(FileHandle const&); | |
FileHandle& operator= (FileHandle const&); | |
FILE *p; | |
}; | |
void use_file(char const* fn) | |
{ | |
FILE* f = fopen(fn, "r"); // 获取资源 | |
// 使用资源 | |
try { | |
if (!g()) { fclose(f); return; } | |
// ... | |
if (!h()) { fclose(f); return; } | |
// ... | |
} | |
catch (...) { | |
fclose(f); // 释放资源 | |
throw; | |
} | |
fclose(f); // 释放资源 | |
} | |
## Cxx11 | |
std::lock_guard | |
#include <mutex> | |
#include <iostream> | |
#include <string> | |
#include <fstream> | |
#include <stdexcept> | |
void write_to_file (const std::string & message) { | |
// mutex to protect file access (shared across threads) | |
static std::mutex mutex; | |
// lock mutex before accessing file | |
std::lock_guard<std::mutex> lock(mutex); | |
// try to open file | |
std::ofstream file("example.txt"); | |
if (!file.is_open()) | |
throw std::runtime_error("unable to open file"); | |
// write message to file | |
file << message << std::endl; | |
// file will be closed 1st when leaving scope (regardless of exception) | |
// mutex will be unlocked 2nd (from lock destructor) when leaving | |
// scope (regardless of exception) | |
} | |
## android mutex::autolock class | |
https://android.googlesource.com/platform/frameworks/native/+/jb-dev/include/utils/Singleton.h | |
http://blog.csdn.net/zjq2008wd/article/details/17417001 | |
// Manages the mutex automatically. It'll be locked when Autolock is | |
// constructed and released when Autolock goes out of scope. | |
class Autolock { | |
public: | |
inline Autolock(Mutex& mutex) : mLock(mutex) { mLock.lock(); } | |
inline Autolock(Mutex* mutex) : mLock(*mutex) { mLock.lock(); } | |
inline ~Autolock() { mLock.unlock(); } | |
private: | |
Mutex& mLock; | |
}; | |
// AutoLock in Singleton class | |
template <typename TYPE> | |
class ANDROID_API Singleton | |
{ | |
public: | |
static TYPE& getInstance() { | |
Mutex::Autolock _l(sLock); | |
TYPE* instance = sInstance; | |
if (instance == 0) { | |
instance = new TYPE(); | |
sInstance = instance; | |
} | |
return *instance; | |
} | |
static bool hasInstance() { | |
Mutex::Autolock _l(sLock); | |
return sInstance != 0; | |
} | |
protected: | |
~Singleton() { }; | |
Singleton() { }; | |
private: | |
Singleton(const Singleton&); | |
Singleton& operator = (const Singleton&); | |
static Mutex sLock; | |
static TYPE* sInstance; | |
}; | |
## with close in python | |
with open(filepath, 'r') as openfile: | |
content = str(openfile.read()).strip() | |
## defer in go | |
from: go/src/archive/tar/reader_test.go | |
f, err := os.Open("testdata/gnu.tar") | |
if err != nil { | |
t.Fatalf("Unexpected error: %v", err) | |
} | |
defer f.Close() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment