Closure templates

zenoh-c closure is a structure with 3 fields: context pointer context, callback call and finalizer drop. Context is a pointer to closure data, callback is a function pointer and finalizer is a function pointer which is called when closure is destroyed. Example of zenoh-c closure:

typedef struct z_owned_closure_query_t {
   void *context;
   void (*call)(const struct z_query_t*, void *context);
   void (*drop)(void*);
} z_owned_closure_query_t;

zenoh-cpp closures are wrappers around zenoh-c closures. These wrappers allows to construct closures from any callable C++ object, like lambda or function pointer.

All zenoh-cpp closures are based on the one of the following templates: zenohcxx::ClosureConstPtrParam and zenohcxx::ClosureMoveParam. First one accepts const ZCPP_PARAM&, second one accepts ZCPP_PARAM&&.

These templates allows to construct closures from any callable C++ object:

  • function pointer of type void (func*)(const ZCPP_PARAM&) or void (func*)(ZCPP_PARAM&&)

    Example:

    void on_query(const Query&) { ... }
    
    session.declare_queryable("foo/bar", on_query);
    
  • any object which can be called with corresponding parameter, e.g. lambda or custom object. If object is passed by move, closure will take ownership of it, otherwise it will store reference to it.

    Example:

    session.declare_queryable("foo/bar", [](const Query&) { ... });
    

    or

    struct OnQuery {
       void operator()(const Query&) { ... }
       ~OnQuery() { ... }
    };
    
    OnQuery on_query;
    session.declare_queryable("foo/bar", std::move(on_query));
    
template<typename ZC_CLOSURE_TYPE, typename ZC_PARAM, typename ZCPP_PARAM, typename std::enable_if_t<std::is_base_of_v<ZC_PARAM, ZCPP_PARAM> && sizeof(ZC_PARAM) == sizeof(ZCPP_PARAM), bool> = true>
class ClosureConstRefParam : public zenohcxx::Owned<ZC_CLOSURE_TYPE>

Base type for C++ wrappers of Zenoh closures with const pointer parameter.

Template Parameters:
  • ZC_CLOSURE_TYPE – - zenoh-c closure type ::z_owned_closure_XXX_t

  • ZC_PARAM – - zenoh-c parameter type which is passed to closure ::z_owned_XXX_t

  • ZCPP_PARAM – - zenoh-cpp parameter type which wraps zenoh-c parameter type (e.g. Reply for ::z_owned_reply_t)

Constructors

inline ClosureConstRefParam()

Construct uninitialized closure.

template<typename C>
inline ClosureConstRefParam(C &&on_call)

Construct closure from the data handler: any object with operator()(const ZCPP_PARAM&) defined.

Parameters:

on_call – data handler - any object with operator()(const ZCPP_PARAM&) defined

template<typename C, typename D>
inline ClosureConstRefParam(C &&on_call, D &&on_drop)

Construct closure from the data handler and the drop handler.

Drop handler is convenient when it’s necessary to catch dropping of the closure costructed from function pointer, object lvalue reference or lambda. If the closure holds the user’s object, the additional drop handler is probably excessive. The cleanup in this case may be done in the object’s destructor.

Parameters:

Methods

inline bool check() const

Check if closure is valid. Closure is valid if it can be called, i.e. call is not nullptr. drop operation is optional.

Returns:

true if closure is valid

inline ZC_RETVAL call(const ZC_PARAM *v)

Call closure with pointer to C parameter (::z_owned_XXX_t)

Parameters:

v – - pointer to C parameter

Returns:

value returned by closure

inline ZC_RETVAL operator()(const ZCPP_PARAM &v)

Call closure with const reference to C++ parameter (wrapper for ::z_owned_XXX_t)

Parameters:

v – - const reference to C++ parameter

template<typename ZC_CLOSURE_TYPE, typename ZC_PARAM, typename ZCPP_PARAM, typename std::enable_if_t<std::is_base_of_v<Owned<ZC_PARAM>, ZCPP_PARAM> && sizeof(ZC_PARAM) == sizeof(ZCPP_PARAM), bool> = true>
class ClosureMoveParam : public zenohcxx::Owned<ZC_CLOSURE_TYPE>

Base type for C++ wrappers of Zenoh closures with owned parameter.

Template Parameters:
  • ZC_CLOSURE_TYPE – - zenoh-c closure type ::z_owned_closure_XXX_t

  • ZC_PARAM – - zenoh-c parameter type which is passed to closure ::z_XXX_t

  • ZCPP_PARAM – - zenoh-cpp parameter type which wraps zenoh-c parameter type (e.g. Sample for ::z_sample_t)

Constructors

inline ClosureMoveParam()

Construct uninitialized closure.

template<typename T>
inline ClosureMoveParam(T &&on_call)

Construct closure from the data handler: any object with operator()(ZCPP_PARAM&) or operator()(ZCPP_PARAM&&) defined.

Parameters:

on_call – data handler - any object with operator()(ZCPP_PARAM&) or operator()(ZCPP_PARAM&&)

template<typename T, typename D>
inline ClosureMoveParam(T &&on_call, D &&on_drop)

Construct closure from the data handler and the drop handler.

Drop handler is convenient when it’s necessary to catch dropping of the closure costructed from function pointer, object lvalue reference or lambda. If the closure holds the user’s object, the additional drop handler is probably excessive. The cleanup in this case may be done in the object’s destructor.

Parameters:
inline bool check() const

Check if closure is valid. Closure is valid if it can be called, i.e. call is not nullptr. drop operation is optional.

Returns:

true if closure is valid

inline ZC_RETVAL call(ZC_PARAM *v)

Call closure with pointer to C parameter (::z_XXX_t)

Parameters:

v – - pointer to C parameter

Returns:

value returned by closure

inline ZC_RETVAL operator()(ZCPP_PARAM &&v)

Call closure with rvalue reference to C++ parameter.

Parameters:

v – - rvalue reference to C++ parameter

Returns:

value returned by closure

inline ZC_RETVAL operator()(ZCPP_PARAM &v)

Call closure with lvalue reference to C++ parameter.

Parameters:

v – - lvalue reference to C++ parameter

Returns:

value returned by closure