diff options
Diffstat (limited to 'src/util')
| -rw-r--r-- | src/util/asset_manager.cc | 34 | ||||
| -rw-r--r-- | src/util/check_return.h | 17 |
2 files changed, 25 insertions, 26 deletions
diff --git a/src/util/asset_manager.cc b/src/util/asset_manager.cc index 2a41876..a0e2a97 100644 --- a/src/util/asset_manager.cc +++ b/src/util/asset_manager.cc @@ -86,11 +86,11 @@ const uint8_t* GetAsset(AssetId asset_id, size_t* out_size) { } CHECK_RETURN_BEGIN(proc_gen_func_ptr == nullptr, nullptr) - if (out_size) - *out_size = 0; - ERROR_MSG("Unknown procedural function at runtime: %s", - source_record.proc_func_name_str); - return nullptr; + if (out_size) + *out_size = 0; + ERROR_MSG("Unknown procedural function at runtime: %s", + source_record.proc_func_name_str); + return nullptr; CHECK_RETURN_END // For this demo, assuming procedural textures are RGBA8 256x256 (for @@ -101,10 +101,10 @@ const uint8_t* GetAsset(AssetId asset_id, size_t* out_size) { size_t data_size = header_size + (size_t)width * height * 4; // RGBA8 uint8_t* generated_data = new (std::nothrow) uint8_t[data_size]; CHECK_RETURN_BEGIN(!generated_data, nullptr) - if (out_size) - *out_size = 0; - ERROR_MSG("Failed to allocate memory for procedural asset"); - return nullptr; + if (out_size) + *out_size = 0; + ERROR_MSG("Failed to allocate memory for procedural asset"); + return nullptr; CHECK_RETURN_END // Write header @@ -114,15 +114,15 @@ const uint8_t* GetAsset(AssetId asset_id, size_t* out_size) { // Generate data after header CHECK_RETURN_BEGIN(!proc_gen_func_ptr(generated_data + header_size, width, - height, source_record.proc_params, - source_record.num_proc_params), + height, source_record.proc_params, + source_record.num_proc_params), nullptr) - delete[] generated_data; - if (out_size) - *out_size = 0; - ERROR_MSG("Procedural generation failed for asset: %s", - source_record.proc_func_name_str); - return nullptr; + delete[] generated_data; + if (out_size) + *out_size = 0; + ERROR_MSG("Procedural generation failed for asset: %s", + source_record.proc_func_name_str); + return nullptr; CHECK_RETURN_END cached_record.data = generated_data; diff --git a/src/util/check_return.h b/src/util/check_return.h index cd2c293..89ed4bc 100644 --- a/src/util/check_return.h +++ b/src/util/check_return.h @@ -45,17 +45,14 @@ // CHECK_RETURN_END // // Allows cleanup code before return. -#define CHECK_RETURN_BEGIN(cond, retval) \ - if (cond) { - +#define CHECK_RETURN_BEGIN(cond, retval) if (cond) { #define ERROR_MSG(...) \ do { \ fprintf(stderr, "Error: " __VA_ARGS__); \ fprintf(stderr, " [%s:%d]\n", __FILE__, __LINE__); \ } while (0) -#define CHECK_RETURN_END \ - } +#define CHECK_RETURN_END } // Warning message (non-fatal, execution continues) // Usage: WARN_IF(count == 0, "No items found"); @@ -84,7 +81,6 @@ // Block-based check - cleanup code preserved, messages stripped #define CHECK_RETURN_BEGIN(cond, retval) if (cond) { - #define ERROR_MSG(...) ((void)0) #define CHECK_RETURN_END } @@ -103,7 +99,8 @@ // CHECK_RETURN_IF: // - Simple error checks with no cleanup needed (90% of cases) // - Input validation: CHECK_RETURN_IF(argc < 2, 1, "Too few arguments") -// - Null checks: CHECK_RETURN_IF(ptr == nullptr, nullptr, "Not found: %s", name) +// - Null checks: CHECK_RETURN_IF(ptr == nullptr, nullptr, "Not found: %s", +// name) // - Range checks: CHECK_RETURN_IF(x < 0 || x > max, -1, "Out of range") // // CHECK_RETURN_BEGIN/END: @@ -141,13 +138,15 @@ // - Programming errors (assertion failures, invariant violations) // - Corrupted state that cannot be recovered // - Internal consistency checks -// - Example: FATAL_CHECK(idx >= size, "Index out of bounds: %d >= %d", idx, size) +// - Example: FATAL_CHECK(idx >= size, "Index out of bounds: %d >= %d", idx, +// size) // // Use CHECK_RETURN for: // - Recoverable errors (invalid input, missing files) // - Runtime configuration issues // - API parameter validation -// - Example: CHECK_RETURN_IF(file == nullptr, false, "File not found: %s", path) +// - Example: CHECK_RETURN_IF(file == nullptr, false, "File not found: %s", +// path) // // Key difference: // - FATAL_XXX: abort() - program terminates |
