Documentation for OpenFLUID 2.2.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
WareCppWriterHelpers.hpp
Go to the documentation of this file.
1 /*
2 
3  This file is part of OpenFLUID software
4  Copyright(c) 2007, INRA - Montpellier SupAgro
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 
48 #include <openfluid/dllexport.hpp>
50 #include <openfluid/ware/TypeDefs.hpp>
53 
54 
55 namespace openfluid { namespace waresdev {
56 
57 
58 struct CppWriter {
59 
60  static std::string getHead(const std::string CommentChar)
61  {
62  std::string Head =
63  CommentChar + " ======\n" +
64  CommentChar + " AUTOMATICALLY GENERATED FILE. DO NOT EDIT MANUALLY\n" +
65  CommentChar + " ======\n\n";
66 
67  return Head;
68  }
69 
70 
71  // =====================================================================
72  // =====================================================================
73 
74 
75  static std::string getCPPHead(const std::string& WareIncludeStr, const std::string& WareTypeStr)
76  {
77  std::string Head = getHead("//");
78 
79  Head +=
80  "#include <"+WareIncludeStr+">\n\n"
81  "extern \"C\" {\n"
82  "OPENFLUID_PLUGIN "+WareTypeStr+"* "+std::string(WARESIGNATURE_PROC_NAME)+"()\n"
83  "{\n"
84  +WareTypeStr+"* Signature = new "+WareTypeStr+"();\n\n";
85 
86  return Head;
87  }
88 
89 
90  // =====================================================================
91  // =====================================================================
92 
93 
94  static std::string getQuotedString(const std::string& Str)
95  {
96  return "\""+Str+"\"";
97  }
98 
99 
100  // =====================================================================
101  // =====================================================================
102 
103 
104  static std::string getCPPDateTimeString(const openfluid::core::DateTime& DT) // TODO EXPORT AS UTILS?
105  {
106  std::string Str = "openfluid::core::DateTime(";
107 
108  Str += std::to_string(DT.getYear())+",";
109  Str += std::to_string(DT.getMonth())+",";
110  Str += std::to_string(DT.getDay())+",";
111  Str += std::to_string(DT.getHour())+",";
112  Str += std::to_string(DT.getMinute())+",";
113  Str += std::to_string(DT.getSecond())+")";
114 
115  return Str;
116  }
117 
118 
119  // =====================================================================
120  // =====================================================================
121 
122 
123  static std::string getCPPVectorString(const std::vector<std::string>& StrVect,bool AutoQuote = false)
124  {
125  std::string Str;
126 
127  Str += "{";
128  bool isFirst = true;
129  for (const auto& S : StrVect)
130  {
131  if (!isFirst)
132  {
133  Str += ",";
134  }
135  if (AutoQuote)
136  {
137  Str += getQuotedString(S);
138  }
139  else
140  {
141  Str += S;
142  }
143  isFirst = false;
144  }
145  Str += "}";
146 
147  return Str;
148  }
149 
150 
151  // =====================================================================
152  // =====================================================================
153 
154 
155  static std::string getCPPEntry(const std::string& Member)
156  {
157  return "Signature->"+Member;
158  }
159 
160 
161  // =====================================================================
162  // =====================================================================
163 
164 
165  static std::string getCPPAssignment(const std::string& Member, const std::string& Value, bool AutoQuote = false)
166  {
167  std::string QuotedVal(Value);
168 
169  if (AutoQuote)
170  {
171  QuotedVal = getQuotedString(QuotedVal);
172  }
173 
174  return getCPPEntry(Member)+" = "+QuotedVal+";\n";
175  }
176 
177 
178  // =====================================================================
179  // =====================================================================
180 
181 
182  static std::string getCPPMethod(const std::string& Member, const std::string& Method,
183  const std::vector<std::string>& Args, const std::string& Access = ".")
184  {
185  std::string ArgsStr;
186  bool FirstArg = true;
187 
188  for (const auto& A : Args)
189  {
190  if (FirstArg)
191  {
192  FirstArg = false;
193  }
194  else
195  {
196  ArgsStr += ",";
197  }
198 
199  ArgsStr += A;
200  }
201 
202  return getCPPEntry(Member)+Access+Method+"("+ArgsStr+");\n";
203  }
204 
205 
206  // =====================================================================
207  // =====================================================================
208 
209 
210  static std::string getCPPLinkUIDProc(const std::string LinkUID)
211  {
212  std::string Str =
213  "extern \"C\" {\n"
214  "OPENFLUID_PLUGIN const std::string* "+std::string(WARELINKUID_PROC_NAME)+"()\n"
215  "{\n"
216  "return new std::string(\""+LinkUID+"\");\n"
217  "}\n"
218  "}";
219  return Str;
220  }
221 
222 
223  // =====================================================================
224  // =====================================================================
225 
226 
227  static std::string getCPPSpatialDataString(
228  const std::string Member,const std::vector<openfluid::ware::SignatureSpatialDataItem>& Data)
229  {
230  std::string Str;
231 
232  for (const auto& D : Data)
233  {
234  Str += getCPPMethod(Member,"push_back",{"{"+CppWriter::getQuotedString(D.Name)+","+
235  CppWriter::getQuotedString(D.UnitsClass)+","+
236  CppWriter::getQuotedString(D.Description)+","+
237  CppWriter::getQuotedString(D.SIUnit)+","+
238  CppWriter::getCPPValueType(D.DataType)+"}"});
239  }
240 
241  return Str;
242  }
243 
244 
245  // =====================================================================
246  // =====================================================================
247 
248 
249  static std::string getCPPTail()
250  {
251  return "\nreturn Signature;\n}\n}\n";
252  }
253 
254 
255  // =====================================================================
256  // =====================================================================
257 
258 
259  static std::string getCPPValueType(const openfluid::core::Value::Type ValueType)
260  {
261  switch (ValueType)
262  {
264  return "openfluid::core::Value::NONE";
266  return "openfluid::core::Value::DOUBLE";
268  return "openfluid::core::Value::INTEGER";
270  return "openfluid::core::Value::BOOLEAN";
272  return "openfluid::core::Value::VECTOR";
274  return "openfluid::core::Value::MATRIX";
276  return "openfluid::core::Value::MAP";
278  return "openfluid::core::Value::TREE";
280  return "openfluid::core::Value::STRING";
282  return "openfluid::core::Value::NULLL";
283  default:
284  return "";
285  }
286  }
287 
288 
289  // =====================================================================
290  // =====================================================================
291 
292 
293  static std::string getCPPDataString(const std::string Member,
294  const std::vector<openfluid::ware::SignatureDataItem>& Data)
295  {
296  std::string Str;
297 
298  for (const auto& D : Data)
299  {
300 
301  Str += CppWriter::getCPPMethod(Member,"push_back",{"{"+getQuotedString(D.Name)+","+
302  getQuotedString(D.Description)+","+
303  getQuotedString(D.SIUnit)+","+
304  getCPPValueType(D.DataType)+"}"});
305  }
306 
307  return Str;
308  }
309 
310 
311  // =====================================================================
312  // =====================================================================
313 
314 
315  static std::string toWareCPPParams(const openfluid::ware::SignatureHandledData& HandledData)
316  {
317  std::string CPP;
318 
319  // Parameters
320  CPP += getCPPDataString("HandledData.UsedParams", HandledData.UsedParams);
321  CPP += getCPPDataString("HandledData.RequiredParams", HandledData.RequiredParams);
322 
323  return CPP;
324  }
325 };
326 
327 
328 // =====================================================================
329 // =====================================================================
330 
331 
333 {
334 
336  {
338 
339  Data.Name = Item.value("name","");
340 
342  {
343  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"Missing or invalid data name");
344  }
345 
346  Data.Description = Item.value("description","");
347  Data.SIUnit = Item.value("siunit","");
348 
350 
351  if (openfluid::core::Value::getValueTypeFromString(Item.value("type",""),VT))
352  {
353  Data.DataType = VT;
354  }
355 
356  return Data;
357  }
358 
359 
360  // =====================================================================
361  // =====================================================================
362 
363 
365  {
366  openfluid::thirdparty::json Json = openfluid::thirdparty::json::object();
367 
368  Json["name"] = Item.Name;
369  Json["description"] = Item.Description;
370  Json["siunit"] = Item.SIUnit;
372 
373  return Json;
374  }
375 
376 
377  // =====================================================================
378  // =====================================================================
379 
380 
381  std::vector<openfluid::ware::SignatureDataItem>
383  {
384  std::vector<openfluid::ware::SignatureDataItem> List;
385 
386  if (Json.is_array())
387  {
388  for (const auto& I : Json)
389  {
390  List.push_back(readDataItemFromJSON(I));
391  }
392  }
393 
394  return List;
395  }
396 
397 
398  // =====================================================================
399  // =====================================================================
400 
401 
403  {
404  openfluid::thirdparty::json Json = openfluid::thirdparty::json::object();
405 
406  auto JsonReq = openfluid::thirdparty::json::array();
407  for (const auto& P : HandledData.RequiredParams)
408  {
409  JsonReq.push_back(serializeDataItemToJSON(P));
410  }
411  Json["required"] = JsonReq;
412 
413  auto JsonUs = openfluid::thirdparty::json::array();
414  for (const auto& P : HandledData.UsedParams)
415  {
416  JsonUs.push_back(serializeDataItemToJSON(P));
417  }
418  Json["used"] = JsonUs;
419 
420  return Json;
421  }
422 
423 
424  // =====================================================================
425  // =====================================================================
426 
427 
429  Item)
430  {
431  openfluid::thirdparty::json Json = openfluid::thirdparty::json::object();
432 
433  Json["name"] = Item.Name;
434  Json["unitsclass"] = Item.UnitsClass;
435  Json["description"] = Item.Description;
436  Json["siunit"] = Item.SIUnit;
438 
439  return Json;
440  }
441 
442 
443  // =====================================================================
444  // =====================================================================
445 
446 
449  {
450  openfluid::thirdparty::json Json = openfluid::thirdparty::json::object();
451 
452  auto JsonReq = openfluid::thirdparty::json::array();
453  for (const auto& A : HandledData.RequiredAttributes)
454  {
455  JsonReq.push_back(serializeSpatialDataItemToJSON(A));
456  }
457  Json["required"] = JsonReq;
458 
459  auto JsonUs = openfluid::thirdparty::json::array();
460  for (const auto& A : HandledData.UsedAttributes)
461  {
462  JsonUs.push_back(serializeSpatialDataItemToJSON(A));
463  }
464  Json["used"] = JsonUs;
465 
466  return Json;
467  }
468 
469 
470  // =====================================================================
471  // =====================================================================
472 
473 
476  {
477  openfluid::thirdparty::json Json = openfluid::thirdparty::json::object();
478 
479  auto JsonReq = openfluid::thirdparty::json::array();
480  for (const auto& V : HandledData.RequiredVars)
481  {
482  JsonReq.push_back(serializeSpatialDataItemToJSON(V));
483  }
484  Json["required"] = JsonReq;
485 
486  auto JsonUs = openfluid::thirdparty::json::array();
487  for (const auto& V : HandledData.UsedVars)
488  {
489  JsonUs.push_back(serializeSpatialDataItemToJSON(V));
490  }
491  Json["used"] = JsonUs;
492 
493  return Json;
494  }
495 
496 
497  // =====================================================================
498  // =====================================================================
499 
500 
503  {
504  openfluid::thirdparty::json Json = openfluid::thirdparty::json::object();
505 
506  Json["required"] = HandledData.RequiredExtraFiles;
507  Json["used"] = HandledData.UsedExtraFiles;
508 
509  return Json;
510  }
511 
512 
513  // =====================================================================
514  // =====================================================================
515 
516 
518  {
519  openfluid::thirdparty::json Json = openfluid::thirdparty::json::object();
520 
521  Json["parameters"] = serializeParametersToJSON(HandledData);
522  Json["attributes"] = serializeAttributesToJSON(HandledData);
523  Json["variables"] = serializeVariablesToJSON(HandledData);
524  Json["extrafiles"] = serializeExtrafilesToJSON(HandledData);
525 
526 
527  return Json;
528  }
529 
530 
531  // =====================================================================
532  // =====================================================================
533 
534 
537  {
538  if (Json.contains("used"))
539  {
540  HandledData.UsedParams = readDataListFromJSON(Json.at("used"));
541  }
542 
543  if (Json.contains("required"))
544  {
545  HandledData.RequiredParams = readDataListFromJSON(Json.at("required"));
546  }
547  }
548 
549 
550  // =====================================================================
551  // =====================================================================
552 
553 
555  {
557 
558  Data.Name = Item.value("name","");
560  {
561  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"Missing or invalid data name");
562  }
563 
564  Data.UnitsClass = Item.value("unitsclass","");
565  if (Data.UnitsClass.empty())
566  {
567  throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,"Missing or invalid units class for data");
568  }
569 
570  Data.Description = Item.value("description","");
571  Data.SIUnit = Item.value("siunit","");
572 
574 
575  if (openfluid::core::Value::getValueTypeFromString(Item.value("type",""),VT))
576  {
577  Data.DataType = VT;
578  }
579 
580  return Data;
581  }
582 
583 
584  // =====================================================================
585  // =====================================================================
586 
587 
588  static std::vector<openfluid::ware::SignatureSpatialDataItem>
590  {
591  std::vector<openfluid::ware::SignatureSpatialDataItem> List;
592 
593  if (Json.is_array())
594  {
595  for (const auto& I : Json)
596  {
597  List.push_back(readSpatialDataItemFromJSON(I));
598  }
599  }
600 
601  return List;
602  }
603 
604 
605  // =====================================================================
606  // =====================================================================
607 
608 
611  {
612 
613  if (Json.contains("used"))
614  {
616  }
617 
618  if (Json.contains("required"))
619  {
620  Sign.HandledData.RequiredAttributes = readSpatialDataListFromJSON(Json.at("required"));
621  }
622  }
623 
624 
625  // =====================================================================
626  // =====================================================================
627 
628 
631  {
632  if (Json.contains("used"))
633  {
634  Sign.HandledData.UsedVars = readSpatialDataListFromJSON(Json.at("used"));
635  }
636 
637  if (Json.contains("required"))
638  {
639  Sign.HandledData.RequiredVars = readSpatialDataListFromJSON(Json.at("required"));
640  }
641  }
642 
643 
644  // =====================================================================
645  // =====================================================================
646 
647 
650  {
651  if (Json.contains("used"))
652  {
653  Sign.HandledData.UsedExtraFiles = Json.at("used").get<std::vector<std::string>>();
654  }
655 
656  if (Json.contains("required"))
657  {
658  Sign.HandledData.RequiredExtraFiles = Json.at("required").get<std::vector<std::string>>();
659  }
660 
661  }
662 
663 };
664 
665 
666 } } // namespaces
667 
668 
669 #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:331
SignatureHandledData HandledData
Definition: WareSignature.hpp:336
Definition: WareSignature.hpp:233
openfluid::core::Value::Type DataType
Definition: WareSignature.hpp:239
std::string SIUnit
Definition: WareSignature.hpp:238
std::string Name
Definition: WareSignature.hpp:236
std::string Description
Definition: WareSignature.hpp:237
Definition: WareSignature.hpp:287
std::vector< SignatureDataItem > UsedParams
Definition: WareSignature.hpp:290
std::vector< SignatureSpatialDataItem > UsedVars
Definition: WareSignature.hpp:296
std::vector< std::string > RequiredExtraFiles
Definition: WareSignature.hpp:302
std::vector< std::string > UsedExtraFiles
Definition: WareSignature.hpp:304
std::vector< SignatureSpatialDataItem > RequiredAttributes
Definition: WareSignature.hpp:298
std::vector< SignatureSpatialDataItem > RequiredVars
Definition: WareSignature.hpp:294
std::vector< SignatureSpatialDataItem > UsedAttributes
Definition: WareSignature.hpp:300
std::vector< SignatureDataItem > RequiredParams
Definition: WareSignature.hpp:292
Definition: WareSignature.hpp:259
openfluid::core::UnitsClass_t UnitsClass
Definition: WareSignature.hpp:262
nlohmann::ordered_json json
Definition: JSON.hpp:52
bool OPENFLUID_API isValidVariableName(const openfluid::core::VariableName_t &Name)
Definition: ApplicationException.hpp:47
Definition: WareCppWriterHelpers.hpp:58
static std::string getCPPTail()
Definition: WareCppWriterHelpers.hpp:249
static std::string getHead(const std::string CommentChar)
Definition: WareCppWriterHelpers.hpp:60
static std::string getCPPEntry(const std::string &Member)
Definition: WareCppWriterHelpers.hpp:155
static std::string getCPPVectorString(const std::vector< std::string > &StrVect, bool AutoQuote=false)
Definition: WareCppWriterHelpers.hpp:123
static std::string getCPPDateTimeString(const openfluid::core::DateTime &DT)
Definition: WareCppWriterHelpers.hpp:104
static std::string getQuotedString(const std::string &Str)
Definition: WareCppWriterHelpers.hpp:94
static std::string getCPPHead(const std::string &WareIncludeStr, const std::string &WareTypeStr)
Definition: WareCppWriterHelpers.hpp:75
static std::string getCPPLinkUIDProc(const std::string LinkUID)
Definition: WareCppWriterHelpers.hpp:210
static std::string getCPPAssignment(const std::string &Member, const std::string &Value, bool AutoQuote=false)
Definition: WareCppWriterHelpers.hpp:165
static std::string getCPPSpatialDataString(const std::string Member, const std::vector< openfluid::ware::SignatureSpatialDataItem > &Data)
Definition: WareCppWriterHelpers.hpp:227
static std::string getCPPDataString(const std::string Member, const std::vector< openfluid::ware::SignatureDataItem > &Data)
Definition: WareCppWriterHelpers.hpp:293
static std::string toWareCPPParams(const openfluid::ware::SignatureHandledData &HandledData)
Definition: WareCppWriterHelpers.hpp:315
static std::string getCPPValueType(const openfluid::core::Value::Type ValueType)
Definition: WareCppWriterHelpers.hpp:259
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:182
Definition: WareCppWriterHelpers.hpp:333
static openfluid::thirdparty::json serializeAttributesToJSON(const openfluid::ware::SignatureHandledData &HandledData)
Definition: WareCppWriterHelpers.hpp:448
static void unserializeReadVariablesFromJSON(const openfluid::thirdparty::json &Json, openfluid::ware::DataWareSignature &Sign)
Definition: WareCppWriterHelpers.hpp:629
static openfluid::thirdparty::json serializeDataItemToJSON(const openfluid::ware::SignatureDataItem &Item)
Definition: WareCppWriterHelpers.hpp:364
static openfluid::thirdparty::json serializeSpatialDataItemToJSON(const openfluid::ware::SignatureSpatialDataItem &Item)
Definition: WareCppWriterHelpers.hpp:428
static openfluid::thirdparty::json serializeDataToJSON(const openfluid::ware::SignatureHandledData &HandledData)
Definition: WareCppWriterHelpers.hpp:517
static openfluid::thirdparty::json serializeExtrafilesToJSON(const openfluid::ware::SignatureHandledData &HandledData)
Definition: WareCppWriterHelpers.hpp:502
static openfluid::thirdparty::json serializeVariablesToJSON(const openfluid::ware::SignatureHandledData &HandledData)
Definition: WareCppWriterHelpers.hpp:475
static void unserializeParametersFromJSON(const openfluid::thirdparty::json &Json, openfluid::ware::SignatureHandledData &HandledData)
Definition: WareCppWriterHelpers.hpp:535
static void unserializeExtrafilesFromJSON(const openfluid::thirdparty::json &Json, openfluid::ware::DataWareSignature &Sign)
Definition: WareCppWriterHelpers.hpp:648
static openfluid::ware::SignatureDataItem readDataItemFromJSON(const openfluid::thirdparty::json &Item)
Definition: WareCppWriterHelpers.hpp:335
static void unserializeReadAttributesFromJSON(const openfluid::thirdparty::json &Json, openfluid::ware::DataWareSignature &Sign)
Definition: WareCppWriterHelpers.hpp:609
static openfluid::thirdparty::json serializeParametersToJSON(const openfluid::ware::SignatureHandledData &HandledData)
Definition: WareCppWriterHelpers.hpp:402
static std::vector< openfluid::ware::SignatureDataItem > readDataListFromJSON(const openfluid::thirdparty::json &Json)
Definition: WareCppWriterHelpers.hpp:382
static std::vector< openfluid::ware::SignatureSpatialDataItem > readSpatialDataListFromJSON(const openfluid::thirdparty::json &Json)
Definition: WareCppWriterHelpers.hpp:589
static openfluid::ware::SignatureSpatialDataItem readSpatialDataItemFromJSON(const openfluid::thirdparty::json &Item)
Definition: WareCppWriterHelpers.hpp:554