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

Rescope raylib.h into raylib::impl namespace #45

Open
Algias opened this issue May 7, 2020 · 5 comments
Open

Rescope raylib.h into raylib::impl namespace #45

Algias opened this issue May 7, 2020 · 5 comments

Comments

@Algias
Copy link

Algias commented May 7, 2020

Currently, the following code is included outside of namespace raylib, which pollutes the global namespace with raylib functions and types which can occasionally cause collisions. Is it possible to move this include into the raylib namespace? Many of the scope operators will have to be adjusted but I think this might result in some cleaner code. Also, I believe that the __cplusplus guards are not necessary when directly including a c header. I think this is typically used around C code that can be compiled by both a c & c++ compiler. The extern "C" is definitely still necessary though. Apologies if I am missing something.
https://isocpp.org/wiki/faq/mixing-c-and-cpp#include-c-hdrs-system

#ifdef __cplusplus
	extern "C" {
#endif
#include "raylib.h"
#ifdef __cplusplus
	}
#endif

Edit: extern "C" guards are in raylib.h already. It does not appear that it is necessary to include it in raylib-cpp

@RobLoach
Copy link
Owner

RobLoach commented May 7, 2020

Mind submitting a PR as a demonstration of what you're suggesting? Seems like a neat idea.

@Algias
Copy link
Author

Algias commented May 8, 2020

Mind submitting a PR as a demonstration of what you're suggesting? Seems like a neat idea.

Please see #46. A quick example showing what I mean. As I mention in the pr, its really a matter of design preference. Ultimately what I would personally like to see is a complete c++ wrapper around the c functionality so that it can be fully object-oriented. If that is not how you guys want to handle it, totally understand and I am more than happy to work with it as is.

@RobLoach
Copy link
Owner

@marinedalek said....
I'd tend to agree with Algias's suggestion here. If raylib-cpp is to be an "idiomatic" C++ wrapper, then it ought not to transitively dump names into the global namespace when included. My opinion is that changing how people (i.e. C++ users) use Raylib is the whole point of such a wrapper. Any and all functionality should be presented in the raylib:: namespace and the choice of whether to expose that in the global namespace left up to the consumer of the header.

My own preference would be for the low-level C raylib functions to be present in a nested namespace, perhaps raylib::impl:: , to signal to users that they should be using the high-level OOP/RAII constructs rather than the C underpinning. Even then, a determined user would be able to use the header in the current "mixed" mode by adding using namespace raylib::impl; to their source file, while everyone else enjoyed a clean, unpolluted global namespace.

Some concerns I have with rescoping the C library....

  • raylib will include other header files, which will be moved into the namespace. glfw, for example, doesn't expect to have any namespaces.
  • As you mentioned, it does change how people expect to use raylib. raylib-cpp doesn't aim to change the way people interact with raylib, however. raylib-cpp hopes to add on top of it to improve the experience, not explicitly change it.

We could introduce a #define to allow the rescope, but I'm not sure the benefits outweigh the potential harm it may cause.

@RobLoach RobLoach changed the title Migrate raylib.h includes into a raylib namespace? Rescope raylib.h into raylib::impl namespace Feb 21, 2021
@thar0x29a
Copy link

So ... why not putting the cpp version into its own namespace then? That would at least make the --allow-multiple-definition flag obsolete. And I consider it as pretty dangerous to be active in my projects..

@RobLoach
Copy link
Owner

RobLoach commented Sep 3, 2023

So ... why not putting the cpp version into its own namespace then? That would at least make the --allow-multiple-definition flag obsolete. And I consider it as pretty dangerous to be active in my projects..

To avoid conflicts. When you use...

Vector4 vec

Are using raylib's Vector4 struct? Or are referencing raylib-cpp's Vector4 class?

There are the shorthand aliases available, however...

RVector4
RMatrix
RTexture

... etc.

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

3 participants