From 17eb5f91433f2414dc73f89abfdd316407259b61 Mon Sep 17 00:00:00 2001 From: Alexander Tkachev Date: Sat, 21 May 2016 00:44:09 +0600 Subject: CLOUD: Add complex callbacks Originally, I intended to add Storage API, StorageFile and StorageInfo stubs. When I tried to implement a simple info() call, I ended up fixing Request to contain some pointer field and all callbacks to have Request* parameter. And, now I have to place callback pointer into Request. which calls another callback. And, eventually, these "simple" callbacks would again require another pointer (to some caller class). --- backends/networking/curl/curljsonrequest.cpp | 2 +- backends/networking/curl/request.h | 12 +++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) (limited to 'backends/networking') diff --git a/backends/networking/curl/curljsonrequest.cpp b/backends/networking/curl/curljsonrequest.cpp index 59bc830692..929723c671 100644 --- a/backends/networking/curl/curljsonrequest.cpp +++ b/backends/networking/curl/curljsonrequest.cpp @@ -75,7 +75,7 @@ bool CurlJsonRequest::handle() { if (_callback) { char *contents = getPreparedContents(); Common::JSONValue *json = Common::JSON::parse(contents); - _callback(json); //potential memory leak, free it in your callbacks! + _callback(this, json); //potential memory leak, free it in your callbacks! } return true; } diff --git a/backends/networking/curl/request.h b/backends/networking/curl/request.h index 4f901f7c94..d405ec8551 100644 --- a/backends/networking/curl/request.h +++ b/backends/networking/curl/request.h @@ -27,7 +27,7 @@ namespace Networking { class Request { protected: - typedef void(*Callback)(void *result); + typedef void(*Callback)(Request* request, void *result); /** * Callback, which should be called before Request returns true in handle(). @@ -36,6 +36,13 @@ protected: Callback _callback; + /** + * Pointer, which could be set by Request creating code. It might be accessed + * from this Request when callback is called, for example. + */ + + void *_pointer; + public: Request(Callback cb): _callback(cb) {}; virtual ~Request() {}; @@ -47,6 +54,9 @@ public: */ virtual bool handle() = 0; + + void setPointer(void *ptr) { _pointer = ptr; } + void *pointer() const { return _pointer; } }; } //end of namespace Cloud -- cgit v1.2.3