raii

Any RAII template in boost or C++0x

雨燕双飞 提交于 2019-11-30 16:41:11
问题 Is there any template available in boost for RAII . There are classes like scoped_ptr , shared_ptr which basically work on pointer. Can those classes be used for any other resources other than pointers. Is there any template which works with a general resources. Take for example some resource which is acquired in the beginning of a scope and has to be somehow released at the end of scope. Both acquire and release take some steps. We could write a template which takes two(or maybe one object)

How can I create a smart pointer that locks and unlocks a mutex?

大憨熊 提交于 2019-11-30 14:48:55
问题 I have a threaded class from which I would like to occasionally acquire a pointer an instance variable. I would like this access to be guarded by a mutex so that the thread is blocked from accessing this resource until the client is finished with its pointer. My initial approach to this is to return a pair of objects: one a pointer to the resource and one a shared_ptr to a lock object on the mutex. This shared_ptr holds the only reference to the lock object so the mutex should be unlocked

Move constructor and const member variables

大城市里の小女人 提交于 2019-11-30 14:31:03
问题 I like the idea of const member variables especially when I wrap C functions into classes. The constructor takes a resource handle (e.g. a file descriptor) that stays valid during the whole object life time and the destructor finally closes it. (That is the idea behind RAII, right?) But with the C++0x move constructor i run into a problem. Since the destructor is also called on the "unloaded" object i need to prevent the cleanup of the resource handle. Since the member variable is const i

Making a HANDLE RAII-compliant using shared_ptr with a custom deleter

偶尔善良 提交于 2019-11-30 13:00:41
I've recently posted a general question about RAII at SO . However, I still have some implementation issues with my HANDLE example. A HANDLE is typedeffed to void * in windows.h . Therefore, the correct shared_ptr definition needs to be std::tr1::shared_ptr<void> myHandle (INVALID_HANDLE_VALUE, CloseHandle); Example 1 CreateToolhelp32Snapshot : returns HANDLE and works. const std::tr1::shared_ptr<void> h (CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL), CloseHandle); As I use void in the definition (what is the correct way?) problems go on, when I try to call some more winapi commands with

Can you use a shared_ptr for RAII of C-style arrays?

拜拜、爱过 提交于 2019-11-30 11:22:43
问题 I'm working on a section of code that has many possible failure points which cause it to exit the function early. The libraries I'm interacting with require that C-style arrays be passed to the functions. So, instead of calling delete on the arrays at every exit point, I'm doing this: void SomeFunction(int arrayLength) { shared_ptr<char> raiiArray(new char[arrayLength]); pArray = raiiArray.get(); if(SomeFunctionThatRequiresCArray(pArray) == FAILED) { return; } //etc. } I wanted to use unique

How does RAII work when a constructor throws an exception?

戏子无情 提交于 2019-11-30 11:06:55
I am learning about the RAII idiom in C++, and how to use smart pointers. In my reading, I have come across two things that, to me, seem to contradict each other. Quoted from http://www.hackcraft.net/raii/ : ...if a member object with RAII semantics has been created and an exception happens before the constructor has completed then its destructor will be called as part of the stack unwinding. Hence an object which controls multiple resources can guarnatee their cleanup even if it isn’t fully constructed by using member RAII objects. But quoted from http://www.parashift.com/c++-faq-lite

Move constructor and const member variables

泄露秘密 提交于 2019-11-30 10:55:14
I like the idea of const member variables especially when I wrap C functions into classes. The constructor takes a resource handle (e.g. a file descriptor) that stays valid during the whole object life time and the destructor finally closes it. (That is the idea behind RAII, right?) But with the C++0x move constructor i run into a problem. Since the destructor is also called on the "unloaded" object i need to prevent the cleanup of the resource handle. Since the member variable is const i have no way to assign the value -1 or INVALID_HANDLE (or equivalent values) to indicate to the destructor

What is standard defer/finalizer implementation in C++?

偶尔善良 提交于 2019-11-30 07:25:28
General idea of Golang-style defer is explained here and here . I wonder, does STL (of C++11, C++14, ...) or maybe Boost or maybe some other library contain implementation of such a class? So I could just use it without reimplementing it in every new project. Jens There is a proposal for std::unique_resource_t which will enable code like auto file=make_unique_resource(::fopen(filename.c_str(),"w"),&::fclose); for resources, and it defines a general scope_exit , which should be the same as defer : // Always say goodbye before returning, auto goodbye = make_scope_exit([&out]() ->void { out <<

Do programmers of other languages, besides C++, use, know or understand RAII?

六月ゝ 毕业季﹏ 提交于 2019-11-30 01:24:44
I've noticed RAII has been getting lots of attention on Stackoverflow, but in my circles (mostly C++) RAII is so obvious its like asking what's a class or a destructor. So I'm really curious if that's because I'm surrounded daily, by hard-core C++ programmers, and RAII just isn't that well known in general (including C++), or if all this questioning on Stackoverflow is due to the fact that I'm now in contact with programmers that didn't grow up with C++, and in other languages people just don't use/know about RAII? For people who are commenting in this thread about RAII (resource acquisition

Preventing users from creating unnamed instances of a class [duplicate]

£可爱£侵袭症+ 提交于 2019-11-30 00:42:17
问题 This question already has answers here : How to avoid C++ anonymous objects (7 answers) Closed 2 years ago . For many RAII "guard" classes, being instantiated as anonymous variables does not make sense at all: { std::lock_guard<std::mutex>{some_mutex}; // Does not protect the scope! // The unnamed instance is immediately destroyed. } { scope_guard{[]{ cleanup(); }}; // `cleanup()` is executed immediately! // The unnamed instance is immediately destroyed. } From this article: Anonymous