Files
MapLibreParticleLab/platform/darwin/src/MGLConversion.h
Minh Nguyễn fb5b8d34f2 Reimplement style values atop NSExpression (#10726)
* [ios, macos] Import headers, not implementation files

* [core] Added accessors for various expression parameters

Added missing parameter accessors to various expression operator classes, as well as a method on InterpolatorBase and Step that enumerates the stops and their values.

* [ios, macos] Silenced warning in test of error condition

* [ios, macos] Made MGLSphericalPosition boxable

* [ios, macos] Implemented array enumeration during conversion

* [ios, macos] Temporarily ignore heatmap layer type

* [ios, macos] Migrated MGLSymbolStyleLayer.text to NSExpression

MGLSymbolStyleLayer.text is now of type NSExpression instead of MGLStyleValue, as a first step toward migrating the entire layer API from style values to expressions.

Implemented conversions from NSExpression to JSON arrays and vice versa. The most common NSExpression functions are now converted into style expressions, but not all of the most common style expression operators are supported yet.

* [ios, macos] Implemented string coercion

* [ios, macos] Color literals

* [ios, macos] Null constant expressions

* [ios, macos] Convert dictionary literals

* [ios, macos] Interpolation expressions

* [ios, macos] to-boolean, to-number, get from object

* [ios, macos] Variable expressions

Implemented custom expression functions for assigning and referring to variables within the context of an expression. Variables are assigned via a “context dictionary” and applied to an subexpression that is given as another argument to the same expression. Also implemented built-in variable expressions for zoom level and heatmap density.

* [ios, macos] Convert colors, offsets, padding in expressions to JSON objects

* [ios, macos] Expression-based style property getters

Implemented a conversion from mbgl::style::PropertyValues to Objective-C JSON objects, which are then converted to NSExpressions.

* [ios, macos] Consolidated property value–expression conversion in MGLStyleValueTransformer

* [ios, macos] Predicate and expression guide

Extracted documentation about predicates from a documentation comment in MGLVectorStyleLayer.h to a new jazzy guide. Added details about NSExpression support as well. Began updating the “For Style Authors” guide to reflect the transition from style values to expressions.

* [ios, macos] Updated style authoring guide

Updated the Information for Style Authors guide to discuss expressions instead of style functions. Included a table mapping style specification expression operators to NSExpression syntaxes.

* [ios, macos] Migrated codegen templates to expressions

* [ios, macos] Applied expression changes via codegen

Ran make darwin-style-code.

* [macos] Migrated macosapp to expressions

* [ios, macos] Updated style function guide

This guide needs to be thoroughly rewritten, but for now the example code has been migrated to expressions.

* [ios, macos] Eviscerated style function tests

* [ios, macos] Updated changelogs

* [ios] Migrated iosapp to expressions

* [ios, macos] Exposed JSON conversion methods publicly

* [ios, macos] Removed MGLStyleValue, MGLStyleFunction
2018-01-24 00:04:02 -08:00

161 lines
4.9 KiB
C++

#include <mbgl/style/conversion.hpp>
NS_ASSUME_NONNULL_BEGIN
namespace mbgl {
namespace style {
namespace conversion {
// A wrapper class for `id`, so as not to confuse ARC.
class Holder {
public:
Holder(const id v) : value(v) {}
const id value;
};
template <>
class ConversionTraits<Holder> {
public:
static bool isUndefined(const Holder& holder) {
const id value = holder.value;
return !value || value == [NSNull null];
}
static bool isArray(const Holder& holder) {
const id value = holder.value;
return [value isKindOfClass:[NSArray class]];
}
static bool isObject(const Holder& holder) {
const id value = holder.value;
return [value isKindOfClass:[NSDictionary class]];
}
static std::size_t arrayLength(const Holder& holder) {
const id value = holder.value;
NSCAssert([value isKindOfClass:[NSArray class]], @"Value must be an NSArray for getLength().");
NSArray *array = value;
auto length = [array count];
NSCAssert(length <= std::numeric_limits<size_t>::max(), @"Array length out of bounds.");
return length;
}
static Holder arrayMember(const Holder& holder, std::size_t i) {
const id value = holder.value;
NSCAssert([value isKindOfClass:[NSArray class]], @"Value must be an NSArray for get(int).");
NSCAssert(i < NSUIntegerMax, @"Index must be less than NSUIntegerMax");
return {[value objectAtIndex: i]};
}
static optional<Holder> objectMember(const Holder& holder, const char *key) {
const id value = holder.value;
NSCAssert([value isKindOfClass:[NSDictionary class]], @"Value must be an NSDictionary for get(string).");
NSObject *member = [value objectForKey: @(key)];
if (member && member != [NSNull null]) {
return {member};
} else {
return {};
}
}
// Compiler is wrong about `Fn` parameter missing a nullability specifier.
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wnullability-completeness"
template <class Fn>
static optional<Error> eachMember(const Holder& holder, Fn&& visit) {
#pragma clang diagnostic pop
[holder.value enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
auto result = visit(std::string(static_cast<const char *>([key UTF8String])), obj);
if (result) {
*stop = YES;
}
}];
return {};
}
static optional<bool> toBool(const Holder& holder) {
const id value = holder.value;
if (_isBool(value)) {
return ((NSNumber *)value).boolValue;
} else {
return {};
}
}
static optional<float> toNumber(const Holder& holder) {
const id value = holder.value;
if (_isNumber(value)) {
return ((NSNumber *)value).floatValue;
} else {
return {};
}
}
static optional<double> toDouble(const Holder& holder) {
const id value = holder.value;
if (_isNumber(value)) {
return ((NSNumber *)value).doubleValue;
} else {
return {};
}
}
static optional<std::string> toString(const Holder& holder) {
const id value = holder.value;
if (_isString(value)) {
return std::string(static_cast<const char *>([value UTF8String]));
} else {
return {};
}
}
static optional<mbgl::Value> toValue(const Holder& holder) {
const id value = holder.value;
if (isUndefined(value)) {
return {};
} else if (_isBool(value)) {
return { *toBool(holder) };
} else if ( _isString(value)) {
return { *toString(holder) };
} else if (_isNumber(value)) {
// Need to cast to a double here as the float is otherwise considered a bool...
return { static_cast<double>(*toNumber(holder)) };
} else {
return {};
}
}
static optional<GeoJSON> toGeoJSON(const Holder& holder, Error& error) {
error = { "toGeoJSON not implemented" };
return {};
}
private:
static bool _isBool(const id value) {
if (![value isKindOfClass:[NSNumber class]]) return false;
// char: 32-bit boolean
// BOOL: 64-bit boolean
NSNumber *number = value;
return ((strcmp([number objCType], @encode(char)) == 0) ||
(strcmp([number objCType], @encode(BOOL)) == 0));
}
static bool _isNumber(const id value) {
return [value isKindOfClass:[NSNumber class]] && !_isBool(value);
}
static bool _isString(const id value) {
return [value isKindOfClass:[NSString class]];
}
};
inline Convertible makeConvertible(const id value) {
return Convertible(Holder(value));
}
} // namespace conversion
} // namespace style
} // namespace mbgl
NS_ASSUME_NONNULL_END