I want to know how to use assert() correctly in asset loading.

Asked 1 years ago, Updated 1 years ago, 323 views

Regarding assert(0) inside if(scene==nullptr), is this the correct way to use assert?I think there are three main ways to get errors: printf(), assert and text file log. If there is no data, what is the correct way to get the error?

In this case, I decided to use assert() because I want it to stop working when an error occurs.

I want to know how to use assert().

1, https://c.keicode.com/lib/assert-when-you-should-use.php
2, https://cpprefjp.github.io/reference/cassert/assert.html
3, http://marupeke296.com/DBG_No2_Assert.html

/*############################################################################################
#   moderu   a load
############################################################################################*/
std::unique_ptr<FrameWork::Model> FrameWork::Resource::LoadModel(const std::string p)
{
    path = p + "/";
    std::string modelName = "/model.fbx";
    std::string pa = path + modelName;

    Assimp::Importer importer;
    const aiScene* scene = importer.ReadFile(path + modelName, aiProcess_CalcTangentSpace | aiProcess_Triangulate | aiProcess_JoinIdenticalVertices | aiProcess_SortByPType | aiProcess_Triangulate | aiProcess_FlipUVs);

    if (scene == nullptr)
    {

        std::cout << "ロード出来ません: " << path + modelName << std::endl;
        assert(0);
    }

    ProcessNode(scene->mRootNode, scene);


    return std::make_unique<Model>(meshes,path);
}

c++

2022-11-05 00:00

1 Answers

assertis

This macro takes effect if the macro NDEBUG was not defined at the time of inclusion of the previous <cassert> (or assert.h>), and is invalid if NDEBUG was defined.

Conditions continue to run, as described in .In other words, the process must be able to continue even if the condition is not met.
In this case, as long as the file has not been opened, I think it should be finished clearly.

In this case, why don't you honestly throw an exception? catch and the recovery process will continue, and if catch is not done, the program will stop.


2022-11-05 00:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.