[core] move SourceInfo to its own file

This commit is contained in:
Konstantin Käfer
2015-12-08 16:32:15 -08:00
parent 65b3220368
commit 2b9f20461a
4 changed files with 158 additions and 116 deletions

View File

@@ -33,93 +33,6 @@
namespace mbgl {
void parse(const rapidjson::Value& value, std::vector<std::string>& target, const char *name) {
if (!value.HasMember(name))
return;
const rapidjson::Value& property = value[name];
if (!property.IsArray())
return;
for (rapidjson::SizeType i = 0; i < property.Size(); i++)
if (!property[i].IsString())
return;
for (rapidjson::SizeType i = 0; i < property.Size(); i++)
target.emplace_back(std::string(property[i].GetString(), property[i].GetStringLength()));
}
void parse(const rapidjson::Value& value, std::string& target, const char* name) {
if (!value.HasMember(name))
return;
const rapidjson::Value& property = value[name];
if (!property.IsString())
return;
target = { property.GetString(), property.GetStringLength() };
}
void parse(const rapidjson::Value& value, uint16_t& target, const char* name) {
if (!value.HasMember(name))
return;
const rapidjson::Value& property = value[name];
if (!property.IsUint())
return;
unsigned int uint = property.GetUint();
if (uint > std::numeric_limits<uint16_t>::max())
return;
target = uint;
}
template <size_t N>
void parse(const rapidjson::Value& value, std::array<float, N>& target, const char* name) {
if (!value.HasMember(name))
return;
const rapidjson::Value& property = value[name];
if (!property.IsArray() || property.Size() != N)
return;
for (rapidjson::SizeType i = 0; i < property.Size(); i++)
if (!property[i].IsNumber())
return;
for (rapidjson::SizeType i = 0; i < property.Size(); i++)
target[i] = property[i].GetDouble();
}
void SourceInfo::parseTileJSONProperties(const rapidjson::Value& value) {
parse(value, tiles, "tiles");
parse(value, min_zoom, "minzoom");
parse(value, max_zoom, "maxzoom");
parse(value, attribution, "attribution");
parse(value, center, "center");
parse(value, bounds, "bounds");
}
std::string SourceInfo::tileURL(const TileID& id, float pixelRatio) const {
std::string result = tiles.at(0);
result = util::mapbox::normalizeTileURL(result, url, type);
result = util::replaceTokens(result, [&](const std::string &token) -> std::string {
if (token == "z") return util::toString(std::min(id.z, static_cast<int8_t>(max_zoom)));
if (token == "x") return util::toString(id.x);
if (token == "y") return util::toString(id.y);
if (token == "prefix") {
std::string prefix { 2 };
prefix[0] = "0123456789abcdef"[id.x % 16];
prefix[1] = "0123456789abcdef"[id.y % 16];
return prefix;
}
if (token == "ratio") return pixelRatio > 1.0 ? "@2x" : "";
return "";
});
return result;
}
Source::Source() {}
Source::~Source() = default;

View File

@@ -1,24 +1,13 @@
#ifndef MBGL_MAP_SOURCE
#define MBGL_MAP_SOURCE
#include <mbgl/map/tile_id.hpp>
#include <mbgl/map/tile_data.hpp>
#include <mbgl/map/tile_cache.hpp>
#include <mbgl/style/types.hpp>
#include <mbgl/map/source_info.hpp>
#include <mbgl/util/noncopyable.hpp>
#include <mbgl/util/mat4.hpp>
#include <mbgl/util/ptr.hpp>
#include <mbgl/util/chrono.hpp>
#include <mbgl/util/constants.hpp>
#include <rapidjson/document.h>
#include <cstdint>
#include <forward_list>
#include <iosfwd>
#include <map>
#include <unordered_set>
namespace mbgl {
@@ -30,23 +19,6 @@ class Tile;
struct ClipID;
struct box;
class SourceInfo : private util::noncopyable {
public:
SourceType type = SourceType::Vector;
std::string url;
std::vector<std::string> tiles;
uint16_t tile_size = util::tileSize;
uint16_t min_zoom = 0;
uint16_t max_zoom = 22;
std::string attribution;
std::array<float, 3> center = {{0, 0, 0}};
std::array<float, 4> bounds = {{-180, -90, 180, 90}};
std::string source_id = "";
void parseTileJSONProperties(const rapidjson::Value&);
std::string tileURL(const TileID& id, float pixelRatio) const;
};
class Source : private util::noncopyable {
public:
class Observer {

View File

@@ -0,0 +1,119 @@
#include <mbgl/map/source_info.hpp>
#include <mbgl/util/mapbox.hpp>
#include <mbgl/util/string.hpp>
#include <mbgl/util/token.hpp>
namespace mbgl {
namespace {
void parse(const rapidjson::Value& value, std::vector<std::string>& target, const char* name) {
if (!value.HasMember(name)) {
return;
}
const rapidjson::Value& property = value[name];
if (!property.IsArray()) {
return;
}
for (rapidjson::SizeType i = 0; i < property.Size(); i++) {
if (!property[i].IsString()) {
return;
}
}
for (rapidjson::SizeType i = 0; i < property.Size(); i++) {
target.emplace_back(std::string(property[i].GetString(), property[i].GetStringLength()));
}
}
void parse(const rapidjson::Value& value, std::string& target, const char* name) {
if (!value.HasMember(name)) {
return;
}
const rapidjson::Value& property = value[name];
if (!property.IsString()) {
return;
}
target = { property.GetString(), property.GetStringLength() };
}
void parse(const rapidjson::Value& value, uint16_t& target, const char* name) {
if (!value.HasMember(name)) {
return;
}
const rapidjson::Value& property = value[name];
if (!property.IsUint()) {
return;
}
unsigned int uint = property.GetUint();
if (uint > std::numeric_limits<uint16_t>::max()) {
return;
}
target = uint;
}
template <size_t N>
void parse(const rapidjson::Value& value, std::array<float, N>& target, const char* name) {
if (!value.HasMember(name)) {
return;
}
const rapidjson::Value& property = value[name];
if (!property.IsArray() || property.Size() != N) {
return;
}
for (rapidjson::SizeType i = 0; i < property.Size(); i++) {
if (!property[i].IsNumber()) {
return;
}
}
for (rapidjson::SizeType i = 0; i < property.Size(); i++) {
target[i] = property[i].GetDouble();
}
}
} // end namespace
void SourceInfo::parseTileJSONProperties(const rapidjson::Value& value) {
parse(value, tiles, "tiles");
parse(value, min_zoom, "minzoom");
parse(value, max_zoom, "maxzoom");
parse(value, attribution, "attribution");
parse(value, center, "center");
parse(value, bounds, "bounds");
}
std::string SourceInfo::tileURL(const TileID& id, float pixelRatio) const {
std::string result = tiles.at(0);
result = util::mapbox::normalizeTileURL(result, url, type);
result = util::replaceTokens(result, [&](const std::string& token) -> std::string {
if (token == "z") {
return util::toString(std::min(id.z, static_cast<int8_t>(max_zoom)));
} else if (token == "x") {
return util::toString(id.x);
} else if (token == "y") {
return util::toString(id.y);
} else if (token == "prefix") {
std::string prefix{ 2 };
prefix[0] = "0123456789abcdef"[id.x % 16];
prefix[1] = "0123456789abcdef"[id.y % 16];
return prefix;
} else if (token == "ratio") {
return pixelRatio > 1.0 ? "@2x" : "";
} else {
return "";
}
});
return result;
}
} // namespace mbgl

View File

@@ -0,0 +1,38 @@
#ifndef MBGL_MAP_SOURCE_INFO
#define MBGL_MAP_SOURCE_INFO
#include <mbgl/style/types.hpp>
#include <mbgl/map/tile_id.hpp>
#include <mbgl/util/noncopyable.hpp>
#include <mbgl/util/constants.hpp>
#include <mapbox/variant.hpp>
#include <rapidjson/document.h>
#include <array>
#include <vector>
#include <cstdint>
namespace mbgl {
class SourceInfo : private util::noncopyable {
public:
SourceType type = SourceType::Vector;
std::string url;
std::vector<std::string> tiles;
uint16_t tile_size = util::tileSize;
uint16_t min_zoom = 0;
uint16_t max_zoom = 22;
std::string attribution;
std::array<float, 3> center = { { 0, 0, 0 } };
std::array<float, 4> bounds = { { -180, -90, 180, 90 } };
std::string source_id = "";
void parseTileJSONProperties(const rapidjson::Value&);
std::string tileURL(const TileID& id, float pixelRatio) const;
};
} // namespace mbgl
#endif // MBGL_MAP_SOURCE_INFO