tudocomp
– The TU Dortmund Compression Framework
InputSource.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <iostream>
4 #include <string>
5 #include <tudocomp/util/View.hpp>
6 
7 namespace tdc {namespace io {
12  class InputSource {
13  public:
14  enum class Content {
15  View,
16  File,
17  Stream
18  };
19  private:
20  Content m_content;
21 
22  View m_view = ""_v;
23  std::string m_path = "";
24  std::istream* m_stream = nullptr;
25  public:
26  friend inline bool operator==(const InputSource&, const InputSource&);
27 
28  inline InputSource(const std::string& path):
29  m_content(Content::File),
30  m_path(path) {}
31  inline InputSource(const View& view):
32  m_content(Content::View),
33  m_view(view) {}
34  inline InputSource(std::istream* stream):
35  m_content(Content::Stream),
36  m_stream(stream) {}
37 
38  inline bool is_view() const { return m_content == Content::View; }
39  inline bool is_stream() const { return m_content == Content::Stream; }
40  inline bool is_file() const { return m_content == Content::File; }
41 
42  inline const View& view() const {
43  DCHECK(is_view());
44  return m_view;
45  }
46 
47  inline std::istream* stream() const {
48  DCHECK(is_stream());
49  return m_stream;
50  }
51 
52  inline const std::string& file() const {
53  DCHECK(is_file());
54  return m_path;
55  }
56  };
57 
58  inline bool operator==(const InputSource& lhs, const InputSource& rhs) {
59  return lhs.m_content == rhs.m_content
60  && lhs.m_view.data() == rhs.m_view.data()
61  && lhs.m_view.size() == rhs.m_view.size()
62  && lhs.m_path == rhs.m_path
63  && lhs.m_stream == rhs.m_stream;
64  }
65 
66  inline std::ostream& operator<<(std::ostream& o, const InputSource& v) {
67  if (v.is_view()) {
68  return o << "{ view: " << std::hex << size_t(v.view().data()) << std::dec
69  << " with len " << v.view().size()
70  << " }";
71  }
72  if (v.is_stream()) {
73  return o << "{ stream: " << std::hex << size_t(v.stream()) << std::dec << " }";
74  }
75  if (v.is_file()) {
76  return o << "{ file: " << v.file() << " }";
77  }
78  return o;
79  }
80 }}
Contains the text compression and encoding framework.
Definition: namespaces.hpp:11
A const view into a slice of memory.
const View & view() const
Definition: InputSource.hpp:42
std::ostream & operator<<(std::ostream &o, const InputRestrictions &v)
bool is_file() const
Definition: InputSource.hpp:40
size_type size() const
Returns size of the View.
InputSource(std::istream *stream)
Definition: InputSource.hpp:34
friend bool operator==(const InputSource &, const InputSource &)
Definition: InputSource.hpp:58
std::istream * stream() const
Definition: InputSource.hpp:47
Class that stores the source of input data.
Definition: InputSource.hpp:12
InputSource(const View &view)
Definition: InputSource.hpp:31
InputSource(const std::string &path)
Definition: InputSource.hpp:28
const std::string & file() const
Definition: InputSource.hpp:52
const value_type * data() const noexcept
The backing memory location.
bool is_stream() const
Definition: InputSource.hpp:39
bool is_view() const
Definition: InputSource.hpp:38