Salome HOME
Add licence.
[tools/ydefx.git] / src / cpp / Launcher.hxx
1 // Copyright (C) 2019  EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 #ifndef YDEFX_LAUNCHER_H
20 #define YDEFX_LAUNCHER_H
21
22 #include "TMonoPyJob.hxx"
23
24 namespace ydefx
25 {
26 class Job;
27 class Launcher
28 {
29 public:
30   Launcher():_lastError(){}
31   const std::string& lastError()const{return _lastError;}
32
33   /*!
34    * Create and submit a new job.
35    * Return nullptr in case of failure. Check the error with lastError().
36    */
37   template <class ...Ts>
38   Job* submitMonoPyJob(const PyStudyFunction& fnScript,
39                        Sample<Ts...>& sample,
40                        const JobParametersProxy& params);
41
42   /*!
43    * Connect to an already created job.
44    * Return nullptr in case of failure. Check the error with lastError().
45    */
46   template <class ...Ts>
47   Job* connectJob(const std::string& jobDump, Sample<Ts...>& sample);
48
49   template <class ...Ts>
50   Job* submitMultiPyJob(const PyStudyFunction& fn,
51                         Sample<Ts...>& sample,
52                         const JobParametersProxy& params);
53
54 private:
55   std::string _lastError;
56 };
57
58 ////////////////////////////////////////////////////////////////////////////////
59 // Template implementations
60 ////////////////////////////////////////////////////////////////////////////////
61
62 template <class ...Ts>
63 Job* Launcher::submitMonoPyJob(const PyStudyFunction& fnScript,
64                     Sample<Ts...>& sample,
65                     const JobParametersProxy& params)
66 {
67   Job* result = nullptr;
68   _lastError = "";
69   try
70   {
71     result = new TMonoPyJob<Ts...>(fnScript, sample, params);
72   }
73   catch(std::exception& e)
74   {
75     if(result != nullptr)
76       delete result;
77     result = nullptr;
78     _lastError = e.what();
79     return result;
80   }
81
82   if(!result->launch())
83   {
84     _lastError = "Failed to submit job.\n";
85     _lastError  += result->lastError();
86     delete result;
87     result = nullptr;
88   }
89   return result;
90 }
91
92 template <class ...Ts>
93 Job* Launcher::connectJob(const std::string& jobDump,
94                       Sample<Ts...>& sample)
95 {
96   Job* result = nullptr;
97   _lastError = "";
98   try
99   {
100     result = new TMonoPyJob<Ts...>(jobDump, sample);
101   }
102   catch(std::exception& e)
103   {
104     if(result != nullptr)
105       delete result;
106     result = nullptr;
107     _lastError = e.what();
108   }
109
110   return result;
111 }
112
113 template <class ...Ts>
114 Job* Launcher::submitMultiPyJob(const PyStudyFunction& fn,
115                       Sample<Ts...>& sample,
116                       const JobParametersProxy& params)
117 {
118   // TODO
119   _lastError = "Not implemented!";
120   return nullptr;
121 }
122
123 }
124 #endif // YDEFX_LAUNCHER_H