[core] Add layer serialization method
This commit is contained in:
@@ -28,6 +28,8 @@ public:
|
||||
|
||||
bool operator()(const expression::EvaluationContext& context) const;
|
||||
|
||||
operator bool() const { return expression || legacyFilter; }
|
||||
|
||||
friend bool operator==(const Filter& lhs, const Filter& rhs) {
|
||||
if (!lhs.expression || !rhs.expression) {
|
||||
return lhs.expression == rhs.expression;
|
||||
|
||||
@@ -114,6 +114,7 @@ public:
|
||||
optional<conversion::Error> setVisibility(const conversion::Convertible& value);
|
||||
|
||||
virtual StyleProperty getProperty(const std::string&) const = 0;
|
||||
virtual Value serialize() const;
|
||||
|
||||
// Private implementation
|
||||
// TODO : We should not have public mutable data members.
|
||||
@@ -140,6 +141,7 @@ public:
|
||||
|
||||
protected:
|
||||
virtual Mutable<Impl> mutableBaseImpl() const = 0;
|
||||
void serializeProperty(Value&, const StyleProperty&, const char* propertyName, bool isPaint) const;
|
||||
|
||||
LayerObserver* observer;
|
||||
mapbox::base::WeakPtrFactory<Layer> weakFactory {this};
|
||||
|
||||
@@ -40,6 +40,7 @@ public:
|
||||
optional<conversion::Error> setProperty(const std::string& name, const conversion::Convertible& value) final;
|
||||
|
||||
StyleProperty getProperty(const std::string& name) const final;
|
||||
Value serialize() const final;
|
||||
|
||||
<% if (layoutProperties.length) { -%>
|
||||
// Layout properties
|
||||
|
||||
@@ -91,6 +91,54 @@ void Layer::setMaxZoom(float maxZoom) {
|
||||
observer->onLayerChanged(*this);
|
||||
}
|
||||
|
||||
Value Layer::serialize() const {
|
||||
mapbox::base::ValueObject result;
|
||||
result.emplace(std::make_pair<std::string, Value>("id", getID()));
|
||||
result.emplace(std::make_pair<std::string, Value>("type", Layer::getTypeInfo()->type));
|
||||
|
||||
auto source = getSourceID();
|
||||
if (!source.empty()) {
|
||||
result.emplace(std::make_pair<std::string, Value>("source", std::move(source)));
|
||||
}
|
||||
|
||||
auto sourceLayer = getSourceLayer();
|
||||
if (!sourceLayer.empty()) {
|
||||
result.emplace(std::make_pair<std::string, Value>("source-layer", std::move(sourceLayer)));
|
||||
}
|
||||
|
||||
if (getFilter()) {
|
||||
result.emplace(std::make_pair<std::string, Value>("filter", getFilter().serialize()));
|
||||
}
|
||||
|
||||
if (getMinZoom() != -std::numeric_limits<float>::infinity()) {
|
||||
result.emplace(std::make_pair<std::string, Value>("minzoom", getMinZoom()));
|
||||
}
|
||||
|
||||
if (getMaxZoom() != std::numeric_limits<float>::infinity()) {
|
||||
result.emplace(std::make_pair<std::string, Value>("maxzoom", getMaxZoom()));
|
||||
}
|
||||
|
||||
if (getVisibility() == VisibilityType::None) {
|
||||
result["layout"] = mapbox::base::ValueObject{std::make_pair<std::string, Value>("visibility", "none")};
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void Layer::serializeProperty(Value& out, const StyleProperty& property, const char* propertyName, bool isPaint) const {
|
||||
assert(out.getObject());
|
||||
auto& object = *(out.getObject());
|
||||
std::string propertyType = isPaint ? "paint" : "layout";
|
||||
auto it = object.find(propertyType);
|
||||
auto pair = std::make_pair<std::string, Value>(std::string(propertyName), Value{property.getValue()});
|
||||
if (it != object.end()) {
|
||||
assert(it->second.getObject());
|
||||
it->second.getObject()->emplace(std::move(pair));
|
||||
} else {
|
||||
object[propertyType] = mapbox::base::ValueObject{{std::move(pair)}};
|
||||
}
|
||||
}
|
||||
|
||||
void Layer::setObserver(LayerObserver* observer_) {
|
||||
observer = observer_ ? observer_ : &nullObserver;
|
||||
}
|
||||
|
||||
@@ -200,6 +200,8 @@ using namespace conversion;
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr uint8_t kPaintPropertyCount = <%- paintProperties.length * 2 -%>u;
|
||||
|
||||
enum class Property : uint8_t {
|
||||
<% for (const property of paintProperties) { -%>
|
||||
<%- camelize(property.name) %>,
|
||||
@@ -207,8 +209,12 @@ enum class Property : uint8_t {
|
||||
<% for (const property of paintProperties) { -%>
|
||||
<%- camelize(property.name) %>Transition,
|
||||
<% } -%>
|
||||
<% for (const property of layoutProperties) { -%>
|
||||
<%- camelize(property.name) %>,
|
||||
<% for (let i = 0; i < layoutProperties.length; ++i) { -%>
|
||||
<% if (i===0) { -%>
|
||||
<%- camelize(layoutProperties[i].name) %> = kPaintPropertyCount,
|
||||
<% } else { -%>
|
||||
<%- camelize(layoutProperties[i].name) %>,
|
||||
<% } -%>
|
||||
<% } -%>
|
||||
};
|
||||
|
||||
@@ -225,8 +231,46 @@ MAPBOX_ETERNAL_CONSTEXPR const auto layerProperties = mapbox::eternal::hash_map<
|
||||
<%- paintProperties.map(p => `{"${p.name}-transition", toUint8(Property::${camelize(p.name)}Transition)}`).join(',\n ') %>,
|
||||
<%- layoutProperties.map(p => `{"${p.name}", toUint8(Property::${camelize(p.name)})}`).join(',\n ') %>});
|
||||
<% } -%>
|
||||
|
||||
StyleProperty getLayerProperty(const <%- camelize(type) %>Layer& layer, Property property) {
|
||||
switch (property) {
|
||||
<% for (const property of paintProperties) { -%>
|
||||
case Property::<%- camelize(property.name) %>:
|
||||
return makeStyleProperty(layer.get<%- camelize(property.name) %>());
|
||||
<% } -%>
|
||||
<% for (const property of paintProperties) { -%>
|
||||
case Property::<%- camelize(property.name) %>Transition:
|
||||
return makeStyleProperty(layer.get<%- camelize(property.name) %>Transition());
|
||||
<% } -%>
|
||||
<% for (const property of layoutProperties) { -%>
|
||||
case Property::<%- camelize(property.name) %>:
|
||||
return makeStyleProperty(layer.get<%- camelize(property.name) %>());
|
||||
<% } -%>
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
StyleProperty getLayerProperty(const <%- camelize(type) -%>Layer& layer, const std::string& name) {
|
||||
const auto it = layerProperties.find(name.c_str());
|
||||
if (it == layerProperties.end()) {
|
||||
return {};
|
||||
}
|
||||
return getLayerProperty(layer, static_cast<Property>(it->second));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
Value <%- camelize(type) %>Layer::serialize() const {
|
||||
auto result = Layer::serialize();
|
||||
assert(result.getObject());
|
||||
for (const auto& property : layerProperties) {
|
||||
auto styleProperty = getLayerProperty(*this, static_cast<Property>(property.second));
|
||||
if (styleProperty.getKind() == StyleProperty::Kind::Undefined) continue;
|
||||
serializeProperty(result, styleProperty, property.first.c_str(), property.second < kPaintPropertyCount);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
optional<Error> <%- camelize(type) %>Layer::setProperty(const std::string& name, const Convertible& value) {
|
||||
const auto it = layerProperties.find(name.c_str());
|
||||
if (it == layerProperties.end()) {
|
||||
@@ -289,26 +333,7 @@ optional<Error> <%- camelize(type) %>Layer::setProperty(const std::string& name,
|
||||
}
|
||||
|
||||
StyleProperty <%- camelize(type) %>Layer::getProperty(const std::string& name) const {
|
||||
const auto it = layerProperties.find(name.c_str());
|
||||
if (it == layerProperties.end()) {
|
||||
return {};
|
||||
}
|
||||
|
||||
switch (static_cast<Property>(it->second)) {
|
||||
<% for (const property of paintProperties) { -%>
|
||||
case Property::<%- camelize(property.name) %>:
|
||||
return makeStyleProperty(get<%- camelize(property.name) %>());
|
||||
<% } -%>
|
||||
<% for (const property of paintProperties) { -%>
|
||||
case Property::<%- camelize(property.name) %>Transition:
|
||||
return makeStyleProperty(get<%- camelize(property.name) %>Transition());
|
||||
<% } -%>
|
||||
<% for (const property of layoutProperties) { -%>
|
||||
case Property::<%- camelize(property.name) %>:
|
||||
return makeStyleProperty(get<%- camelize(property.name) %>());
|
||||
<% } -%>
|
||||
}
|
||||
return {};
|
||||
return getLayerProperty(*this, name);
|
||||
}
|
||||
|
||||
Mutable<Layer::Impl> <%- camelize(type) %>Layer::mutableBaseImpl() const {
|
||||
|
||||
Reference in New Issue
Block a user