GDevelop Core
Core library for developing platforms and tools compatible with GDevelop.
DiagnosticReport.h
1 /*
2  * GDevelop Core
3  * Copyright 2008-present Florian Rival ([email protected]). All rights
4  * reserved. This project is released under the MIT License.
5  */
6 #pragma once
7 
8 #include <memory>
9 #include <vector>
10 
11 #include "GDCore/String.h"
12 #include "GDCore/Tools/MakeUnique.h"
13 
14 namespace gd {
15 
19 class GD_CORE_API ProjectDiagnostic {
20  public:
21  enum ErrorType {
22  UndeclaredVariable,
23  MissingBehavior,
24  UnknownObject,
25  MismatchedObjectType,
26  };
27 
28  ProjectDiagnostic(ErrorType type_,
29  const gd::String &message_,
30  const gd::String &actualValue_,
31  const gd::String &expectedValue_,
32  const gd::String &objectName_ = "")
33  : type(type_),
34  message(message_),
35  actualValue(actualValue_),
36  expectedValue(expectedValue_),
37  objectName(objectName_) {};
38  virtual ~ProjectDiagnostic() {};
39 
40  ErrorType GetType() const { return type; };
41  const gd::String &GetMessage() const { return message; }
42  const gd::String &GetObjectName() const { return objectName; }
43  const gd::String &GetActualValue() const { return actualValue; }
44  const gd::String &GetExpectedValue() const { return expectedValue; }
45 
46  private:
47  ErrorType type;
48  gd::String message;
49  gd::String objectName;
50  gd::String actualValue;
51  gd::String expectedValue;
52 };
53 
57 class GD_CORE_API DiagnosticReport {
58  public:
59  DiagnosticReport() {};
60  virtual ~DiagnosticReport() {};
61 
62  void Add(const gd::ProjectDiagnostic &projectDiagnostic) {
63  projectDiagnostics.push_back(
64  gd::make_unique<gd::ProjectDiagnostic>(projectDiagnostic));
65  };
66 
67  const ProjectDiagnostic &Get(std::size_t index) const {
68  return *projectDiagnostics[index].get();
69  };
70 
71  std::size_t Count() const { return projectDiagnostics.size(); };
72 
73  const gd::String &GetSceneName() const { return sceneName; }
74 
75  void SetSceneName(const gd::String &sceneName_) { sceneName = sceneName_; }
76 
77  void LogAllDiagnostics() {
78  for (auto &diagnostic : projectDiagnostics) {
79  std::cout << diagnostic->GetMessage()
80  << "(object: " << diagnostic->GetObjectName()
81  << ", actual value: " << diagnostic->GetActualValue()
82  << ", expected value: " << diagnostic->GetExpectedValue() << ")"
83  << std::endl;
84  }
85  }
86 
87  private:
88  std::vector<std::unique_ptr<gd::ProjectDiagnostic>> projectDiagnostics;
89  gd::String sceneName;
90 };
91 
95 class GD_CORE_API WholeProjectDiagnosticReport {
96  public:
98  virtual ~WholeProjectDiagnosticReport() {};
99 
100  const DiagnosticReport &Get(std::size_t index) const {
101  return *diagnosticReports[index].get();
102  };
103 
104  void Clear() { diagnosticReports.clear(); };
105 
106  DiagnosticReport &AddNewDiagnosticReportForScene(
107  const gd::String &sceneName) {
108  auto diagnosticReport = gd::make_unique<gd::DiagnosticReport>();
109  diagnosticReport->SetSceneName(sceneName);
110  diagnosticReports.push_back(std::move(diagnosticReport));
111  return *diagnosticReports[diagnosticReports.size() - 1].get();
112  };
113 
114  std::size_t Count() const { return diagnosticReports.size(); };
115 
116  bool HasAnyIssue() {
117  for (auto &diagnosticReport : diagnosticReports) {
118  if (diagnosticReport->Count() > 0) {
119  return true;
120  }
121  }
122  return false;
123  }
124 
125  private:
126  std::vector<std::unique_ptr<gd::DiagnosticReport>> diagnosticReports;
127 };
128 
129 } // namespace gd
Definition: DiagnosticReport.h:57
Definition: DiagnosticReport.h:19
String represents an UTF8 encoded string.
Definition: String.h:33
Definition: DiagnosticReport.h:95
Definition: CommonTools.h:24