You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The current implementation uses inheritance to define the relationship between components and the types they implement and are derived from. This keeps the data structures short and readable.
Problem
The downside is that inheritance causes the data types are not guaranteed to obey standard layout. This may result in less efficient data structures, and also makes the code more bug-prone. As an example, #241 was caused by the fact that the data type does not have standard layout.
Proposed solution:
Plain text with implicit reference operator conversion
// Example program
#include<iostream>
#include<limits>using ID = int;
structBaseInput { ID id; };
structNodeInput{
ID id;
double u_rated;
// Sonar Cloud warnings about `explicit` keyword and reinterpret_cast should be disabled hereoperator BaseInput&() { returnreinterpret_cast<BaseInput&>(*this); }
operator BaseInput const&() const { returnreinterpret_cast<BaseInput const&>(*this); }
};
intmain()
{
NodeInput const node_input{ 5, 10.0 };
BaseInput const& base = node_input;
std::cout << base.id << '\n';
}
Other considered options
Concepts
The standard layout solution can be achieved using a plain struct types without references to any base classes. For written code, that would cause a lot of code duplication, but since the code under consideration is generated, this is not an issue.
The notion of the "base" types are enabled by declaring them concepts, which can be tested using static_assert.
This also will make the initializer list constructors a lot more readable for e.g. testing purposes.
Intellisense does provide suggestions on e.g. the member functions, but only when the type is constraint auto obj, not constraint auto const& obj.
Composition
Another option could be to use composition, e.g. using data types like
structBaseOutput{
ID id{};
};
structDerivedOutput{
BaseOutput base{};
int other_field{};
};
but it becomes prohibitively non-trivial to query data fields of underlying types without getters and setters (e.g. we would need transformer_output.line.base.id to query the id field of a transformer).
The text was updated successfully, but these errors were encountered:
Background
The underlying input, output and update data structures behind the data types retrieved by initialize_array are generated using code generation.
The current implementation uses inheritance to define the relationship between components and the types they implement and are derived from. This keeps the data structures short and readable.
Problem
The downside is that inheritance causes the data types are not guaranteed to obey standard layout. This may result in less efficient data structures, and also makes the code more bug-prone. As an example, #241 was caused by the fact that the data type does not have standard layout.
Proposed solution:
Plain text with implicit reference operator conversion
Other considered options
Concepts
The standard layout solution can be achieved using a plain struct types without references to any base classes. For written code, that would cause a lot of code duplication, but since the code under consideration is generated, this is not an issue.
The notion of the "base" types are enabled by declaring them
concept
s, which can be tested usingstatic_assert
.Constructors of components will then take the following form (and equivalent for their update and output functions):
This also will make the initializer list constructors a lot more readable for e.g. testing purposes.
Intellisense does provide suggestions on e.g. the member functions, but only when the type is
constraint auto obj
, notconstraint auto const& obj
.Composition
Another option could be to use composition, e.g. using data types like
but it becomes prohibitively non-trivial to query data fields of underlying types without getters and setters (e.g. we would need
transformer_output.line.base.id
to query theid
field of a transformer).The text was updated successfully, but these errors were encountered: