diff options
| author | Jake Woltersdorf <jake@playruyi.com> | 2019-03-04 12:35:19 +0800 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2019-04-07 19:13:44 -0700 |
| commit | c3e062661388f70386d6766e3ce648030af340ee (patch) | |
| tree | a6d247bd24a17e3201a75b1a164e75d16e37196c /src/core | |
| parent | 989c5e90d48066b12ad44ed903cfc163d8e89b29 (diff) | |
| download | nng-c3e062661388f70386d6766e3ce648030af340ee.tar.gz nng-c3e062661388f70386d6766e3ce648030af340ee.tar.bz2 nng-c3e062661388f70386d6766e3ce648030af340ee.zip | |
fixes #901 shorter option get/set functions for all types
- Renamed internal nng_*_getx/setx methods with "nni" prefix
- Moved stream get/set option definition macros to options.h and added "NNI_" prefix
- "_PTR" variant of get/set option definition macros is for when first arg is passed as pointer (`nng_stream *s` vs `nng_pipe s`)
- New get/set option functions for `nng_socket` are `nng_socket_get_X` eschewing the previous `nng_getopt` pattern
- Macro-fy legacy getopt/setopt and implement in terms of "new" API
- nng_setopt* use "new" shorter API. Add missing uint64 set functions.
- Shorter get/set option functions get own man page and old getopt/setopt link to them
- Built with -DNNG_ENABLE_DOC=ON and part of central libnng index
- Update copyright
Diffstat (limited to 'src/core')
| -rw-r--r-- | src/core/options.h | 170 | ||||
| -rw-r--r-- | src/core/stream.c | 88 |
2 files changed, 176 insertions, 82 deletions
diff --git a/src/core/options.h b/src/core/options.h index 7b66dbfb..6df05805 100644 --- a/src/core/options.h +++ b/src/core/options.h @@ -90,4 +90,174 @@ extern int nni_setopt( extern int nni_chkopt( const nni_chkoption *, const char *, const void *, size_t, nni_type); +// +// This next block sets up to define the various typed option functions. +// To make it easier to cover them all at once, we use macros. +// + +#define NNI_DEFGET(base, pointer) \ + int nng_##base##_get( \ + nng_##base pointer s, const char *nm, void *vp, size_t *szp) \ + { \ + return (nni_##base##_getx(s, nm, vp, szp, NNI_TYPE_OPAQUE)); \ + } + +#define NNI_DEFTYPEDGET(base, suffix, pointer, type, nnitype) \ + int nng_##base##_get_##suffix( \ + nng_##base pointer s, const char *nm, type *vp) \ + { \ + size_t sz = sizeof(*vp); \ + return (nni_##base##_getx(s, nm, vp, &sz, nnitype)); \ + } + +#define NNI_DEFGETALL(base) \ + NNI_DEFGET(base, ) \ + NNI_DEFTYPEDGET(base, int, , int, NNI_TYPE_INT32) \ + NNI_DEFTYPEDGET(base, bool, , bool, NNI_TYPE_BOOL) \ + NNI_DEFTYPEDGET(base, size, , size_t, NNI_TYPE_SIZE) \ + NNI_DEFTYPEDGET(base, uint64, , uint64_t, NNI_TYPE_UINT64) \ + NNI_DEFTYPEDGET(base, string, , char *, NNI_TYPE_STRING) \ + NNI_DEFTYPEDGET(base, ptr, , void *, NNI_TYPE_POINTER) \ + NNI_DEFTYPEDGET(base, ms, , nng_duration, NNI_TYPE_DURATION) \ + NNI_DEFTYPEDGET(base, addr, , nng_sockaddr, NNI_TYPE_SOCKADDR) + +#define NNI_DEFGETALL_PTR(base) \ + NNI_DEFGET(base, *) \ + NNI_DEFTYPEDGET(base, int, *, int, NNI_TYPE_INT32) \ + NNI_DEFTYPEDGET(base, bool, *, bool, NNI_TYPE_BOOL) \ + NNI_DEFTYPEDGET(base, size, *, size_t, NNI_TYPE_SIZE) \ + NNI_DEFTYPEDGET(base, uint64, *, uint64_t, NNI_TYPE_UINT64) \ + NNI_DEFTYPEDGET(base, string, *, char *, NNI_TYPE_STRING) \ + NNI_DEFTYPEDGET(base, ptr, *, void *, NNI_TYPE_POINTER) \ + NNI_DEFTYPEDGET(base, ms, *, nng_duration, NNI_TYPE_DURATION) \ + NNI_DEFTYPEDGET(base, addr, *, nng_sockaddr, NNI_TYPE_SOCKADDR) + +#define NNI_DEFSET(base, pointer) \ + int nng_##base##_set( \ + nng_##base pointer s, const char *nm, const void *vp, size_t sz) \ + { \ + return (nni_##base##_setx(s, nm, vp, sz, NNI_TYPE_OPAQUE)); \ + } + +#define NNI_DEFTYPEDSETEX(base, suffix, pointer, type, len, nnitype) \ + int nng_##base##_set_##suffix( \ + nng_##base pointer s, const char *nm, type v) \ + { \ + return (nni_##base##_setx(s, nm, &v, len, nnitype)); \ + } + +#define NNI_DEFTYPEDSET(base, suffix, pointer, type, nnitype) \ + int nng_##base##_set_##suffix( \ + nng_##base pointer s, const char *nm, type v) \ + { \ + return (nni_##base##_setx(s, nm, &v, sizeof(v), nnitype)); \ + } + +#define NNI_DEFSTRINGSET(base, pointer) \ + int nng_##base##_set_string( \ + nng_##base pointer s, const char *nm, const char *v) \ + { \ + return (nni_##base##_setx(s, nm, v, \ + v != NULL ? strlen(v) + 1 : 0, NNI_TYPE_STRING)); \ + } + +#define NNI_DEFSOCKADDRSET(base, pointer) \ + int nng_##base##_set_addr( \ + nng_##base pointer s, const char *nm, const nng_sockaddr *v) \ + { \ + return (nni_##base##_setx( \ + s, nm, v, sizeof(*v), NNI_TYPE_SOCKADDR)); \ + } + +#define NNI_DEFSETALL(base) \ + NNI_DEFSET(base, ) \ + NNI_DEFTYPEDSET(base, int, , int, NNI_TYPE_INT32) \ + NNI_DEFTYPEDSET(base, bool, , bool, NNI_TYPE_BOOL) \ + NNI_DEFTYPEDSET(base, size, , size_t, NNI_TYPE_SIZE) \ + NNI_DEFTYPEDSET(base, uint64, , uint64_t, NNI_TYPE_UINT64) \ + NNI_DEFTYPEDSET(base, ms, , nng_duration, NNI_TYPE_DURATION) \ + NNI_DEFTYPEDSET(base, ptr, , void *, NNI_TYPE_POINTER) \ + NNI_DEFSTRINGSET(base, ) \ + NNI_DEFSOCKADDRSET(base, ) + +#define NNI_DEFSETALL_PTR(base) \ + NNI_DEFSET(base, *) \ + NNI_DEFTYPEDSET(base, int, *, int, NNI_TYPE_INT32) \ + NNI_DEFTYPEDSET(base, bool, *, bool, NNI_TYPE_BOOL) \ + NNI_DEFTYPEDSET(base, size, *, size_t, NNI_TYPE_SIZE) \ + NNI_DEFTYPEDSET(base, uint64, *, uint64_t, NNI_TYPE_UINT64) \ + NNI_DEFTYPEDSET(base, ms, *, nng_duration, NNI_TYPE_DURATION) \ + NNI_DEFTYPEDSET(base, ptr, *, void *, NNI_TYPE_POINTER) \ + NNI_DEFSTRINGSET(base, *) \ + NNI_DEFSOCKADDRSET(base, *) + +#define NNI_LEGACY_DEFGET(base) \ + int nng_##base##_getopt( \ + nng_##base s, const char *nm, void *vp, size_t *szp) \ + { \ + return (nng_##base##_get(s, nm, vp, szp)); \ + } + +#define NNI_LEGACY_DEFTYPEDGET(base, suffix, type) \ + int nng_##base##_getopt_##suffix( \ + nng_##base s, const char *nm, type *vp) \ + { \ + return (nng_##base##_get_##suffix(s, nm, vp)); \ + } + +#define NNI_LEGACY_DEFSOCKADDRGET(base) \ + int nng_##base##_getopt_sockaddr( \ + nng_##base s, const char *nm, nng_sockaddr *vp) \ + { \ + return (nng_##base##_get_addr(s, nm, vp)); \ + } + +#define NNI_LEGACY_DEFGETALL(base) \ + NNI_LEGACY_DEFGET(base) \ + NNI_LEGACY_DEFTYPEDGET(base, int, int) \ + NNI_LEGACY_DEFTYPEDGET(base, bool, bool) \ + NNI_LEGACY_DEFTYPEDGET(base, size, size_t) \ + NNI_LEGACY_DEFTYPEDGET(base, uint64, uint64_t) \ + NNI_LEGACY_DEFTYPEDGET(base, string, char *) \ + NNI_LEGACY_DEFTYPEDGET(base, ptr, void *) \ + NNI_LEGACY_DEFTYPEDGET(base, ms, nng_duration) \ + NNI_LEGACY_DEFSOCKADDRGET(base) + +#define NNI_LEGACY_DEFSET(base) \ + int nng_##base##_setopt( \ + nng_##base s, const char *nm, const void *vp, size_t sz) \ + { \ + return (nng_##base##_set(s, nm, vp, sz)); \ + } + +#define NNI_LEGACY_DEFTYPEDSET(base, suffix, type) \ + int nng_##base##_setopt_##suffix(nng_##base s, const char *nm, type v) \ + { \ + return (nng_##base##_set_##suffix(s, nm, v)); \ + } + +#define NNI_LEGACY_DEFSTRINGSET(base) \ + int nng_##base##_setopt_string( \ + nng_##base s, const char *nm, const char *v) \ + { \ + return (nng_##base##_set_string(s, nm, v)); \ + } + +#define NNI_LEGACY_DEFSOCKADDRSET(base) \ + int nng_##base##_setopt_sockaddr( \ + nng_##base s, const char *nm, const nng_sockaddr *v) \ + { \ + return (nng_##base##_set_addr(s, nm, v)); \ + } + +#define NNI_LEGACY_DEFSETALL(base) \ + NNI_LEGACY_DEFSET(base) \ + NNI_LEGACY_DEFTYPEDSET(base, int, int) \ + NNI_LEGACY_DEFTYPEDSET(base, bool, bool) \ + NNI_LEGACY_DEFTYPEDSET(base, size, size_t) \ + NNI_LEGACY_DEFTYPEDSET(base, ms, nng_duration) \ + NNI_LEGACY_DEFTYPEDSET(base, ptr, void*) \ + NNI_LEGACY_DEFSTRINGSET(base) \ + NNI_LEGACY_DEFSOCKADDRSET(base) + #endif // CORE_OPTIONS_H diff --git a/src/core/stream.c b/src/core/stream.c index c0dc38d6..e7ebc4e0 100644 --- a/src/core/stream.c +++ b/src/core/stream.c @@ -282,86 +282,10 @@ nni_stream_checkopt(const char *scheme, const char *name, const void *data, return (NNG_ENOTSUP); } -// -// This next block sets up to define the various typed option functions. -// To make it easier to cover them all at once, we use macros. -// - -#define DEFGET(base) \ - int nng_##base##_get( \ - nng_##base *s, const char *nm, void *vp, size_t *szp) \ - { \ - return (nni_##base##_getx(s, nm, vp, szp, NNI_TYPE_OPAQUE)); \ - } - -#define DEFTYPEDGET(base, suffix, type, nnitype) \ - int nng_##base##_get_##suffix( \ - nng_##base *s, const char *nm, type *vp) \ - { \ - size_t sz = sizeof(*vp); \ - return (nni_##base##_getx(s, nm, vp, &sz, nnitype)); \ - } - -#define DEFGETALL(base) \ - DEFGET(base) \ - DEFTYPEDGET(base, int, int, NNI_TYPE_INT32) \ - DEFTYPEDGET(base, bool, bool, NNI_TYPE_BOOL) \ - DEFTYPEDGET(base, size, size_t, NNI_TYPE_SIZE) \ - DEFTYPEDGET(base, uint64, uint64_t, NNI_TYPE_UINT64) \ - DEFTYPEDGET(base, string, char *, NNI_TYPE_STRING) \ - DEFTYPEDGET(base, ptr, void *, NNI_TYPE_POINTER) \ - DEFTYPEDGET(base, ms, nng_duration, NNI_TYPE_DURATION) \ - DEFTYPEDGET(base, addr, nng_sockaddr, NNI_TYPE_SOCKADDR) - -DEFGETALL(stream) -DEFGETALL(stream_dialer) -DEFGETALL(stream_listener) - -#define DEFSET(base) \ - int nng_##base##_set( \ - nng_##base *s, const char *nm, const void *vp, size_t sz) \ - { \ - return (nni_##base##_setx(s, nm, vp, sz, NNI_TYPE_OPAQUE)); \ - } - -#define DEFTYPEDSETEX(base, suffix, type, len, nnitype) \ - int nng_##base##_set_##suffix(nng_##base *s, const char *nm, type v) \ - { \ - return (nni_##base##_setx(s, nm, &v, len, nnitype)); \ - } - -#define DEFTYPEDSET(base, suffix, type, nnitype) \ - int nng_##base##_set_##suffix(nng_##base *s, const char *nm, type v) \ - { \ - return (nni_##base##_setx(s, nm, &v, sizeof(v), nnitype)); \ - } - -#define DEFSTRINGSET(base) \ - int nng_##base##_set_string( \ - nng_##base *s, const char *nm, const char *v) \ - { \ - return (nni_##base##_setx(s, nm, v, \ - v != NULL ? strlen(v) + 1 : 0, NNI_TYPE_STRING)); \ - } - -#define DEFSOCKADDRSET(base) \ - int nng_##base##_set_adddr( \ - nng_##base *s, const char *nm, const nng_sockaddr *v) \ - { \ - return (nni_##base##_setx( \ - s, nm, v, sizeof(*v), NNI_TYPE_SOCKADDR)); \ - } +NNI_DEFGETALL_PTR(stream) +NNI_DEFGETALL_PTR(stream_dialer) +NNI_DEFGETALL_PTR(stream_listener) -#define DEFSETALL(base) \ - DEFSET(base) \ - DEFTYPEDSET(base, int, int, NNI_TYPE_INT32) \ - DEFTYPEDSET(base, bool, bool, NNI_TYPE_BOOL) \ - DEFTYPEDSET(base, size, size_t, NNI_TYPE_SIZE) \ - DEFTYPEDSET(base, ms, nng_duration, NNI_TYPE_DURATION) \ - DEFTYPEDSET(base, ptr, void *, NNI_TYPE_POINTER) \ - DEFSTRINGSET(base) \ - DEFSOCKADDRSET(base) - -DEFSETALL(stream) -DEFSETALL(stream_dialer) -DEFSETALL(stream_listener)
\ No newline at end of file +NNI_DEFSETALL_PTR(stream) +NNI_DEFSETALL_PTR(stream_dialer) +NNI_DEFSETALL_PTR(stream_listener)
\ No newline at end of file |
