class Closure
Defined at line 78 of file ../../third_party/protobuf/src/google/protobuf/stubs/callback.h
Abstract interface for a callback. When calling an RPC, you must provide
a Closure to call when the procedure completes. See the Service interface
in service.h.
To automatically construct a Closure which calls a particular function or
method with a particular set of parameters, use the NewCallback() function.
Example:
void FooDone(const FooResponse* response) {
...
}
void CallFoo() {
...
// When done, call FooDone() and pass it a pointer to the response.
Closure* callback = NewCallback(
&FooDone
, response);
// Make the call.
service->Foo(controller, request, response, callback);
}
Example that calls a method:
class Handler {
public:
...
void FooDone(const FooResponse* response) {
...
}
void CallFoo() {
...
// When done, call FooDone() and pass it a pointer to the response.
Closure* callback = NewCallback(this,
&Handler
::FooDone, response);
// Make the call.
service->Foo(controller, request, response, callback);
}
};
Currently NewCallback() supports binding zero, one, or two arguments.
Callbacks created with NewCallback() automatically delete themselves when
executed. They should be used when a callback is to be called exactly
once (usually the case with RPC callbacks). If a callback may be called
a different number of times (including zero), create it with
NewPermanentCallback() instead. You are then responsible for deleting the
callback (using the "delete" keyword as normal).
Note that NewCallback() is a bit touchy regarding argument types. Generally,
the values you provide for the parameter bindings must exactly match the
types accepted by the callback function. For example:
void Foo(std::string s);
NewCallback(
&Foo
, "foo"); // WON'T WORK: const char* != string
NewCallback(
&Foo
, std::string("foo")); // WORKS
Also note that the arguments cannot be references:
void Foo(const std::string
&
s);
std::string my_str;
NewCallback(
&Foo
, my_str); // WON'T WORK: Can't use references.
However, correctly-typed pointers will work just fine.
Public Methods
void Closure ()
Defined at line 80 of file ../../third_party/protobuf/src/google/protobuf/stubs/callback.h
void Closure (const Closure & )
Defined at line 81 of file ../../third_party/protobuf/src/google/protobuf/stubs/callback.h
Closure & operator= (const Closure & )
Defined at line 82 of file ../../third_party/protobuf/src/google/protobuf/stubs/callback.h
void ~Closure ()
void Run ()