good-lambda.cpp
author František Kučera <franta-hg@frantovo.cz>
Wed, 30 Oct 2024 22:08:49 +0100
changeset 2 64777e5b619f
permissions -rw-r--r--
class Finally with lambda
franta-hg@2
     1
/**
franta-hg@2
     2
 * cpp-finally
franta-hg@2
     3
 * Copyright © 2024 František Kučera (Frantovo.cz, GlobalCode.info)
franta-hg@2
     4
 *
franta-hg@2
     5
 * This program is free software: you can redistribute it and/or modify
franta-hg@2
     6
 * it under the terms of the GNU General Public License as published by
franta-hg@2
     7
 * the Free Software Foundation, version 3 of the License.
franta-hg@2
     8
 *
franta-hg@2
     9
 * This program is distributed in the hope that it will be useful,
franta-hg@2
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@2
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@2
    12
 * GNU General Public License for more details.
franta-hg@2
    13
 *
franta-hg@2
    14
 * You should have received a copy of the GNU General Public License
franta-hg@2
    15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
franta-hg@2
    16
 */
franta-hg@2
    17
franta-hg@2
    18
#include <iostream>
franta-hg@2
    19
#include <exception>
franta-hg@2
    20
#include <functional>
franta-hg@2
    21
franta-hg@2
    22
class Finally {
franta-hg@2
    23
private:
franta-hg@2
    24
	Finally(const Finally&) = delete;
franta-hg@2
    25
	Finally& operator=(const Finally&) = delete;
franta-hg@2
    26
public:
franta-hg@2
    27
	std::function<void(void) > fx;
franta-hg@2
    28
franta-hg@2
    29
	Finally(std::function<void(void) > fx) : fx(fx) {
franta-hg@2
    30
	}
franta-hg@2
    31
franta-hg@2
    32
	virtual ~Finally() {
franta-hg@2
    33
		fx();
franta-hg@2
    34
	}
franta-hg@2
    35
};
franta-hg@2
    36
franta-hg@2
    37
void fxThrowing(bool fail, void* data) {
franta-hg@2
    38
	std::cout << "  doing something with data " << data << std::endl;
franta-hg@2
    39
	if (fail) throw std::logic_error("error from fxThrowing()");
franta-hg@2
    40
}
franta-hg@2
    41
franta-hg@2
    42
void fxAllocating(bool fail) {
franta-hg@2
    43
	void* buf = malloc(486);
franta-hg@2
    44
	void* tmp = malloc(123);
franta-hg@2
    45
	std::cout << "  allocated memory at: " << buf << std::endl;
franta-hg@2
    46
	std::cout << "  allocated memory at: " << tmp << std::endl;
franta-hg@2
    47
franta-hg@2
    48
	Finally finally([&]() {
franta-hg@2
    49
		free(buf);
franta-hg@2
    50
		free(tmp);
franta-hg@2
    51
		std::cout << "  freed memory at: " << buf << std::endl;
franta-hg@2
    52
		std::cout << "  freed memory at: " << tmp << std::endl;
franta-hg@2
    53
	});
franta-hg@2
    54
franta-hg@2
    55
	fxThrowing(fail, buf);
franta-hg@2
    56
	fxThrowing(fail, tmp);
franta-hg@2
    57
}
franta-hg@2
    58
franta-hg@2
    59
int main(int argc, char** argv) {
franta-hg@2
    60
	bool fail = argc == 2 && std::string("fail") == argv[1];
franta-hg@2
    61
	const char* name = "good-lambda";
franta-hg@2
    62
	std::cout << name << " (fail=" << fail << ")\n";
franta-hg@2
    63
	try {
franta-hg@2
    64
		fxAllocating(fail);
franta-hg@2
    65
	} catch (const std::exception& e) {
franta-hg@2
    66
		std::cout << "  caught exception: " << e.what() << std::endl;
franta-hg@2
    67
	}
franta-hg@2
    68
}