Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ******************* Вариант 1 ********************
- // Не удалось загрузить текстуру - да и хрен с ним. Не мои проблемы. Я устал, я ухожу.
- #define LOG_ERROR(msg) do { fprintf(stderr, msg); std::terminate(); } while (false)
- class Texture {
- public:
- Texture(const std::string_view path) {
- uint8_t *pixels = somelib_load_image(path);
- if (!pixels) {
- LOG_ERROR("failed to load image " + path);
- }
- }
- };
- // ******************* Вариант 2 ********************
- // Не удалось загрузить текстуру - кину исключение, которое можно обработать
- class FileNotFound : std::exception {
- public:
- FileNotFound(const std::string_view msg) {
- fprintf(stderr, msg);
- }
- };
- class Texture {
- public:
- Texture(const std::string_view path) {
- uint8_t *pixels = somelib_load_image(path);
- if (!pixels) {
- throw FileNotFound("failed to load image " + path);
- }
- }
- };
- try {
- auto texture = Texture("nonexistent.png");
- }
- catch (const FileNotFound &e) {
- ...
- }
- // ******************* Вариант 3 ********************
- // Не удалось загрузить текстуру - верну ошибку, с которой можно что-то сделать уровнем выше
- class Texture {
- public:
- static std::expected<Texture, std::string> createFromFile(const std::string_view path) {
- uint8_t *pixels = somelib_load_image(path);
- if (!pixels) {
- return std::make_unexpected("failed to load image " + path);
- }
- return Texture(pixels);
- }
- private:
- Texture(uint8_t *pixels);
- };
- auto texture = Texture::createFromFile("nonexistent.png");
- if (texture) {
- ...
- }
- else {
- ...
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement