c++: Fix multiple definition and cast
[src/app-framework-binder.git] / include / afb / c++ / binding-wrap.hpp
1 /*
2  * Copyright (C) 2016-2019 "IoT.bzh"
3  * Author: José Bollo <jose.bollo@iot.bzh>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #pragma once
19
20 #include <cstddef>
21 #include <cstdlib>
22 #include <cstdarg>
23 #include <functional>
24
25 /* ensure version */
26 #ifndef AFB_BINDING_VERSION
27 # define AFB_BINDING_VERSION   3
28 #endif
29
30 /* check the version */
31 #if AFB_BINDING_VERSION < 3
32 # error "AFB_BINDING_VERSION must be at least 3"
33 #endif
34
35 /* get C definitions of bindings */
36 extern "C" {
37 #include <afb/afb-binding.h>
38 }
39
40 namespace afb {
41 /*************************************************************************/
42 /* pre-declaration of classes                                            */
43 /*************************************************************************/
44
45 class arg;
46 class event;
47 class req;
48
49 /*************************************************************************/
50 /* declaration of functions                                              */
51 /*************************************************************************/
52
53 int broadcast_event(const char *name, json_object *object = nullptr);
54
55 event make_event(const char *name);
56
57 void verbose(int level, const char *file, int line, const char * func, const char *fmt, va_list args);
58
59 void verbose(int level, const char *file, int line, const char * func, const char *fmt, ...);
60
61 int rootdir_get_fd();
62
63 int rootdir_open_locale_fd(const char *filename, int flags, const char *locale = nullptr);
64
65 int queue_job(void (*callback)(int signum, void *arg), void *argument, void *group, int timeout);
66
67 int require_api(const char *apiname, bool initialized = true);
68
69 int add_alias(const char *apiname, const char *aliasname);
70
71 int verbosity();
72
73 bool wants_errors();
74 bool wants_warnings();
75 bool wants_notices();
76 bool wants_infos();
77 bool wants_debugs();
78
79 void call(const char *api, const char *verb, struct json_object *args, void (*callback)(void*closure, int iserror, struct json_object *result, afb_api_t api), void *closure);
80
81 template <class T> void call(const char *api, const char *verb, struct json_object *args, void (*callback)(T*closure, int iserror, struct json_object *result, afb_api_t api), T *closure);
82
83 bool callsync(const char *api, const char *verb, struct json_object *args, struct json_object *&result);
84
85 /*************************************************************************/
86 /* effective declaration of classes                                      */
87 /*************************************************************************/
88
89 /* events */
90 class event
91 {
92         afb_event_t event_;
93 public:
94         event() { invalidate(); }
95         event(afb_event_t e);
96         event(const event &other);
97         event &operator=(const event &other);
98
99         operator afb_event_t() const;
100         afb_event_t operator->() const;
101
102         operator bool() const;
103         bool is_valid() const;
104
105         void invalidate();
106
107         int broadcast(json_object *object) const;
108         int push(json_object *object) const;
109
110         void unref();
111         void addref();
112         const char *name() const;
113 };
114
115 /* args */
116 class arg
117 {
118         struct afb_arg arg_;
119 public:
120         arg() = delete;
121         arg(const struct afb_arg &a);
122         arg(const arg &other);
123         arg &operator=(const arg &other);
124
125         operator const struct afb_arg&() const;
126
127         bool has_name() const;
128         bool has_value() const;
129         bool has_path() const;
130
131         const char *name() const;
132         const char *value() const;
133         const char *path() const;
134 };
135
136 /* req(uest) */
137 class req
138 {
139         afb_req_t req_;
140
141 public:
142         req() = delete;
143         req(afb_req_t r);
144         req(const req &other);
145         req &operator=(const req &other);
146
147         operator afb_req_t() const;
148         afb_req_t operator->() const;
149
150         operator bool() const;
151         bool is_valid() const;
152
153         arg get(const char *name) const;
154
155         const char *value(const char *name) const;
156
157         const char *path(const char *name) const;
158
159         json_object *json() const;
160
161         void reply(json_object *obj = nullptr, const char *error = nullptr, const char *info = nullptr) const;
162         void replyf(json_object *obj, const char *error, const char *info, ...) const;
163         void replyv(json_object *obj, const char *error, const char *info, va_list args) const;
164
165         void success(json_object *obj = nullptr, const char *info = nullptr) const;
166         void successf(json_object *obj, const char *info, ...) const;
167         void successv(json_object *obj, const char *info, va_list args) const;
168
169         void fail(const char *error = "failed", const char *info = nullptr) const;
170         void failf(const char *error, const char *info, ...) const;
171         void failv(const char *error, const char *info, va_list args) const;
172
173         template < class T > T *context() const;
174
175         void addref() const;
176
177         void unref() const;
178
179         void session_close() const;
180
181         bool session_set_LOA(unsigned level) const;
182
183         bool subscribe(const event &event) const;
184
185         bool unsubscribe(const event &event) const;
186
187         void subcall(const char *api, const char *verb, json_object *args, void (*callback)(void *closure, int iserror, json_object *result, afb_req_t req), void *closure) const;
188         template <class T> void subcall(const char *api, const char *verb, json_object *args, void (*callback)(T *closure, int iserror, json_object *result, afb_req_t req), T *closure) const;
189
190         bool subcallsync(const char *api, const char *verb, json_object *args, struct json_object *&result) const;
191
192         void subcall(const char *api, const char *verb, json_object *args, int flags, void (*callback)(void *closure, json_object *object, const char *error, const char *info, afb_req_t req), void *closure) const;
193
194         template <class T> void subcall(const char *api, const char *verb, json_object *args, int flags, void (*callback)(T *closure, json_object *object, const char *error, const char *info, afb_req_t req), T *closure) const;
195
196         bool subcallsync(const char *api, const char *verb, json_object *args, int flags, struct json_object *&object, char *&error, char *&info) const;
197
198         void verbose(int level, const char *file, int line, const char * func, const char *fmt, va_list args) const;
199
200         void verbose(int level, const char *file, int line, const char * func, const char *fmt, ...) const;
201
202         bool has_permission(const char *permission) const;
203
204         char *get_application_id() const;
205
206         int get_uid() const;
207
208         json_object *get_client_info() const;
209 };
210
211 /*************************************************************************/
212 /* effective declaration of classes                                      */
213 /*************************************************************************/
214 /////////////////////////////////////////////////////////////////////////////////////////////////////
215 /////////////////////////////////////////////////////////////////////////////////////////////////////
216 /////////////////////////////////////////////////////////////////////////////////////////////////////
217 /////////////////////////////////////////////////////////////////////////////////////////////////////
218 /////////////////////////////////////////////////////////////////////////////////////////////////////
219 /////////////////////////////////////////////////////////////////////////////////////////////////////
220 /////////////////////////////////////////////////////////////////////////////////////////////////////
221 /////////////////////////////////////////////////////////////////////////////////////////////////////
222 /////////////////////////////////////////////////////////////////////////////////////////////////////
223 /////////////////////////////////////////////////////////////////////////////////////////////////////
224 /////////////////////////////////////////////////////////////////////////////////////////////////////
225 /////////////////////////////////////////////////////////////////////////////////////////////////////
226 /////////////////////////////////////////////////////////////////////////////////////////////////////
227 /////////////////////////////////////////////////////////////////////////////////////////////////////
228 /////////////////////////////////////////////////////////////////////////////////////////////////////
229 /////////////////////////////////////////////////////////////////////////////////////////////////////
230
231
232 /*************************************************************************/
233 /* effective declaration of classes                                      */
234 /*************************************************************************/
235
236 /* events */
237 inline event::event(afb_event_t e) : event_(e) { }
238 inline event::event(const event &other) : event_(other.event_) { }
239 inline event &event::operator=(const event &other) { event_ = other.event_; return *this; }
240
241 inline event::operator afb_event_t() const { return event_; }
242 inline afb_event_t event::operator->() const { return event_; }
243
244 inline event::operator bool() const { return is_valid(); }
245 inline bool event::is_valid() const { return afb_event_is_valid(event_); }
246
247 inline void event::invalidate() { event_ = nullptr; }
248
249 inline int event::broadcast(json_object *object) const { return afb_event_broadcast(event_, object); }
250 inline int event::push(json_object *object) const { return afb_event_push(event_, object); }
251
252 inline void event::unref() { afb_event_unref(event_); invalidate(); }
253 inline void event::addref() { afb_event_addref(event_); }
254 inline const char *event::name() const { return afb_event_name(event_); }
255
256 /* args */
257 inline arg::arg(const struct afb_arg &a) : arg_(a) {}
258 inline arg::arg(const arg &other) : arg_(other.arg_) {}
259 inline arg &arg::operator=(const arg &other) { arg_ = other.arg_; return *this; }
260
261 inline arg::operator const struct afb_arg&() const { return arg_; }
262
263 inline bool arg::has_name() const { return !!arg_.name; }
264 inline bool arg::has_value() const { return !!arg_.value; }
265 inline bool arg::has_path() const { return !!arg_.path; }
266
267 inline const char *arg::name() const { return arg_.name; }
268 inline const char *arg::value() const { return arg_.value; }
269 inline const char *arg::path() const { return arg_.path; }
270
271 /* req(uests)s */
272
273
274 inline req::req(afb_req_t r) : req_(r) {}
275 inline req::req(const req &other) : req_(other.req_) {}
276 inline req &req::operator=(const req &other) { req_ = other.req_; return *this; }
277
278 inline req::operator afb_req_t() const { return req_; }
279 inline afb_req_t req::operator->() const { return req_; }
280
281 inline req::operator bool() const { return is_valid(); }
282 inline bool req::is_valid() const { return afb_req_is_valid(req_); }
283
284 inline arg req::get(const char *name) const { return arg(afb_req_get(req_, name)); }
285
286 inline const char *req::value(const char *name) const { return afb_req_value(req_, name); }
287
288 inline const char *req::path(const char *name) const { return afb_req_path(req_, name); }
289
290 inline json_object *req::json() const { return afb_req_json(req_); }
291
292 inline void req::reply(json_object *obj, const char *error, const char *info) const { afb_req_reply(req_, obj, error, info); }
293 inline void req::replyv(json_object *obj, const char *error, const char *info, va_list args) const { afb_req_reply_v(req_, obj, error, info, args); }
294 inline void req::replyf(json_object *obj, const char *error, const char *info, ...) const
295 {
296         va_list args;
297         va_start(args, info);
298         replyv(obj, error, info, args);
299         va_end(args);
300 }
301
302 inline void req::success(json_object *obj, const char *info) const { reply(obj, nullptr, info); }
303 inline void req::successv(json_object *obj, const char *info, va_list args) const { replyv(obj, nullptr, info, args); }
304 inline void req::successf(json_object *obj, const char *info, ...) const
305 {
306         va_list args;
307         va_start(args, info);
308         successv(obj, info, args);
309         va_end(args);
310 }
311
312 inline void req::fail(const char *error, const char *info) const { reply(nullptr, error, info); }
313 inline void req::failv(const char *error, const char *info, va_list args) const { replyv(nullptr, error, info, args); }
314 inline void req::failf(const char *error, const char *info, ...) const
315 {
316         va_list args;
317         va_start(args, info);
318         failv(error, info, args);
319         va_end(args);
320 }
321
322 template < class T >
323 inline T *req::context() const
324 {
325         T* (*creater)(void*) = [](){return new T();};
326         void (*freer)(T*) = [](T*t){delete t;};
327         return reinterpret_cast<T*>(afb_req_context(req_, 0,
328                         reinterpret_cast<void *(*)(void*)>(creater),
329                         reinterpret_cast<void (*)(void*)>(freer), nullptr));
330 }
331
332 inline void req::addref() const { afb_req_addref(req_); }
333
334 inline void req::unref() const { afb_req_unref(req_); }
335
336 inline void req::session_close() const { afb_req_session_close(req_); }
337
338 inline bool req::session_set_LOA(unsigned level) const { return !afb_req_session_set_LOA(req_, level); }
339
340 inline bool req::subscribe(const event &event) const { return !afb_req_subscribe(req_, event); }
341
342 inline bool req::unsubscribe(const event &event) const { return !afb_req_unsubscribe(req_, event); }
343
344 inline void req::subcall(const char *api, const char *verb, json_object *args, int flags, void (*callback)(void *closure, json_object *result, const char *error, const char *info, afb_req_t req), void *closure) const
345 {
346         afb_req_subcall(req_, api, verb, args, flags, callback, closure);
347 }
348
349 template <class T>
350 inline void req::subcall(const char *api, const char *verb, json_object *args, int flags, void (*callback)(T *closure, json_object *result, const char *error, const char *info, afb_req_t req), T *closure) const
351 {
352         subcall(api, verb, args, flags, reinterpret_cast<void(*)(void*,json_object*,const char*,const char*,afb_req_t)>(callback), reinterpret_cast<void*>(closure));
353 }
354
355 inline bool req::subcallsync(const char *api, const char *verb, json_object *args, int flags, struct json_object *&object, char *&error, char *&info) const
356 {
357         return !afb_req_subcall_sync(req_, api, verb, args, flags, &object, &error, &info);
358 }
359
360 inline void req::subcall(const char *api, const char *verb, json_object *args, void (*callback)(void *closure, int iserror, json_object *result, afb_req_t req), void *closure) const
361 {
362         afb_req_subcall_legacy(req_, api, verb, args, callback, closure);
363 }
364
365 template <class T>
366 inline void req::subcall(const char *api, const char *verb, json_object *args, void (*callback)(T *closure, int iserror, json_object *result, afb_req_t req), T *closure) const
367 {
368         subcall(api, verb, args, reinterpret_cast<void(*)(void*,int,json_object*,afb_req_t)>(callback), reinterpret_cast<void*>(closure));
369 }
370
371 inline bool req::subcallsync(const char *api, const char *verb, json_object *args, struct json_object *&result) const
372 {
373         return !afb_req_subcall_sync_legacy(req_, api, verb, args, &result);
374 }
375
376 inline void req::verbose(int level, const char *file, int line, const char * func, const char *fmt, va_list args) const
377 {
378         afb_req_verbose(req_, level, file, line, func, fmt, args);
379 }
380
381 inline void req::verbose(int level, const char *file, int line, const char * func, const char *fmt, ...) const
382 {
383         va_list args;
384         va_start(args, fmt);
385         afb_req_verbose(req_, level, file, line, func, fmt, args);
386         va_end(args);
387 }
388
389 inline bool req::has_permission(const char *permission) const
390 {
391         return bool(afb_req_has_permission(req_, permission));
392 }
393
394 inline char *req::get_application_id() const
395 {
396         return afb_req_get_application_id(req_);
397 }
398
399 inline int req::get_uid() const
400 {
401         return afb_req_get_uid(req_);
402 }
403
404 inline json_object *req::get_client_info() const
405 {
406         return afb_req_get_client_info(req_);
407 }
408
409 /* commons */
410 inline int broadcast_event(const char *name, json_object *object)
411         { return afb_daemon_broadcast_event(name, object); }
412
413 inline event make_event(const char *name)
414         { return afb_daemon_make_event(name); }
415
416 inline void verbose(int level, const char *file, int line, const char * func, const char *fmt, va_list args)
417         { afb_daemon_verbose(level, file, line, func, fmt, args); }
418
419 inline void verbose(int level, const char *file, int line, const char * func, const char *fmt, ...)
420         { va_list args; va_start(args, fmt); verbose(level, file, line, func, fmt, args); va_end(args); }
421
422 inline int rootdir_get_fd()
423         { return afb_daemon_rootdir_get_fd(); }
424
425 inline int rootdir_open_locale_fd(const char *filename, int flags, const char *locale)
426         { return afb_daemon_rootdir_open_locale(filename, flags, locale); }
427
428 inline int queue_job(void (*callback)(int signum, void *arg), void *argument, void *group, int timeout)
429         { return afb_daemon_queue_job(callback, argument, group, timeout); }
430
431 inline int require_api(const char *apiname, bool initialized)
432         { return afb_daemon_require_api(apiname, int(initialized)); }
433
434 inline int add_alias(const char *apiname, const char *aliasname)
435         { return afb_daemon_add_alias(apiname, aliasname); }
436
437 inline int logmask()
438         { return afb_get_logmask(); }
439
440 inline bool wants_errors()
441         { return AFB_SYSLOG_MASK_WANT_ERROR(logmask()); }
442
443 inline bool wants_warnings()
444         { return AFB_SYSLOG_MASK_WANT_WARNING(logmask()); }
445
446 inline bool wants_notices()
447         { return AFB_SYSLOG_MASK_WANT_NOTICE(logmask()); }
448
449 inline bool wants_infos()
450         { return AFB_SYSLOG_MASK_WANT_INFO(logmask()); }
451
452 inline bool wants_debugs()
453         { return AFB_SYSLOG_MASK_WANT_DEBUG(logmask()); }
454
455 inline void call(const char *api, const char *verb, struct json_object *args, void (*callback)(void*closure, struct json_object *result, const char *error, const char *info, afb_api_t api), void *closure)
456 {
457         afb_service_call(api, verb, args, callback, closure);
458 }
459
460 template <class T>
461 inline void call(const char *api, const char *verb, struct json_object *args, void (*callback)(T*closure, struct json_object *result, const char *error, const char *info, afb_api_t api), T *closure)
462 {
463         afb_service_call(api, verb, args, reinterpret_cast<void(*)(void*,json_object*,const char*, const char*,afb_api_t)>(callback), reinterpret_cast<void*>(closure));
464 }
465
466 inline bool callsync(const char *api, const char *verb, struct json_object *args, struct json_object *&result, char *&error, char *&info)
467 {
468         return !!afb_service_call_sync(api, verb, args, &result, &error, &info);
469 }
470
471 /*************************************************************************/
472 /* declaration of the binding's authorization s                          */
473 /*************************************************************************/
474
475 constexpr afb_auth auth_no()
476 {
477         afb_auth r = { afb_auth_No, 0, 0};
478         r.type = afb_auth_No;
479         return r;
480 }
481
482 constexpr afb_auth auth_yes()
483 {
484         afb_auth r = { afb_auth_No, 0, 0};
485         r.type = afb_auth_Yes;
486         return r;
487 }
488
489 constexpr afb_auth auth_token()
490 {
491         afb_auth r = { afb_auth_No, 0, 0};
492         r.type = afb_auth_Token;
493         return r;
494 }
495
496 constexpr afb_auth auth_LOA(unsigned loa)
497 {
498         afb_auth r = { afb_auth_No, 0, 0};
499         r.type = afb_auth_LOA;
500         r.loa = loa;
501         return r;
502 }
503
504 constexpr afb_auth auth_permission(const char *permission)
505 {
506         afb_auth r = { afb_auth_No, 0, 0};
507         r.type = afb_auth_Permission;
508         r.text = permission;
509         return r;
510 }
511
512 constexpr afb_auth auth_not(const afb_auth *other)
513 {
514         afb_auth r = { afb_auth_No, 0, 0};
515         r.type = afb_auth_Not;
516         r.first = other;
517         return r;
518 }
519
520 constexpr afb_auth auth_not(const afb_auth &other)
521 {
522         return auth_not(&other);
523 }
524
525 constexpr afb_auth auth_or(const afb_auth *first, const afb_auth *next)
526 {
527         afb_auth r = { afb_auth_No, 0, 0};
528         r.type = afb_auth_Or;
529         r.first = first;
530         r.next = next;
531         return r;
532 }
533
534 constexpr afb_auth auth_or(const afb_auth &first, const afb_auth &next)
535 {
536         return auth_or(&first, &next);
537 }
538
539 constexpr afb_auth auth_and(const afb_auth *first, const afb_auth *next)
540 {
541         afb_auth r = { afb_auth_No, 0, 0};
542         r.type = afb_auth_And;
543         r.first = first;
544         r.next = next;
545         return r;
546 }
547
548 constexpr afb_auth auth_and(const afb_auth &first, const afb_auth &next)
549 {
550         return auth_and(&first, &next);
551 }
552
553 constexpr afb_verb_t verb(
554         const char *name,
555         void (*callback)(afb_req_t),
556         const char *info = nullptr,
557         uint16_t session = 0,
558         const afb_auth *auth = nullptr,
559         bool glob = false,
560         void *vcbdata = nullptr
561 )
562 {
563         afb_verb_t r = { 0, 0, 0, 0, 0, 0, 0 };
564         r.verb = name;
565         r.callback = callback;
566         r.info = info;
567         r.session = session;
568         r.auth = auth;
569         r.glob = (unsigned)glob;
570         r.vcbdata = vcbdata;
571         return r;
572 }
573
574 void __attribute__((weak)) __afb__verb__cb__for__global__(afb_req_t r)
575 {
576         void *vcbdata;
577         void (*callback)(req);
578
579         vcbdata = afb_req_get_vcbdata(r);
580         callback = reinterpret_cast<void(*)(req)>(vcbdata);
581         callback(req(r));
582 }
583
584 constexpr afb_verb_t verb(
585         const char *name,
586         void (&callback)(req),
587         const char *info = nullptr,
588         uint16_t session = 0,
589         const afb_auth *auth = nullptr,
590         bool glob = false,
591         void *vcbdata = nullptr
592 )
593 {
594         return verb(
595                 name,
596                 __afb__verb__cb__for__global__,
597                 info,
598                 session,
599                 auth,
600                 glob,
601                 (void*)(&callback)
602         );
603 }
604
605 constexpr afb_verb_t verbend()
606 {
607         afb_verb_t r = verb(nullptr, nullptr);
608         return r;
609 }
610
611 constexpr afb_binding_t binding(
612         const char *name,
613         const afb_verb_t *verbs,
614         const char *info = nullptr,
615         int (*init)(afb_api_t) = nullptr,
616         const char *specification = nullptr,
617         void (*onevent)(afb_api_t, const char*, struct json_object*) = nullptr,
618         bool noconcurrency = false,
619         int (*preinit)(afb_api_t) = nullptr,
620         void *userdata = nullptr
621 )
622 {
623         afb_binding_t r = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
624         r.api = name;
625         r.specification = specification;
626         r.info = info;
627         r.verbs = verbs;
628         r.preinit = preinit;
629         r.init = init;
630         r.onevent = onevent;
631         r.noconcurrency = noconcurrency ? 1 : 0;
632         r.userdata = userdata;
633         return r;
634 };
635
636 /*************************************************************************/
637 /***                         E N D                                     ***/
638 /*************************************************************************/
639 }