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