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

Partial implementation of runtime styling API for node bindings #5318

Merged
merged 6 commits into from
Jun 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/mbgl/map/map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ class Map : private util::noncopyable {

AnnotationIDs getPointAnnotationsInBounds(const LatLngBounds&);

style::Layer* getLayer(const std::string& layerID);
void addLayer(std::unique_ptr<style::Layer>, const optional<std::string>& beforeLayerID = {});
void removeLayer(const std::string& layerID);

Expand Down
31 changes: 0 additions & 31 deletions include/mbgl/platform/event.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#pragma once

#include <mbgl/util/enum.hpp>

#include <cstdint>

namespace mbgl {
Expand All @@ -13,14 +11,6 @@ enum class EventSeverity : uint8_t {
Error,
};

MBGL_DEFINE_ENUM_CLASS(EventSeverityClass, EventSeverity, {
{ EventSeverity::Debug, "DEBUG" },
{ EventSeverity::Info, "INFO" },
{ EventSeverity::Warning, "WARNING" },
{ EventSeverity::Error, "ERROR" },
{ EventSeverity(-1), "UNKNOWN" },
})

enum class Event : uint8_t {
General,
Setup,
Expand All @@ -40,27 +30,6 @@ enum class Event : uint8_t {
Glyph,
};

MBGL_DEFINE_ENUM_CLASS(EventClass, Event, {
{ Event::General, "General" },
{ Event::Setup, "Setup" },
{ Event::Shader, "Shader" },
{ Event::ParseStyle, "ParseStyle" },
{ Event::ParseTile, "ParseTile" },
{ Event::Render, "Render" },
{ Event::Style, "Style" },
{ Event::Database, "Database" },
{ Event::HttpRequest, "HttpRequest" },
{ Event::Sprite, "Sprite" },
{ Event::Image, "Image" },
{ Event::OpenGL, "OpenGL" },
{ Event::JNI, "JNI" },
{ Event::Android, "Android" },
{ Event::Crash, "Crash" },
{ Event::Glyph, "Glyph" },
{ Event(-1), "Unknown" },
})


struct EventPermutation {
const EventSeverity severity;
const Event event;
Expand Down
76 changes: 76 additions & 0 deletions include/mbgl/style/layers/layer.hpp.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<%
const type = locals.type;
const layoutProperties = locals.layoutProperties;
const paintProperties = locals.paintProperties;
-%>
// This file is generated. Do not edit.

#pragma once

#include <mbgl/style/layer.hpp>
#include <mbgl/style/filter.hpp>
#include <mbgl/style/property_value.hpp>

#include <mbgl/util/color.hpp>

<% if (type === 'line' || type === 'symbol') { -%>
#include <vector>

<% } -%>
namespace mbgl {
namespace style {

class <%- camelize(type) %>Layer : public Layer {
public:
<%- camelize(type) %>Layer(const std::string& layerID);
~<%- camelize(type) %>Layer() final;

<% if (type === 'raster') { -%>
// Source

void setSource(const std::string& sourceID);
const std::string& getSourceID() const;

<% } else if (type !== 'background') { -%>
// Source

void setSource(const std::string& sourceID, const std::string& sourceLayer);
const std::string& getSourceID() const;
const std::string& getSourceLayer() const;

void setFilter(const Filter&);
const Filter& getFilter() const;

<% } -%>
<% if (layoutProperties.length) { -%>
// Layout properties

<% for (const property of layoutProperties) { -%>
PropertyValue<<%- propertyType(property) %>> get<%- camelize(property.name) %>() const;
void set<%- camelize(property.name) %>(PropertyValue<<%- propertyType(property) %>>);

<% } -%>
<% } -%>
// Paint properties

<% for (const property of paintProperties) { -%>
PropertyValue<<%- propertyType(property) %>> get<%- camelize(property.name) %>() const;
void set<%- camelize(property.name) %>(PropertyValue<<%- propertyType(property) %>>);

<% } -%>
// Private implementation

class Impl;
Impl* const impl;

<%- camelize(type) %>Layer(const Impl&);
<%- camelize(type) %>Layer(const <%- camelize(type) %>Layer&) = delete;
};

template <>
inline bool Layer::is<<%- camelize(type) %>Layer>() const {
return type == Type::<%- camelize(type) %>;
}

} // namespace style
} // namespace mbgl
10 changes: 1 addition & 9 deletions include/mbgl/style/types.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <mbgl/util/enum.hpp>
#include <cstdint>

namespace mbgl {

Expand All @@ -13,14 +13,6 @@ enum class SourceType : uint8_t {
Annotations
};

MBGL_DEFINE_ENUM_CLASS(SourceTypeClass, SourceType, {
{ SourceType::Vector, "vector" },
{ SourceType::Raster, "raster" },
{ SourceType::GeoJSON, "geojson" },
{ SourceType::Video, "video" },
{ SourceType::Annotations, "annotations" },
})

namespace style {

enum class VisibilityType : bool {
Expand Down
60 changes: 22 additions & 38 deletions include/mbgl/util/enum.hpp
Original file line number Diff line number Diff line change
@@ -1,52 +1,36 @@
#pragma once

#include <iosfwd>
#include <mbgl/util/optional.hpp>

#include <algorithm>
#include <cassert>
#include <string>

namespace mbgl {

template <typename Type>
struct EnumValue {
const Type value;
const char *name;
};

template <typename EnumName, const EnumValue<EnumName> *names, const size_t length>
struct Enum {
using Type = EnumName;
Type value;
static const constexpr size_t l = length;
private:
static constexpr inline bool compare(const char *a, const char *b) {
return *a == *b && (*a == '\0' || compare(a + 1, b + 1));
}
static constexpr inline const char *lookup_type(Type e, EnumValue<Type> const * const list, size_t r) {
return r == 0 ? "" : list->value == e ? list->name : lookup_type(e, list + 1, r - 1);
}
static constexpr inline Type lookup_name(const char *n, EnumValue<Type> const * const list, size_t r) {
return r == 0 ? Type(-1) : compare(list->name, n) ? list->value : lookup_name(n, list + 1, r - 1);
}
template <typename T>
class Enum {
public:
inline constexpr Enum(const char *n) : value(lookup_name(n, names, length)) {}
inline constexpr Enum(const std::string &n) : value(lookup_name(n.c_str(), names, length)) {}
inline constexpr Enum(Type t) : value(t) {}

inline void operator=(const char *n) { value = lookup_name(n, names, length); }
inline void operator=(const std::string &n) { *this = n.c_str(); }
inline void operator=(Type t) { value = t; }
using Value = std::pair<const T, const char *>;

inline constexpr bool valid() const { return value != Type(-1); }
static const char * toString(T t) {
auto it = std::find_if(begin, end, [&] (const auto& v) { return t == v.first; });
assert(it != end); return it->second;
}

inline constexpr const char *c_str() const { return lookup_type(value, names, length); }
inline std::string str() const { return c_str(); }
static optional<T> toEnum(const std::string& s) {
auto it = std::find_if(begin, end, [&] (const auto& v) { return s == v.second; });
return it == end ? optional<T>() : it->first;
}

inline constexpr operator Type() const { return value; }
private:
static const Value* begin;
static const Value* end;
};

#define MBGL_DEFINE_ENUM_CLASS(name, type, strings...) \
const constexpr ::mbgl::EnumValue<type> type##_names[] = strings; \
using name = ::mbgl::Enum<type, type##_names, sizeof(type##_names) / sizeof(::mbgl::EnumValue<type>)>; \
inline std::ostream& operator<<(std::ostream& os, type t) { return os << name(t).str(); }
#define MBGL_DEFINE_ENUM(type, strings...) \
const constexpr Enum<type>::Value type##_names[] = strings; \
template <> const Enum<type>::Value* Enum<type>::begin = std::begin(type##_names); \
template <> const Enum<type>::Value* Enum<type>::end = std::end(type##_names)

} // namespace mbgl

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"express": "^4.11.1",
"mapbox-gl-shaders": "mapbox/mapbox-gl-shaders#09ee512cd59a8fb1a241c78833b7c8022bf4f263",
"mapbox-gl-style-spec": "mapbox/mapbox-gl-style-spec#2461efc3d883f2f2e56a6c6b2bfd7d54bbfe9f86",
"mapbox-gl-test-suite": "mapbox/mapbox-gl-test-suite#e78f09dce98080a9a6fa436d11fdf6fc2f271d7a",
"mapbox-gl-test-suite": "mapbox/mapbox-gl-test-suite#ce8146c048487dab444d92ce06ef0b0ca8515c73",
"node-gyp": "^3.3.1",
"request": "^2.72.0",
"tape": "^4.5.1"
Expand Down
3 changes: 2 additions & 1 deletion platform/darwin/src/log_nslog.mm
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <mbgl/platform/log.hpp>
#include <mbgl/util/enum.hpp>

#import <Foundation/Foundation.h>

Expand All @@ -7,7 +8,7 @@
void Log::platformRecord(EventSeverity severity, const std::string &msg) {
NSString *message =
[[NSString alloc] initWithBytes:msg.data() length:msg.size() encoding:NSUTF8StringEncoding];
NSLog(@"[%s] %@", EventSeverityClass(severity).c_str(), message);
NSLog(@"[%s] %@", Enum<EventSeverity>::toString(severity), message);
}

}
3 changes: 2 additions & 1 deletion platform/default/log_stderr.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#include <mbgl/platform/log.hpp>
#include <mbgl/util/enum.hpp>

#include <iostream>

namespace mbgl {

void Log::platformRecord(EventSeverity severity, const std::string &msg) {
std::cerr << "[" << severity << "] " << msg << std::endl;
std::cerr << "[" << Enum<EventSeverity>::toString(severity) << "] " << msg << std::endl;
}

} // namespace mbgl
6 changes: 4 additions & 2 deletions platform/node/src/node_log.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "node_log.hpp"
#include "util/async_queue.hpp"

#include <mbgl/util/enum.hpp>

namespace node_mbgl {

struct NodeLogObserver::LogMessage {
Expand All @@ -23,10 +25,10 @@ NodeLogObserver::NodeLogObserver(v8::Local<v8::Object> target)
auto msg = Nan::New<v8::Object>();

Nan::Set(msg, Nan::New("class").ToLocalChecked(),
Nan::New(mbgl::EventClass(message.event).c_str()).ToLocalChecked());
Nan::New(mbgl::Enum<mbgl::Event>::toString(message.event)).ToLocalChecked());

Nan::Set(msg, Nan::New("severity").ToLocalChecked(),
Nan::New(mbgl::EventSeverityClass(message.severity).c_str()).ToLocalChecked());
Nan::New(mbgl::Enum<mbgl::EventSeverity>::toString(message.severity)).ToLocalChecked());

if (message.code != -1) {
Nan::Set(msg, Nan::New("code").ToLocalChecked(),
Expand Down
Loading