1 #pragma once
2 
3 #include <mbgl/storage/response.hpp>
4 #include <mbgl/util/optional.hpp>
5 #include <mbgl/util/font_stack.hpp>
6 #include <mbgl/util/tileset.hpp>
7 #include <mbgl/util/util.hpp>
8 #include <mbgl/util/traits.hpp>
9 
10 #include <string>
11 
12 namespace mbgl {
13 
14 class Resource {
15 public:
16     enum Kind : uint8_t {
17         Unknown = 0,
18         Style,
19         Source,
20         Tile,
21         Glyphs,
22         SpriteImage,
23         SpriteJSON,
24         Image
25     };
26 
27     struct TileData {
28         std::string urlTemplate;
29         uint8_t pixelRatio;
30         int32_t x;
31         int32_t y;
32         int8_t z;
33     };
34 
35     enum class LoadingMethod : uint8_t {
36         None        = 0b00,
37         Cache       = 0b01,
38         Network     = 0b10,
39 
40         CacheOnly   = Cache,
41         NetworkOnly = Network,
42         All         = Cache | Network,
43     };
44 
Resource(Kind kind_,std::string url_,optional<TileData> tileData_={},LoadingMethod loadingMethod_=LoadingMethod::All)45     Resource(Kind kind_,
46              std::string url_,
47              optional<TileData> tileData_ = {},
48              LoadingMethod loadingMethod_ = LoadingMethod::All)
49         : kind(kind_),
50           loadingMethod(loadingMethod_),
51           url(std::move(url_)),
52           tileData(std::move(tileData_)) {
53     }
54 
55     bool hasLoadingMethod(LoadingMethod method);
56 
57     static Resource style(const std::string& url);
58     static Resource source(const std::string& url);
59     static Resource tile(const std::string& urlTemplate,
60                          float pixelRatio,
61                          int32_t x,
62                          int32_t y,
63                          int8_t z,
64                          Tileset::Scheme scheme,
65                          LoadingMethod = LoadingMethod::All);
66     static Resource glyphs(const std::string& urlTemplate,
67                            const FontStack& fontStack,
68                            const std::pair<uint16_t, uint16_t>& glyphRange);
69     static Resource spriteImage(const std::string& base, float pixelRatio);
70     static Resource spriteJSON(const std::string& base, float pixelRatio);
71     static Resource image(const std::string& url);
72 
73     Kind kind;
74     LoadingMethod loadingMethod;
75     std::string url;
76 
77     // Includes auxiliary data if this is a tile request.
78     optional<TileData> tileData;
79 
80     optional<Timestamp> priorModified = {};
81     optional<Timestamp> priorExpires = {};
82     optional<std::string> priorEtag = {};
83     std::shared_ptr<const std::string> priorData;
84 };
85 
86 
operator |(Resource::LoadingMethod a,Resource::LoadingMethod b)87 MBGL_CONSTEXPR Resource::LoadingMethod operator|(Resource::LoadingMethod a, Resource::LoadingMethod b) {
88     return Resource::LoadingMethod(mbgl::underlying_type(a) | mbgl::underlying_type(b));
89 }
90 
operator |=(Resource::LoadingMethod & a,Resource::LoadingMethod b)91 MBGL_CONSTEXPR Resource::LoadingMethod& operator|=(Resource::LoadingMethod& a, Resource::LoadingMethod b) {
92     return (a = a | b);
93 }
94 
operator &(Resource::LoadingMethod a,Resource::LoadingMethod b)95 MBGL_CONSTEXPR Resource::LoadingMethod operator&(Resource::LoadingMethod a, Resource::LoadingMethod b) {
96     return Resource::LoadingMethod(mbgl::underlying_type(a) & mbgl::underlying_type(b));
97 }
98 
hasLoadingMethod(Resource::LoadingMethod method)99 inline bool Resource::hasLoadingMethod(Resource::LoadingMethod method) {
100     return (loadingMethod & method) != Resource::LoadingMethod::None;
101 }
102 
103 } // namespace mbgl
104