class Finally with lambda default tip
authorFrantišek Kučera <franta-hg@frantovo.cz>
Wed, 30 Oct 2024 22:08:49 +0100
changeset 264777e5b619f
parent 1 4502b1c7346d
class Finally with lambda
Makefile
good-lambda.cpp
     1.1 --- a/Makefile	Wed Oct 30 02:51:12 2024 +0100
     1.2 +++ b/Makefile	Wed Oct 30 22:08:49 2024 +0100
     1.3 @@ -31,10 +31,10 @@
     1.4  
     1.5  run: $(BIN)
     1.6  	@echo "\e[1;32mHappy path without exceptions:\e[0m"
     1.7 -	@for bin in $(BIN); do ./$$bin     ; done
     1.8 +	@for bin in $(BIN); do ./$$bin     ; true; done
     1.9  	@echo; echo
    1.10  	@echo "\e[1;32mInterrupted by exceptions:\e[0m"
    1.11 -	@for bin in $(BIN); do ./$$bin fail; done
    1.12 +	@for bin in $(BIN); do ./$$bin fail; true; done
    1.13  
    1.14  $(BIN): $(SRC)
    1.15  	$(CXX) $(CXXFLAGS) -o $(@) $(@).cpp $(LDFLAGS)
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/good-lambda.cpp	Wed Oct 30 22:08:49 2024 +0100
     2.3 @@ -0,0 +1,68 @@
     2.4 +/**
     2.5 + * cpp-finally
     2.6 + * Copyright © 2024 František Kučera (Frantovo.cz, GlobalCode.info)
     2.7 + *
     2.8 + * This program is free software: you can redistribute it and/or modify
     2.9 + * it under the terms of the GNU General Public License as published by
    2.10 + * the Free Software Foundation, version 3 of the License.
    2.11 + *
    2.12 + * This program is distributed in the hope that it will be useful,
    2.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    2.15 + * GNU General Public License for more details.
    2.16 + *
    2.17 + * You should have received a copy of the GNU General Public License
    2.18 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
    2.19 + */
    2.20 +
    2.21 +#include <iostream>
    2.22 +#include <exception>
    2.23 +#include <functional>
    2.24 +
    2.25 +class Finally {
    2.26 +private:
    2.27 +	Finally(const Finally&) = delete;
    2.28 +	Finally& operator=(const Finally&) = delete;
    2.29 +public:
    2.30 +	std::function<void(void) > fx;
    2.31 +
    2.32 +	Finally(std::function<void(void) > fx) : fx(fx) {
    2.33 +	}
    2.34 +
    2.35 +	virtual ~Finally() {
    2.36 +		fx();
    2.37 +	}
    2.38 +};
    2.39 +
    2.40 +void fxThrowing(bool fail, void* data) {
    2.41 +	std::cout << "  doing something with data " << data << std::endl;
    2.42 +	if (fail) throw std::logic_error("error from fxThrowing()");
    2.43 +}
    2.44 +
    2.45 +void fxAllocating(bool fail) {
    2.46 +	void* buf = malloc(486);
    2.47 +	void* tmp = malloc(123);
    2.48 +	std::cout << "  allocated memory at: " << buf << std::endl;
    2.49 +	std::cout << "  allocated memory at: " << tmp << std::endl;
    2.50 +
    2.51 +	Finally finally([&]() {
    2.52 +		free(buf);
    2.53 +		free(tmp);
    2.54 +		std::cout << "  freed memory at: " << buf << std::endl;
    2.55 +		std::cout << "  freed memory at: " << tmp << std::endl;
    2.56 +	});
    2.57 +
    2.58 +	fxThrowing(fail, buf);
    2.59 +	fxThrowing(fail, tmp);
    2.60 +}
    2.61 +
    2.62 +int main(int argc, char** argv) {
    2.63 +	bool fail = argc == 2 && std::string("fail") == argv[1];
    2.64 +	const char* name = "good-lambda";
    2.65 +	std::cout << name << " (fail=" << fail << ")\n";
    2.66 +	try {
    2.67 +		fxAllocating(fail);
    2.68 +	} catch (const std::exception& e) {
    2.69 +		std::cout << "  caught exception: " << e.what() << std::endl;
    2.70 +	}
    2.71 +}