api-v3: First draft
[src/app-framework-binder.git] / include / afb / afb-event-x2.h
1 /*
2  * Copyright (C) 2016, 2017, 2018 "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 "afb-event-x2-itf.h"
21
22 /**
23  * Checks whether the 'event' is valid or not.
24  *
25  * @param event the event to check
26  *
27  * @return 0 if not valid or 1 if valid.
28  */
29 static inline int afb_event_x2_is_valid(struct afb_event_x2 *event)
30 {
31         return !!event;
32 }
33
34 /**
35  * Broadcasts widely an event of 'event' with the data 'object'.
36  * 'object' can be NULL.
37  *
38  * For convenience, the function calls 'json_object_put' for 'object'.
39  * Thus, in the case where 'object' should remain available after
40  * the function returns, the function 'json_object_get' shall be used.
41  *
42  * @param event the event to broadcast
43  * @param object the companion object to associate to the broadcasted event (can be NULL)
44  *
45  * @return the count of clients that received the event.
46  */
47 static inline int afb_event_x2_broadcast(
48                         struct afb_event_x2 *event,
49                         struct json_object *object)
50 {
51         return event->itf->broadcast(event, object);
52 }
53
54 /**
55  * Pushes an event of 'event' with the data 'object' to its observers.
56  * 'object' can be NULL.
57  *
58  * For convenience, the function calls 'json_object_put' for 'object'.
59  * Thus, in the case where 'object' should remain available after
60  * the function returns, the function 'json_object_get' shall be used.
61  *
62  * @param event the event to push
63  * @param object the companion object to associate to the pushed event (can be NULL)
64  *
65  * @return the count of clients that received the event.
66  */
67 static inline int afb_event_x2_push(
68                         struct afb_event_x2 *event,
69                         struct json_object *object)
70 {
71         return event->itf->push(event, object);
72 }
73
74 /**
75  * Gets the name associated to 'event'.
76  *
77  * @param event the event whose name is requested
78  *
79  * @return the name of the event
80  *
81  * The returned name can be used until call to 'afb_event_x2_unref'.
82  * It shouldn't be freed.
83  */
84 static inline const char *afb_event_x2_name(struct afb_event_x2 *event)
85 {
86         return event->itf->name(event);
87 }
88
89 /**
90  * Decrease the count of references to 'event'.
91  * Call this function when the evenid is no more used.
92  * It destroys the event_x2 when the reference count falls to zero.
93  *
94  * @param event the event
95  */
96 static inline void afb_event_x2_unref(struct afb_event_x2 *event)
97 {
98         event->itf->unref(event);
99 }
100
101 /**
102  * Increases the count of references to 'event'
103  *
104  * @param event the event
105  *
106  * @return the event
107  */
108 static inline struct afb_event_x2 *afb_event_x2_addref(
109                                         struct afb_event_x2 *event)
110 {
111         return event->itf->addref(event);
112 }
113