-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedsmwrapper.h
84 lines (65 loc) · 3.02 KB
/
edsmwrapper.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#pragma once
#include <QString>
#include <QStringList>
#include <atomic>
#include <memory>
#include "edsmapiv1.h"
#include "utils/conditional_wait.h"
//this class solves some exact tasks, using caching, so do not stress edsm too much
class EDSMWrapper
{
public:
using callback_t = EdsmApiV1::callback_t;
using progress_update = std::function<bool(size_t, size_t)>; // 1st current value, 2nd total
EDSMWrapper() = delete;
static QString getNameFromJson(const nlohmann::json& js);
//does not block caller thread, callback executed in other thread scope (not the caller) if network request made
//or immediatly in caller thread if cache used
static void selectSystemsInRadius(const QString& center_name, int radius, callback_t callback);
//blocks caller thread until have return value, may return empty list
static QStringList selectSystemsInRadiusNamesOnly(const QString& center_name, int radius);
//blocks caller thread until all done
static std::vector<nlohmann::json> requestManySysInfo(const QStringList& names, const progress_update& progress = [](auto, auto)
{
return false;
});
static std::vector<nlohmann::json> requestManySysInfoInRadius(const QString& center_name, int radius, const progress_update& progress = [](auto, auto)
{
return false;
});
//does not block caller thread, callback executed in other thread scope (not the caller) if network request made
//or immediatly in caller thread if cache used
static void requestSysInfo(const QString& sys_name, callback_t callback);
//blocks caller thread
static nlohmann::json requestSysInfo(const QString& sys_name);
//blocks caller thread
static QString tooltipWithSysInfo(const QString& sys_name);
//does not block caller thread, callback executed in other thread scope (not the caller) if network request made
//or immediatly in caller thread if cache used
static void requestBodiesInfo(const QString& sys_name, callback_t callback);
//blocks caller thread
static nlohmann::json requestBodiesInfo(const QString& sys_name);
//blocks caller thread until all done
static std::vector<nlohmann::json> requestManyBodiesInfo(const QStringList& names, const progress_update& progress = [](auto, auto)
{
return false;
});
static std::vector<nlohmann::json> requestManyBodiesInfoInRadius(const QString& center_name, int radius, const progress_update& progress = [](auto, auto)
{
return false;
});
template<class Res>
static Res valueFromJson(const nlohmann::json& object, const std::string& fieldName)
{
const auto it = object.find(fieldName);
if (it == object.end())
throw std::runtime_error(stringfmt("JSON object does not have field '%s'", fieldName));
return it->get<Res>();
}
static EdsmApiV1& api()
{
static EdsmApiV1 edsm((int)std::max(1u, std::thread::hardware_concurrency() / 2u));
return edsm;
}
static QString getSystemUrl(const QString& systemName);
};