Coverage Report

Created: 2026-01-10 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qpdf/include/qpdf/Pipeline.hh
Line
Count
Source
1
// Copyright (c) 2005-2021 Jay Berkenbilt
2
// Copyright (c) 2022-2026 Jay Berkenbilt and Manfred Holger
3
//
4
// This file is part of qpdf.
5
//
6
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
7
// in compliance with the License. You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing, software distributed under the License
12
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13
// or implied. See the License for the specific language governing permissions and limitations under
14
// the License.
15
//
16
// Versions of qpdf prior to version 7 were released under the terms of version 2.0 of the Artistic
17
// License. At your option, you may continue to consider qpdf to be licensed under those terms.
18
// Please see the manual for additional information.
19
20
#ifndef PIPELINE_HH
21
#define PIPELINE_HH
22
23
#include <qpdf/DLL.h>
24
25
#include <memory>
26
#include <string>
27
28
// Generalized Pipeline interface.  By convention, subclasses of Pipeline are called Pl_Something.
29
//
30
// When an instance of Pipeline is created with a pointer to a next pipeline, that pipeline writes
31
// its data to the next one when it finishes with it.  In order to make possible a usage style in
32
// which a pipeline may be passed to a function which may stick other pipelines in front of it, the
33
// allocator of a pipeline is responsible for its destruction.  In other words, one pipeline object
34
// does not attempt to manage the memory of its successor.
35
//
36
// The client is required to call finish() before destroying a Pipeline in order to avoid loss of
37
// data.  A Pipeline class should not throw an exception in the destructor if this hasn't been done
38
// though since doing so causes too much trouble when deleting pipelines during error conditions.
39
//
40
// Some pipelines are reusable (i.e., you can call write() after calling finish() and can call
41
// finish() multiple times) while others are not.  It is up to the caller to use a pipeline
42
// according to its own restrictions.
43
//
44
// Remember to use QPDF_DLL_CLASS on anything derived from Pipeline so it will work with
45
// dynamic_cast across the shared object boundary.
46
class QPDF_DLL_CLASS Pipeline
47
{
48
  public:
49
    QPDF_DLL
50
    Pipeline(char const* identifier, Pipeline* next);
51
52
796
    virtual ~Pipeline() = default;
53
54
    // Subclasses should implement write and finish to do their jobs and then, if they are not
55
    // end-of-line pipelines, call getNext()->write or getNext()->finish.
56
    QPDF_DLL
57
    virtual void write(unsigned char const* data, size_t len) = 0;
58
    QPDF_DLL
59
    virtual void finish() = 0;
60
    QPDF_DLL
61
    std::string getIdentifier() const;
62
63
    // These are convenience methods for making it easier to write certain other types of data to
64
    // pipelines without having to cast. The methods that take char const* expect null-terminated C
65
    // strings and do not write the null terminators.
66
    QPDF_DLL
67
    void writeCStr(char const* cstr);
68
    QPDF_DLL
69
    void writeString(std::string const&);
70
    // This allows *p << "x" << "y" but is not intended to be a general purpose << compatible with
71
    // ostream and does not have local awareness or the ability to be "imbued" with properties.
72
    QPDF_DLL
73
    Pipeline& operator<<(char const* cstr);
74
    QPDF_DLL
75
    Pipeline& operator<<(std::string const&);
76
    QPDF_DLL
77
    Pipeline& operator<<(short);
78
    QPDF_DLL
79
    Pipeline& operator<<(int);
80
    QPDF_DLL
81
    Pipeline& operator<<(long);
82
    QPDF_DLL
83
    Pipeline& operator<<(long long);
84
    QPDF_DLL
85
    Pipeline& operator<<(unsigned short);
86
    QPDF_DLL
87
    Pipeline& operator<<(unsigned int);
88
    QPDF_DLL
89
    Pipeline& operator<<(unsigned long);
90
    QPDF_DLL
91
    Pipeline& operator<<(unsigned long long);
92
93
    // Overloaded write to reduce casting
94
    QPDF_DLL
95
    void write(char const* data, size_t len);
96
97
  protected:
98
    QPDF_DLL
99
    Pipeline* getNext(bool allow_null = false);
100
101
    Pipeline*
102
    next() const noexcept
103
1.10M
    {
104
1.10M
        return next_;
105
1.10M
    }
106
    std::string identifier;
107
108
  private:
109
    Pipeline(Pipeline const&) = delete;
110
    Pipeline& operator=(Pipeline const&) = delete;
111
112
    Pipeline* next_;
113
};
114
115
#endif // PIPELINE_HH