MapLibre Native Core
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
mbgl::style::conversion Namespace Reference

Classes

struct  Converter< ColorRampPropertyValue >
 
struct  Converter< bool >
 
struct  Converter< float >
 
struct  Converter< std::string >
 
struct  Converter< T, typename std::enable_if_t< std::is_enum_v< T > > >
 
struct  Converter< std::vector< T >, typename std::enable_if_t< std::is_enum_v< T > > >
 
struct  Converter< Color >
 
struct  Converter< std::array< float, N > >
 
struct  Converter< std::vector< float > >
 
struct  Converter< std::array< double, N > >
 
struct  Converter< std::vector< std::string > >
 
struct  Converter< LatLng >
 
struct  Converter< CustomGeometrySource::Options >
 
struct  Converter< Filter >
 
struct  Converter< GeoJSON >
 
struct  Converter< GeoJSONOptions >
 
struct  Converter< std::unique_ptr< Layer > >
 
struct  Converter< Light >
 
struct  Converter< Position >
 
struct  Converter< PropertyValue< T > >
 
struct  Converter< PropertyValue< std::array< double, 3 > >, void >
 
struct  Converter< style::Rotation >
 
struct  Converter< std::unique_ptr< Source > >
 
struct  Converter< Tileset >
 
struct  Converter< TransitionOptions >
 
struct  Error
 
class  ConversionTraits
 
struct  Converter
 
struct  ValueFactory
 
class  Convertible
 
struct  ValueFactory< ColorRampPropertyValue >
 
struct  ValueFactory< TransitionOptions >
 
struct  ValueFactory< Color >
 
struct  ValueFactory< T, typename std::enable_if_t<(!std::is_enum_v< T > &&!is_linear_container< T >::value)> >
 
struct  ValueFactory< T, typename std::enable_if_t< std::is_enum_v< T > > >
 
struct  ValueFactory< T, typename std::enable_if_t< is_linear_container< T >::value > >
 
struct  ValueFactory< Position >
 
struct  ValueFactory< Rotation >
 
struct  ValueFactory< float >
 
struct  Converter< expression::Formatted >
 
struct  ValueFactory< expression::Formatted >
 
struct  Converter< expression::Image >
 
struct  ValueFactory< expression::Image >
 

Functions

bool hasTokens (const std::string &)
 
std::unique_ptr< expression::ExpressionconvertTokenStringToFormatExpression (const std::string &)
 
std::unique_ptr< expression::ExpressionconvertTokenStringToImageExpression (const std::string &)
 
std::unique_ptr< expression::ExpressionconvertTokenStringToExpression (const std::string &)
 
std::optional< std::unique_ptr< expression::Expression > > convertFunctionToExpression (expression::type::Type, const Convertible &, Error &, bool convertTokens)
 
template<class T >
std::optional< PropertyExpression< T > > convertFunctionToExpression (const Convertible &value, Error &error, bool convertTokens)
 
std::optional< GeoJSONparseGeoJSON (const std::string &, Error &)
 
std::string getJSONType (const Convertible &value)
 
std::optional< ErrorsetPaintProperties (Layer &layer, const Convertible &value)
 
template<class T , class... Args>
std::optional< T > convert (const Convertible &value, Error &error, Args &&...args)
 
template<typename T >
Value makeValue (T &&arg)
 
template<typename T >
StyleProperty makeStyleProperty (const PropertyValue< T > &value)
 
StyleProperty makeStyleProperty (const TransitionOptions &value)
 
StyleProperty makeStyleProperty (const ColorRampPropertyValue &value)
 

Detailed Description

The conversion namespace defines conversions from JSON structures conforming to the schema defined by the MapLibre Style Specification, to the various C++ types that form the C++ model of that domain:

  `std::unique_ptr<Source>`
  `std::unique_ptr<Layer>`
  `Filter`
  `PropertyValue<T>`

A single template function serves as the public interface:

template <class T>
std::optional<T> convert(const Convertible& input, Error& error);

Where T is one of the above types. If the conversion fails, the result is empty, and the error parameter includes diagnostic text suitable for presentation to a library user. Otherwise, a filled optional is returned.

Convertible is a type that encapsulates a special form of polymorphism over various underlying types that can serve as input to the conversion algorithm. For instance, on macOS, we need to support conversion from both RapidJSON types, and a JSON structure represented with NSArray/NSDictionary/etc. On Qt, we need to support conversion from RapidJSON types and QVariant.

We don't want to use traditional forms of polymorphism to accomplish this:

Compile time polymorphism using a template parameter for the actual value type leads to
excessive code bloat and long compile times.
Runtime polymorphism using virtual methods requires extra heap allocation and ubiquitous
use of std::unique_ptr, unsuitable for this performance-sensitive code.

