Merge branch 'libuv-0.10-headless-display' of github.com:mapbox/mapbox-gl-native into libuv-0.10-headless-display

This commit is contained in:
Mike Morris
2014-10-08 14:47:48 -04:00
4 changed files with 39 additions and 12 deletions

View File

@@ -14,17 +14,22 @@ enum class ClassID : uint32_t {
};
class ClassDictionary {
private:
ClassDictionary();
public:
static ClassDictionary &Get();
// Returns an ID for a class name. If the class name does not yet have an ID, one is
// auto-generated and stored for future reference.
static ClassID Lookup(const std::string &class_name);
ClassID lookup(const std::string &class_name);
// Returns either Fallback, Default or Named, depending on the type of the class id.
static ClassID Normalize(ClassID id);
ClassID normalize(ClassID id);
private:
static std::unordered_map<std::string, ClassID> store;
static uint32_t offset;
std::unordered_map<std::string, ClassID> store = { { "", ClassID::Default } };
uint32_t offset = 0;
};
}

View File

@@ -1,8 +1,34 @@
#include <mbgl/style/class_dictionary.hpp>
#include <uv.h>
namespace mbgl {
ClassID ClassDictionary::Lookup(const std::string &class_name) {
ClassDictionary::ClassDictionary() {}
ClassDictionary &ClassDictionary::Get() {
// Note: We should eventually switch to uv_key_* functions, but libuv 0.10 doesn't have these
// yet. Instead, we're using the pthread functions directly for now.
static pthread_once_t store_once = PTHREAD_ONCE_INIT;
static pthread_key_t store_key;
// Create the key.
pthread_once(&store_once, []() {
pthread_key_create(&store_key, [](void *ptr) {
delete reinterpret_cast<ClassDictionary *>(ptr);
});
});
ClassDictionary *ptr = reinterpret_cast<ClassDictionary *>(pthread_getspecific(store_key));
if (ptr == nullptr) {
ptr = new ClassDictionary();
pthread_setspecific(store_key, ptr);
}
return *ptr;
}
ClassID ClassDictionary::lookup(const std::string &class_name) {
auto it = store.find(class_name);
if (it == store.end()) {
// Insert the class name into the store.
@@ -14,7 +40,7 @@ ClassID ClassDictionary::Lookup(const std::string &class_name) {
}
}
ClassID ClassDictionary::Normalize(ClassID id) {
ClassID ClassDictionary::normalize(ClassID id) {
if (id >= ClassID::Named) {
return ClassID::Named;
} else {
@@ -22,8 +48,4 @@ ClassID ClassDictionary::Normalize(ClassID id) {
}
}
std::unordered_map<std::string, ClassID> ClassDictionary::store = { { "", ClassID::Default } };
uint32_t ClassDictionary::offset = 0;
}

View File

@@ -23,7 +23,7 @@ void StyleLayer::setClasses(const std::vector<std::string> &class_names, const t
for (auto it = class_names.rbegin(); it != class_names.rend(); it++) {
const std::string &class_name = *it;
// From here on, we're only dealing with IDs to avoid comparing strings all the time.
const ClassID class_id = ClassDictionary::Lookup(class_name);
const ClassID class_id = ClassDictionary::Get().lookup(class_name);
applyClassProperties(class_id, already_applied, now, defaultTransition);
}

View File

@@ -554,7 +554,7 @@ void StyleParser::parseStyles(JSVal value, std::map<ClassID, ClassProperties> &s
if (name == "style") {
parseStyle(replaceConstant(itr->value), styles[ClassID::Default]);
} else if (name.compare(0, 6, "style.") == 0 && name.length() > 6) {
const ClassID class_id = ClassDictionary::Lookup(name.substr(6));
const ClassID class_id = ClassDictionary::Get().lookup(name.substr(6));
parseStyle(replaceConstant(itr->value), styles[class_id]);
}
}