geopro/src/net/ApiBatch.hpp

30 lines
905 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#pragma once
#include <functional>
#include <QList>
#include <QPointer>
#include <QObject>
#include "IApiCall.hpp"
namespace geopro::net {
// 并发汇聚 N 个 IApiCall全成功 → succeeded(按下标对齐);任一失败 → fail-fast
// failed(index,resp) + abort 其余在飞 call。安全不变量见 spec §5.0。
class ApiBatch : public QObject {
Q_OBJECT
public:
using Predicate = std::function<bool(const ApiResponse&)>;
ApiBatch(QList<IApiCall*> calls, Predicate isFailure, QObject* parent = nullptr); // 接管 calls
void abort();
signals:
void succeeded(const QList<geopro::net::ApiResponse>& responses);
void failed(int index, const geopro::net::ApiResponse& resp);
private:
QList<QPointer<IApiCall>> calls_;
QList<ApiResponse> responses_;
Predicate isFailure_;
int remaining_ = 0;
bool aborted_ = false;
};
} // namespace geopro::net