Skip to content

[C++] Use a custom optional instead of std::optional for non-scalar type  #359

Open
@SirusDoma

Description

Is your feature request related to a problem? Please describe.
Currently bebop use concrete type when generating a field with non-scalar types. This make things simple since everything is value-based, but prevents user to make cyclic dependency which gracefully prevented in #111.

Describe the solution you'd like
I would like to suggest to revise and support cyclic dependency instead. Here what I had in mind:

  1. Define a custom std::optional implementation, but instead of use value type, use heap (e.g smart pointer). This is relatively simple to do.
  2. Determine definitions that are cyclic in parser, and then in the generator, forward declare them before generating the actual definitions.
  3. The custom optional will incur extra cost in runtime; typically it use 4-8 bytes (the std::optional use extra 1 byte only) and it loss cache locality. Thus, this custom optional should only be used only for non-scalar type where the size is trivial

Describe alternatives you've considered
The easiest way (and relatively safe) to do this is to expose smart pointer for non-scalar type. But this would be a breaking change to the user.

Additional context
N/A

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions