c++: Reworked the event class
[src/app-framework-binder.git] / include / afb / c++ / binding-wrap.hpp
index f6f427b..6fdf0f6 100644 (file)
@@ -91,10 +91,13 @@ class event
 {
        afb_event_t event_;
 public:
-       event() { invalidate(); }
+       event();
        event(afb_event_t e);
        event(const event &other);
+       event(event &&other);
+       ~event();
        event &operator=(const event &other);
+       event &operator=(event &&other);
 
        operator afb_event_t() const;
        afb_event_t operator->() const;
@@ -102,8 +105,6 @@ public:
        operator bool() const;
        bool is_valid() const;
 
-       void invalidate();
-
        int broadcast(json_object *object) const;
        int push(json_object *object) const;
 
@@ -234,9 +235,13 @@ public:
 /*************************************************************************/
 
 /* events */
-inline event::event(afb_event_t e) : event_(e) { }
-inline event::event(const event &other) : event_(other.event_) { }
+inline event::event() : event_{nullptr} { }
+inline event::event(afb_event_t e) : event_{e} { }
+inline event::event(event &&other) : event_{other.event_} { other.event_ = nullptr; }
+inline event::event(const event &other) : event_{other.event_} { addref(); }
+inline event::~event() { unref(); }
 inline event &event::operator=(const event &other) { event_ = other.event_; return *this; }
+inline event &event::operator=(event &&other) { event_ = other.event_; other.event_ = nullptr; return *this;}
 
 inline event::operator afb_event_t() const { return event_; }
 inline afb_event_t event::operator->() const { return event_; }
@@ -244,12 +249,10 @@ inline afb_event_t event::operator->() const { return event_; }
 inline event::operator bool() const { return is_valid(); }
 inline bool event::is_valid() const { return afb_event_is_valid(event_); }
 
-inline void event::invalidate() { event_ = nullptr; }
-
 inline int event::broadcast(json_object *object) const { return afb_event_broadcast(event_, object); }
 inline int event::push(json_object *object) const { return afb_event_push(event_, object); }
 
-inline void event::unref() { afb_event_unref(event_); invalidate(); }
+inline void event::unref() { if (event_) { afb_event_unref(event_); } event_ = nullptr; }
 inline void event::addref() { afb_event_addref(event_); }
 inline const char *event::name() const { return afb_event_name(event_); }
 
@@ -460,7 +463,7 @@ inline void call(const char *api, const char *verb, struct json_object *args, vo
 template <class T>
 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)
 {
-       afb_service_call(api, verb, args, reinterpret_cast<void(*)(void*,int,json_object*,afb_api_t)>(callback), reinterpret_cast<void*>(closure));
+       afb_service_call(api, verb, args, reinterpret_cast<void(*)(void*,json_object*,const char*, const char*,afb_api_t)>(callback), reinterpret_cast<void*>(closure));
 }
 
 inline bool callsync(const char *api, const char *verb, struct json_object *args, struct json_object *&result, char *&error, char *&info)
@@ -571,6 +574,37 @@ constexpr afb_verb_t verb(
        return r;
 }
 
+void __attribute__((weak)) __afb__verb__cb__for__global__(afb_req_t r)
+{
+       void *vcbdata;
+       void (*callback)(req);
+
+       vcbdata = afb_req_get_vcbdata(r);
+       callback = reinterpret_cast<void(*)(req)>(vcbdata);
+       callback(req(r));
+}
+
+constexpr afb_verb_t verb(
+       const char *name,
+       void (&callback)(req),
+       const char *info = nullptr,
+       uint16_t session = 0,
+       const afb_auth *auth = nullptr,
+       bool glob = false,
+       void *vcbdata = nullptr
+)
+{
+       return verb(
+               name,
+               __afb__verb__cb__for__global__,
+               info,
+               session,
+               auth,
+               glob,
+               (void*)(&callback)
+       );
+}
+
 constexpr afb_verb_t verbend()
 {
        afb_verb_t r = verb(nullptr, nullptr);