CLOUD: Add object-oriented Callbacks
These callbacks can call object's methods, not some global C functions. DropboxStorage::info2() and DropboxStorage::infoMethodCallback() demonstrate the idea.
This commit is contained in:
parent
17eb5f9143
commit
ca456a7241
6 changed files with 95 additions and 7 deletions
|
@ -92,6 +92,30 @@ void infoCallback(Networking::Request* request, void *jsonPointer) {
|
|||
}
|
||||
}
|
||||
|
||||
void info2Callback(Networking::Request* request, void *jsonPointer) {
|
||||
if (!request) {
|
||||
warning("infoCallback: got NULL instead of Request");
|
||||
|
||||
Common::JSONValue *json = (Common::JSONValue *)jsonPointer;
|
||||
if (json) delete json; //yeah I know we can delete NULL safely
|
||||
return;
|
||||
}
|
||||
|
||||
Common::BaseCallback *callback = (Common::BaseCallback *)request->pointer();
|
||||
|
||||
Common::JSONValue *json = (Common::JSONValue *)jsonPointer;
|
||||
if (json) {
|
||||
//Common::JSONObject result = json->asObject();
|
||||
if (callback) {
|
||||
(*callback)(new StorageInfo(json->stringify()));
|
||||
delete callback;
|
||||
}
|
||||
delete json;
|
||||
} else {
|
||||
warning("infoCallback: got NULL instead of JSON!");
|
||||
}
|
||||
}
|
||||
|
||||
DropboxStorage::DropboxStorage(Common::String accessToken, Common::String userId): _token(accessToken), _uid(userId) {
|
||||
curl_global_init(CURL_GLOBAL_ALL);
|
||||
}
|
||||
|
@ -104,9 +128,14 @@ void syncSavesInfoCallback(StorageInfo info) {
|
|||
debug("info: %s", info.info().c_str());
|
||||
}
|
||||
|
||||
void DropboxStorage::infoMethodCallback(void *storageInfo) {
|
||||
StorageInfo *info = (StorageInfo *)storageInfo;
|
||||
debug("info: %s", info->info().c_str());
|
||||
}
|
||||
|
||||
void DropboxStorage::syncSaves(OperationCallback callback) {
|
||||
//this is not the real syncSaves() implementation
|
||||
info(syncSavesInfoCallback);
|
||||
info2(new Common::Callback<DropboxStorage>(this, &DropboxStorage::infoMethodCallback));
|
||||
}
|
||||
|
||||
void DropboxStorage::info(InfoCallback callback) {
|
||||
|
@ -117,6 +146,14 @@ void DropboxStorage::info(InfoCallback callback) {
|
|||
request->setPointer(callback);
|
||||
}
|
||||
|
||||
void DropboxStorage::info2(Common::Callback<DropboxStorage> *callback) {
|
||||
Networking::CurlJsonRequest *request = new Networking::CurlJsonRequest(info2Callback, "https://api.dropboxapi.com/1/account/info");
|
||||
request->addHeader("Authorization: Bearer " + _token);
|
||||
ConnMan.addRequest(request);
|
||||
|
||||
request->setPointer(callback);
|
||||
}
|
||||
|
||||
DropboxStorage *DropboxStorage::loadFromConfig() {
|
||||
KEY = ConfMan.get("DROPBOX_KEY", "cloud");
|
||||
SECRET = ConfMan.get("DROPBOX_SECRET", "cloud");
|
||||
|
|
|
@ -25,6 +25,7 @@
|
|||
|
||||
#include "backends/cloud/storage.h"
|
||||
#include "backends/cloud/manager.h"
|
||||
#include "common/callback.h"
|
||||
|
||||
namespace Cloud {
|
||||
namespace Dropbox {
|
||||
|
@ -39,6 +40,8 @@ class DropboxStorage: public Cloud::Storage {
|
|||
|
||||
static void getAccessToken(Common::String code);
|
||||
|
||||
void infoMethodCallback(void *serviceInfoPtr);
|
||||
|
||||
public:
|
||||
virtual ~DropboxStorage();
|
||||
|
||||
|
@ -65,6 +68,7 @@ public:
|
|||
|
||||
/** Returns pointer to the ServiceInfo struct. */
|
||||
virtual void info(InfoCallback callback);
|
||||
void info2(Common::Callback<DropboxStorage> *callback);
|
||||
|
||||
/** Returns whether saves sync process is running. */
|
||||
virtual bool isSyncing() { return false; } //TODO
|
||||
|
|
|
@ -31,7 +31,7 @@
|
|||
|
||||
namespace Networking {
|
||||
|
||||
CurlJsonRequest::CurlJsonRequest(Callback cb, const char *url) : Request(cb), _stream(0), _headersList(0), _contentsStream(DisposeAfterUse::YES) {
|
||||
CurlJsonRequest::CurlJsonRequest(SimpleCallback cb, const char *url) : Request(cb), _stream(0), _headersList(0), _contentsStream(DisposeAfterUse::YES) {
|
||||
_url = url;
|
||||
}
|
||||
|
||||
|
|
|
@ -43,7 +43,7 @@ class CurlJsonRequest : public Request {
|
|||
char *getPreparedContents();
|
||||
|
||||
public:
|
||||
CurlJsonRequest(Callback cb, const char *url);
|
||||
CurlJsonRequest(SimpleCallback cb, const char *url);
|
||||
virtual ~CurlJsonRequest();
|
||||
|
||||
virtual bool handle();
|
||||
|
|
|
@ -27,14 +27,14 @@ namespace Networking {
|
|||
|
||||
class Request {
|
||||
protected:
|
||||
typedef void(*Callback)(Request* request, void *result);
|
||||
typedef void(*SimpleCallback)(Request* request, void *result);
|
||||
|
||||
/**
|
||||
* Callback, which should be called before Request returns true in handle().
|
||||
* That's the way Requests pass the result to the code which asked to create this request.
|
||||
*/
|
||||
|
||||
Callback _callback;
|
||||
SimpleCallback _callback;
|
||||
|
||||
/**
|
||||
* Pointer, which could be set by Request creating code. It might be accessed
|
||||
|
@ -44,7 +44,7 @@ protected:
|
|||
void *_pointer;
|
||||
|
||||
public:
|
||||
Request(Callback cb): _callback(cb) {};
|
||||
Request(SimpleCallback cb): _callback(cb) {};
|
||||
virtual ~Request() {};
|
||||
|
||||
/**
|
||||
|
|
47
common/callback.h
Normal file
47
common/callback.h
Normal file
|
@ -0,0 +1,47 @@
|
|||
/* ScummVM - Graphic Adventure Engine
|
||||
*
|
||||
* ScummVM is the legal property of its developers, whose names
|
||||
* are too numerous to list here. Please refer to the COPYRIGHT
|
||||
* file distributed with this source distribution.
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef COMMON_CALLBACK_H
|
||||
#define COMMON_CALLBACK_H
|
||||
|
||||
namespace Common {
|
||||
|
||||
class BaseCallback {
|
||||
public:
|
||||
BaseCallback() {}
|
||||
virtual ~BaseCallback() {}
|
||||
virtual void operator()(void *ptr) = 0;
|
||||
};
|
||||
|
||||
template<class T> class Callback: public BaseCallback {
|
||||
typedef void(T::*TMethod)(void *);
|
||||
T *_object;
|
||||
TMethod _method;
|
||||
public:
|
||||
Callback(T *object, TMethod method): _object(object), _method(method) {}
|
||||
virtual ~Callback() {}
|
||||
void operator()(void *ptr) { (_object->*_method)(ptr); }
|
||||
};
|
||||
|
||||
} // End of namespace Common
|
||||
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue