Commit 6d6c3148 by Paweł Wegner

RecursiveRequest: added.

parent 9e3872bc
......@@ -42,7 +42,8 @@ libcloudstorage_la_SOURCES = \
Request/MoveItemRequest.cpp \
Request/RenameItemRequest.cpp \
Request/ExchangeCodeRequest.cpp \
Request/GetItemUrlRequest.cpp
Request/GetItemUrlRequest.cpp \
Request/RecursiveRequest.cpp
noinst_HEADERS = \
IAuth.h \
......@@ -76,7 +77,8 @@ noinst_HEADERS = \
Request/MoveItemRequest.h \
Request/RenameItemRequest.h \
Request/ExchangeCodeRequest.h \
Request/GetItemUrlRequest.h
Request/GetItemUrlRequest.h \
Request/RecursiveRequest.h
libcloudstorage_la_HEADERS = \
IItem.h \
......
/*****************************************************************************
* RecursiveRequest.cpp
*
*****************************************************************************
* Copyright (C) 2016-2016 VideoLAN
*
* Authors: Paweł Wegner <pawel.wegner95@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser 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.
*****************************************************************************/
#include "RecursiveRequest.h"
#include "CloudProvider/CloudProvider.h"
namespace cloudstorage {
template <class T>
RecursiveRequest<T>::RecursiveRequest(std::shared_ptr<CloudProvider> p,
IItem::Pointer item,
CompleteCallback callback,
Visitor visitor)
: Request<T>(p, callback, [=](typename Request<T>::Pointer r) {
visit(r, item, [=](const T& e) { r->done(e); }, visitor);
}) {}
template <class T>
void RecursiveRequest<T>::visit(typename Request<T>::Pointer r,
IItem::Pointer item, CompleteCallback callback,
Visitor visitor) {
if (item->type() != IItem::FileType::Directory)
visitor(r, item, callback);
else
r->subrequest(r->provider()->listDirectoryAsync(
item, [=](EitherError<std::vector<IItem::Pointer>> lst) {
if (lst.left()) return callback(lst.left());
visit(r, *lst.right(),
[=](T e) {
if (e.left()) return callback(e);
visitor(r, item, callback);
},
visitor);
}));
}
template <class T>
void RecursiveRequest<T>::visit(typename Request<T>::Pointer r,
std::vector<IItem::Pointer> lst,
CompleteCallback callback, Visitor visitor) {
if (lst.empty()) return callback(T());
auto i = lst.back();
lst.pop_back();
visit(r, i,
[=](const T& e) {
if (e.left()) return callback(e.left());
visit(r, lst, callback, visitor);
},
visitor);
}
template class RecursiveRequest<EitherError<void>>;
template class RecursiveRequest<EitherError<IItem>>;
} // namespace cloudstorage
/*****************************************************************************
* RecursiveRequest.h
*
*****************************************************************************
* Copyright (C) 2016-2016 VideoLAN
*
* Authors: Paweł Wegner <pawel.wegner95@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser 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 RECURSIVEREQUEST_H
#define RECURSIVEREQUEST_H
#include "Request.h"
namespace cloudstorage {
template <class ReturnValue>
class RecursiveRequest : public Request<ReturnValue> {
public:
using CompleteCallback = std::function<void(ReturnValue)>;
using Visitor = std::function<void(typename Request<ReturnValue>::Pointer,
IItem::Pointer, CompleteCallback)>;
RecursiveRequest(std::shared_ptr<CloudProvider>, IItem::Pointer item,
CompleteCallback, Visitor);
private:
static void visit(typename Request<ReturnValue>::Pointer, IItem::Pointer item,
CompleteCallback, Visitor);
static void visit(typename Request<ReturnValue>::Pointer,
std::vector<IItem::Pointer> lst, CompleteCallback, Visitor);
};
} // namespace cloudstorage
#endif // RECURSIVEREQUEST_H
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