Last active
March 3, 2020 00:49
-
-
Save Loki-Astari/74931d709fa043f41ce4c82a49aedfd3 to your computer and use it in GitHub Desktop.
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
#include <FileThatDoesNotWriteOnException> | |
int main() | |
{ | |
FileThatDoesNotWriteOnException myFile; | |
// DO STUFF | |
myFile.setOK(): | |
} | |
// Automatically writes on completion because the destructor of the file | |
// checks to make sure it completed without an exception being thrown. | |
// If an exception was thrown in "DO STUFF" then `setOK()` would never | |
// have been called. | |
// | |
// Here we encourage the author of the class "FileThatDoesNotWriteOnException" | |
// to create the class appropriately once. That way the user of the class never | |
// ever has to do it. Thus reducing the chances of error becuase of the user having | |
// to use boilerplate to do the validation manually. |
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
// So you just want to use standard functions. | |
// You don't want to write a `FileThatDoesNotWriteOnException` and nobody else has. | |
// | |
// Even though the whole point of the article is about moving responcability of this | |
// onto the author so that users don't need to do this in the first place. Let us | |
// jsut examine how it can be done using standard libraries. | |
// As a note: The GSL is a set of basically standard libraries. | |
// Though not standard yet they are part of big C++ initiative being organized | |
// by the C++ standards comitee which will eventually make it into the standard. | |
// These libraries should be known to all serious C++ developers. | |
#include <gsl/gsl_util> | |
int main() | |
{ | |
bool ok = false; | |
gsl::final_action finalAction([&ok](){if (ok){write_myFile();}}); | |
// DO STUFF | |
ok = true; | |
} | |
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
int main() | |
{ | |
bool ok = false | |
try | |
{ | |
// Do Stuff | |
ok = true; | |
} | |
finally | |
{ | |
// Now we can try and write the file | |
try | |
{ | |
if (ok) { | |
write_myFile(); | |
} | |
} | |
catch(Throwable e) | |
{ | |
/* Ignore */ | |
// Make sure not to throw exception if one is already propagating. | |
} | |
// If you allow this exception to propogate the user is debugging the wrong problem. | |
// You should solve the initial problem first. If this problem still exisits then | |
// debug it and solve it in the correct context. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment