Panini 1.4.0
Header-only library for generating C++, written in C++17
IncludeSet.hpp
Go to the documentation of this file.
1/*
2 MIT No Attribution
3
4 Copyright 2021-2023 Mr. Hands
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to
8 deal in the Software without restriction, including without limitation the
9 rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 sell copies of the Software, and to permit persons to whom the Software is
11 furnished to do so.
12
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19 DEALINGS IN THE SOFTWARE.
20*/
21
22#pragma once
23
24#include "data/IncludeEntry.hpp"
25
26#include <filesystem>
27
28namespace panini
29{
30
46 {
47
48 public:
49 inline IncludeSet() = default;
50
54 inline IncludeSet(std::initializer_list<IncludeEntry> entries)
55 {
56 for (const IncludeEntry& entry : entries)
57 {
58 Add(entry.path, entry.style);
59 }
60 }
61
65 inline IncludeSet(
66 std::initializer_list<std::filesystem::path> paths,
67 IncludeStyle style)
68 {
69 for (const std::filesystem::path& path : paths)
70 {
71 Add(path, style);
72 }
73 }
74
78 inline const std::vector<IncludeEntry>& GetEntries() const
79 {
80 return m_entries;
81 }
82
87 inline std::vector<IncludeEntry>::const_iterator begin() const
88 {
89 return m_entries.begin();
90 }
91
96 inline std::vector<IncludeEntry>::iterator begin()
97 {
98 return m_entries.begin();
99 }
100
105 inline std::vector<IncludeEntry>::const_iterator end() const
106 {
107 return m_entries.end();
108 }
109
114 inline std::vector<IncludeEntry>::iterator end()
115 {
116 return m_entries.end();
117 }
118
125 inline void Add(
126 const std::filesystem::path& path,
128 {
129 // check if the path is not already known
130
131 auto found = std::find_if(
132 m_entries.begin(),
133 m_entries.end(),
134 [&path, &style](const IncludeEntry& it) {
135 return it.path == path && it.style == style;
136 }
137 );
138 if (found != m_entries.end())
139 {
140 return;
141 }
142
143 // add new entry to list
144
145 m_entries.emplace_back(IncludeEntry{ path, style });
146 }
147
165 inline void Sort(IncludeStyle resolvedStyle)
166 {
167 if (resolvedStyle == IncludeStyle::Inherit)
168 {
169 return;
170 }
171
172 // resolve priority for all entries
173
174 for (IncludeEntry& entry : m_entries)
175 {
176 IncludeStyle style =
177 (entry.style == IncludeStyle::Inherit)
178 ? resolvedStyle
179 : entry.style;
180
181 // base priority on include style
182
183 entry.priority = 1000;
184
185 switch (style)
186 {
187
189 entry.priority = 0;
190 break;
191
193 entry.priority = 100;
194 break;
195
197 entry.priority = 200;
198 break;
199
200 default:
201 break;
202
203 }
204
205 // paths with folders should come before files
206
207 const std::string path = entry.path.string();
208
209 size_t offset = 0;
210 size_t next = 0;
211 while ((next = path.find_first_of('/', offset)) != std::string::npos)
212 {
213 entry.priority--;
214 offset = next + 1;
215 }
216 }
217
218 // sort by priority and path
219
220 std::sort(
221 m_entries.begin(),
222 m_entries.end(),
223 [](IncludeEntry& left, IncludeEntry& right) {
224 if (left.priority != right.priority)
225 {
226 return left.priority < right.priority;
227 }
228 else
229 {
230 return left.path < right.path;
231 }
232 }
233 );
234 }
235
236 private:
237 std::vector<IncludeEntry> m_entries;
238
239 };
240
241};
Collection of unique file system paths.
Definition: IncludeSet.hpp:46
IncludeSet(std::initializer_list< std::filesystem::path > paths, IncludeStyle style)
Definition: IncludeSet.hpp:65
void Sort(IncludeStyle resolvedStyle)
Definition: IncludeSet.hpp:165
std::vector< IncludeEntry >::iterator begin()
Definition: IncludeSet.hpp:96
std::vector< IncludeEntry >::const_iterator begin() const
Definition: IncludeSet.hpp:87
IncludeSet()=default
std::vector< IncludeEntry >::const_iterator end() const
Definition: IncludeSet.hpp:105
void Add(const std::filesystem::path &path, IncludeStyle style=IncludeStyle::Inherit)
Definition: IncludeSet.hpp:125
IncludeSet(std::initializer_list< IncludeEntry > entries)
Definition: IncludeSet.hpp:54
const std::vector< IncludeEntry > & GetEntries() const
Definition: IncludeSet.hpp:78
std::vector< IncludeEntry >::iterator end()
Definition: IncludeSet.hpp:114
IncludeStyle
Include style to use when writing to output.
Definition: IncludeStyle.hpp:36
@ DoubleQuotes
Output double quotation marks "".
@ Inherit
Inherit setting from the config, not valid on Writer.
@ SingleQuotes
Output single quotation marks '' (not valid for C++)
@ AngularBrackets
Output angular brackets <>
Definition: Braces.hpp:29
Data for includes.
Definition: IncludeEntry.hpp:36