Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
Move conversions to cpp files where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
jfirebaugh committed Sep 26, 2017
1 parent 143b7ac commit cd7e4c2
Show file tree
Hide file tree
Showing 25 changed files with 1,112 additions and 978 deletions.
14 changes: 12 additions & 2 deletions cmake/core-files.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -373,16 +373,26 @@ set(MBGL_CORE_FILES
include/mbgl/style/conversion/geojson_options.hpp
include/mbgl/style/conversion/layer.hpp
include/mbgl/style/conversion/light.hpp
include/mbgl/style/conversion/make_property_setters.hpp
include/mbgl/style/conversion/position.hpp
include/mbgl/style/conversion/property_setter.hpp
include/mbgl/style/conversion/property_value.hpp
include/mbgl/style/conversion/source.hpp
include/mbgl/style/conversion/tileset.hpp
include/mbgl/style/conversion/transition_options.hpp
src/mbgl/style/conversion/constant.cpp
src/mbgl/style/conversion/coordinate.cpp
src/mbgl/style/conversion/filter.cpp
src/mbgl/style/conversion/geojson.cpp
src/mbgl/style/conversion/geojson_options.cpp
src/mbgl/style/conversion/json.hpp
src/mbgl/style/conversion/layer.cpp
src/mbgl/style/conversion/light.cpp
src/mbgl/style/conversion/make_property_setters.hpp
src/mbgl/style/conversion/position.cpp
src/mbgl/style/conversion/property_setter.hpp
src/mbgl/style/conversion/source.cpp
src/mbgl/style/conversion/stringify.hpp
src/mbgl/style/conversion/tileset.cpp
src/mbgl/style/conversion/transition_options.cpp

# style/function
include/mbgl/style/function/camera_function.hpp
Expand Down
86 changes: 6 additions & 80 deletions include/mbgl/style/conversion/constant.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once

#include <mbgl/style/conversion.hpp>
#include <mbgl/util/optional.hpp>
#include <mbgl/util/color.hpp>
#include <mbgl/util/enum.hpp>
#include <mbgl/util/string.hpp>
Expand All @@ -16,38 +15,17 @@ namespace conversion {

template <>
struct Converter<bool> {
optional<bool> operator()(const Value& value, Error& error) const {
optional<bool> converted = toBool(value);
if (!converted) {
error = { "value must be a boolean" };
return {};
}
return *converted;
}
optional<bool> operator()(const Value& value, Error& error) const;
};

template <>
struct Converter<float> {
optional<float> operator()(const Value& value, Error& error) const {
optional<float> converted = toNumber(value);
if (!converted) {
error = { "value must be a number" };
return {};
}
return *converted;
}
optional<float> operator()(const Value& value, Error& error) const;
};

template <>
struct Converter<std::string> {
optional<std::string> operator()(const Value& value, Error& error) const {
optional<std::string> converted = toString(value);
if (!converted) {
error = { "value must be a string" };
return {};
}
return *converted;
}
optional<std::string> operator()(const Value& value, Error& error) const;
};

template <class T>
Expand All @@ -71,21 +49,7 @@ struct Converter<T, typename std::enable_if_t<std::is_enum<T>::value>> {

template <>
struct Converter<Color> {
optional<Color> operator()(const Value& value, Error& error) const {
optional<std::string> string = toString(value);
if (!string) {
error = { "value must be a string" };
return {};
}

optional<Color> color = Color::parse(*string);
if (!color) {
error = { "value must be a valid color" };
return {};
}

return *color;
}
optional<Color> operator()(const Value& value, Error& error) const;
};

template <size_t N>
Expand All @@ -111,50 +75,12 @@ struct Converter<std::array<float, N>> {

template <>
struct Converter<std::vector<float>> {
optional<std::vector<float>> operator()(const Value& value, Error& error) const {
if (!isArray(value)) {
error = { "value must be an array" };
return {};
}

std::vector<float> result;
result.reserve(arrayLength(value));

for (std::size_t i = 0; i < arrayLength(value); ++i) {
optional<float> number = toNumber(arrayMember(value, i));
if (!number) {
error = { "value must be an array of numbers" };
return {};
}
result.push_back(*number);
}

return result;
}
optional<std::vector<float>> operator()(const Value& value, Error& error) const;
};

template <>
struct Converter<std::vector<std::string>> {
optional<std::vector<std::string>> operator()(const Value& value, Error& error) const {
if (!isArray(value)) {
error = { "value must be an array" };
return {};
}

std::vector<std::string> result;
result.reserve(arrayLength(value));

for (std::size_t i = 0; i < arrayLength(value); ++i) {
optional<std::string> string = toString(arrayMember(value, i));
if (!string) {
error = { "value must be an array of strings" };
return {};
}
result.push_back(*string);
}

return result;
}
optional<std::vector<std::string>> operator()(const Value& value, Error& error) const;
};

} // namespace conversion
Expand Down
20 changes: 1 addition & 19 deletions include/mbgl/style/conversion/coordinate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,7 @@ namespace conversion {
template<>
struct Converter<LatLng> {
public:
optional<LatLng> operator() (const Value& value, Error& error) const {
if (!isArray(value) || arrayLength(value) < 2 ) {
error = { "coordinate array must contain numeric longitude and latitude values" };
return {};
}
//Style spec uses GeoJSON convention for specifying coordinates
optional<double> latitude = toDouble(arrayMember(value, 1));
optional<double> longitude = toDouble(arrayMember(value, 0));

if (!latitude || !longitude) {
error = { "coordinate array must contain numeric longitude and latitude values" };
return {};
}
if (*latitude < -90 || *latitude > 90 ){
error = { "coordinate latitude must be between -90 and 90" };
return {};
}
return LatLng(*latitude, *longitude);
}
optional<LatLng> operator() (const Value& value, Error& error) const;
};

} // namespace conversion
Expand Down
Loading

0 comments on commit cd7e4c2

Please sign in to comment.