1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
// license:GPL-2.0+
// copyright-holders:Couriersud
#ifndef PSOURCE_H_
#define PSOURCE_H_
///
/// \file putil.h
///
#include "palloc.h"
#include "pexception.h"
#include "pstring.h"
#include "pstream.h"
#include <algorithm>
#include <initializer_list>
#include <sstream>
#include <vector>
#define PSOURCELOC() plib::source_location(__FILE__, __LINE__)
namespace plib
{
/// \brief Source code locations.
///
/// The c++20 draft for source locations is based on const char * strings.
/// It is thus only suitable for c++ source code and not for programmatic
/// parsing of files. This class is a replacement for dynamic use cases.
///
struct source_location
{
source_location() noexcept
: m_file("unknown"), m_func(m_file), m_line(0), m_col(0)
{ }
source_location(pstring file, unsigned line) noexcept
: m_file(std::move(file)), m_func("unknown"), m_line(line), m_col(0)
{ }
source_location(pstring file, pstring func, unsigned line) noexcept
: m_file(std::move(file)), m_func(std::move(func)), m_line(line), m_col(0)
{ }
PCOPYASSIGNMOVE(source_location, default)
~source_location() = default;
unsigned line() const noexcept { return m_line; }
unsigned column() const noexcept { return m_col; }
pstring file_name() const noexcept { return m_file; }
pstring function_name() const noexcept { return m_func; }
source_location &operator ++() noexcept
{
++m_line;
return *this;
}
private:
pstring m_file;
pstring m_func;
unsigned m_line;
unsigned m_col;
};
/// \brief Base source class.
///
/// Pure virtual class all other source implementations are based on.
/// Sources provide an abstraction to read input from a variety of
/// sources, e.g. files, memory, remote locations.
///
class psource_t
{
public:
psource_t() noexcept = default;
PCOPYASSIGNMOVE(psource_t, delete)
virtual ~psource_t() noexcept = default;
virtual istream_uptr stream(const pstring &name) = 0;
private:
};
/// \brief Generic string source.
///
/// Will return the given string when name matches.
/// Is used in preprocessor code to eliminate inclusion of certain files.
///
class psource_str_t : public psource_t
{
public:
psource_str_t(pstring name, pstring str)
: m_name(std::move(name)), m_str(std::move(str))
{}
PCOPYASSIGNMOVE(psource_str_t, delete)
~psource_str_t() noexcept override = default;
istream_uptr stream(const pstring &name) override
{
if (name == m_name)
return istream_uptr(std::make_unique<std::stringstream>(putf8string(m_str)), name);
return istream_uptr();
}
private:
pstring m_name;
pstring m_str;
};
/// \brief Generic sources collection.
///
class psource_collection_t
{
public:
using source_ptr = std::unique_ptr<psource_t>;
using list_t = std::vector<source_ptr>;
psource_collection_t() noexcept = default;
PCOPYASSIGNMOVE(psource_collection_t, delete)
virtual ~psource_collection_t() noexcept = default;
template <typename S, typename... Args>
void add_source(Args&&... args)
{
static_assert(std::is_base_of<psource_t, S>::value, "S must inherit from plib::psource_t");
auto src(std::make_unique<S>(std::forward<Args>(args)...));
m_collection.push_back(std::move(src));
}
template <typename S = psource_t>
istream_uptr get_stream(pstring name)
{
for (auto &s : m_collection)
{
auto *source(dynamic_cast<S *>(s.get()));
if (source)
{
auto strm = source->stream(name);
if (!strm.empty())
return strm;
}
}
return istream_uptr();
}
template <typename S, typename F>
bool for_all(F lambda)
{
for (auto &s : m_collection)
{
auto *source(dynamic_cast<S *>(s.get()));
if (source)
{
if (lambda(source))
return true;
}
}
return false;
}
private:
list_t m_collection;
};
} // namespace plib
#endif // PSOURCE_H_
|