New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
use a singleton pattern to register the gdal driver once in one place #4628
Changes from all commits
733a332
56c6a5e
c8ff6d9
6b81a83
da5c7de
82e11dd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,23 @@ | |
using namespace valhalla::baldr::json; | ||
|
||
namespace { | ||
|
||
// allows us to only ever register the driver once per process without having to put it | ||
// in every executable that might call into this code | ||
struct gdal_singleton_t { | ||
static const gdal_singleton_t& get() { | ||
static const gdal_singleton_t instance; | ||
return instance; | ||
} | ||
|
||
private: | ||
gdal_singleton_t() { | ||
#ifdef ENABLE_GDAL | ||
GDALRegister_GTiff(); | ||
#endif | ||
} | ||
}; | ||
|
||
using rgba_t = std::tuple<float, float, float>; | ||
|
||
using namespace valhalla; | ||
|
@@ -169,7 +186,7 @@ std::string serializeGeoTIFF(Api& request, const std::shared_ptr<const GriddedDa | |
char** geotiff_options = NULL; | ||
geotiff_options = CSLSetNameValue(geotiff_options, "COMPRESS", "PACKBITS"); | ||
|
||
// TODO: check if there's an easy way to only instantiate the driver once | ||
gdal_singleton_t::get(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this first time this is called (per process) the constructor above is called and the driver registration happens. subsequent calls are no-ops |
||
auto driver_manager = GetGDALDriverManager(); | ||
auto geotiff_driver = driver_manager->GetDriverByName("GTiff"); | ||
auto geotiff_dataset = | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -355,7 +355,6 @@ void check_raster_edges(size_t x, size_t y, uint16_t* data) { | |
} | ||
|
||
TEST(Isochrones, test_geotiff_output_distance) { | ||
GDALRegister_GTiff(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i dont really like removing these because the tests themselves use gdal directly to load the response from the library. now in this case technically we can remove them because hidden in the source of the serializer we are doing this initialization but the tests (and just in general any calling code) shouldnt know/assume that its preconditions are met by something it calls into. the only way to get around this would be to expose gdal initialization as a public api in the library and have the tests call that but i hate that idea even worse. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah I didn’t check that.. I kinda assumed those driver inits were there to make the test work (it wouldn’t have without). That’s true.. anyways, the alternative to test this bit is indeed worse. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the other idea is to leave them there though what i would do is in the suite initialization call it once There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Both is fine with me tbh, I get your point, but also think it’ll be fine probably forever ® |
||
loki_worker_t loki_worker(cfg); | ||
thor_worker_t thor_worker(cfg); | ||
|
||
|
@@ -401,7 +400,6 @@ TEST(Isochrones, test_geotiff_output_distance) { | |
} | ||
|
||
TEST(Isochrones, test_geotiff_output_time) { | ||
GDALRegister_GTiff(); | ||
loki_worker_t loki_worker(cfg); | ||
thor_worker_t thor_worker(cfg); | ||
|
||
|
@@ -448,7 +446,6 @@ TEST(Isochrones, test_geotiff_output_time) { | |
|
||
// test request with two metrics | ||
TEST(Isochrones, test_geotiff_output_time_distance) { | ||
GDALRegister_GTiff(); | ||
loki_worker_t loki_worker(cfg); | ||
thor_worker_t thor_worker(cfg); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i thought it would be better to default turning it ON if we can and if we couldnt we simply report that was the case. if you tell it you definitely dont want it by setting it to OFF then we dont even try
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm but doesn’t OFF still want to pull in GDAL::GDAL in tyr’s cmakelists? That’s what that variable was for, though you could handle that with a cmake expression in tyr as well, that’s probably more explicit than the previous var
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep will do that real quick