Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [paho-dev] Using C++ with the Paho C client library

Hi Frank,

On 16/05/13 13:54, Frank Pagliughi wrote:
Hello All,

I'm writing C++ wrapper classes around the C library, with the goal to have it mimic the Java API as closely as possible. There are a few minor things in the C library that if updated might help out.
it's been my plan to have a C++ wrapper for the C APIs.  If you are able to contribute your code to this project, that would be great.  If not, I'll do my own.  That's the place for proper C++ conformance.

(1) Make the headers C++ aware internally:
#if defined(__cplusplus)
extern "C" {
#endif
Once the C++ wrapper is written, then C++ programs can use that, and this would become moot.  I have a preference for keeping C++ related stuff out of the C code, but I can see the logic.

(2) Observe proper const-ness for pointer parameters if there is no intent in updating the item, particularly "const char*" for "char*"
DLLExport int MQTTAsync_create(MQTTAsync* handle,
        const char* serverURI, const char* clientId, ...
When I've used const in the past, it's only caused pain.  This is the description of const in K&R 2nd Edition:

"The const and volatile properties are new with the ANSI standard. The purpose of const is to announce objects that may be placed in read-only memory, and perhaps to increase opportunities for optimization."

Which is not the same as the purpose here.  In the latest version of gcc on my machine, using const seems not to cause the problems I was expecting, but I'd have to check on all the compilers we use, MS, and old versions of gcc, before adding this change.

Shouldn't the definition for clientId for example, be "const char const * clientId"?  That is, both the pointer and the contents are immutable.

(3) Use const values instead of #define whenever possible, since #define has no respect for namespaces, etc.
const int MQTTASYNC_SUCCESS = 0;
instead of
#define MQTTASYNC_SUCCESS 0

See above.  I've encountered pain when this before when this should be simple.  This is better done in a C++ header I think.

(4) Use "size_t" instead of "int" to specify array lengths, to prevent signed/unsigned warnings when using STL containers:
DLLExport int MQTTAsync_subscribeMany(MQTTAsync handle, size_t count,
        const char** topic, const int* qos, MQTTAsync_responseOptions* response);

That makes sense.  I'll have to check if it causes any backward compatibility issues.


These all make sense in the C realm, but are particularly helpful when working in C++.
Let me know if I can help in any way.

Thanks,
Frank


_______________________________________________
paho-dev mailing list
paho-dev@xxxxxxxxxxx
http://dev.eclipse.org/mailman/listinfo/paho-dev


Back to the top