move style classes to MapData object and make thread safe

This commit is contained in:
Konstantin Käfer
2015-03-20 19:46:52 +01:00
parent 3d2dcbb6ef
commit d2faca1861
4 changed files with 80 additions and 27 deletions

View File

@@ -82,6 +82,7 @@ public:
StyleInfo = 1 << 0,
Debug = 1 << 1,
DefaultTransitionDuration = 1 << 2,
Classes = 1 << 3,
};
void triggerUpdate(Update = Update::Nothing);
@@ -251,8 +252,6 @@ private:
const std::unique_ptr<MapData> data;
std::vector<std::string> classes;
std::set<util::ptr<StyleSource>> activeSources;
std::atomic<UpdateType> updated;

View File

@@ -609,43 +609,28 @@ std::chrono::steady_clock::time_point Map::getTime() const {
}
void Map::addClass(const std::string& klass) {
if (hasClass(klass)) return;
classes.push_back(klass);
if (style) {
style->cascadeClasses(classes);
if (style->hasTransitions()) {
triggerUpdate();
}
if (data->addClass(klass)) {
triggerUpdate(Update::Classes);
}
}
void Map::removeClass(const std::string& klass) {
if (!hasClass(klass)) return;
classes.erase(std::remove(classes.begin(), classes.end(), klass), classes.end());
if (style) {
style->cascadeClasses(classes);
if (style->hasTransitions()) {
triggerUpdate();
}
if (data->removeClass(klass)) {
triggerUpdate(Update::Classes);
}
}
void Map::setClasses(const std::vector<std::string>& classes_) {
classes = classes_;
if (style) {
style->cascadeClasses(classes);
if (style->hasTransitions()) {
triggerUpdate();
}
}
void Map::setClasses(const std::vector<std::string>& classes) {
data->setClasses(classes);
triggerUpdate(Update::Classes);
}
bool Map::hasClass(const std::string& klass) const {
return std::find(classes.begin(), classes.end(), klass) != classes.end();
return data->hasClass(klass);
}
std::vector<std::string> Map::getClasses() const {
return classes;
return data->getClasses();
}
void Map::setDefaultTransitionDuration(std::chrono::steady_clock::duration duration) {
@@ -753,7 +738,7 @@ void Map::loadStyleJSON(const std::string& json, const std::string& base) {
style = std::make_shared<Style>();
style->base = base;
style->loadJSON((const uint8_t *)json.c_str());
style->cascadeClasses(classes);
style->cascadeClasses(data->getClasses());
style->setDefaultTransitionDuration(data->getDefaultTransitionDuration());
const std::string glyphURL = util::mapbox::normalizeGlyphsURL(style->glyph_url, getAccessToken());
@@ -778,6 +763,11 @@ void Map::prepare() {
style->setDefaultTransitionDuration(data->getDefaultTransitionDuration());
}
}
if (u & static_cast<UpdateType>(Update::Classes)) {
if (style) {
style->cascadeClasses(data->getClasses());
}
}
// Update transform transitions.

44
src/mbgl/map/map_data.cpp Normal file
View File

@@ -0,0 +1,44 @@
#include "map_data.hpp"
#include <algorithm>
namespace mbgl {
// Adds the class if it's not yet set. Returns true when it added the class, and false when it
// was already present.
bool MapData::addClass(const std::string& klass) {
Lock lock(mtx);
if (std::find(classes.begin(), classes.end(), klass) != classes.end()) return false;
classes.push_back(klass);
return true;
}
// Removes the class if it's present. Returns true when it remvoed the class, and false when it
// was not present.
bool MapData::removeClass(const std::string& klass) {
Lock lock(mtx);
const auto it = std::find(classes.begin(), classes.end(), klass);
if (it != classes.end()) {
classes.erase(it);
return true;
}
return false;
}
// Returns true when class is present in the list of currently set classes.
bool MapData::hasClass(const std::string& klass) const {
Lock lock(mtx);
return std::find(classes.begin(), classes.end(), klass) != classes.end();
}
void MapData::setClasses(const std::vector<std::string>& klasses) {
Lock lock(mtx);
classes = klasses;
}
std::vector<std::string> MapData::getClasses() const {
Lock lock(mtx);
return classes;
}
}

View File

@@ -5,6 +5,7 @@
#include <mutex>
#include <atomic>
#include <chrono>
#include <vector>
namespace mbgl {
@@ -41,6 +42,24 @@ public:
accessToken = token;
}
// Adds the class if it's not yet set. Returns true when it added the class, and false when it
// was already present.
bool addClass(const std::string& klass);
// Removes the class if it's present. Returns true when it remvoed the class, and false when it
// was not present.
bool removeClass(const std::string& klass);
// Returns true when class is present in the list of currently set classes.
bool hasClass(const std::string& klass) const;
// Changes the list of currently set classes to the new list.
void setClasses(const std::vector<std::string>& klasses);
// Returns a list of all currently set classes.
std::vector<std::string> getClasses() const;
inline bool getDebug() const {
return debug;
}
@@ -72,6 +91,7 @@ private:
StyleInfo styleInfo;
std::string accessToken;
std::vector<std::string> classes;
std::atomic<uint8_t> debug { false };
std::atomic<std::chrono::steady_clock::time_point::duration> animationTime;
std::atomic<std::chrono::steady_clock::duration> defaultTransitionDuration;