Thread<T> is a generic thread management object that starts a thread, creates an object T in that thread and calls its .start() function. When the Thread<T> object is destructed (which must happen in the thread that created it), it'll call the object's .stop() function, and wait for thread termination. The .stop() function must somehow get the thread to terminate. Note that object T's constructor/destructor/start()/stop() must be protected/private, and Thread<T> must be declared as a friend class. All public functions may be called from any thread and are exposed through operator->(). RunLoop encapsulates a libuv runloop and has a facility of dispatching function objects to the thread. It can be used either as a worker thread class by itself, or it can be derived from. This commit converts SQLiteCache to derive from RunLoop and removes the custom code used previously for managing the cache thread.
152 lines
5.3 KiB
Plaintext
152 lines
5.3 KiB
Plaintext
#include <mbgl/platform/log.hpp>
|
|
#include <mbgl/platform/platform.hpp>
|
|
#include <mbgl/platform/darwin/settings_nsuserdefaults.hpp>
|
|
#include <mbgl/platform/darwin/Reachability.h>
|
|
#include <mbgl/platform/default/glfw_view.hpp>
|
|
#include <mbgl/storage/default_file_source.hpp>
|
|
#include <mbgl/storage/default/sqlite_cache.hpp>
|
|
#include <mbgl/storage/network_status.hpp>
|
|
#include <mbgl/util/thread.hpp>
|
|
|
|
#include <mbgl/util/geo.hpp>
|
|
|
|
#import <Foundation/Foundation.h>
|
|
|
|
@interface URLHandler : NSObject
|
|
@property (nonatomic) mbgl::Map *map;
|
|
|
|
- (void)handleGetURLEvent:(NSAppleEventDescriptor *)event
|
|
withReplyEvent:(NSAppleEventDescriptor *)replyEvent;
|
|
@end
|
|
|
|
@implementation URLHandler
|
|
|
|
- (void)handleGetURLEvent:(NSAppleEventDescriptor *)event
|
|
withReplyEvent:(NSAppleEventDescriptor *)replyEvent {
|
|
(void)replyEvent;
|
|
|
|
NSString* urlString = [[event paramDescriptorForKeyword:keyDirectObject] stringValue];
|
|
NSURL *url = [NSURL URLWithString:urlString];
|
|
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
|
|
for (NSString *param in [[url query] componentsSeparatedByString:@"&"]) {
|
|
NSArray *parts = [param componentsSeparatedByString:@"="];
|
|
if([parts count] < 2) continue;
|
|
[params setObject:[parts objectAtIndex:1] forKey:[parts objectAtIndex:0]];
|
|
}
|
|
|
|
mbgl::LatLng latLng = mbgl::LatLng(0, 0);
|
|
double zoom = 0, bearing = 0;
|
|
bool hasCenter = false, hasZoom = false, hasBearing = false;
|
|
|
|
NSString *centerString = [params objectForKey:@"center"];
|
|
if (centerString) {
|
|
NSArray *latLngValues = [centerString componentsSeparatedByString:@","];
|
|
if ([latLngValues count] == 2) {
|
|
latLng.latitude = [latLngValues[0] doubleValue];
|
|
latLng.longitude = [latLngValues[1] doubleValue];
|
|
hasCenter = true;
|
|
}
|
|
}
|
|
|
|
NSString *zoomString = [params objectForKey:@"zoom"];
|
|
if (zoomString) {
|
|
zoom = [zoomString doubleValue];
|
|
hasZoom = true;
|
|
}
|
|
|
|
NSString *bearingString = [params objectForKey:@"bearing"];
|
|
if (bearingString) {
|
|
bearing = [bearingString doubleValue];
|
|
hasBearing = true;
|
|
}
|
|
|
|
if ([self map]) {
|
|
if (hasCenter && hasZoom) {
|
|
[self map]->setLatLngZoom(latLng, zoom);
|
|
} else if (hasCenter) {
|
|
[self map]->setLatLng(latLng);
|
|
} else if (hasZoom) {
|
|
[self map]->setZoom(zoom);
|
|
}
|
|
|
|
if (hasBearing) {
|
|
[self map]->setBearing(bearing);
|
|
}
|
|
}
|
|
}
|
|
@end
|
|
|
|
// Returns the path to the default cache database on this system.
|
|
const std::string &defaultCacheDatabase() {
|
|
static const std::string path = []() -> std::string {
|
|
NSArray *paths = NSSearchPathForDirectoriesInDomains(
|
|
NSApplicationSupportDirectory, NSUserDomainMask, YES);
|
|
if ([paths count] == 0) {
|
|
// Disable the cache if we don't have a location to write.
|
|
return "";
|
|
}
|
|
|
|
NSString *p = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Mapbox GL"];
|
|
|
|
if (![[NSFileManager defaultManager] createDirectoryAtPath:p
|
|
withIntermediateDirectories:YES
|
|
attributes:nil
|
|
error:nil]) {
|
|
// Disable the cache if we couldn't create the directory.
|
|
return "";
|
|
}
|
|
|
|
return [[p stringByAppendingPathComponent:@"cache.db"] UTF8String];
|
|
}();
|
|
return path;
|
|
}
|
|
|
|
int main() {
|
|
GLFWView view;
|
|
|
|
mbgl::util::Thread<mbgl::SQLiteCache> cache(defaultCacheDatabase());
|
|
mbgl::DefaultFileSource fileSource(cache);
|
|
mbgl::Map map(view, fileSource);
|
|
|
|
URLHandler *handler = [[URLHandler alloc] init];
|
|
[handler setMap:&map];
|
|
NSAppleEventManager *appleEventManager = [NSAppleEventManager sharedAppleEventManager];
|
|
[appleEventManager setEventHandler:handler andSelector:@selector(handleGetURLEvent:withReplyEvent:) forEventClass:kInternetEventClass andEventID:kAEGetURL];
|
|
|
|
// Notify map object when network reachability status changes.
|
|
Reachability* reachability = [Reachability reachabilityForInternetConnection];
|
|
reachability.reachableBlock = ^(Reachability *) {
|
|
mbgl::NetworkStatus::Reachable();
|
|
};
|
|
[reachability startNotifier];
|
|
|
|
// Load settings
|
|
mbgl::Settings_NSUserDefaults settings;
|
|
map.setLatLngZoom(mbgl::LatLng(settings.latitude, settings.longitude), settings.zoom);
|
|
map.setBearing(settings.bearing);
|
|
map.setDebug(settings.debug);
|
|
|
|
// Set access token if present
|
|
NSString *accessToken = [[NSProcessInfo processInfo] environment][@"MAPBOX_ACCESS_TOKEN"];
|
|
if (!accessToken) mbgl::Log::Warning(mbgl::Event::Setup, "No access token set. Mapbox vector tiles won't work.");
|
|
if (accessToken) map.setAccessToken([accessToken cStringUsingEncoding:[NSString defaultCStringEncoding]]);
|
|
|
|
// Load style
|
|
map.setStyleURL("asset://styles/bright-v7.json");
|
|
|
|
int ret = view.run();
|
|
|
|
[reachability stopNotifier];
|
|
|
|
// Save settings
|
|
mbgl::LatLng latLng = map.getLatLng();
|
|
settings.latitude = latLng.latitude;
|
|
settings.longitude = latLng.longitude;
|
|
settings.zoom = map.getZoom();
|
|
settings.bearing = map.getBearing();
|
|
settings.debug = map.getDebug();
|
|
settings.save();
|
|
|
|
return ret;
|
|
}
|