raii

Is it possible to write/wrap the exception handling components (try,catch) in different class?

梦想与她 提交于 2019-12-04 06:42:45
This is about wrapping the exception handling logic in some sort of class. While writing c++ code, many time we need to catch many type/variants of exception depending on what client throw. This lead us to write similar types of code(many times) in catch() clause. In below sample example, I have written the function(), which can throw exception in the many possible form. I wanted to know is it possible to write/wrap such logic in the form of class so that end user would have to write similar types of code at once place?. Does it make any sense or it has any meaning? #include<vector> #include

Are there cases where a “finally” construct would be useful in C++?

妖精的绣舞 提交于 2019-12-04 06:12:00
Bjarne Stroustrup writes in his C++ Style and Technique FAQ , emphasis mine: Because C++ supports an alternative that is almost always better : The "resource acquisition is initialization" technique (TC++PL3 section 14.4). The basic idea is to represent a resource by a local object, so that the local object's destructor will release the resource. That way, the programmer cannot forget to release the resource. For example: class File_handle { FILE* p; public: File_handle(const char* n, const char* a) { p = fopen(n,a); if (p==0) throw Open_error(errno); } File_handle(FILE* pp) { p = pp; if (p==0

call function when leaving scope

那年仲夏 提交于 2019-12-04 05:10:57
问题 What is the most elegant solution for calling a function automatically when leaving a scope? My current approach (see below) works but I guess there should be something more general as writing a custom class for this. #include <iostream> #include <functional> using namespace std; class DoInDtor { public: typedef function<void()> F; DoInDtor(F f) : f_(f) {}; ~DoInDtor() { f_(); } private: F f_; }; void foo() { DoInDtor byeSayerCustom([](){ cout << "bye\n"; }); auto cond = true; // could of

Implementing RAII in C#

霸气de小男生 提交于 2019-12-04 03:06:56
问题 I have an InfoPath form which I need to conditionally disable it's OnChange events. Since it's not possible to bind the event handlers after the form has loaded, I'm forced to rely on a global counter which indicates whether an OnChange event should be executed. Inside each OnChange event, I check whether SuppressEventsCount == 0 before performing any actions. To suppress events during the execution of some function or another, I simply set SuppressEventsCount++, and -- again when the

Is there a better deterministic disposal pattern than nested “using”s?

故事扮演 提交于 2019-12-04 00:02:49
In C#, if I want to deterministically clean up non-managed resources, I can use the "using" keyword. But for multiple dependent objects, this ends up nesting further and further: using (FileStream fs = new FileStream("c:\file.txt", FileMode.Open)) { using (BufferedStream bs = new BufferedStream(fs)) { using (StreamReader sr = new StreamReader(bs)) { // use sr, and have everything cleaned up when done. } } } In C++, I'm used to being able to use destructors to do it like this: { FileStream fs("c:\file.txt", FileMode.Open); BufferedStream bs(fs); StreamReader sr(bs); // use sr, and have

Why the Destructor in C++ de-allocated memory in reverse order of how they were initialised?

感情迁移 提交于 2019-12-03 23:43:32
What is the advantage in de-allocating memory in reverse order to variables? Consider this example: Type1 Object1; Type2 Object2(Object1); Suppose that Object2 uses some internal resources of Object1 and is valid as long as Object1 is valid. For example, Object2 s destructor accesses Object1 's internal resource. If it weren't for the guarantee of reverse order of destruction, this would lead to problems. It's not just about deallocating memory, it's about symmetry in a broader sense. Each time you create an object you are creating a new context to work in. You "push" into these contexts as

Is shared ownership of objects a sign of bad design?

前提是你 提交于 2019-12-03 17:09:30
问题 Background : When reading Dr. Stroustrup's papers and FAQs, I notice some strong "opinions" and great advices from legendary CS scientist and programmer. One of them is about shared_ptr in C++0x. He starts explaining about shared_ptr and how it represents shared ownership of the pointed object. At the last line, he says and I quote: . A shared_ptr represents shared ownership but shared ownership isn't my ideal: It is better if an object has a definite owner and a definite, predictable

C++ RAII not working?

江枫思渺然 提交于 2019-12-03 16:29:23
问题 I'm just getting started with RAII in C++ and set up a little test case. Either my code is deeply confused, or RAII is not working! (I guess it is the former). If I run: #include <exception> #include <iostream> class A { public: A(int i) { i_ = i; std::cout << "A " << i_ << " constructed" << std::endl; } ~A() { std::cout << "A " << i_ << " destructed" << std::endl; } private: int i_; }; int main(void) { A a1(1); A a2(2); throw std::exception(); return 0; } with the exception commented out I

SBRM/RAII for std::va_list/va_start()/va_end use

寵の児 提交于 2019-12-03 13:30:50
My code contains snippets like these: std::va_list ap; va_start(ap, msgfmt); snprintf_buf buf; const tchar * msg = buf.print_va_list(msgfmt, ap); va_end(ap); These are short and va_start() and va_end() are close together so they are not much of a problem. Exceptions from calls in between the two could be a problem (or not?). Simple test shows that calling va_start() from a function without ellipsis is not allowed. Is calling va_end() from a different function than va_start() was called from allowed or not? Basically, I am curious if it is possible to use the SBRM/RAII idiom for these calls,

Forcing the order in which struct fields are dropped

我是研究僧i 提交于 2019-12-03 12:02:53
I'm implementing an object that owns several resources created from C libraries through FFI. In order to clean up what's already been done if the constructor panics, I'm wrapping each resource in its own struct and implementing Drop for them. However, when it comes to dropping the object itself, I cannot guarantee that resources will be dropped in a safe order because Rust doesn't define the order that a struct's fields are dropped. Normally, you would solve this by making it so the object doesn't own the resources but rather borrows them (so that the resources may borrow each other). In