-
Notifications
You must be signed in to change notification settings - Fork 461
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
Use of enum variable for bit flags prevents compilation as C++ source #619
Comments
I guess the flags should all be declared as preprocessor defines: #define OPJ_STREAM_STATUS_OUTPUT 0x1U
#define OPJ_STREAM_STATUS_INPUT 0x2U
#define OPJ_STREAM_STATUS_END 0x4U
#define OPJ_STREAM_STATUS_ERROR 0x8U The structure shall be defined as: typedef struct opj_stream_private
{
...
/**
* Flags to tell the status of the stream.
*/
OPJ_UINT32 m_status;
}
opj_stream_private_t; I think it's best to use unsigned integers for bitfields (some static analyzers will report a warning for bitwise operations on signed integers). |
@mayeut: I see, you are right that an unsigned integer makes more sense. I did not pay attention that the enum member Can I change the sources according to your above proposal and send a pull request? |
@smuehlst : Why not add a | operator for the enum in question? Here is an example: Of course the operator would need conditional compilation (only for C++). |
@stweil: I wouldn't want to spoil the code with any C++ constructs for this purpose, and you would have to ask the OpenJPEG developers whether they want this. Do you see any value in the declaration of m_status as an enum typedef? Or do you see any downside in declaring m_status as an unsigned int? |
@smuehlst, As you said, do not use C++ constructs. |
@malaterre, |
@mayeut sorry for the noise, using Eg:
|
@mayeut, I don't think that C++ code in header files is a problem, as long as it is encapsulated with #ifdef __cplusplus ... #endif. This is quite common for system header files. I'd keep the enum type because it is more safe and accept a few lines of C++ (as in my pull request) instead of using other integer types. @smuehlst: My pull request #621 also fixes some more issues for C++ and passes compilation with g++. Perhaps you can try it with your compiler, too. |
@mayeut: I created pull request #622 with the changes that you proposed. @stweil: Thanks for the patch, but I personally consider the introduction of C++ operators overkill. The use of an enum type is actually misleading in my view, because the variable can contain a combination of the bit flags, so the actual value of the m_status variable can be different from any of the four defined enum members. |
I'm compiling OpenJPEG with the .NET compiler in C++ mode. There are two problems that prevent successful compilation out of the box. One is missing casts for return values of opj_malloc()/opj_calloc(). For that I already created pull request #618.
The other problem is the use of an enum typedef for a variable that is intended for holding bit flags. From src/lib/openjp2/cio.h:
And in src/lib/openjp2/cio.c for example:
This fails to compile in C++ mode. The declaration of the
m_status
member asopj_stream_flag
in my view does not add value here, because as the variable can contain a combination of theopj_stream_*
flags it is not restricted anyway to the actual members of theopj_stream_flag
enumeration.To be able to compile in C++ mode I changed the type of the
m_status
variable fromopj_stream_flag
toOPJ_INT32
in my local repository. Would you consider to apply this change? If so, I would create a corresponding pull request.The text was updated successfully, but these errors were encountered: