Documentation for OpenFLUID 2.2.2
Loading...
Searching...
No Matches
WareCppWriterHelpers.hpp
Go to the documentation of this file.
1/*
2
3 This file is part of OpenFLUID software
4 Copyright(c) 2021-2026, INRAE
5
6
7 == GNU General Public License Usage ==
8
9 OpenFLUID is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 OpenFLUID is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with OpenFLUID. If not, see <http://www.gnu.org/licenses/>.
21
22
23 == Other Usage ==
24
25 Other Usage means a use of OpenFLUID that is inconsistent with the GPL
26 license, and requires a written agreement between You and INRA.
27 Licensees for Other Usage of OpenFLUID may use this file in accordance
28 with the terms contained in the written agreement between You and INRA.
29
30*/
31
32
33/**
34 @file WareCppWriterHelpers.hpp
35
36 @author Jean-Christophe FABRE <jean-christophe.fabre@inrae.fr>
37 @author Armel THÖNI <armel.thoni@inrae.fr>
38 */
39
40
41#ifndef __OPENFLUID_WARESDEV_WARECPPWRITERHELPERS_HPP__
42#define __OPENFLUID_WARESDEV_WARECPPWRITERHELPERS_HPP__
43
44
45#include <string>
46#include <vector>
47
50#include <openfluid/ware/TypeDefs.hpp>
54
55
56namespace openfluid { namespace waresdev {
57
58
59struct CppWriter {
60
61 static std::string getHead(const std::string CommentChar)
62 {
63 std::string Head =
64 CommentChar + " ======\n" +
65 CommentChar + " AUTOMATICALLY GENERATED FILE. DO NOT EDIT MANUALLY\n" +
66 CommentChar + " ======\n\n";
67
68 return Head;
69 }
70
71
72 // =====================================================================
73 // =====================================================================
74
75
76 static std::string getCPPHead(const std::string& WareIncludeStr, const std::string& WareTypeStr)
77 {
78 std::string Head = getHead("//");
79
80 Head +=
81 "#include <"+WareIncludeStr+">\n\n"
82 "extern \"C\" {\n"
83 "OPENFLUID_PLUGIN "+WareTypeStr+"* "+std::string(WARESIGNATURE_PROC_NAME)+"()\n"
84 "{\n"
85 +WareTypeStr+"* Signature = new "+WareTypeStr+"();\n\n";
86
87 return Head;
88 }
89
90
91 // =====================================================================
92 // =====================================================================
93
94
95 static std::string getQuotedString(const std::string& Str)
96 {
97 return "\""+Str+"\"";
98 }
99
100
101 // =====================================================================
102 // =====================================================================
103
104
105 static std::string getCPPDateTimeString(const openfluid::core::DateTime& DT) // TODO EXPORT AS UTILS?
106 {
107 std::string Str = "openfluid::core::DateTime(";
108
109 Str += std::to_string(DT.getYear())+",";
110 Str += std::to_string(DT.getMonth())+",";
111 Str += std::to_string(DT.getDay())+",";
112 Str += std::to_string(DT.getHour())+",";
113 Str += std::to_string(DT.getMinute())+",";
114 Str += std::to_string(DT.getSecond())+")";
115
116 return Str;
117 }
118
119
120 // =====================================================================
121 // =====================================================================
122
123
124 static std::string getCPPVectorString(const std::vector<std::string>& StrVect,bool AutoQuote = false)
125 {
126 std::string Str;
127
128 Str += "{";
129 bool isFirst = true;
130 for (const auto& S : StrVect)
131 {
132 if (!isFirst)
133 {
134 Str += ",";
135 }
136 if (AutoQuote)
137 {
138 Str += getQuotedString(S);
139 }
140 else
141 {
142 Str += S;
143 }
144 isFirst = false;
145 }
146 Str += "}";
147
148 return Str;
149 }
150
151
152 // =====================================================================
153 // =====================================================================
154
155
156 static std::string getCPPEntry(const std::string& Member)
157 {
158 return "Signature->"+Member;
159 }
160
161
162 // =====================================================================
163 // =====================================================================
164
165
166 static std::string getCPPAssignment(const std::string& Member, const std::string& Value, bool AutoQuote = false)
167 {
168 std::string QuotedVal(Value);
169
170 if (AutoQuote)
171 {
172 QuotedVal = getQuotedString(QuotedVal);
173 }
174
175 return getCPPEntry(Member)+" = "+QuotedVal+";\n";
176 }
177
178
179 // =====================================================================
180 // =====================================================================
181
182
183 static std::string getCPPMethod(const std::string& Member, const std::string& Method,
184 const std::vector<std::string>& Args, const std::string& Access = ".")
185 {
186 std::string ArgsStr;
187 bool FirstArg = true;
188
189 for (const auto& A : Args)
190 {
191 if (FirstArg)
192 {
193 FirstArg = false;
194 }
195 else
196 {
197 ArgsStr += ",";
198 }
199
200 ArgsStr += A;
201 }
202
203 return getCPPEntry(Member)+Access+Method+"("+ArgsStr+");\n";
204 }
205
206
207 // =====================================================================
208 // =====================================================================
209
210
211 static std::string getCPPLinkUIDProc(const std::string LinkUID)
212 {
213 std::string Str =
214 "extern \"C\" {\n"
215 "OPENFLUID_PLUGIN const std::string* "+std::string(WARELINKUID_PROC_NAME)+"()\n"
216 "{\n"
217 "return new std::string(\""+LinkUID+"\");\n"
218 "}\n"
219 "}";
220 return Str;
221 }
222
223
224 // =====================================================================
225 // =====================================================================
226
227
228 static std::string getCPPSpatialDataString(
229 const std::string Member,const std::vector<openfluid::ware::SignatureSpatialDataItem>& Data)
230 {
231 std::string Str;
232
233 for (const auto& D : Data)
234 {
235 Str += getCPPMethod(Member,"push_back",{"{"+CppWriter::getQuotedString(D.Name)+","+
236 CppWriter::getQuotedString(D.UnitsClass)+","+
237 CppWriter::getQuotedString(D.Description)+","+
238 CppWriter::getQuotedString(D.SIUnit)+","+
239 CppWriter::getCPPValueType(D.DataType)+"}"});
240 }
241
242 return Str;
243 }
244
245
246 // =====================================================================
247 // =====================================================================
248
249
250 static std::string getCPPTail()
251 {
252 return "\nreturn Signature;\n}\n}\n";
253 }
254
255
256 // =====================================================================
257 // =====================================================================
258
259
260 static std::string getCPPValueType(const openfluid::core::Value::Type ValueType)
261 {
262 switch (ValueType)
263 {
265 return "openfluid::core::Value::NONE";
267 return "openfluid::core::Value::DOUBLE";
269 return "openfluid::core::Value::INTEGER";
271 return "openfluid::core::Value::BOOLEAN";
273 return "openfluid::core::Value::VECTOR";
275 return "openfluid::core::Value::MATRIX";
277 return "openfluid::core::Value::MAP";
279 return "openfluid::core::Value::TREE";
281 return "openfluid::core::Value::STRING";
283 return "openfluid::core::Value::NULLL";
284 default:
285 return "";
286 }
287 }
288
289
290 // =====================================================================
291 // =====================================================================
292
293
294 static std::string getCPPDataString(const std::string Member,
295 const std::vector<openfluid::ware::SignatureDataItem>& Data)
296 {
297 std::string Str;
298
299 for (const auto& D : Data)
300 {
301
302 Str += CppWriter::getCPPMethod(Member,"push_back",{"{"+getQuotedString(D.Name)+","+
303 getQuotedString(D.Description)+","+
304 getQuotedString(D.SIUnit)+","+
305 getCPPValueType(D.DataType)+"}"});
306 }
307
308 return Str;
309 }
310
311
312 // =====================================================================
313 // =====================================================================
314
315
316 static std::string toWareCPPParams(const openfluid::ware::SignatureHandledData& HandledData)
317 {
318 std::string CPP;
319
320 // Parameters
321 CPP += getCPPDataString("HandledData.UsedParams", HandledData.UsedParams);
322 CPP += getCPPDataString("HandledData.RequiredParams", HandledData.RequiredParams);
323
324 return CPP;
325 }
326};
327
328
329// =====================================================================
330// =====================================================================
331
332
334{
335
337 std::function<bool(const std::string&)> Validator=openfluid::ware::isNonEmpty)
338 {
340
341 Data.Name = Item.value("name","");
342
343 if (!Validator(Data.Name))
344 {
345 throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"Missing or invalid data name: "+Data.Name);
346 }
347
348 Data.Description = Item.value("description","");
349 Data.SIUnit = Item.value("siunit","");
350
352
353 if (openfluid::core::Value::getValueTypeFromString(Item.value("type",""),VT))
354 {
355 Data.DataType = VT;
356 }
357
358 return Data;
359 }
360
361
362 // =====================================================================
363 // =====================================================================
364
365
367 {
368 openfluid::thirdparty::json Json = openfluid::thirdparty::json::object();
369
370 Json["name"] = Item.Name;
371 Json["description"] = Item.Description;
372 Json["siunit"] = Item.SIUnit;
374
375 return Json;
376 }
377
378
379 // =====================================================================
380 // =====================================================================
381
382
383 std::vector<openfluid::ware::SignatureDataItem>
385 {
386 std::vector<openfluid::ware::SignatureDataItem> List;
387
388 if (Json.is_array())
389 {
390 for (const auto& I : Json)
391 {
392 List.push_back(readDataItemFromJSON(I));
393 }
394 }
395
396 return List;
397 }
398
399
400 // =====================================================================
401 // =====================================================================
402
403
405 {
406 openfluid::thirdparty::json Json = openfluid::thirdparty::json::object();
407
408 auto JsonReq = openfluid::thirdparty::json::array();
409 for (const auto& P : HandledData.RequiredParams)
410 {
411 JsonReq.push_back(serializeDataItemToJSON(P));
412 }
413 Json["required"] = JsonReq;
414
415 auto JsonUs = openfluid::thirdparty::json::array();
416 for (const auto& P : HandledData.UsedParams)
417 {
418 JsonUs.push_back(serializeDataItemToJSON(P));
419 }
420 Json["used"] = JsonUs;
421
422 return Json;
423 }
424
425
426 // =====================================================================
427 // =====================================================================
428
429
431 Item)
432 {
433 openfluid::thirdparty::json Json = openfluid::thirdparty::json::object();
434
435 Json["name"] = Item.Name;
436 Json["unitsclass"] = Item.UnitsClass;
437 Json["description"] = Item.Description;
438 Json["siunit"] = Item.SIUnit;
440
441 return Json;
442 }
443
444
445 // =====================================================================
446 // =====================================================================
447
448
451 {
452 openfluid::thirdparty::json Json = openfluid::thirdparty::json::object();
453
454 auto JsonReq = openfluid::thirdparty::json::array();
455 for (const auto& A : HandledData.RequiredAttributes)
456 {
457 JsonReq.push_back(serializeSpatialDataItemToJSON(A));
458 }
459 Json["required"] = JsonReq;
460
461 auto JsonUs = openfluid::thirdparty::json::array();
462 for (const auto& A : HandledData.UsedAttributes)
463 {
464 JsonUs.push_back(serializeSpatialDataItemToJSON(A));
465 }
466 Json["used"] = JsonUs;
467
468 return Json;
469 }
470
471
472 // =====================================================================
473 // =====================================================================
474
475
478 {
479 openfluid::thirdparty::json Json = openfluid::thirdparty::json::object();
480
481 auto JsonReq = openfluid::thirdparty::json::array();
482 for (const auto& V : HandledData.RequiredVars)
483 {
484 JsonReq.push_back(serializeSpatialDataItemToJSON(V));
485 }
486 Json["required"] = JsonReq;
487
488 auto JsonUs = openfluid::thirdparty::json::array();
489 for (const auto& V : HandledData.UsedVars)
490 {
491 JsonUs.push_back(serializeSpatialDataItemToJSON(V));
492 }
493 Json["used"] = JsonUs;
494
495 return Json;
496 }
497
498
499 // =====================================================================
500 // =====================================================================
501
502
505 {
506 openfluid::thirdparty::json Json = openfluid::thirdparty::json::object();
507
508 Json["required"] = HandledData.RequiredExtraFiles;
509 Json["used"] = HandledData.UsedExtraFiles;
510
511 return Json;
512 }
513
514
515 // =====================================================================
516 // =====================================================================
517
518
520 {
521 openfluid::thirdparty::json Json = openfluid::thirdparty::json::object();
522
523 Json["parameters"] = serializeParametersToJSON(HandledData);
524 Json["attributes"] = serializeAttributesToJSON(HandledData);
525 Json["variables"] = serializeVariablesToJSON(HandledData);
526 Json["extrafiles"] = serializeExtrafilesToJSON(HandledData);
527
528
529 return Json;
530 }
531
532
533 // =====================================================================
534 // =====================================================================
535
536
539 {
540 if (Json.contains("used"))
541 {
542 HandledData.UsedParams = readDataListFromJSON(Json.at("used"));
543 }
544
545 if (Json.contains("required"))
546 {
547 HandledData.RequiredParams = readDataListFromJSON(Json.at("required"));
548 }
549 }
550
551
552 // =====================================================================
553 // =====================================================================
554
555
557 std::function<bool(const std::string&)> Validator=openfluid::ware::isNonEmpty)
558 {
560
561 Data.Name = Item.value("name","");
562 if (!Validator(Data.Name))
563 {
564 throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,
565 "Missing or invalid spatial data name: "+Data.Name);
566 }
567
568 Data.UnitsClass = Item.value("unitsclass","");
569 if (Data.UnitsClass.empty())
570 {
571 throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"Missing or invalid units class for data");
572 }
573
574 Data.Description = Item.value("description","");
575 Data.SIUnit = Item.value("siunit","");
576
578
579 if (openfluid::core::Value::getValueTypeFromString(Item.value("type",""),VT))
580 {
581 Data.DataType = VT;
582 }
583
584 return Data;
585 }
586
587
588 // =====================================================================
589 // =====================================================================
590
591
592 static std::vector<openfluid::ware::SignatureSpatialDataItem>
594 std::function<bool(const std::string&)> Validator=openfluid::ware::isNonEmpty)
595 {
596 std::vector<openfluid::ware::SignatureSpatialDataItem> List;
597
598 if (Json.is_array())
599 {
600 for (const auto& I : Json)
601 {
602 List.push_back(readSpatialDataItemFromJSON(I, Validator));
603 }
604 }
605
606 return List;
607 }
608
609
610 // =====================================================================
611 // =====================================================================
612
613
614 static std::vector<openfluid::ware::SignatureSpatialDataItem>
619
620
621 // =====================================================================
622 // =====================================================================
623
624
627 {
628
629 if (Json.contains("used"))
630 {
632 }
633
634 if (Json.contains("required"))
635 {
637 }
638 }
639
640
641 // =====================================================================
642 // =====================================================================
643
644
647 {
648 if (Json.contains("used"))
649 {
650 Sign.HandledData.UsedVars = readVariableListFromJSON(Json.at("used"));
651 }
652
653 if (Json.contains("required"))
654 {
655 Sign.HandledData.RequiredVars = readVariableListFromJSON(Json.at("required"));
656 }
657 }
658
659
660 // =====================================================================
661 // =====================================================================
662
663
666 {
667 if (Json.contains("used"))
668 {
669 Sign.HandledData.UsedExtraFiles = Json.at("used").get<std::vector<std::string>>();
670 }
671
672 if (Json.contains("required"))
673 {
674 Sign.HandledData.RequiredExtraFiles = Json.at("required").get<std::vector<std::string>>();
675 }
676
677 }
678
679};
680
681
682} } // namespaces
683
684
685#endif /* __OPENFLUID_WARESDEV_WARECPPWRITERHELPERS_HPP__ */
#define WARELINKUID_PROC_NAME
Definition PluggableWare.hpp:75
#define WARESIGNATURE_PROC_NAME
Definition PluggableWare.hpp:62
Definition FrameworkException.hpp:51
Class for management of date and time information.
Definition DateTime.hpp:88
int getYear() const
Definition DateTime.hpp:211
int getSecond() const
Definition DateTime.hpp:256
int getHour() const
Definition DateTime.hpp:238
int getDay() const
Definition DateTime.hpp:229
int getMonth() const
Definition DateTime.hpp:220
int getMinute() const
Definition DateTime.hpp:247
static bool getValueTypeFromString(const std::string &ValueTypeString, Value::Type &ValueType)
Type
Definition Value.hpp:66
@ VECTOR
Definition Value.hpp:66
@ NONE
Definition Value.hpp:66
@ TREE
Definition Value.hpp:66
@ STRING
Definition Value.hpp:66
@ MAP
Definition Value.hpp:66
@ MATRIX
Definition Value.hpp:66
@ DOUBLE
Definition Value.hpp:66
@ NULLL
Definition Value.hpp:66
@ BOOLEAN
Definition Value.hpp:66
@ INTEGER
Definition Value.hpp:66
static std::string getStringFromValueType(const Value::Type ValueType)
Definition WareSignature.hpp:338
SignatureHandledData HandledData
Definition WareSignature.hpp:343
Definition WareSignature.hpp:235
openfluid::core::Value::Type DataType
Definition WareSignature.hpp:241
std::string SIUnit
Definition WareSignature.hpp:240
std::string Name
Definition WareSignature.hpp:238
std::string Description
Definition WareSignature.hpp:239
Definition WareSignature.hpp:294
std::vector< SignatureDataItem > UsedParams
Definition WareSignature.hpp:297
std::vector< SignatureSpatialDataItem > UsedVars
Definition WareSignature.hpp:303
std::vector< std::string > RequiredExtraFiles
Definition WareSignature.hpp:309
std::vector< std::string > UsedExtraFiles
Definition WareSignature.hpp:311
std::vector< SignatureSpatialDataItem > RequiredAttributes
Definition WareSignature.hpp:305
std::vector< SignatureSpatialDataItem > RequiredVars
Definition WareSignature.hpp:301
std::vector< SignatureSpatialDataItem > UsedAttributes
Definition WareSignature.hpp:307
std::vector< SignatureDataItem > RequiredParams
Definition WareSignature.hpp:299
Definition WareSignature.hpp:263
openfluid::core::UnitsClass_t UnitsClass
Definition WareSignature.hpp:266
nlohmann::ordered_json json
Definition JSON.hpp:52
bool OPENFLUID_API isValidVariableName(const openfluid::core::VariableName_t &Name)
bool OPENFLUID_API isNonEmpty(const std::string &Str)
Definition DataItemUtils.hpp:75
Definition ApplicationException.hpp:47
Definition WareCppWriterHelpers.hpp:59
static std::string getCPPTail()
Definition WareCppWriterHelpers.hpp:250
static std::string getHead(const std::string CommentChar)
Definition WareCppWriterHelpers.hpp:61
static std::string getCPPEntry(const std::string &Member)
Definition WareCppWriterHelpers.hpp:156
static std::string getCPPVectorString(const std::vector< std::string > &StrVect, bool AutoQuote=false)
Definition WareCppWriterHelpers.hpp:124
static std::string getCPPDateTimeString(const openfluid::core::DateTime &DT)
Definition WareCppWriterHelpers.hpp:105
static std::string getQuotedString(const std::string &Str)
Definition WareCppWriterHelpers.hpp:95
static std::string getCPPHead(const std::string &WareIncludeStr, const std::string &WareTypeStr)
Definition WareCppWriterHelpers.hpp:76
static std::string getCPPLinkUIDProc(const std::string LinkUID)
Definition WareCppWriterHelpers.hpp:211
static std::string getCPPAssignment(const std::string &Member, const std::string &Value, bool AutoQuote=false)
Definition WareCppWriterHelpers.hpp:166
static std::string getCPPSpatialDataString(const std::string Member, const std::vector< openfluid::ware::SignatureSpatialDataItem > &Data)
Definition WareCppWriterHelpers.hpp:228
static std::string getCPPDataString(const std::string Member, const std::vector< openfluid::ware::SignatureDataItem > &Data)
Definition WareCppWriterHelpers.hpp:294
static std::string toWareCPPParams(const openfluid::ware::SignatureHandledData &HandledData)
Definition WareCppWriterHelpers.hpp:316
static std::string getCPPValueType(const openfluid::core::Value::Type ValueType)
Definition WareCppWriterHelpers.hpp:260
static std::string getCPPMethod(const std::string &Member, const std::string &Method, const std::vector< std::string > &Args, const std::string &Access=".")
Definition WareCppWriterHelpers.hpp:183
Definition WareCppWriterHelpers.hpp:334
static openfluid::thirdparty::json serializeAttributesToJSON(const openfluid::ware::SignatureHandledData &HandledData)
Definition WareCppWriterHelpers.hpp:450
static void unserializeReadVariablesFromJSON(const openfluid::thirdparty::json &Json, openfluid::ware::DataWareSignature &Sign)
Definition WareCppWriterHelpers.hpp:645
static openfluid::thirdparty::json serializeDataItemToJSON(const openfluid::ware::SignatureDataItem &Item)
Definition WareCppWriterHelpers.hpp:366
static openfluid::thirdparty::json serializeSpatialDataItemToJSON(const openfluid::ware::SignatureSpatialDataItem &Item)
Definition WareCppWriterHelpers.hpp:430
static openfluid::thirdparty::json serializeDataToJSON(const openfluid::ware::SignatureHandledData &HandledData)
Definition WareCppWriterHelpers.hpp:519
static std::vector< openfluid::ware::SignatureDataItem > readDataListFromJSON(const openfluid::thirdparty::json &Json)
Definition WareCppWriterHelpers.hpp:384
static openfluid::ware::SignatureDataItem readDataItemFromJSON(const openfluid::thirdparty::json &Item, std::function< bool(const std::string &)> Validator=openfluid::ware::isNonEmpty)
Definition WareCppWriterHelpers.hpp:336
static openfluid::thirdparty::json serializeExtrafilesToJSON(const openfluid::ware::SignatureHandledData &HandledData)
Definition WareCppWriterHelpers.hpp:504
static openfluid::thirdparty::json serializeVariablesToJSON(const openfluid::ware::SignatureHandledData &HandledData)
Definition WareCppWriterHelpers.hpp:477
static std::vector< openfluid::ware::SignatureSpatialDataItem > readVariableListFromJSON(const openfluid::thirdparty::json &Json)
Definition WareCppWriterHelpers.hpp:615
static void unserializeParametersFromJSON(const openfluid::thirdparty::json &Json, openfluid::ware::SignatureHandledData &HandledData)
Definition WareCppWriterHelpers.hpp:537
static void unserializeExtrafilesFromJSON(const openfluid::thirdparty::json &Json, openfluid::ware::DataWareSignature &Sign)
Definition WareCppWriterHelpers.hpp:664
static openfluid::ware::SignatureSpatialDataItem readSpatialDataItemFromJSON(const openfluid::thirdparty::json &Item, std::function< bool(const std::string &)> Validator=openfluid::ware::isNonEmpty)
Definition WareCppWriterHelpers.hpp:556
static void unserializeReadAttributesFromJSON(const openfluid::thirdparty::json &Json, openfluid::ware::DataWareSignature &Sign)
Definition WareCppWriterHelpers.hpp:625
static openfluid::thirdparty::json serializeParametersToJSON(const openfluid::ware::SignatureHandledData &HandledData)
Definition WareCppWriterHelpers.hpp:404
static std::vector< openfluid::ware::SignatureSpatialDataItem > readSpatialDataListFromJSON(const openfluid::thirdparty::json &Json, std::function< bool(const std::string &)> Validator=openfluid::ware::isNonEmpty)
Definition WareCppWriterHelpers.hpp:593