Therefore, we're using a custom implementation of runtime polymorphism where we manually create and dispatch through a table of function pointers (vtable), while keeping the storage for any of the possible underlying types inline on the stack, using std::aligned_storage.

For a given underlying type T, an explicit specialization of ConversionTraits<T> must be provided. This specialization must provide the following static methods:

 `isUndefined(v)` -- returns a boolean indication whether `v` is undefined or a JSON null

 `isArray(v)` -- returns a boolean indicating whether `v` represents a JSON array
 `arrayLength(v)` -- called only if `isArray(v)`; returns a size_t length
 `arrayMember(v)` -- called only if `isArray(v)`; returns `V` or `V&`

 `isObject(v)` -- returns a boolean indicating whether `v` represents a JSON object
 `objectMember(v, name)` -- called only if `isObject(v)`; `name` is `const char *`; return value:
    is true when evaluated in a boolean context iff the named member exists
    is convertable to a `V` or `V&` when dereferenced
 `eachMember(v, [] (const std::string&, const V&) -> std::optional<Error> {...})` -- called
  only if `isObject(v)`; calls the provided lambda once for each key and value of the object;
  short-circuits if any call returns an `Error`

 `toBool(v)` -- returns `optional<bool>`, absence indicating `v` is not a JSON boolean
 `toNumber(v)` -- returns `optional<float>`, absence indicating `v` is not a JSON number
 `toDouble(v)` -- returns `optional<double>`, absence indicating `v` is not a JSON number
 `toString(v)` -- returns `optional<std::string>`, absence indicating `v` is not a JSON string
 `toValue(v)` -- returns `optional<Value>`, a variant type, for generic conversion,
 absence indicating `v` is not a boolean, number, or string. Numbers should be converted to
 unsigned integer, signed integer, or floating point, in descending preference.

In addition, the type T must be move-constructable. And finally, Convertible::Storage, a typedef for std::aligned_storage_t, must be large enough to satisfy the memory requirements for any of the possible underlying types. (A static assert will fail if this is not the case.)

Convertible itself is movable, but not copyable. A moved-from Convertible is in an invalid state; you must not do anything with it except let it go out of scope.

Function Documentation

◆ convert()

template<class T , class... Args>
std::optional<T> mbgl::style::conversion::convert ( const Convertible value,
Error error,
Args &&...  args 
)

Definition at line 300 of file conversion_impl.hpp.

◆ convertFunctionToExpression() [1/2]

template<class T >
std::optional<PropertyExpression<T> > mbgl::style::conversion::convertFunctionToExpression ( const Convertible value,
Error error,
bool  convertTokens 
)

◆ convertFunctionToExpression() [2/2]

std::optional<std::unique_ptr<expression::Expression> > mbgl::style::conversion::convertFunctionToExpression ( expression::type::Type  ,
const Convertible ,
Error ,
bool  convertTokens 
)

◆ convertTokenStringToExpression()

std::unique_ptr<expression::Expression> mbgl::style::conversion::convertTokenStringToExpression ( const std::string &  )

◆ convertTokenStringToFormatExpression()

std::unique_ptr<expression::Expression> mbgl::style::conversion::convertTokenStringToFormatExpression ( const std::string &  )

◆ convertTokenStringToImageExpression()

std::unique_ptr<expression::Expression> mbgl::style::conversion::convertTokenStringToImageExpression ( const std::string &  )

◆ getJSONType()

std::string mbgl::style::conversion::getJSONType ( const Convertible value)

◆ hasTokens()

bool mbgl::style::conversion::hasTokens ( const std::string &  )

◆ makeStyleProperty() [1/3]

StyleProperty mbgl::style::conversion::makeStyleProperty ( const ColorRampPropertyValue value)
inline

Definition at line 382 of file conversion_impl.hpp.

◆ makeStyleProperty() [2/3]

template<typename T >
StyleProperty mbgl::style::conversion::makeStyleProperty ( const PropertyValue< T > &  value)

Definition at line 364 of file conversion_impl.hpp.

◆ makeStyleProperty() [3/3]

StyleProperty mbgl::style::conversion::makeStyleProperty ( const TransitionOptions value)
inline

Definition at line 377 of file conversion_impl.hpp.

◆ makeValue()

template<typename T >
Value mbgl::style::conversion::makeValue ( T &&  arg)

Definition at line 359 of file conversion_impl.hpp.

◆ parseGeoJSON()

std::optional<GeoJSON> mbgl::style::conversion::parseGeoJSON ( const std::string &  ,
Error  
)

◆ setPaintProperties()

std::optional<Error> mbgl::style::conversion::setPaintProperties ( Layer &  layer,
const Convertible value 
)