Use Optional in CJX_Object.
Change-Id: Ib3c6371b0ba458575a5b5123b8578785db7c971d
Reviewed-on: https://pdfium-review.googlesource.com/c/47335
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/fxjs/xfa/cjx_object.cpp b/fxjs/xfa/cjx_object.cpp
index b6b2828..ac8fbea 100644
--- a/fxjs/xfa/cjx_object.cpp
+++ b/fxjs/xfa/cjx_object.cpp
@@ -313,13 +313,7 @@
Optional<XFA_ATTRIBUTEINFO> attr = XFA_GetAttributeByName(wsAttr);
if (attr.has_value())
return TryAttribute(attr.value().attribute, bUseDefault);
-
- void* pKey = GetMapKey_Custom(wsAttr);
- WideStringView wsValueC;
- if (!GetMapModuleString(pKey, &wsValueC))
- return {};
-
- return WideString(wsValueC);
+ return GetMapModuleString(GetMapKey_Custom(wsAttr));
}
void CJX_Object::RemoveAttribute(WideStringView wsAttr) {
@@ -329,13 +323,12 @@
}
Optional<bool> CJX_Object::TryBoolean(XFA_Attribute eAttr, bool bUseDefault) {
- void* pValue = nullptr;
void* pKey = GetMapKey_Element(GetXFAObject()->GetElementType(), eAttr);
- if (GetMapModuleValue(pKey, &pValue))
- return !!pValue;
+ Optional<void*> value = GetMapModuleValue(pKey);
+ if (value.has_value())
+ return !!value.value();
if (!bUseDefault)
return {};
-
return ToNode(GetXFAObject())->GetDefaultBoolean(eAttr);
}
@@ -368,9 +361,9 @@
Optional<int32_t> CJX_Object::TryInteger(XFA_Attribute eAttr,
bool bUseDefault) {
void* pKey = GetMapKey_Element(GetXFAObject()->GetElementType(), eAttr);
- void* pValue = nullptr;
- if (GetMapModuleValue(pKey, &pValue))
- return static_cast<int32_t>(reinterpret_cast<uintptr_t>(pValue));
+ Optional<void*> value = GetMapModuleValue(pKey);
+ if (value.has_value())
+ return static_cast<int32_t>(reinterpret_cast<uintptr_t>(value.value()));
if (!bUseDefault)
return {};
return ToNode(GetXFAObject())->GetDefaultInteger(eAttr);
@@ -379,9 +372,11 @@
Optional<XFA_AttributeValue> CJX_Object::TryEnum(XFA_Attribute eAttr,
bool bUseDefault) const {
void* pKey = GetMapKey_Element(GetXFAObject()->GetElementType(), eAttr);
- void* pValue = nullptr;
- if (GetMapModuleValue(pKey, &pValue))
- return static_cast<XFA_AttributeValue>(reinterpret_cast<uintptr_t>(pValue));
+ Optional<void*> value = GetMapModuleValue(pKey);
+ if (value.has_value()) {
+ return static_cast<XFA_AttributeValue>(
+ reinterpret_cast<uintptr_t>(value.value()));
+ }
if (!bUseDefault)
return {};
return ToNode(GetXFAObject())->GetDefaultEnum(eAttr);
@@ -516,9 +511,9 @@
if (pStr)
return *pStr;
} else {
- WideStringView wsValueC;
- if (GetMapModuleString(pKey, &wsValueC))
- return WideString(wsValueC);
+ Optional<WideString> value = GetMapModuleString(pKey);
+ if (value.has_value())
+ return value;
}
if (!bUseDefault)
return {};
@@ -899,34 +894,31 @@
CreateMapModuleData()->m_ValueMap[pKey] = pValue;
}
-bool CJX_Object::GetMapModuleValue(void* pKey, void** pValue) const {
+Optional<void*> CJX_Object::GetMapModuleValue(void* pKey) const {
for (const CXFA_Node* pNode = ToNode(GetXFAObject()); pNode;
pNode = pNode->GetTemplateNodeIfExists()) {
XFA_MAPMODULEDATA* pModule = pNode->JSObject()->GetMapModuleData();
if (pModule) {
auto it = pModule->m_ValueMap.find(pKey);
- if (it != pModule->m_ValueMap.end()) {
- *pValue = it->second;
- return true;
- }
+ if (it != pModule->m_ValueMap.end())
+ return it->second;
}
if (pNode->GetPacketType() == XFA_PacketType::Datasets)
break;
}
- return false;
+ return {};
}
-bool CJX_Object::GetMapModuleString(void* pKey, WideStringView* pValue) {
+Optional<WideString> CJX_Object::GetMapModuleString(void* pKey) {
void* pRawValue;
int32_t iBytes;
if (!GetMapModuleBuffer(pKey, &pRawValue, &iBytes))
- return false;
+ return {};
// Defensive measure: no out-of-bounds pointers even if zero length.
int32_t iChars = iBytes / sizeof(wchar_t);
- *pValue = WideStringView(
- iChars ? static_cast<const wchar_t*>(pRawValue) : nullptr, iChars);
- return true;
+ return WideString(iChars ? static_cast<const wchar_t*>(pRawValue) : nullptr,
+ iChars);
}
void CJX_Object::SetMapModuleBuffer(
diff --git a/fxjs/xfa/cjx_object.h b/fxjs/xfa/cjx_object.h
index cdcfa1d..352cbee 100644
--- a/fxjs/xfa/cjx_object.h
+++ b/fxjs/xfa/cjx_object.h
@@ -243,8 +243,8 @@
XFA_MAPMODULEDATA* CreateMapModuleData();
XFA_MAPMODULEDATA* GetMapModuleData() const;
void SetMapModuleValue(void* pKey, void* pValue);
- bool GetMapModuleValue(void* pKey, void** pValue) const;
- bool GetMapModuleString(void* pKey, WideStringView* pValue);
+ Optional<void*> GetMapModuleValue(void* pKey) const;
+ Optional<WideString> GetMapModuleString(void* pKey);
void SetMapModuleBuffer(void* pKey,
void* pValue,
int32_t iBytes,