diff options
| author | Garrett D'Amore <garrett@damore.org> | 2018-04-26 15:36:13 -0700 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2018-04-26 19:13:59 -0700 |
| commit | fd06aba05381055ab56e1ec81d56055b66462f0b (patch) | |
| tree | 99633af36e1c393bffeda213c0ac85e83fc4a6ee /src/nng.h | |
| parent | 3de2b56557c80b310341c423492bd8ba895c1abe (diff) | |
| download | nng-fd06aba05381055ab56e1ec81d56055b66462f0b.tar.gz nng-fd06aba05381055ab56e1ec81d56055b66462f0b.tar.bz2 nng-fd06aba05381055ab56e1ec81d56055b66462f0b.zip | |
fixes #375 integer types are error prone
This change converts the various integer types like nng_socket
in the public API to opaque structures that are passed by value.
Basically we just wrap the integer ID. This "hack" give us strong
type checks by the compiler (yay!), at the expense of not being able
to directly use these as numbers (so comparisions for example don't
work, and neither does initialization to zero using the normal
method.
Comparison of disassembly output shows that at least with the optimizer
enabled there is no difference in the compiler output between using
a structure or an integral value.
Diffstat (limited to 'src/nng.h')
| -rw-r--r-- | src/nng.h | 32 |
1 files changed, 27 insertions, 5 deletions
@@ -59,11 +59,33 @@ extern "C" { #define NNG_MAXADDRLEN (128) // Types common to nng. -typedef uint32_t nng_socket; -typedef uint32_t nng_ctx; -typedef uint32_t nng_dialer; -typedef uint32_t nng_listener; -typedef uint32_t nng_pipe; + +// Identifiers are wrapped in a structure to improve compiler validation +// of incorrect passing. This gives us strong type checking. Modern +// compilers compile passing these by value to identical code as passing +// the integer type (at least with optimization applied). Please do not +// access the ID member directly. + +typedef struct nng_ctx_s { + uint32_t id; +} nng_ctx; + +typedef struct nng_dialer_s { + uint32_t id; +} nng_dialer; + +typedef struct nng_listener_s { + uint32_t id; +} nng_listener; + +typedef struct nng_pipe_s { + uint32_t id; +} nng_pipe; + +typedef struct nng_socket_s { + uint32_t id; +} nng_socket; + typedef int32_t nng_duration; // in milliseconds typedef struct nng_msg nng_msg; typedef struct nng_snapshot nng_snapshot; |
