[core] Introduce Light::getProperty()

This commit is contained in:
Mikhail Pozdnyakov
2019-09-26 18:26:11 +03:00
parent 5304bbc438
commit d90b2bb6dc
5 changed files with 109 additions and 44 deletions

View File

@@ -342,6 +342,13 @@ struct ValueFactory<T, typename std::enable_if<is_linear_container<T>::value>::t
}
};
template <>
struct ValueFactory<Position> {
static Value make(const Position& position) {
return ValueFactory<std::array<float, 3>>::make(position.getSpherical());
}
};
template <typename T>
Value makeValue(T&& arg) {
return ValueFactory<std::decay_t<T>>::make(std::forward<T>(arg));

View File

@@ -6,6 +6,7 @@
#include <mbgl/style/conversion.hpp>
#include <mbgl/style/property_value.hpp>
#include <mbgl/style/style_property.hpp>
#include <mbgl/style/transition_options.hpp>
#include <mbgl/style/types.hpp>
#include <mbgl/util/immutable.hpp>
@@ -22,6 +23,7 @@ public:
// Dynamic properties
optional<conversion::Error> setProperty(const std::string& name, const conversion::Convertible& value);
StyleProperty getProperty(const std::string&) const;
static LightAnchorType getDefaultAnchor();
PropertyValue<LightAnchorType> getAnchor() const;

View File

@@ -9,6 +9,7 @@
#include <mbgl/style/conversion.hpp>
#include <mbgl/style/property_value.hpp>
#include <mbgl/style/style_property.hpp>
#include <mbgl/style/transition_options.hpp>
#include <mbgl/style/types.hpp>
#include <mbgl/util/immutable.hpp>
@@ -25,6 +26,7 @@ public:
// Dynamic properties
optional<conversion::Error> setProperty(const std::string& name, const conversion::Convertible& value);
StyleProperty getProperty(const std::string&) const;
<% for (const property of properties) { -%>
static <%- evaluatedType(property) %> getDefault<%- camelize(property.name) %>();

View File

@@ -1,5 +1,3 @@
// clang-format off
// This file is generated. Do not edit.
#include <mbgl/style/light.hpp>
@@ -36,29 +34,37 @@ Mutable<Light::Impl> Light::mutableImpl() const {
using namespace conversion;
namespace {
enum class Property : uint8_t {
Anchor,
Color,
Intensity,
Position,
AnchorTransition,
ColorTransition,
IntensityTransition,
PositionTransition,
};
template <typename T>
constexpr uint8_t toUint8(T t) noexcept {
return uint8_t(mbgl::underlying_type(t));
}
MAPBOX_ETERNAL_CONSTEXPR const auto properties = mapbox::eternal::hash_map<mapbox::eternal::string, uint8_t>(
{{"anchor", toUint8(Property::Anchor)},
{"color", toUint8(Property::Color)},
{"intensity", toUint8(Property::Intensity)},
{"position", toUint8(Property::Position)},
{"anchor-transition", toUint8(Property::AnchorTransition)},
{"color-transition", toUint8(Property::ColorTransition)},
{"intensity-transition", toUint8(Property::IntensityTransition)},
{"position-transition", toUint8(Property::PositionTransition)}});
} // namespace
optional<Error> Light::setProperty(const std::string& name, const Convertible& value) {
enum class Property {
Anchor,
Color,
Intensity,
Position,
AnchorTransition,
ColorTransition,
IntensityTransition,
PositionTransition,
};
MAPBOX_ETERNAL_CONSTEXPR const auto properties = mapbox::eternal::hash_map<mapbox::eternal::string, uint8_t>({
{ "anchor", mbgl::underlying_type(Property::Anchor) },
{ "color", mbgl::underlying_type(Property::Color) },
{ "intensity", mbgl::underlying_type(Property::Intensity) },
{ "position", mbgl::underlying_type(Property::Position) },
{ "anchor-transition", mbgl::underlying_type(Property::AnchorTransition) },
{ "color-transition", mbgl::underlying_type(Property::ColorTransition) },
{ "intensity-transition", mbgl::underlying_type(Property::IntensityTransition) },
{ "position-transition", mbgl::underlying_type(Property::PositionTransition) }
});
const auto it = properties.find(name.c_str());
if (it == properties.end()) {
return Error { "light doesn't support this property" };
@@ -146,6 +152,33 @@ optional<Error> Light::setProperty(const std::string& name, const Convertible& v
return Error { "light doesn't support this property" };
}
StyleProperty Light::getProperty(const std::string& name) const {
const auto it = properties.find(name.c_str());
if (it == properties.end()) {
return {};
}
switch (static_cast<Property>(it->second)) {
case Property::Anchor:
return makeStyleProperty(getAnchor());
case Property::Color:
return makeStyleProperty(getColor());
case Property::Intensity:
return makeStyleProperty(getIntensity());
case Property::Position:
return makeStyleProperty(getPosition());
case Property::AnchorTransition:
return makeStyleProperty(getAnchorTransition());
case Property::ColorTransition:
return makeStyleProperty(getColorTransition());
case Property::IntensityTransition:
return makeStyleProperty(getIntensityTransition());
case Property::PositionTransition:
return makeStyleProperty(getPositionTransition());
}
return {};
}
LightAnchorType Light::getDefaultAnchor() {
return LightAnchor::defaultValue();
}
@@ -253,5 +286,3 @@ TransitionOptions Light::getPositionTransition() const {
} // namespace style
} // namespace mbgl
// clang-format on

View File

@@ -1,8 +1,6 @@
<%
const properties = locals.properties;
-%>
// clang-format off
// This file is generated. Do not edit.
#include <mbgl/style/light.hpp>
@@ -39,21 +37,29 @@ Mutable<Light::Impl> Light::mutableImpl() const {
using namespace conversion;
namespace {
enum class Property : uint8_t {
<% for (const property of properties) { -%>
<%- camelize(property.name) %>,
<% } -%>
<% for (const property of properties) { -%>
<%- camelize(property.name) %>Transition,
<% } -%>
};
template <typename T>
constexpr uint8_t toUint8(T t) noexcept {
return uint8_t(mbgl::underlying_type(t));
}
MAPBOX_ETERNAL_CONSTEXPR const auto properties = mapbox::eternal::hash_map<mapbox::eternal::string, uint8_t>(
{<%- properties.map(p => `{"${p.name}", toUint8(Property::${camelize(p.name)})}`).join(',\n ') %>,
<%- properties.map(p => `{"${p.name}-transition", toUint8(Property::${camelize(p.name)}Transition)}`).join(',\n ') %>});
} // namespace
optional<Error> Light::setProperty(const std::string& name, const Convertible& value) {
enum class Property {
<% for (const property of properties) { -%>
<%- camelize(property.name) %>,
<% } -%>
<% for (const property of properties) { -%>
<%- camelize(property.name) %>Transition,
<% } -%>
};
MAPBOX_ETERNAL_CONSTEXPR const auto properties = mapbox::eternal::hash_map<mapbox::eternal::string, uint8_t>({
<%- properties.map(p => `{ "${p.name}", mbgl::underlying_type(Property::${camelize(p.name)}) }`).join(',\n ') %>,
<%- properties.map(p => `{ "${p.name}-transition", mbgl::underlying_type(Property::${camelize(p.name)}Transition) }`).join(',\n ') %>
});
const auto it = properties.find(name.c_str());
if (it == properties.end()) {
return Error { "light doesn't support this property" };
@@ -107,6 +113,25 @@ optional<Error> Light::setProperty(const std::string& name, const Convertible& v
return Error { "light doesn't support this property" };
}
StyleProperty Light::getProperty(const std::string& name) const {
const auto it = properties.find(name.c_str());
if (it == properties.end()) {
return {};
}
switch (static_cast<Property>(it->second)) {
<% for (const property of properties) { -%>
case Property::<%- camelize(property.name) %>:
return makeStyleProperty(get<%- camelize(property.name) %>());
<% } -%>
<% for (const property of properties) { -%>
case Property::<%- camelize(property.name) %>Transition:
return makeStyleProperty(get<%- camelize(property.name) %>Transition());
<% } -%>
}
return {};
}
<% for (const property of properties) { -%>
<%- evaluatedType(property) %> Light::getDefault<%- camelize(property.name) %>() {
return Light<%- camelize(property.name) %>::defaultValue();
@@ -138,5 +163,3 @@ TransitionOptions Light::get<%- camelize(property.name) %>Transition() const {
} // namespace style
} // namespace mbgl
// clang-format on