SILICON
Toggle main menu visibility
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
31
#include <
core/callbackRegistry.hpp
>
32
33
namespace
SILICON::project
{
34
35
class
ProjectContext
;
36
37
enum class
DocumentType
{
Circuit
,
Verilog
,
Sisl
,
RawBinary
};
38
39
enum class
DocumentCategory
{
Diagram
,
Code
,
Architecture
,
Binary
};
40
41
struct
DocumentTypeInfo
{
42
DocumentType
type
;
43
DocumentCategory
category
;
44
std::string_view
root
;
45
std::string_view
suffix
;
46
bool
editableText
;
47
};
48
49
inline
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
,
61
.category =
DocumentCategory::Architecture
,
62
.root =
"isa/"
,
63
.suffix =
"/instr_format.sisl"
,
64
.editableText =
true
},
65
{.type =
DocumentType::RawBinary
,
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
91
enum class
DocumentChangeKind
{
Added
,
Updated
,
Removed
,
Reset
};
92
93
struct
DocumentChange
{
94
DocumentChangeKind
kind
;
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
109
class
Document
{
110
public
:
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
119
private
:
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
137
class
DocumentStore
{
138
public
:
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
151
private:
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
>
163
binaryContentsSnapshot
(const
DocumentStore
&
documents
, std::string_view slug);
164
165
}
// namespace SILICON::project
callbackRegistry.hpp
Document::Document
Document(std::string path, std::string contents)
Definition
projectDocument.cpp:202
Document::path
std::string path
Definition
projectDocument.hpp:120
Document::contents
std::string contents
Definition
projectDocument.hpp:121
SILICON::project::DocumentStore
Definition
projectDocument.hpp:137
SILICON::project::DocumentStore::ProjectContext
friend class ProjectContext
Definition
projectDocument.hpp:152
SILICON::project::DocumentStore::listeners
SILICON::core::CallbackRegistry< const DocumentChange & > listeners
Definition
projectDocument.hpp:158
SILICON::project::DocumentStore::getDocuments
const std::vector< Document > & getDocuments() const noexcept
Definition
projectDocument.cpp:278
SILICON::project::DocumentStore::addListener
std::uint64_t addListener(Listener listener) const
Definition
projectDocument.cpp:292
SILICON::project::DocumentStore::indexOf
std::optional< std::size_t > indexOf(std::string_view documentPath) const
Definition
projectDocument.cpp:284
SILICON::project::DocumentStore::contains
bool contains(std::string_view documentPath) const noexcept
Definition
projectDocument.cpp:267
SILICON::project::DocumentStore::removeListener
void removeListener(std::uint64_t id) const
Definition
projectDocument.cpp:297
SILICON::project::DocumentStore::find
const Document * find(std::string_view documentPath) const noexcept
Definition
projectDocument.cpp:261
SILICON::project::DocumentStore::commitDocuments
void commitDocuments(std::vector< Document > documents, const DocumentChange &change) noexcept
Definition
projectDocument.cpp:302
SILICON::project::DocumentStore::Listener
std::function< void(const DocumentChange &)> Listener
Definition
projectDocument.hpp:139
SILICON::project::DocumentStore::documents
std::vector< Document > documents
Definition
projectDocument.hpp:157
SILICON::project::Document
Definition
projectDocument.hpp:109
SILICON::project::Document::setContents
void setContents(std::string contents)
Definition
projectDocument.cpp:225
SILICON::project::Document::getType
DocumentType getType() const noexcept
Definition
projectDocument.cpp:220
SILICON::project::Document::Document
Document(std::string path, std::string contents)
Definition
projectDocument.cpp:202
SILICON::project::Document::getPath
const std::string & getPath() const noexcept
Definition
projectDocument.cpp:210
SILICON::project::Document::path
std::string path
Definition
projectDocument.hpp:120
SILICON::project::Document::getContents
const std::string & getContents() const noexcept
Definition
projectDocument.cpp:215
SILICON::project::Document::contents
std::string contents
Definition
projectDocument.hpp:121
SILICON::project::ProjectContext
Definition
projectContext.hpp:30
SILICON::core
Definition
activeKeyGuard.hpp:28
SILICON::project
Definition
circuitDependencyGraph.cpp:30
SILICON::project::documentPathForSlug
std::string documentPathForSlug(const DocumentType type, const std::string_view slug)
Definition
projectDocument.cpp:191
SILICON::project::DocumentCategory
DocumentCategory
Definition
projectDocument.hpp:39
SILICON::project::DocumentCategory::Architecture
@ Architecture
Definition
projectDocument.hpp:39
SILICON::project::DocumentCategory::Binary
@ Binary
Definition
projectDocument.hpp:39
SILICON::project::DocumentCategory::Code
@ Code
Definition
projectDocument.hpp:39
SILICON::project::DocumentCategory::Diagram
@ Diagram
Definition
projectDocument.hpp:39
SILICON::project::documentTypeInfo
constexpr const DocumentTypeInfo & documentTypeInfo(const DocumentType type)
Definition
projectDocument.hpp:72
SILICON::project::DOCUMENT_TYPE_INFO
constexpr std::array< DocumentTypeInfo, 4 > DOCUMENT_TYPE_INFO
Definition
projectDocument.hpp:49
SILICON::project::documentFileName
std::string documentFileName(const Document &document)
Definition
projectDocument.cpp:256
SILICON::project::categoryOf
constexpr DocumentCategory categoryOf(const DocumentType type)
Definition
projectDocument.hpp:81
SILICON::project::DocumentChangeKind
DocumentChangeKind
Definition
projectDocument.hpp:91
SILICON::project::DocumentChangeKind::Reset
@ Reset
Definition
projectDocument.hpp:91
SILICON::project::DocumentChangeKind::Removed
@ Removed
Definition
projectDocument.hpp:91
SILICON::project::DocumentChangeKind::Added
@ Added
Definition
projectDocument.hpp:91
SILICON::project::DocumentChangeKind::Updated
@ Updated
Definition
projectDocument.hpp:91
SILICON::project::documentSlugForPath
std::optional< std::string > documentSlugForPath(const std::string_view path)
Definition
projectDocument.cpp:174
SILICON::project::importDocument
Document importDocument(const std::string_view sourcePath, std::string contents)
Definition
projectDocument.cpp:230
SILICON::project::isValidDocumentSlug
bool isValidDocumentSlug(const std::string_view slug)
Definition
projectDocument.cpp:185
SILICON::project::documentTypeForPath
std::optional< DocumentType > documentTypeForPath(const std::string_view path)
Definition
projectDocument.cpp:165
SILICON::project::DocumentType
DocumentType
Definition
projectDocument.hpp:37
SILICON::project::DocumentType::RawBinary
@ RawBinary
Definition
projectDocument.hpp:37
SILICON::project::DocumentType::Circuit
@ Circuit
Definition
projectDocument.hpp:37
SILICON::project::DocumentType::Sisl
@ Sisl
Definition
projectDocument.hpp:37
SILICON::project::DocumentType::Verilog
@ Verilog
Definition
projectDocument.hpp:37
SILICON::project::binaryContentsSnapshot
std::shared_ptr< const std::string > binaryContentsSnapshot(const DocumentStore &documents, const std::string_view slug)
Definition
projectDocument.cpp:309
SILICON::project::isCodeDocument
constexpr bool isCodeDocument(const DocumentType type)
Definition
projectDocument.hpp:86
SILICON
Definition
activeKeyGuard.hpp:28
SILICON::project::DocumentChange
Definition
projectDocument.hpp:93
SILICON::project::DocumentChange::kind
DocumentChangeKind kind
Definition
projectDocument.hpp:94
SILICON::project::DocumentChange::operator==
bool operator==(const DocumentChange &) const =default
SILICON::project::DocumentChange::path
std::optional< std::string > path
Definition
projectDocument.hpp:95
SILICON::project::DocumentTypeInfo
Definition
projectDocument.hpp:41
SILICON::project::DocumentTypeInfo::category
DocumentCategory category
Definition
projectDocument.hpp:43
SILICON::project::DocumentTypeInfo::root
std::string_view root
Definition
projectDocument.hpp:44
SILICON::project::DocumentTypeInfo::editableText
bool editableText
Definition
projectDocument.hpp:46
SILICON::project::DocumentTypeInfo::suffix
std::string_view suffix
Definition
projectDocument.hpp:45
SILICON::project::DocumentTypeInfo::type
DocumentType type
Definition
projectDocument.hpp:42
src
core
projectDocument.hpp
Generated by
1.17.0