Add new Pipeline convenience methods

This commit is contained in:
Jay Berkenbilt 2022-05-03 17:58:58 -04:00
parent 59f3e09edf
commit f1c6bb97db
5 changed files with 69 additions and 8 deletions

View File

@ -1,5 +1,10 @@
2022-05-03 Jay Berkenbilt <ejb@ql.org>
* Add new convenience methods to Pipeline: writeCStr and
writeString. Also add a limit << operator that takes C strings and
std::strings. Also add an overloaded version of write that takes
"char const*".
* API change: Pipeline::write now takes "unsigned char const *"
instead of "unsigned char*". Callers shouldn't have to change
anything, though can stop using writable strings or

8
TODO
View File

@ -50,14 +50,6 @@ Need new pipelines:
* Pl_OStream(std::ostream) with semantics like Pl_StdioFile
* Pl_String to std::string with semantics like Pl_Buffer
New Pipeline methods:
* writeString(std::string const&)
* writeCString(char*)
* writeChars(char*, size_t)
* Consider templated operator<< which could specialize for char* and
std::string and could use std::ostringstream otherwise
See if I can change all output and error messages issued by the
library, when context is available, to have a pipeline rather than a
FILE* or std::ostream. This makes it possible for people to capture

View File

@ -71,6 +71,26 @@ class QPDF_DLL_CLASS Pipeline
QPDF_DLL
std::string getIdentifier() const;
// These are convenience methods for making it easier to write
// certain other types of data to pipelines without having to
// cast. The methods that take char const* expect null-terminated
// C strings and do not write the null terminators.
QPDF_DLL
void writeCStr(char const* cstr);
QPDF_DLL
void writeString(std::string const&);
// This allows *p << "x" << "y" but is not intended to be a
// general purpose << compatible with ostream and does not have
// local awareness or the ability to be "imbued" with properties.
QPDF_DLL
Pipeline& operator<<(char const* cstr);
QPDF_DLL
Pipeline& operator<<(std::string const&);
// Overloaded write to reduce casting
QPDF_DLL
void write(char const* data, size_t len);
protected:
QPDF_DLL
Pipeline* getNext(bool allow_null = false);

View File

@ -25,3 +25,35 @@ Pipeline::getIdentifier() const
{
return this->identifier;
}
void
Pipeline::writeCStr(char const* cstr)
{
this->write(cstr, strlen(cstr));
}
void
Pipeline::writeString(std::string const& str)
{
this->write(str.c_str(), str.length());
}
Pipeline&
Pipeline::operator<<(char const* cstr)
{
this->writeCStr(cstr);
return *this;
}
Pipeline&
Pipeline::operator<<(std::string const& str)
{
this->writeString(str);
return *this;
}
void
Pipeline::write(char const* data, size_t len)
{
this->write(reinterpret_cast<unsigned char const*>(data), len);
}

View File

@ -105,6 +105,18 @@ For a detailed list of changes, please see the file
``appendItemAndGet``, ``eraseItemAndGet``, ``replaceKeyAndGet``,
and ``removeKeyAndGet`` return the newly added or removed object.
- Add new ``Pipeline`` methods to reduce the amount of casting that is
needed:
- ``write``: overloaded version that takes `char const*` in
addition to the one that takes `unsigned char const*`
- ``writeCstr``: writes a null-terminated C string
- ``writeString``: writes a std::string
- ``operator <<``: for null-terminated C strings and std::strings
- Other changes
- A new chapter on contributing to qpdf has been added to the