Commit 9f58e96b6d0235f5187895103312e3cbf950fb55

Authored by m-holger
1 parent 46425856

Introduce private-API `Name` class and update name handling in `QPDFObjectHandle`.

libqpdf/QPDFObjectHandle.cc
@@ -952,7 +952,33 @@ QPDFObjectHandle::getValueAsReal(std::string& value) const @@ -952,7 +952,33 @@ QPDFObjectHandle::getValueAsReal(std::string& value) const
952 return true; 952 return true;
953 } 953 }
954 954
955 -// Name accessors 955 +// Name methods
  956 +
  957 +QPDFObjectHandle
  958 +QPDFObjectHandle::newName(std::string const& name)
  959 +{
  960 + return {QPDFObject::create<QPDF_Name>(name)};
  961 +}
  962 +
  963 +Name::Name(std::string const& name) :
  964 + BaseHandle(QPDFObject::create<QPDF_Name>(name))
  965 +{
  966 +}
  967 +
  968 +Name::Name(std::string&& name) :
  969 + BaseHandle(QPDFObject::create<QPDF_Name>(std::move(name)))
  970 +{
  971 +}
  972 +
  973 +std::string const&
  974 +Name::value() const
  975 +{
  976 + auto* n = as<QPDF_Name>();
  977 + if (!n) {
  978 + throw invalid_error("Name");
  979 + }
  980 + return n->name;
  981 +}
956 982
957 std::string 983 std::string
958 QPDFObjectHandle::getName() const 984 QPDFObjectHandle::getName() const
@@ -1695,12 +1721,6 @@ QPDFObjectHandle::newReal(double value, int decimal_places, bool trim_trailing_z @@ -1695,12 +1721,6 @@ QPDFObjectHandle::newReal(double value, int decimal_places, bool trim_trailing_z
1695 } 1721 }
1696 1722
1697 QPDFObjectHandle 1723 QPDFObjectHandle
1698 -QPDFObjectHandle::newName(std::string const& name)  
1699 -{  
1700 - return {QPDFObject::create<QPDF_Name>(name)};  
1701 -}  
1702 -  
1703 -QPDFObjectHandle  
1704 QPDFObjectHandle::newString(std::string const& str) 1724 QPDFObjectHandle::newString(std::string const& str)
1705 { 1725 {
1706 return {QPDFObject::create<QPDF_String>(str)}; 1726 return {QPDFObject::create<QPDF_String>(str)};
libqpdf/qpdf/QPDFObjectHandle_private.hh
@@ -324,7 +324,7 @@ namespace qpdf @@ -324,7 +324,7 @@ namespace qpdf
324 { 324 {
325 } 325 }
326 326
327 - // Return the integer value. If the object is not a valid integer, throw a 327 + // Return the integer value. If the object is not a valid Integer, throw a
328 // std::invalid_argument exception. If the object is out of range for the target type, 328 // std::invalid_argument exception. If the object is out of range for the target type,
329 // throw a std::overflow_error or std::underflow_error exception. 329 // throw a std::overflow_error or std::underflow_error exception.
330 template <std::integral T> 330 template <std::integral T>
@@ -391,6 +391,45 @@ namespace qpdf @@ -391,6 +391,45 @@ namespace qpdf
391 // escaping. Return {false, false} if the name is not valid utf-8, otherwise return {true, 391 // escaping. Return {false, false} if the name is not valid utf-8, otherwise return {true,
392 // true} if no characters require or {true, false} if escaping is required. 392 // true} if no characters require or {true, false} if escaping is required.
393 static std::pair<bool, bool> analyzeJSONEncoding(std::string const& name); 393 static std::pair<bool, bool> analyzeJSONEncoding(std::string const& name);
  394 +
  395 + Name() = default;
  396 + Name(Name const&) = default;
  397 + Name(Name&&) = default;
  398 + Name& operator=(Name const&) = default;
  399 + Name& operator=(Name&&) = default;
  400 + ~Name() = default;
  401 +
  402 + explicit Name(std::string const&);
  403 + explicit Name(std::string&&);
  404 +
  405 + Name(QPDFObjectHandle const& oh) :
  406 + BaseHandle(oh.type_code() == ::ot_name ? oh : QPDFObjectHandle())
  407 + {
  408 + }
  409 +
  410 + Name(QPDFObjectHandle&& oh) :
  411 + BaseHandle(oh.type_code() == ::ot_name ? std::move(oh) : QPDFObjectHandle())
  412 + {
  413 + }
  414 +
  415 + // Return the name value. If the object is not a valid Name, throw a
  416 + // std::invalid_argument exception.
  417 + operator std::string() const&
  418 + {
  419 + return value();
  420 + }
  421 +
  422 + // Return the integer value. If the object is not a valid integer, throw a
  423 + // std::invalid_argument exception.
  424 + std::string const& value() const;
  425 +
  426 + // Return true if object value is equal to the 'rhs' value. Return false if the object is
  427 + // not a valid Name.
  428 + friend bool
  429 + operator==(Name const& lhs, std::string_view rhs)
  430 + {
  431 + return lhs && lhs.value() == rhs;
  432 + }
394 }; 433 };
395 434
396 class Stream final: public BaseHandle 435 class Stream final: public BaseHandle
libqpdf/qpdf/QPDFObject_private.hh
@@ -28,6 +28,7 @@ namespace qpdf @@ -28,6 +28,7 @@ namespace qpdf
28 class BaseDictionary; 28 class BaseDictionary;
29 class Dictionary; 29 class Dictionary;
30 class Integer; 30 class Integer;
  31 + class Name;
31 class Stream; 32 class Stream;
32 } // namespace qpdf 33 } // namespace qpdf
33 34
@@ -138,6 +139,7 @@ class QPDF_Name final @@ -138,6 +139,7 @@ class QPDF_Name final
138 { 139 {
139 friend class QPDFObject; 140 friend class QPDFObject;
140 friend class qpdf::BaseHandle; 141 friend class qpdf::BaseHandle;
  142 + friend class qpdf::Name;
141 143
142 explicit QPDF_Name(std::string name) : 144 explicit QPDF_Name(std::string name) :
143 name(std::move(name)) 145 name(std::move(name))