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

Avoid using <locale> and <iostream> #13135

Merged
merged 12 commits into from
Oct 23, 2018
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
2 changes: 0 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,6 @@ else()
message(STATUS "Can't find ccache — consider installing ccache to improve recompilation performance")
endif()

include(cmake/codecvt.cmake)

if(NOT EXISTS ${CMAKE_SOURCE_DIR}/platform/${MBGL_PLATFORM}/config.cmake)
message(ERROR "Can't find config.cmake file for platform ${MBGL_PLATFORM}")
endif()
Expand Down
21 changes: 21 additions & 0 deletions bin/offline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,27 @@ mapbox::geometry::geometry<double> parseGeometry(const std::string& json) {
});
}

std::ostream& operator<<(std::ostream& os, mbgl::Response::Error::Reason r) {
switch (r) {
case mbgl::Response::Error::Reason::Success:
return os << "Response::Error::Reason::Success";
case mbgl::Response::Error::Reason::NotFound:
return os << "Response::Error::Reason::NotFound";
case mbgl::Response::Error::Reason::Server:
return os << "Response::Error::Reason::Server";
case mbgl::Response::Error::Reason::Connection:
return os << "Response::Error::Reason::Connection";
case mbgl::Response::Error::Reason::RateLimit:
return os << "Response::Error::Reason::RateLimit";
case mbgl::Response::Error::Reason::Other:
return os << "Response::Error::Reason::Other";
kkaefer marked this conversation as resolved.
Show resolved Hide resolved
}

// The above switch is exhaustive, but placate GCC nonetheless:
assert(false);
return os;
}

int main(int argc, char *argv[]) {
args::ArgumentParser argumentParser("Mapbox GL offline tool");
args::HelpFlag helpFlag(argumentParser, "help", "Display this help menu", {'h', "help"});
Expand Down
19 changes: 0 additions & 19 deletions cmake/codecvt.cmake

This file was deleted.

2 changes: 2 additions & 0 deletions cmake/core-files.txt
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,7 @@ src/mbgl/util/geo.cpp
src/mbgl/util/geojson_impl.cpp
src/mbgl/util/grid_index.cpp
src/mbgl/util/grid_index.hpp
src/mbgl/util/hash.hpp
src/mbgl/util/http_header.cpp
src/mbgl/util/http_header.hpp
src/mbgl/util/http_timeout.cpp
Expand All @@ -751,6 +752,7 @@ src/mbgl/util/math.hpp
src/mbgl/util/offscreen_texture.cpp
src/mbgl/util/offscreen_texture.hpp
src/mbgl/util/premultiply.cpp
src/mbgl/util/rapidjson.cpp
src/mbgl/util/rapidjson.hpp
src/mbgl/util/rect.hpp
src/mbgl/util/std.hpp
Expand Down
2 changes: 0 additions & 2 deletions cmake/core.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ target_include_directories(mbgl-core
PRIVATE src
)

target_link_libraries(mbgl-core PRIVATE codecvt)

target_add_mason_package(mbgl-core PUBLIC geometry)
target_add_mason_package(mbgl-core PUBLIC variant)
target_add_mason_package(mbgl-core PRIVATE unique_resource)
Expand Down
2 changes: 1 addition & 1 deletion cmake/mason-dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ mason_use(cheap-ruler VERSION 2.5.3 HEADER_ONLY)
mason_use(vector-tile VERSION 1.0.2 HEADER_ONLY)

if(MBGL_PLATFORM STREQUAL "android")
mason_use(jni.hpp VERSION 4.0.0 HEADER_ONLY)
mason_use(jni.hpp VERSION 4.0.1 HEADER_ONLY)
elseif(MBGL_PLATFORM STREQUAL "ios")
# noop
elseif(MBGL_PLATFORM STREQUAL "linux")
Expand Down
1 change: 1 addition & 0 deletions cmake/test-files.txt
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ test/util/peer.test.cpp
test/util/position.test.cpp
test/util/projection.test.cpp
test/util/run_loop.test.cpp
test/util/string.test.cpp
test/util/text_conversions.test.cpp
test/util/thread.test.cpp
test/util/thread_local.test.cpp
Expand Down
2 changes: 0 additions & 2 deletions include/mbgl/storage/response.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,4 @@ class Response::Error {
Error(Reason, std::string = "", optional<Timestamp> = {});
};

std::ostream& operator<<(std::ostream&, Response::Error::Reason);

} // namespace mbgl
85 changes: 51 additions & 34 deletions include/mbgl/util/string.hpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,14 @@
#pragma once

