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

Add private conversion utilities between corresponding C and C++ data types #226

Open
ChrisThrasher opened this issue Feb 4, 2024 · 2 comments
Labels

Comments

@ChrisThrasher
Copy link
Member

sf::Vector2i sfmlPos = renderWindow->This.getPosition();
position.x = sfmlPos.x;
position.y = sfmlPos.y;
return position;

A common pattern in the implementation of the library is that we have two stack variables that represent the same thing but one type is from the sf:: namespace and the other is a CSFML public data type. Having both variables coexist leads to awkward variable names and requires rewriting the same logic to convert one type to another in many places.

I propose we add function overloads that convert between CSFML and SFML data types, in particular conversions from SFML to CSFML types which seems to be a more common.

sfVector2i toCType(const sf::Vector2i& vector)
{
    sfVector2i vector = {vector.x, vector.y};
    return vector;
}

The above snippet from RenderWindow.cpp would become a one-liner.

 return toCType(renderWindow->This.getPosition());
@eXpl0it3r
Copy link
Member

Can we rely on the compiler to inline effectively or do we have to consider potential additional indirections & copies?

@ChrisThrasher
Copy link
Member Author

https://en.cppreference.com/w/cpp/language/copy_elision

C++17's copy elision rules mean I don't think this would add any copies.

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

No branches or pull requests

2 participants