KiCad PCB EDA Suite
Loading...
Searching...
No Matches
text_eval_environment.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
21#include <wx/debug.h>
22
23namespace TEXT_EVAL
24{
25namespace
26{
27 // Not a class member because MSVC cannot dllexport thread-local data
28 thread_local ENVIRONMENT* s_active = nullptr;
29} // namespace
30
31
33{
34 wxASSERT( !m_sources );
35 wxASSERT( s_active != this );
36}
37
38
40{
41 return s_active;
42}
43
44
46{
47 if( !s_active )
48 return wxDateTime::Now();
49
50 for( SOURCE_SCOPE* scope = s_active->m_sources; scope; scope = scope->m_previous )
51 scope->m_values.time = s_active->m_time;
52
53 return s_active->m_time;
54}
55
56
57void ENVIRONMENT::RecordEnvironmentVariable( const wxString& aName, const std::optional<wxString>& aValue )
58{
59 for( SOURCE_SCOPE* scope = m_sources; scope; scope = scope->m_previous )
60 scope->m_values.environmentVariables.insert_or_assign( aName, aValue );
61}
62
63
65{
66 for( SOURCE_SCOPE* scope = m_sources; scope; scope = scope->m_previous )
67 scope->m_values.randomUsed = true;
68}
69
70
72{
73 for( SOURCE_SCOPE* scope = m_sources; scope; scope = scope->m_previous )
74 scope->m_values.crossReferences.insert_or_assign( aKey, aValue );
75}
76
77
78const wxString& ENVIRONMENT::GitHash( const wxString& aPath, const std::function<wxString()>& aRead )
79{
80 auto found = m_gitHashes.find( aPath );
81
82 if( found == m_gitHashes.end() )
83 found = m_gitHashes.emplace( aPath, aRead() ).first;
84
85 for( SOURCE_SCOPE* scope = m_sources; scope; scope = scope->m_previous )
86 scope->m_values.gitHashes.insert_or_assign( aPath, found->second );
87
88 return found->second;
89}
90
91
92const ENVIRONMENT::VCS_VALUE& ENVIRONMENT::VcsValue( const VCS_KEY& aKey, const std::function<VCS_VALUE()>& aRead )
93{
94 auto found = m_vcsValues.find( aKey );
95
96 if( found == m_vcsValues.end() )
97 found = m_vcsValues.emplace( aKey, aRead() ).first;
98
99 for( SOURCE_SCOPE* scope = m_sources; scope; scope = scope->m_previous )
100 scope->m_values.vcsValues.insert_or_assign( aKey, found->second );
101
102 return found->second;
103}
104
105
107 m_environment( aEnvironment ),
108 m_values( aValues ),
109 m_previous( aEnvironment.m_sources )
110{
111 m_environment.m_sources = this;
112}
113
114
116{
117 wxASSERT( m_environment.m_sources == this );
118 m_environment.m_sources = m_previous;
119}
120
121
123 m_previous( s_active )
124{
125 s_active = &aEnvironment;
126}
127
128
133} // namespace TEXT_EVAL
ENVIRONMENT_SCOPE(ENVIRONMENT &aEnvironment)
A text evaluation frame with one frozen clock and memoized external queries.
std::map< wxString, wxString > m_gitHashes
static wxDateTime CurrentTime()
void RecordEnvironmentVariable(const wxString &aName, const std::optional< wxString > &aValue)
const VCS_VALUE & VcsValue(const VCS_KEY &aKey, const std::function< VCS_VALUE()> &aRead)
ENVIRONMENT(const wxDateTime &aTime=wxDateTime::Now())
const wxString & GitHash(const wxString &aPath, const std::function< wxString()> &aRead)
Return the value memoized for this frame.
static ENVIRONMENT * Current()
void RecordCrossReference(const CROSS_REFERENCE_KEY &aKey, const CROSS_REFERENCE_VALUE &aValue)
std::tuple< VCS_QUERY, wxString, std::string, int, bool > VCS_KEY
Query, absolute path (Git directory for HEAD), selector or pattern, options, and whether the context ...
std::map< VCS_KEY, VCS_VALUE > m_vcsValues
std::pair< wxString, int > CROSS_REFERENCE_KEY
SOURCE_SCOPE(ENVIRONMENT &aEnvironment, ENVIRONMENT::SOURCE_VALUES &aValues)
ENVIRONMENT::SOURCE_VALUES & m_values