Last active
May 28, 2024 12:19
-
-
Save marsgpl/b91d96f6090854173e59ba08ce7c6061 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 <stdio.h> | |
| typedef int (*fn)(void); | |
| void custom_transaction(fn *dos, fn *undos, size_t n) { | |
| for (int i = 0; i < n; ++i) { | |
| if (!(dos[i])()) { | |
| while (--i >= 0) { undos[i](); } | |
| return; | |
| } | |
| } | |
| } | |
| int do1() { printf("do 1\n"); return 1; } | |
| int do2() { printf("do 2\n"); return 1; } | |
| int do3() { printf("do 3\n"); return 0; } | |
| int undo1() { printf("undo 1\n"); return 1; } | |
| int undo2() { printf("undo 2\n"); return 1; } | |
| int undo3() { printf("undo 3\n"); return 1; } | |
| int main() { | |
| fn dos[] = {do1, do2, do3}; | |
| fn undos[] = {undo1, undo2, undo3}; | |
| custom_transaction(dos, undos, 3); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment