summaryrefslogtreecommitdiff
path: root/src/mbgl/style/layout_property.hpp
diff options
context:
space:
mode:
authorJohn Firebaugh <john.firebaugh@gmail.com>2016-10-30 11:06:59 -0700
committerJohn Firebaugh <john.firebaugh@gmail.com>2016-11-17 15:13:38 -0800
commit38fcbe21d48186c4630a3b8a76d1b20e156faadd (patch)
tree098f73bfea98deb5202fe1c13b1277e43e322755 /src/mbgl/style/layout_property.hpp
parentd4fc66af3924805d40576989c1e139ddafcc4670 (diff)
downloadqtlocation-mapboxgl-38fcbe21d48186c4630a3b8a76d1b20e156faadd.tar.gz
[core] Convert style properties to a tuple-based approach
This converts the style property classes (CirclePaintProperties and so on) to the same tuple-based approach as gl::Attribute and gl::Uniform. The approach is outlined in https://github.com/mapbox/cpp/blob/master/C%2B%2B%20Structural%20Metaprogramming.md. The main advantage of this approach is it allows writing algorithms that work on sets of style properties, without resorting to code generation or manually repetitive code. This lets us iterate on approaches to data-driven properties more easily. Another advantage is that the cascading, unevaluated, and evaluated states of a set of properties exist as independent structures, instead of individual properties holding their own state. This is a more functional approach that makes data flow clearer and reduces state.
Diffstat (limited to 'src/mbgl/style/layout_property.hpp')
-rw-r--r--src/mbgl/style/layout_property.hpp64
1 files changed, 38 insertions, 26 deletions
diff --git a/src/mbgl/style/layout_property.hpp b/src/mbgl/style/layout_property.hpp
index db1a1ebf28..6ea06ce556 100644
--- a/src/mbgl/style/layout_property.hpp
+++ b/src/mbgl/style/layout_property.hpp
@@ -1,43 +1,55 @@
#pragma once
-#include <mbgl/style/property_value.hpp>
-#include <mbgl/style/property_parsing.hpp>
#include <mbgl/style/property_evaluator.hpp>
-
-#include <utility>
+#include <mbgl/util/indexed_tuple.hpp>
namespace mbgl {
namespace style {
-template <typename T>
+class PropertyEvaluationParameters;
+
+template <class T>
class LayoutProperty {
public:
- explicit LayoutProperty(T v)
- : value(std::move(v)),
- defaultValue(value) {}
-
- const PropertyValue<T>& get() const {
- return currentValue;
- }
+ using EvaluatorType = PropertyEvaluator<T>;
+ using UnevaluatedType = PropertyValue<T>;
+ using EvaluatedType = T;
+};
- void set(const PropertyValue<T>& value_) {
- currentValue = value_;
+template <class... Ps>
+class LayoutProperties {
+public:
+ using Properties = TypeList<Ps...>;
+ using EvaluatedTypes = TypeList<typename Ps::EvaluatedType...>;
+ using UnevaluatedTypes = TypeList<typename Ps::UnevaluatedType...>;
+
+ template <class TypeList>
+ using Tuple = IndexedTuple<Properties, TypeList>;
+
+ class Evaluated : public Tuple<EvaluatedTypes> {
+ public:
+ using Tuple<EvaluatedTypes>::Tuple;
+ };
+
+ class Unevaluated : public Tuple<UnevaluatedTypes> {
+ public:
+ using Tuple<UnevaluatedTypes>::Tuple;
+ };
+
+ template <class P>
+ auto evaluate(const PropertyEvaluationParameters& parameters) const {
+ using Evaluator = typename P::EvaluatorType;
+ return unevaluated.template get<P>()
+ .evaluate(Evaluator(parameters, P::defaultValue()));
}
- void calculate(const CalculationParameters& parameters) {
- if (currentValue) {
- PropertyEvaluator<T> evaluator(parameters, defaultValue);
- value = PropertyValue<T>::visit(currentValue, evaluator);
- }
+ Evaluated evaluate(const PropertyEvaluationParameters& parameters) const {
+ return Evaluated {
+ evaluate<Ps>(parameters)...
+ };
}
- // TODO: remove / privatize
- operator T() const { return value; }
- T value;
-
-private:
- T defaultValue;
- PropertyValue<T> currentValue;
+ Unevaluated unevaluated;
};
} // namespace style