SILICON
Loading...
Searching...
No Matches
projectDocument.hpp
Go to the documentation of this file.
1/*
2 Copyright (c) 2026. Giulio Cocconi
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#pragma once
19
20#include <array>
21#include <cstddef>
22#include <cstdint>
23#include <functional>
24#include <memory>
25#include <optional>
26#include <stdexcept>
27#include <string>
28#include <string_view>
29#include <vector>
30
32
33namespace SILICON::project {
34
35class ProjectContext;
36
38
40
44 std::string_view root;
45 std::string_view suffix;
47};
48
49inline constexpr std::array<DocumentTypeInfo, 4> DOCUMENT_TYPE_INFO{{
50 {.type = DocumentType::Circuit,
51 .category = DocumentCategory::Diagram,
52 .root = "circuits/",
53 .suffix = ".json",
54 .editableText = false},
55 {.type = DocumentType::Verilog,
56 .category = DocumentCategory::Code,
57 .root = "code/",
58 .suffix = ".v",
59 .editableText = true},
60 {.type = DocumentType::Sisl,
62 .root = "isa/",
63 .suffix = "/instr_format.sisl",
64 .editableText = true},
66 .category = DocumentCategory::Binary,
67 .root = "bin/",
68 .suffix = {},
69 .editableText = false},
70}};
71
72[[nodiscard]] constexpr const DocumentTypeInfo& documentTypeInfo(const DocumentType type)
73{
74 for (const auto& info : DOCUMENT_TYPE_INFO) {
75 if (info.type == type)
76 return info;
77 }
78 throw std::invalid_argument("Unknown document type");
79}
80
81[[nodiscard]] constexpr DocumentCategory categoryOf(const DocumentType type)
82{
83 return documentTypeInfo(type).category;
84}
85
86[[nodiscard]] constexpr bool isCodeDocument(const DocumentType type)
87{
88 return documentTypeInfo(type).editableText;
89}
90
92
95 std::optional<std::string> path;
96
97 bool operator==(const DocumentChange&) const = default;
98};
99
100[[nodiscard]] std::optional<DocumentType> documentTypeForPath(std::string_view path);
101[[nodiscard]] std::optional<std::string> documentSlugForPath(std::string_view path);
102[[nodiscard]] bool isValidDocumentSlug(std::string_view slug);
103[[nodiscard]] std::string documentPathForSlug(DocumentType type, std::string_view slug);
104
109class Document {
110public:
111 Document(std::string path, std::string contents);
112
113 [[nodiscard]] const std::string& getPath() const noexcept;
114 [[nodiscard]] const std::string& getContents() const noexcept;
115 [[nodiscard]] DocumentType getType() const noexcept;
116
117 void setContents(std::string contents);
118
119private:
120 std::string path;
121 std::string contents;
122};
123
132[[nodiscard]] Document importDocument(std::string_view fileName, std::string contents);
133
135[[nodiscard]] std::string documentFileName(const Document& document);
136
138public:
139 using Listener = std::function<void(const DocumentChange&)>;
140
142 [[nodiscard]] const Document* find(std::string_view documentPath) const noexcept;
143 [[nodiscard]] bool contains(std::string_view documentPath) const noexcept;
144 [[nodiscard]] bool contains(DocumentType type) const noexcept;
145 [[nodiscard]] const std::vector<Document>& getDocuments() const noexcept;
146 [[nodiscard]] std::optional<std::size_t> indexOf(std::string_view documentPath) const;
147
148 std::uint64_t addListener(Listener listener) const;
149 void removeListener(std::uint64_t id) const;
150
151private:
152 friend class ProjectContext;
153
154 void commitDocuments(std::vector<Document> documents,
155 const DocumentChange& change) noexcept;
156
157 std::vector<Document> documents;
158 mutable SILICON::core::CallbackRegistry<const DocumentChange&> listeners;
159};
160
162[[nodiscard]] std::shared_ptr<const std::string>
163binaryContentsSnapshot(const DocumentStore& documents, std::string_view slug);
164
165} // namespace SILICON::project
Document(std::string path, std::string contents)
std::string path
std::string contents
SILICON::core::CallbackRegistry< const DocumentChange & > listeners
const std::vector< Document > & getDocuments() const noexcept
std::uint64_t addListener(Listener listener) const
std::optional< std::size_t > indexOf(std::string_view documentPath) const
bool contains(std::string_view documentPath) const noexcept
void removeListener(std::uint64_t id) const
const Document * find(std::string_view documentPath) const noexcept
void commitDocuments(std::vector< Document > documents, const DocumentChange &change) noexcept
std::function< void(const DocumentChange &)> Listener
std::vector< Document > documents
void setContents(std::string contents)
DocumentType getType() const noexcept
Document(std::string path, std::string contents)
const std::string & getPath() const noexcept
const std::string & getContents() const noexcept
std::string documentPathForSlug(const DocumentType type, const std::string_view slug)
constexpr const DocumentTypeInfo & documentTypeInfo(const DocumentType type)
constexpr std::array< DocumentTypeInfo, 4 > DOCUMENT_TYPE_INFO
std::string documentFileName(const Document &document)
constexpr DocumentCategory categoryOf(const DocumentType type)
std::optional< std::string > documentSlugForPath(const std::string_view path)
Document importDocument(const std::string_view sourcePath, std::string contents)
bool isValidDocumentSlug(const std::string_view slug)
std::optional< DocumentType > documentTypeForPath(const std::string_view path)
std::shared_ptr< const std::string > binaryContentsSnapshot(const DocumentStore &documents, const std::string_view slug)
constexpr bool isCodeDocument(const DocumentType type)
bool operator==(const DocumentChange &) const =default
std::optional< std::string > path