JoyShockLibrary icon indicating copy to clipboard operation
JoyShockLibrary copied to clipboard

C friendly header

Open NullMember opened this issue 4 years ago • 1 comments

I'm currently writing a Max/MSP library (external) using JoyShockLibrary in C. Current header file is not so C friendly. Seperating headers for C and C++ or modifying current file could work. What you think?

NullMember avatar Jul 28 '21 23:07 NullMember

I tried this myself and identified these issues:

JoyShockLibrary.h:114:9: error: unknown type name ‘bool’
  114 |         bool t0Down;
      |         ^~~~

Access to the bool type in C requires an include of stdbool.h

JoyShockLibrary.h:122:8: error: expected identifier or ‘(’ before string constant
  122 | extern "C" JOY_SHOCK_API int JslConnectDevices();
      |        ^~~

extern "C" is not valid under C, it is C++ only

These two are easy to fix with some #define

152 extern "C" JOY_SHOCK_API TOUCH_STATE JslGetTouchState(int deviceId, bool previous = false);
153 extern "C" JOY_SHOCK_API bool JslGetTouchpadDimension(int deviceId, int &sizeX, int &sizeY);

The last problem that isn't so quick and uncontroversial to correct is that the API uses references and default arguments. These are both C++ features. I'm honestly not sure how it works at all with the current extern "C" in front of everything, I wouldn't think those would be usable features for functions marked like that.

It would be a breaking change to fix that, which would surely annoy existing users. Perhaps a reason supporting creating a separate header with a C api for the library, though it would be questionable code duplication.

So it leaves the question: Is it worth breaking current programs to leave a C compatible API? If yes, then changing all the references to pointers and removing default arguments is the easiest way to go. If no, then I guess creating a header with a few functions wrapping the C++ feature usages would be the way to go.

The API is very close to being C compatible, but not quite...

I'm happy to submit a PR implementing whichever approach the maintainer(s) deem appropriate.

Peter0x44 avatar Mar 04 '23 21:03 Peter0x44