KiCad PCB EDA Suite
Loading...
Searching...
No Matches
job.h
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 (C) 2022 Mark Roszko <[email protected]>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#pragma once
22
23#include <wx/string.h>
24
25#include <json_common.h>
26#include <kicommon.h>
27#include <map>
29#include <lseq.h>
30#include <lset.h>
31#include <title_block.h>
32
33class PROJECT;
34
36{
37public:
38 JOB_PARAM_BASE( const std::string& aJsonPath );
39
40 virtual ~JOB_PARAM_BASE() = default;
41
42 virtual void FromJson( const nlohmann::json& j ) const = 0;
43
44 virtual void ToJson( nlohmann::json& j ) = 0;
45
46protected:
47 std::string m_jsonPath;
48};
49
50template <typename ValueType>
52{
53public:
54
55 JOB_PARAM( const std::string& aJsonPath, ValueType* aPtr,
56 ValueType aDefault ) :
57 JOB_PARAM_BASE( aJsonPath ), m_ptr( aPtr ), m_default( aDefault )
58 {
59 }
60
61 virtual void FromJson( const nlohmann::json& j ) const override
62 {
63 *m_ptr = j.value( m_jsonPath, m_default );
64 }
65
66 virtual void ToJson( nlohmann::json& j ) override { j[m_jsonPath] = *m_ptr; }
67
68protected:
69 ValueType* m_ptr;
70 ValueType m_default;
71};
72
73
74template <typename ListElementType>
76{
77public:
78 JOB_PARAM_LIST( const std::string& aJsonPath, std::vector<ListElementType>* aPtr,
79 std::vector<ListElementType> aDefault ) :
80 JOB_PARAM_BASE( aJsonPath ),
81 m_ptr( aPtr ),
82 m_default( std::move( aDefault ) )
83 { }
84
85 virtual void FromJson( const nlohmann::json& j ) const override
86 {
87 if( j.contains( m_jsonPath ) )
88 {
89 auto js = j.at( m_jsonPath );
90 std::vector<ListElementType> val;
91
92 if( js.is_array() )
93 {
94 for( const auto& el : js.items() )
95 val.push_back( el.value().template get<ListElementType>() );
96 }
97
98 *m_ptr = val;
99 }
100 else
101 *m_ptr = m_default;
102 }
103
104 void ToJson( nlohmann::json& j ) override
105 {
106 nlohmann::json js = nlohmann::json::array();
107
108 for( const auto& el : *m_ptr )
109 js.push_back( el );
110
111 j[m_jsonPath] = js;
112 }
113
114protected:
115 std::vector<ListElementType>* m_ptr;
116 std::vector<ListElementType> m_default;
117};
118
119
120class JOB_PARAM_LSEQ : public JOB_PARAM<LSEQ>
121{
122public:
123 JOB_PARAM_LSEQ( const std::string& aJsonPath, LSEQ* aPtr, LSEQ aDefault ) :
124 JOB_PARAM<LSEQ>( aJsonPath, aPtr, aDefault )
125 {
126 }
127
128 virtual void FromJson( const nlohmann::json& j ) const override
129 {
130 if( j.contains( m_jsonPath ) )
131 {
132 auto js = j.at( m_jsonPath );
133
134 LSEQ layers;
135 if( js.is_array() )
136 {
137 for( const nlohmann::json& layer : js )
138 {
139 if( layer.is_string() )
140 {
141 wxString name = layer.get<wxString>();
142 int layerId = LSET::NameToLayer( name );
143 if( layerId != UNDEFINED_LAYER )
144 layers.push_back( static_cast<PCB_LAYER_ID>( layerId ) );
145 }
146 else
147 {
148 int layerId = layer.get<int>();
149 if( layerId != UNDEFINED_LAYER )
150 layers.push_back( static_cast<PCB_LAYER_ID>( layerId ) );
151 }
152 }
153 }
154 *m_ptr = layers;
155 }
156 else
157 *m_ptr = m_default;
158 }
159
160 void ToJson( nlohmann::json& j ) override
161 {
162 nlohmann::json js = nlohmann::json::array();
163
164 for( PCB_LAYER_ID layer : ( *m_ptr ) )
165 js.push_back( LSET::Name( layer ) );
166
167 j[m_jsonPath] = js;
168 }
169};
170
172{
174
175 JOB_OUTPUT( wxString outputPath ) { m_outputPath = outputPath; }
176
177 wxString m_outputPath;
178};
179
184{
185public:
186 JOB( const std::string& aType, bool aOutputIsDirectory );
187
188 virtual ~JOB();
189
190 // We own at least one list of raw pointers. Don't let the compiler fill in copy c'tors that
191 // will only land us in trouble.
192 JOB( const JOB& ) = delete;
193 JOB& operator=( const JOB& ) = delete;
194
195 const std::string& GetType() const { return m_type; };
196
197 const std::map<wxString, wxString>& GetVarOverrides() const { return m_varOverrides; }
198
199 void SetVarOverrides( const std::map<wxString, wxString>& aVarOverrides )
200 {
201 m_varOverrides = aVarOverrides;
202 }
203
204 void SetTitleBlock( const TITLE_BLOCK& aTitleBlock ) { m_titleBlock = aTitleBlock; }
205
206 virtual void FromJson( const nlohmann::json& j );
207 virtual void ToJson( nlohmann::json& j ) const;
208
209 virtual wxString GetDefaultDescription() const;
210 virtual wxString GetSettingsDialogTitle() const;
211
212 const std::vector<JOB_PARAM_BASE*>& GetParams() { return m_params; }
213
214 void ClearExistingOutputs() { m_outputs.clear(); }
215 const std::vector<JOB_OUTPUT>& GetOutputs() { return m_outputs; }
216 void AddOutput( wxString aOutputPath ) { m_outputs.emplace_back( aOutputPath ); }
217
223 void SetTempOutputDirectory( const wxString& aBase );
224
228 void SetConfiguredOutputPath( const wxString& aPath );
229
233 wxString GetConfiguredOutputPath() const { return m_outputPath; }
234
239 void SetWorkingOutputPath( const wxString& aPath ) { m_workingOutputPath = aPath; }
240
244 wxString GetWorkingOutputPath() const { return m_workingOutputPath; }
245
252 wxString GetFullOutputPath( PROJECT* aProject ) const;
253
254 wxString ResolveOutputPath( const wxString& aPath, bool aPathIsDirectory, PROJECT* aProject ) const;
255
257
258protected:
259 std::string m_type;
260 std::map<wxString, wxString> m_varOverrides;
262
264
265 wxString m_outputPath;
268
274
275 std::vector<JOB_PARAM_BASE*> m_params;
276
277 std::vector<JOB_OUTPUT> m_outputs;
278};
279
280KICOMMON_API void from_json( const nlohmann::json& j, JOB& f );
281KICOMMON_API void to_json( nlohmann::json& j, const JOB& f );
const char * name
virtual void ToJson(nlohmann::json &j)=0
virtual void FromJson(const nlohmann::json &j) const =0
std::string m_jsonPath
Definition job.h:47
JOB_PARAM_BASE(const std::string &aJsonPath)
Definition job.cpp:175
virtual ~JOB_PARAM_BASE()=default
void ToJson(nlohmann::json &j) override
Definition job.h:104
std::vector< ListElementType > m_default
Definition job.h:116
JOB_PARAM_LIST(const std::string &aJsonPath, std::vector< ListElementType > *aPtr, std::vector< ListElementType > aDefault)
Definition job.h:78
virtual void FromJson(const nlohmann::json &j) const override
Definition job.h:85
std::vector< ListElementType > * m_ptr
Definition job.h:115
JOB_PARAM_LSEQ(const std::string &aJsonPath, LSEQ *aPtr, LSEQ aDefault)
Definition job.h:123
void ToJson(nlohmann::json &j) override
Definition job.h:160
virtual void FromJson(const nlohmann::json &j) const override
Definition job.h:128
JOB_PARAM(const std::string &aJsonPath, ValueType *aPtr, ValueType aDefault)
Definition job.h:55
virtual void ToJson(nlohmann::json &j) override
Definition job.h:66
ValueType m_default
Definition job.h:70
ValueType * m_ptr
Definition job.h:69
virtual void FromJson(const nlohmann::json &j) const override
Definition job.h:61
An simple container class that lets us dispatch output jobs to kifaces.
Definition job.h:184
void SetVarOverrides(const std::map< wxString, wxString > &aVarOverrides)
Definition job.h:199
JOB(const JOB &)=delete
std::vector< JOB_PARAM_BASE * > m_params
Definition job.h:275
JOB(const std::string &aType, bool aOutputIsDirectory)
Definition job.cpp:26
TITLE_BLOCK m_titleBlock
Definition job.h:261
const std::vector< JOB_OUTPUT > & GetOutputs()
Definition job.h:215
void AddOutput(wxString aOutputPath)
Definition job.h:216
const std::vector< JOB_PARAM_BASE * > & GetParams()
Definition job.h:212
wxString GetWorkingOutputPath() const
Returns the working output path for the job, if one has been set.
Definition job.h:244
wxString m_workingOutputPath
The working output path is a transient path that takes priority over the configured output path when ...
Definition job.h:273
bool GetOutputPathIsDirectory() const
Definition job.h:256
std::string m_type
Definition job.h:259
wxString m_tempOutputDirectory
Definition job.h:263
JOB & operator=(const JOB &)=delete
wxString m_description
Definition job.h:267
wxString GetConfiguredOutputPath() const
Returns the configured output path for the job.
Definition job.h:233
std::vector< JOB_OUTPUT > m_outputs
Definition job.h:277
bool m_outputPathIsDirectory
Definition job.h:266
void SetTitleBlock(const TITLE_BLOCK &aTitleBlock)
Definition job.h:204
void SetWorkingOutputPath(const wxString &aPath)
Sets a transient output path for the job, it takes priority over the configured output path when GetF...
Definition job.h:239
const std::string & GetType() const
Definition job.h:195
std::map< wxString, wxString > m_varOverrides
Definition job.h:260
const std::map< wxString, wxString > & GetVarOverrides() const
Definition job.h:197
void ClearExistingOutputs()
Definition job.h:214
wxString m_outputPath
Definition job.h:265
LSEQ is a sequence (and therefore also a set) of PCB_LAYER_IDs.
Definition lseq.h:47
static int NameToLayer(wxString &aName)
Return the layer number from a layer name.
Definition lset.cpp:117
static wxString Name(PCB_LAYER_ID aLayerId)
Return the fixed name association with aLayerId.
Definition lset.cpp:188
Container for project specific data.
Definition project.h:65
Hold the information shown in the lower right corner of a plot, printout, or editing view.
Definition title_block.h:41
KICOMMON_API void to_json(nlohmann::json &j, const JOB &f)
Definition job.cpp:163
KICOMMON_API void from_json(const nlohmann::json &j, JOB &f)
Definition job.cpp:169
#define KICOMMON_API
Definition kicommon.h:28
PCB_LAYER_ID
A quick note on layer IDs:
Definition layer_ids.h:60
@ UNDEFINED_LAYER
Definition layer_ids.h:61
STL namespace.
JOB_OUTPUT()
Definition job.h:173
wxString m_outputPath
Definition job.h:177
JOB_OUTPUT(wxString outputPath)
Definition job.h:175