Commit b686c620 authored by Paweł Wegner's avatar Paweł Wegner

IRequest: don't use std::function as callback classes.

parent 86c03fa5
......@@ -476,10 +476,11 @@ CloudProvider::listDirectoryPageAsync(IItem::Pointer directory,
->run();
}
ICloudProvider::ListDirectoryRequest::Pointer CloudProvider::listDirectoryAsync(
IItem::Pointer item, ListDirectoryCallback callback) {
ICloudProvider::ListDirectoryRequest::Pointer
CloudProvider::listDirectorySimpleAsync(IItem::Pointer item,
ListDirectoryCallback callback) {
return listDirectoryAsync(
item, util::make_unique<::ListDirectoryCallback>(callback));
item, std::make_shared<::ListDirectoryCallback>(callback));
}
ICloudProvider::DownloadFileRequest::Pointer CloudProvider::downloadFileAsync(
......
......@@ -94,7 +94,7 @@ class CloudProvider : public ICloudProvider,
RenameItemCallback) override;
ListDirectoryPageRequest::Pointer listDirectoryPageAsync(
IItem::Pointer, const std::string&, ListDirectoryPageCallback) override;
ListDirectoryRequest::Pointer listDirectoryAsync(
ListDirectoryRequest::Pointer listDirectorySimpleAsync(
IItem::Pointer item, ListDirectoryCallback callback) override;
DownloadFileRequest::Pointer downloadFileAsync(IItem::Pointer item,
const std::string& filename,
......
......@@ -183,9 +183,9 @@ ICloudProvider::UploadFileRequest::Pointer GoogleDrive::uploadFileAsync(
IItem::Pointer directory, const std::string& filename,
IUploadFileCallback::Pointer cb) {
auto resolve = [=](Request<EitherError<IItem>>::Pointer r) {
r->subrequest(listDirectoryAsync(directory, [=](EitherError<
std::vector<IItem::Pointer>>
e) {
r->subrequest(listDirectorySimpleAsync(directory, [=](EitherError<
IItem::List>
e) {
if (e.left()) return r->done(e.left());
IItem::Pointer item = nullptr;
int cnt = 0;
......
......@@ -410,10 +410,9 @@ class ICloudProvider {
* @param callback called when the request is finished
* @return object representing the pending request
*/
virtual ListDirectoryRequest::Pointer listDirectoryAsync(
virtual ListDirectoryRequest::Pointer listDirectorySimpleAsync(
IItem::Pointer item,
ListDirectoryCallback callback =
[](EitherError<std::vector<IItem::Pointer>>) {}) = 0;
ListDirectoryCallback callback = [](EitherError<IItem::List>) {}) = 0;
/**
* Simplified version of downloadFileAsync.
......
......@@ -212,20 +212,53 @@ class Either<Left, void> {
std::shared_ptr<Left> left_;
};
using ExchangeCodeCallback = std::function<void(EitherError<Token>)>;
using GetItemUrlCallback = std::function<void(EitherError<std::string>)>;
using GetItemCallback = std::function<void(EitherError<IItem>)>;
using GetItemDataCallback = std::function<void(EitherError<IItem>)>;
using DeleteItemCallback = std::function<void(EitherError<void>)>;
using CreateDirectoryCallback = std::function<void(EitherError<IItem>)>;
using MoveItemCallback = std::function<void(EitherError<IItem>)>;
using RenameItemCallback = std::function<void(EitherError<IItem>)>;
using ListDirectoryPageCallback = std::function<void(EitherError<PageData>)>;
using ListDirectoryCallback = std::function<void(EitherError<IItem::List>)>;
using DownloadFileCallback = std::function<void(EitherError<void>)>;
using UploadFileCallback = std::function<void(EitherError<IItem>)>;
using GetThumbnailCallback = std::function<void(EitherError<void>)>;
using GeneralDataCallback = std::function<void(EitherError<GeneralData>)>;
template <class... Arguments>
class GenericCallback {
public:
GenericCallback() {}
GenericCallback(const GenericCallback& d) : functor_(d.functor_) {}
template <class Function>
GenericCallback(const Function& callback)
: functor_(std::make_shared<Functor>(callback)) {}
GenericCallback(typename IGenericCallback<Arguments...>::Pointer functor)
: functor_(functor) {}
operator bool() const { return functor_; }
void operator()(Arguments... d) const { functor_->done(d...); }
private:
class Functor : public IGenericCallback<Arguments...> {
public:
Functor(const std::function<void(Arguments...)> callback)
: callback_(callback) {}
void done(Arguments... args) override { callback_(args...); }
private:
std::function<void(Arguments...)> callback_;
};
typename IGenericCallback<Arguments...>::Pointer functor_;
};
using ExchangeCodeCallback = GenericCallback<EitherError<Token>>;
using GetItemUrlCallback = GenericCallback<EitherError<std::string>>;
using GetItemCallback = GenericCallback<EitherError<IItem>>;
using GetItemDataCallback = GenericCallback<EitherError<IItem>>;
using DeleteItemCallback = GenericCallback<EitherError<void>>;
using CreateDirectoryCallback = GenericCallback<EitherError<IItem>>;
using MoveItemCallback = GenericCallback<EitherError<IItem>>;
using RenameItemCallback = GenericCallback<EitherError<IItem>>;
using ListDirectoryPageCallback = GenericCallback<EitherError<PageData>>;
using ListDirectoryCallback = GenericCallback<EitherError<IItem::List>>;
using DownloadFileCallback = GenericCallback<EitherError<void>>;
using UploadFileCallback = GenericCallback<EitherError<IItem>>;
using GetThumbnailCallback = GenericCallback<EitherError<void>>;
using GeneralDataCallback = GenericCallback<EitherError<GeneralData>>;
} // namespace cloudstorage
......
......@@ -58,8 +58,8 @@ void GetItemRequest::work(IItem::Pointer item, std::string p,
? ""
: std::string(path.begin() + it, path.end());
auto request = this->shared_from_this();
subrequest(provider()->listDirectoryAsync(
item, [=](EitherError<std::vector<IItem::Pointer>> e) {
subrequest(provider()->listDirectorySimpleAsync(
item, [=](EitherError<IItem::List> e) {
if (e.left())
request->done(e.left());
else
......
......@@ -42,8 +42,8 @@ void RecursiveRequest<T>::visit(typename Request<T>::Pointer r,
if (item->type() != IItem::FileType::Directory)
visitor(r, item, callback);
else
r->subrequest(r->provider()->listDirectoryAsync(
item, [=](EitherError<std::vector<IItem::Pointer>> lst) {
r->subrequest(r->provider()->listDirectorySimpleAsync(
item, [=](EitherError<IItem::List> lst) {
if (lst.left()) return callback(lst.left());
visit(r, *lst.right(),
[=](T e) {
......
......@@ -138,9 +138,9 @@ class CloudProviderWrapper : public ICloudProvider {
return p_->listDirectoryPageAsync(directory, token, cb);
}
ListDirectoryRequest::Pointer listDirectoryAsync(
ListDirectoryRequest::Pointer listDirectorySimpleAsync(
IItem::Pointer item, ListDirectoryCallback callback) override {
return p_->listDirectoryAsync(item, callback);
return p_->listDirectorySimpleAsync(item, callback);
}
DownloadFileRequest::Pointer downloadFileAsync(
......
......@@ -648,7 +648,7 @@ void FileSystem::list_directory_async(std::shared_ptr<ICloudProvider> p,
IItem::Pointer i,
cloudstorage::ListDirectoryCallback cb) {
if (!p || !i) return cb(Error{IHttpRequest::ServiceUnavailable, ""});
add({p, p->listDirectoryAsync(i, cb)});
add({p, p->listDirectorySimpleAsync(i, cb)});
}
void FileSystem::download_item_async(std::shared_ptr<ICloudProvider> p,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment