Use std::optional instead of mbgl::optional (#911)

This commit is contained in:
Bart Louwers
2023-03-17 12:51:59 +01:00
committed by GitHub
parent 36846439c6
commit 15ad65dd3d
482 changed files with 2605 additions and 2652 deletions

View File

@@ -6,7 +6,6 @@
#include <nan.h>
#pragma GCC diagnostic pop
#include <mbgl/util/optional.hpp>
#include <mbgl/util/feature.hpp>
#include <mbgl/style/conversion/geojson.hpp>
#include <mbgl/style/conversion_impl.hpp>
@@ -47,7 +46,7 @@ public:
return value->IsObject() && !value->IsArray();
}
static optional<v8::Local<v8::Value>> objectMember(const v8::Local<v8::Value>& value, const char * name) {
static std::optional<v8::Local<v8::Value>> objectMember(const v8::Local<v8::Value>& value, const char * name) {
Nan::EscapableHandleScope scope;
if (!Nan::Has(Nan::To<v8::Object>(value).ToLocalChecked(), Nan::New(name).ToLocalChecked()).FromJust()) {
return {};
@@ -60,13 +59,13 @@ public:
}
template <class Fn>
static optional<Error> eachMember(const v8::Local<v8::Value>& value, Fn&& fn) {
static std::optional<Error> eachMember(const v8::Local<v8::Value>& value, Fn&& fn) {
Nan::HandleScope scope;
v8::Local<v8::Array> names = Nan::GetOwnPropertyNames(Nan::To<v8::Object>(value).ToLocalChecked()).ToLocalChecked();
for (uint32_t i = 0; i < names->Length(); ++i) {
v8::Local<v8::Value> k = Nan::Get(names, i).ToLocalChecked();
v8::Local<v8::Value> v = Nan::Get(Nan::To<v8::Object>(value).ToLocalChecked(), k).ToLocalChecked();
optional<Error> result = fn(*Nan::Utf8String(k), std::move(v)); // NOLINT(performance-move-const-arg)
std::optional<Error> result = fn(*Nan::Utf8String(k), std::move(v)); // NOLINT(performance-move-const-arg)
if (result) {
return result;
}
@@ -74,7 +73,7 @@ public:
return {};
}
static optional<bool> toBool(const v8::Local<v8::Value>& value) {
static std::optional<bool> toBool(const v8::Local<v8::Value>& value) {
Nan::HandleScope scope;
if (!value->IsBoolean()) {
return {};
@@ -82,7 +81,7 @@ public:
return Nan::To<bool>(value).ToChecked();
}
static optional<float> toNumber(const v8::Local<v8::Value>& value) {
static std::optional<float> toNumber(const v8::Local<v8::Value>& value) {
Nan::HandleScope scope;
if (!value->IsNumber()) {
return {};
@@ -90,7 +89,7 @@ public:
return static_cast<float>(Nan::To<double>(value).ToChecked());
}
static optional<double> toDouble(const v8::Local<v8::Value>& value) {
static std::optional<double> toDouble(const v8::Local<v8::Value>& value) {
Nan::HandleScope scope;
if (!value->IsNumber()) {
return {};
@@ -98,7 +97,7 @@ public:
return Nan::To<double>(value).ToChecked();
}
static optional<std::string> toString(const v8::Local<v8::Value>& value) {
static std::optional<std::string> toString(const v8::Local<v8::Value>& value) {
Nan::HandleScope scope;
if (!value->IsString()) {
return {};
@@ -106,7 +105,7 @@ public:
return std::string(*Nan::Utf8String(value));
}
static optional<Value> toValue(const v8::Local<v8::Value>& value) {
static std::optional<Value> toValue(const v8::Local<v8::Value>& value) {
if (value->IsFalse()) {
return { false };
} else if (value->IsTrue()) {
@@ -124,7 +123,7 @@ public:
}
}
static optional<GeoJSON> toGeoJSON(const v8::Local<v8::Value>& value, Error& error) {
static std::optional<GeoJSON> toGeoJSON(const v8::Local<v8::Value>& value, Error& error) {
try {
Nan::JSON NanJSON;
v8::Local<v8::Object> obj = Nan::To<v8::Object>(value).ToLocalChecked();
@@ -139,7 +138,7 @@ public:
};
template <class T, class...Args>
optional<T> convert(const v8::Local<v8::Value>& value, Error& error, Args&&...args) {
std::optional<T> convert(const v8::Local<v8::Value>& value, Error& error, Args&&...args) {
return convert<T>(Convertible(value), error, std::forward<Args>(args)...);
}

View File

@@ -60,7 +60,7 @@ type::Type parseType(v8::Local<v8::Object> type) {
v8::Local<v8::Context> context = type->GetCreationContext().ToLocalChecked();
#endif
type::Type itemType = parseType(Nan::Get(type, Nan::New("itemType").ToLocalChecked()).ToLocalChecked()->ToObject(context).ToLocalChecked());
mbgl::optional<std::size_t> N;
std::optional<std::size_t> N;
v8::Local<v8::String> Nkey = Nan::New("N").ToLocalChecked();
if (Nan::Has(type, Nkey).FromMaybe(false)) {
@@ -80,7 +80,7 @@ void NodeExpression::Parse(const Nan::FunctionCallbackInfo<v8::Value>& info) {
return Nan::ThrowTypeError("Requires a JSON style expression argument.");
}
mbgl::optional<type::Type> expected;
std::optional<type::Type> expected;
if (info.Length() > 1 && info[1]->IsObject()) {
expected = parseType(info[1]->ToObject(context).ToLocalChecked());
}
@@ -247,17 +247,17 @@ void NodeExpression::Evaluate(const Nan::FunctionCallbackInfo<v8::Value>& info)
return Nan::ThrowTypeError("Requires globals and feature arguments.");
}
mbgl::optional<float> zoom;
std::optional<float> zoom;
v8::Local<v8::Value> v8zoom = Nan::Get(info[0]->ToObject(context).ToLocalChecked(), Nan::New("zoom").ToLocalChecked()).ToLocalChecked();
if (v8zoom->IsNumber()) zoom = static_cast<float>(Nan::To<double>(v8zoom).FromJust());
mbgl::optional<double> heatmapDensity;
std::optional<double> heatmapDensity;
v8::Local<v8::Value> v8heatmapDensity = Nan::Get(info[0]->ToObject(context).ToLocalChecked(), Nan::New("heatmapDensity").ToLocalChecked()).ToLocalChecked();
if (v8heatmapDensity->IsNumber()) heatmapDensity = Nan::To<double>(v8heatmapDensity).FromJust();
Nan::JSON NanJSON;
conversion::Error conversionError;
mbgl::optional<mbgl::GeoJSON> geoJSON = conversion::convert<mbgl::GeoJSON>(info[1], conversionError);
std::optional<mbgl::GeoJSON> geoJSON = conversion::convert<mbgl::GeoJSON>(info[1], conversionError);
if (!geoJSON) {
Nan::ThrowTypeError(conversionError.message.c_str());
return;

View File

@@ -708,7 +708,7 @@ void NodeMap::AddSource(const Nan::FunctionCallbackInfo<v8::Value>& info) {
}
Error error;
mbgl::optional<std::unique_ptr<Source>> source = convert<std::unique_ptr<Source>>(info[1], error, *Nan::Utf8String(info[0]));
std::optional<std::unique_ptr<Source>> source = convert<std::unique_ptr<Source>>(info[1], error, *Nan::Utf8String(info[0]));
if (!source) {
Nan::ThrowTypeError(error.message.c_str());
return;
@@ -747,7 +747,7 @@ void NodeMap::AddLayer(const Nan::FunctionCallbackInfo<v8::Value>& info) {
}
Error error;
mbgl::optional<std::unique_ptr<Layer>> layer = convert<std::unique_ptr<Layer>>(info[0], error);
std::optional<std::unique_ptr<Layer>> layer = convert<std::unique_ptr<Layer>>(info[0], error);
if (!layer) {
Nan::ThrowTypeError(error.message.c_str());
return;
@@ -912,7 +912,7 @@ void NodeMap::SetLayerProperty(const Nan::FunctionCallbackInfo<v8::Value>& info)
return Nan::ThrowTypeError("Second argument must be a string");
}
mbgl::optional<Error> error = layer->setProperty(*Nan::Utf8String(info[1]), Convertible(info[2]));
std::optional<Error> error = layer->setProperty(*Nan::Utf8String(info[1]), Convertible(info[2]));
if (error) {
return Nan::ThrowTypeError(error->message.c_str());
}
@@ -944,7 +944,7 @@ void NodeMap::SetFilter(const Nan::FunctionCallbackInfo<v8::Value>& info) {
if (!info[1]->IsNull() && !info[1]->IsUndefined()) {
Error error;
mbgl::optional<Filter> converted = convert<Filter>(info[1], error);
std::optional<Filter> converted = convert<Filter>(info[1], error);
if (!converted) {
Nan::ThrowTypeError(error.message.c_str());
return;
@@ -1149,7 +1149,7 @@ void NodeMap::SetFeatureState(const Nan::FunctionCallbackInfo<v8::Value>& info)
std::string sourceID;
std::string featureID;
mbgl::optional<std::string> sourceLayerID;
std::optional<std::string> sourceLayerID;
auto feature = Nan::To<v8::Object>(info[0]).ToLocalChecked();
if (Nan::Has(feature, Nan::New("source").ToLocalChecked()).FromJust()) {
auto sourceOption = Nan::Get(feature, Nan::New("source").ToLocalChecked()).ToLocalChecked();
@@ -1191,9 +1191,9 @@ void NodeMap::SetFeatureState(const Nan::FunctionCallbackInfo<v8::Value>& info)
bool valueParsed = false;
FeatureState newState;
const std::function<optional<Error>(const std::string&, const Convertible&)> convertFn =
[&](const std::string& k, const Convertible& v) -> optional<Error> {
optional<Value> value = toValue(v);
const std::function<std::optional<Error>(const std::string&, const Convertible&)> convertFn =
[&](const std::string& k, const Convertible& v) -> std::optional<Error> {
std::optional<Value> value = toValue(v);
if (value) {
stateValue = std::move(*value);
valueParsed = true;
@@ -1202,7 +1202,7 @@ void NodeMap::SetFeatureState(const Nan::FunctionCallbackInfo<v8::Value>& info)
std::size_t length = arrayLength(v);
array.reserve(length);
for (size_t i = 0; i < length; ++i) {
optional<Value> arrayVal = toValue(arrayMember(v, i));
std::optional<Value> arrayVal = toValue(arrayMember(v, i));
if (arrayVal) {
array.emplace_back(*arrayVal);
}
@@ -1218,11 +1218,11 @@ void NodeMap::SetFeatureState(const Nan::FunctionCallbackInfo<v8::Value>& info)
}
if (!valueParsed) {
Nan::ThrowTypeError("Could not get feature state value");
return nullopt;
return std::nullopt;
}
stateKey = k;
newState[stateKey] = stateValue;
return nullopt;
return std::nullopt;
};
eachMember(state, convertFn);
@@ -1250,7 +1250,7 @@ void NodeMap::GetFeatureState(const Nan::FunctionCallbackInfo<v8::Value>& info)
std::string sourceID;
std::string featureID;
mbgl::optional<std::string> sourceLayerID;
std::optional<std::string> sourceLayerID;
auto feature = Nan::To<v8::Object>(info[0]).ToLocalChecked();
if (Nan::Has(feature, Nan::New("source").ToLocalChecked()).FromJust()) {
auto sourceOption = Nan::Get(feature, Nan::New("source").ToLocalChecked()).ToLocalChecked();
@@ -1307,9 +1307,9 @@ void NodeMap::RemoveFeatureState(const Nan::FunctionCallbackInfo<v8::Value>& inf
}
std::string sourceID;
mbgl::optional<std::string> sourceLayerID;
mbgl::optional<std::string> featureID;
mbgl::optional<std::string> stateKey;
std::optional<std::string> sourceLayerID;
std::optional<std::string> featureID;
std::optional<std::string> stateKey;
auto feature = Nan::To<v8::Object>(info[0]).ToLocalChecked();
if (Nan::Has(feature, Nan::New("source").ToLocalChecked()).FromJust()) {
auto sourceOption = Nan::Get(feature, Nan::New("source").ToLocalChecked()).ToLocalChecked();
@@ -1406,7 +1406,7 @@ void NodeMap::QueryRenderedFeatures(const Nan::FunctionCallbackInfo<v8::Value>&
if (Nan::Has(options, Nan::New("filter").ToLocalChecked()).FromJust()) {
auto filterOption = Nan::Get(options, Nan::New("filter").ToLocalChecked()).ToLocalChecked();
Error error;
mbgl::optional<Filter> converted = convert<Filter>(filterOption, error);
std::optional<Filter> converted = convert<Filter>(filterOption, error);
if (!converted) {
return Nan::ThrowTypeError(error.message.c_str());
}