#include <sstream>
#include <string>
#include <cassert>
#include <cstdlib>
#include <exception>
#include <type_traits>

// Polyfill needed by Qt when building for Android with GCC
#if defined(__ANDROID__) && defined(__GLIBCXX__)

namespace std {

template <typename T>
std::string to_string(T value)
{
std::ostringstream oss;
oss << value;

return oss.str();
}

inline int stoi(const std::string &str)
{
return atoi(str.c_str());
Expand All @@ -36,42 +25,70 @@ inline float stof(const std::string &str) {
namespace mbgl {
namespace util {

template <class T>
inline std::string toString(T t) {
return std::to_string(t);
std::string toString(int64_t);
std::string toString(uint64_t);
std::string toString(int32_t);
std::string toString(uint32_t);
std::string toString(double, bool decimal = false);

inline std::string toString(int16_t t) {
return toString(static_cast<int32_t>(t));
}

inline std::string toString(int8_t num) {
return std::to_string(int(num));
inline std::string toString(uint16_t t) {
return toString(static_cast<uint32_t>(t));
}

inline std::string toString(uint8_t num) {
return std::to_string(unsigned(num));
inline std::string toString(int8_t t) {
return toString(static_cast<int32_t>(t));
}

std::string toString(float);
std::string toString(double);
std::string toString(long double);
inline std::string toString(uint8_t t) {
return toString(static_cast<uint32_t>(t));
}

inline std::string toString(std::exception_ptr error) {
assert(error);
template <typename = std::enable_if<!std::is_same<uint64_t, unsigned long>::value>>
inline std::string toString(unsigned long t) {
return toString(static_cast<uint64_t>(t));
}

if (!error) {
return "(null)";
}
template <typename = std::enable_if<!std::is_same<uint64_t, unsigned long long>::value>>
inline std::string toString(unsigned long long t) {
return toString(static_cast<uint64_t>(t));
}

try {
std::rethrow_exception(error);
} catch (const std::exception& ex) {
return ex.what();
} catch (...) {
return "Unknown exception type";
}
inline std::string toString(float t, bool decimal = false) {
return toString(static_cast<double>(t), decimal);
}

inline std::string toString(long double t, bool decimal = false) {
return toString(static_cast<double>(t), decimal);
}

std::string toString(std::exception_ptr);

template <class T>
std::string toString(T) = delete;

std::string toHex(size_t);

inline float stof(const std::string& str) {
return std::stof(str);
}

} // namespace util
} // namespace mbgl

// Android's libstdc++ doesn't have std::to_string()
#if defined(__ANDROID__) && defined(__GLIBCXX__)

namespace std {

template <typename T>
inline std::string to_string(T value) {
return mbgl::util::toString(value);
}

} // namespace std

#endif
7 changes: 4 additions & 3 deletions platform/android/config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ macro(mbgl_filesource)
target_add_mason_package(mbgl-filesource PUBLIC jni.hpp)

target_link_libraries(mbgl-filesource
PRIVATE codecvt
PUBLIC sqlite
PUBLIC -llog
PUBLIC -landroid
Expand All @@ -84,8 +83,11 @@ add_library(mapbox-gl SHARED
platform/android/src/main.cpp
)

target_include_directories(mapbox-gl
PRIVATE src
)

target_link_libraries(mapbox-gl
PRIVATE codecvt
PRIVATE mbgl-core
PRIVATE mbgl-filesource
)
Expand All @@ -106,7 +108,6 @@ macro(mbgl_platform_test)
)

target_link_libraries(mbgl-test
PRIVATE codecvt
PRIVATE mbgl-core
PRIVATE mbgl-filesource
)
Expand Down
17 changes: 13 additions & 4 deletions platform/android/src/conversion/constant.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "constant.hpp"
#include "collection.hpp"

#include <sstream>
#include <mbgl/util/string.hpp>

namespace mbgl {
namespace android {
Expand All @@ -24,9 +24,18 @@ Result<jni::Local<jni::Object<>>> Converter<jni::Local<jni::Object<>>, std::stri
}

Result<jni::Local<jni::Object<>>> Converter<jni::Local<jni::Object<>>, Color>::operator()(jni::JNIEnv& env, const Color& value) const {
std::stringstream sstream;
sstream << "rgba(" << value.r << ", " << value.g << ", " << value.b << ", " << value.a << ")";
return jni::Make<jni::String>(env, sstream.str());
std::string result;
result.reserve(32);
result += "rgba(";
result += util::toString(value.r);
result += ", ";
result += util::toString(value.g);
result += ", ";
result += util::toString(value.b);
result += ", ";
result += util::toString(value.a);
result += ")";
return jni::Make<jni::String>(env, result);
}

Result<jni::Local<jni::Object<>>> Converter<jni::Local<jni::Object<>>, style::expression::Formatted>::operator()(jni::JNIEnv& env, const style::expression::Formatted& value) const {
Expand Down
2 changes: 1 addition & 1 deletion platform/android/src/geojson/feature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class FeatureIdVisitor {
public:
template<class T>
std::string operator()(const T& i) const {
return std::to_string(i);
return util::toString(i);
}

std::string operator()(const std::string& i) const {
Expand Down
4 changes: 2 additions & 2 deletions platform/android/src/http_file_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,9 @@ void HTTPRequest::onResponse(jni::JNIEnv& env, int code,
}
response.error = std::make_unique<Error>(Error::Reason::RateLimit, "HTTP status code 429", http::parseRetryHeaders(retryAfter, xRateLimitReset));
} else if (code >= 500 && code < 600) {
response.error = std::make_unique<Error>(Error::Reason::Server, std::string{ "HTTP status code " } + std::to_string(code));
response.error = std::make_unique<Error>(Error::Reason::Server, std::string{ "HTTP status code " } + util::toString(code));
} else {
response.error = std::make_unique<Error>(Error::Reason::Other, std::string{ "HTTP status code " } + std::to_string(code));
response.error = std::make_unique<Error>(Error::Reason::Other, std::string{ "HTTP status code " } + util::toString(code));
}

async.send();
Expand Down
30 changes: 0 additions & 30 deletions platform/default/codecvt/codecvt

This file was deleted.

17 changes: 17 additions & 0 deletions platform/default/jni/string_conversion.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

// This file replaces the default implementation in jni.hpp.

#include <mbgl/util/utf.hpp>

namespace jni {

inline std::u16string convertUTF8ToUTF16(const std::string& str) {
return mbgl::util::convertUTF8ToUTF16(str);
}

inline std::string convertUTF16ToUTF8(const std::u16string& str) {
return mbgl::util::convertUTF16ToUTF8(str);
}

} // namespace jni
11 changes: 7 additions & 4 deletions platform/default/utf.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
#include <mbgl/util/utf.hpp>

#include <locale>
#include <codecvt>
#include <boost/locale/encoding_utf.hpp>

namespace mbgl {
namespace util {

std::u16string utf8_to_utf16::convert(const std::string& utf8) {
return std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>().from_bytes(utf8);
std::u16string convertUTF8ToUTF16(const std::string& str) {
return boost::locale::conv::utf_to_utf<char16_t>(str);
}

std::string convertUTF16ToUTF8(const std::u16string& str) {
return boost::locale::conv::utf_to_utf<char>(str);
}

} // namespace util
Expand Down
4 changes: 2 additions & 2 deletions platform/node/test/js/map.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ test('Map', function(t) {

t.throws(function() {
map.load('foo bar');
}, /Failed to parse style: 1 - Invalid value./);
}, /Failed to parse style: Invalid value. at offset 1/);

t.throws(function() {
map.load('""');
Expand Down Expand Up @@ -349,7 +349,7 @@ test('Map', function(t) {

t.throws(function() {
map.load('invalid');
}, /Failed to parse style: 0 - Invalid value./);
}, /Failed to parse style: Invalid value. at offset 0/);
});

t.test('accepts an empty stylesheet string', function(t) {
Expand Down
Loading