Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

object.declaration_file is empty when use Parser to load .fbs schema file. #8342

Open
jerrrykong opened this issue Jun 26, 2024 · 1 comment

Comments

@jerrrykong
Copy link

flatbuffers version is 24.3.25
gcc: 7.3.1

code :

int Load(const char* filename)
{
    std::string str;
    if(!flatbuffers::LoadFile(filename, false, &str))
    {
        fprintf(stderr,  "load file error: %s \n", filename);
        return -1;
    }

    const char* p = strrchr(filename, '/');

    std::string path = p ?  std::string(filename, p - filename) : std::string(".");

    const char* includes[] = { path.c_str(),  nullptr};

    flatbuffers::IDLOptions options;
    //options.lang_to_generate = flatbuffers::IDLOptions::kCpp;
    
    flatbuffers::Parser parser(options);
    if(!parser.Parse(str.c_str(), includes, filename))
    {
        fprintf(stderr,  "parse error: \n%s\n", parser.error_.c_str());
        return -1;
    }

    parser.Serialize();

    auto schema = reflection::GetSchema(parser.builder_.GetBufferPointer());
    printf("\nList Objects: \n");
    auto objects = schema->objects();
    for(uint32_t i = 0; i < objects->size(); i++) {
        auto object= objects->Get(i);
        printf(" [%u] %s: %s, file: %s\n", i, object->is_struct() ? "struct" : "table", 
                object->name()->c_str(), object->declaration_file()->c_str());
    }

    // Enums
    printf("\nList Enums: \n");
    auto enums = schema->enums();
    for (uint32_t i = 0; i < enums->size(); i++) {
        auto eu = enums->Get(i);
        printf(" [%u] %s %s: %s, file: %s\n", i, eu->is_union() ? "Union" : "Enum",  
                eu->name()->c_str(), reflection::EnumNameBaseType(eu->underlying_type()->base_type()), 
                eu->declaration_file()->c_str());
    }
    return 0;
}

So, what is the correct way to determine in which schema file an object is defined?

@jerrrykong
Copy link
Author

Output is like:
List Objects:
[0] struct: res.Area, file:
[1] table: res.ResCard, file:
[2] table: res.ResCardTable, file:
[3] struct: res.WeaponSet, file:

List Enums:
[0] Enum res.ArmorType: Byte, file:
[1] Enum res.BuildingType: Byte, file:
[2] Enum res.CardQuality: Byte, file:
[3] Enum res.UnitType: Byte, file:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant