tudocomp
– The TU Dortmund Compression Framework
Output.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cstddef>
4 #include <cstdint>
5 #include <fstream>
6 #include <iostream>
7 #include <string>
8 #include <utility>
9 #include <vector>
10 
11 #include <tudocomp/io/Path.hpp>
13 
14 namespace tdc {
15 namespace io {
16  class OutputStream;
17 
23  class Output {
24  class Variant {
25  InputRestrictions m_restrictions;
26  protected:
27  inline Variant(const InputRestrictions& restrictions):
28  m_restrictions(restrictions) {}
29  public:
30  inline Variant() {}
31 
32  inline const InputRestrictions& restrictions() const {
33  return m_restrictions;
34  }
35 
36  virtual ~Variant() {}
37  virtual std::unique_ptr<Variant> unrestrict(const InputRestrictions& rest) const = 0;
38  virtual OutputStream as_stream() const = 0;
39  };
40 
41  class Memory: public Variant {
42  std::vector<uint8_t>* m_buffer;
43 
44  Memory(const Memory& other, const InputRestrictions& r):
45  Variant(r),
46  m_buffer(other.m_buffer) {}
47  public:
48  Memory(std::vector<uint8_t>* buffer):
49  m_buffer(buffer) {}
50 
51  inline std::unique_ptr<Variant> unrestrict(const InputRestrictions& rest) const override {
52  return std::make_unique<Memory>(
53  Memory(*this, restrictions() | rest));
54  }
55  inline OutputStream as_stream() const override;
56  };
57  class File: public Variant {
58  std::string m_path;
59  // TODO:
60  mutable bool m_overwrite;
61 
62  File(const File& other, const InputRestrictions& r):
63  Variant(r),
64  m_path(other.m_path),
65  m_overwrite(other.m_overwrite) {}
66  public:
67  File(const std::string& path, bool overwrite):
68  m_path(path),
69  m_overwrite(overwrite) {}
70 
71  inline std::unique_ptr<Variant> unrestrict(const InputRestrictions& rest) const override {
72  return std::make_unique<File>(
73  File(*this, restrictions() | rest));
74  }
75  inline OutputStream as_stream() const override;
76  };
77  class Stream: public Variant {
78  std::ostream* m_stream;
79 
80  Stream(const Stream& other, const InputRestrictions& r):
81  Variant(r),
82  m_stream(other.m_stream) {}
83  public:
84  Stream(std::ostream* stream):
85  m_stream(stream) {}
86 
87  inline std::unique_ptr<Variant> unrestrict(const InputRestrictions& rest) const override {
88  return std::make_unique<Stream>(
89  Stream(*this, restrictions() | rest));
90  }
91  inline OutputStream as_stream() const override;
92  };
93 
94  std::unique_ptr<Variant> m_data;
95 
96  friend class OutputStream;
97  public:
99  inline Output(): Output(std::cout) {}
100 
102  inline Output(Output&& other):
103  m_data(std::move(other.m_data)) {}
104 
111  inline Output(const Path& path, bool overwrite=false):
112  m_data(std::make_unique<File>(std::move(path.path), overwrite)) {}
113 
117  inline Output(std::vector<uint8_t>& buf):
118  m_data(std::make_unique<Memory>(&buf)) {}
119 
123  inline Output(std::ostream& stream):
124  m_data(std::make_unique<Stream>(&stream)) {}
125 
127  inline Output& operator=(Output&& other) {
128  m_data = std::move(other.m_data);
129  return *this;
130  }
131 
139  inline static Output from_path(const Path& path, bool overwrite=false) {
140  return Output(path, overwrite);
141  }
142 
147  inline static Output from_memory(std::vector<uint8_t>& buf) {
148  return Output(buf);
149  }
150 
155  inline static Output from_stream(std::ostream& stream) {
156  return Output(stream);
157  }
158 
160  inline OutputStream as_stream() const;
161 
164  inline Output(const Output& other, const InputRestrictions& restrictions):
165  m_data(other.m_data->unrestrict(restrictions)) {}
167  };
168 
169 }}
170 
172 
Contains the text compression and encoding framework.
Definition: namespaces.hpp:11
Provides a character stream to the underlying output.
Output(std::ostream &stream)
Constructs an output that appends to the output stream.
Definition: Output.hpp:123
Output & operator=(Output &&other)
Move assignment operator.
Definition: Output.hpp:127
Describes a set of restrictions placed on input data.
Output(std::vector< uint8_t > &buf)
Constructs an output that appends to the byte vector.
Definition: Output.hpp:117
Output()
Constructs an output to stdout.
Definition: Output.hpp:99
Output(const Path &path, bool overwrite=false)
Constructs an output that appends to the file at the given path.
Definition: Output.hpp:111
Represents a file path.
Definition: Path.hpp:8
OutputStream as_stream() const
Creates a stream that allows for character-wise output.
static Output from_memory(std::vector< uint8_t > &buf)
Constructs an output to a byte buffer.
Definition: Output.hpp:147
An abstraction layer for algorithm output.
Definition: Output.hpp:23
uint64_t m_data
Definition: uint_t.hpp:30
static Output from_stream(std::ostream &stream)
Constructs an output to a stream.
Definition: Output.hpp:155
static Output from_path(const Path &path, bool overwrite=false)
Constructs a file output writing to the file at the given path.
Definition: Output.hpp:139
Output(Output &&other)
Move constructor.
Definition: Output.hpp:102