X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=include%2Fafb%2Fc%2B%2B%2Fbinding-wrap.hpp;h=6af3f8455f5ce02938a957634d3e2848d40829cd;hb=6401efb14339925e696a65eea8e8548f1fddf3ba;hp=ae0e5eeb786984a3fb69badcf67eea24806b0c61;hpb=3f367d8ef3409d3f671816d1a8e4c1504838ad7e;p=src%2Fapp-framework-binder.git diff --git a/include/afb/c++/binding-wrap.hpp b/include/afb/c++/binding-wrap.hpp index ae0e5eeb..6af3f845 100644 --- a/include/afb/c++/binding-wrap.hpp +++ b/include/afb/c++/binding-wrap.hpp @@ -258,8 +258,6 @@ public: void failf(const char *error, const char *info, ...) const; void failv(const char *error, const char *info, va_list args) const; - template < class T > T *context() const; - void addref() const; void unref() const; @@ -294,6 +292,54 @@ public: int get_uid() const; json_object *get_client_info() const; + + template < class T = void > + class contextclass { + + friend class req; + afb_req_t req_; + contextclass(afb_req_t r) : req_(r) {} + + public: + inline operator T *() const { return get(); } + inline operator T &() const { return *get(); } + inline T* get() const { + return reinterpret_cast( + afb_req_context(req_, 0, + nullptr, + nullptr, + nullptr)); + } + + inline void set(T *value, void (*destroyer)(T*) = [](T*t){delete t;}) const { + afb_req_context(req_, 1, + nullptr, + reinterpret_cast(destroyer), + reinterpret_cast(value)); + } + + inline void unset() { set(nullptr); } + inline void clear() { set(nullptr); } + + inline T *lazy(T *(*allocator)() = []()->T*{return new T();}, void (*destroyer)(T*) = [](T*t){delete t;}) const { + return reinterpret_cast( + afb_req_context(req_, 0, + [allocator](void*)->T*{return allocator();}, + reinterpret_cast(destroyer), + nullptr)); + } + + template + inline T *lazy(I *i, T *(*allocator)(I*) = [](I*i)->T*{return new T(i);}, void (*destroyer)(T*) = [](T*t){delete t;}) const { + return reinterpret_cast( + afb_req_context(req_, 0, + [allocator](void*i)->T*{return allocator(reinterpret_cast(i));}, + reinterpret_cast(destroyer), + reinterpret_cast(i))); + } + }; + + template < class T > contextclass context() const { return contextclass(req_); } }; /*************************************************************************/ @@ -476,16 +522,6 @@ inline void req::failf(const char *error, const char *info, ...) const va_end(args); } -template < class T > -inline T *req::context() const -{ - T* (*creater)(void*) = [](){return new T();}; - void (*freer)(T*) = [](T*t){delete t;}; - return reinterpret_cast(afb_req_context(req_, 0, - reinterpret_cast(creater), - reinterpret_cast(freer), nullptr)); -} - inline void req::addref() const { afb_req_addref(req_); } inline void req::unref() const { afb_req_unref(req_); } @@ -631,28 +667,28 @@ inline bool callsync(const char *api, const char *verb, struct json_object *args constexpr afb_auth auth_no() { - afb_auth r = { afb_auth_No, 0, 0}; + afb_auth r = { afb_auth_No, {0}, nullptr}; r.type = afb_auth_No; return r; } constexpr afb_auth auth_yes() { - afb_auth r = { afb_auth_No, 0, 0}; + afb_auth r = { afb_auth_No, {0}, nullptr}; r.type = afb_auth_Yes; return r; } constexpr afb_auth auth_token() { - afb_auth r = { afb_auth_No, 0, 0}; + afb_auth r = { afb_auth_No, {0}, nullptr}; r.type = afb_auth_Token; return r; } constexpr afb_auth auth_LOA(unsigned loa) { - afb_auth r = { afb_auth_No, 0, 0}; + afb_auth r = { afb_auth_No, {0}, nullptr}; r.type = afb_auth_LOA; r.loa = loa; return r; @@ -660,7 +696,7 @@ constexpr afb_auth auth_LOA(unsigned loa) constexpr afb_auth auth_permission(const char *permission) { - afb_auth r = { afb_auth_No, 0, 0}; + afb_auth r = { afb_auth_No, {0}, nullptr}; r.type = afb_auth_Permission; r.text = permission; return r; @@ -668,7 +704,7 @@ constexpr afb_auth auth_permission(const char *permission) constexpr afb_auth auth_not(const afb_auth *other) { - afb_auth r = { afb_auth_No, 0, 0}; + afb_auth r = { afb_auth_No, {0}, nullptr}; r.type = afb_auth_Not; r.first = other; return r; @@ -681,7 +717,7 @@ constexpr afb_auth auth_not(const afb_auth &other) constexpr afb_auth auth_or(const afb_auth *first, const afb_auth *next) { - afb_auth r = { afb_auth_No, 0, 0}; + afb_auth r = { afb_auth_No, {0}, nullptr}; r.type = afb_auth_Or; r.first = first; r.next = next; @@ -695,7 +731,7 @@ constexpr afb_auth auth_or(const afb_auth &first, const afb_auth &next) constexpr afb_auth auth_and(const afb_auth *first, const afb_auth *next) { - afb_auth r = { afb_auth_No, 0, 0}; + afb_auth r = { afb_auth_No, {0}, nullptr}; r.type = afb_auth_And; r.first = first; r.next = next; @@ -723,7 +759,7 @@ constexpr afb_verb_t verb( r.info = info; r.session = session; r.auth = auth; - r.glob = (unsigned)glob; + r.glob = (uint16_t)glob; r.vcbdata = vcbdata; return r; }