Add VariantImpl::isUnbound()

This commit is contained in:
Benoit Blanchon
2025-07-11 14:32:30 +02:00
parent dca5ba481f
commit 7b7ee6cdeb
7 changed files with 12 additions and 10 deletions

View File

@ -45,7 +45,7 @@ template <template <typename> class TDeserializer, typename TDestination,
DeserializationError doDeserialize(TDestination&& dst, TReader reader, DeserializationError doDeserialize(TDestination&& dst, TReader reader,
TOptions options) { TOptions options) {
auto impl = VariantAttorney::getOrCreateImpl(dst); auto impl = VariantAttorney::getOrCreateImpl(dst);
if (impl.getData() == nullptr) if (impl.isUnbound())
return DeserializationError::NoMemory; return DeserializationError::NoMemory;
auto resources = impl.getResourceManager(); auto resources = impl.getResourceManager();
dst.clear(); dst.clear();

View File

@ -232,7 +232,7 @@ class StringBuilderPrint : public Print {
inline void convertToJson(const ::Printable& src, JsonVariant dst) { inline void convertToJson(const ::Printable& src, JsonVariant dst) {
auto impl = detail::VariantAttorney::getImpl(dst); auto impl = detail::VariantAttorney::getImpl(dst);
if (!impl.getData()) if (impl.isUnbound())
return; return;
impl.clear(); impl.clear();
detail::StringBuilderPrint print(impl.getResourceManager()); detail::StringBuilderPrint print(impl.getResourceManager());

View File

@ -52,8 +52,7 @@ struct Converter<JsonVariant> : private detail::VariantAttorney {
} }
static bool checkJson(JsonVariant src) { static bool checkJson(JsonVariant src) {
auto data = getImpl(src).getData(); return !getImpl(src).isUnbound();
return !!data;
} }
}; };
@ -68,8 +67,7 @@ struct Converter<JsonVariantConst> : private detail::VariantAttorney {
} }
static bool checkJson(JsonVariantConst src) { static bool checkJson(JsonVariantConst src) {
auto data = getImpl(src).getData(); return !getImpl(src).isUnbound();
return !!data;
} }
}; };

View File

@ -53,7 +53,7 @@ class JsonVariantConst : public detail::VariantTag,
// Returns true if the reference is unbound. // Returns true if the reference is unbound.
bool isUnbound() const { bool isUnbound() const {
return impl_.getData() == nullptr; return impl_.isUnbound();
} }
// Returns the depth (nesting level) of the value. // Returns the depth (nesting level) of the value.

View File

@ -319,6 +319,10 @@ class VariantImpl {
} }
} }
bool isUnbound() const {
return !data_;
}
bool isNull() const { bool isNull() const {
return type() == VariantType::Null; return type() == VariantType::Null;
} }

View File

@ -40,7 +40,7 @@ class VariantRefBase : public VariantTag {
// Returns true if the reference is unbound. // Returns true if the reference is unbound.
bool isUnbound() const { bool isUnbound() const {
return !getImpl().getData(); return getImpl().isUnbound();
} }
// Casts the value to the specified type. // Casts the value to the specified type.

View File

@ -145,7 +145,7 @@ template <typename TDerived>
template <typename T, enable_if_t<is_same<T, JsonArray>::value, int>> template <typename T, enable_if_t<is_same<T, JsonArray>::value, int>>
inline JsonArray VariantRefBase<TDerived>::to() const { inline JsonArray VariantRefBase<TDerived>::to() const {
auto impl = getOrCreateImpl(); auto impl = getOrCreateImpl();
if (!impl.getData()) if (impl.isUnbound())
return JsonArray(); return JsonArray();
impl.clear(); impl.clear();
impl.getData()->toArray(); impl.getData()->toArray();
@ -156,7 +156,7 @@ template <typename TDerived>
template <typename T, enable_if_t<is_same<T, JsonObject>::value, int>> template <typename T, enable_if_t<is_same<T, JsonObject>::value, int>>
JsonObject VariantRefBase<TDerived>::to() const { JsonObject VariantRefBase<TDerived>::to() const {
auto impl = getOrCreateImpl(); auto impl = getOrCreateImpl();
if (!impl.getData()) if (impl.isUnbound())
return JsonObject(); return JsonObject();
impl.clear(); impl.clear();
impl.getData()->toObject(); impl.getData()->toObject();