chigraph  master
Systems programming language written for beginners in LLVM
Result.hpp
Go to the documentation of this file.
1 
4 #ifndef CHI_RESULT_HPP
5 #define CHI_RESULT_HPP
6 
7 #pragma once
8 
9 #include "chi/Support/json.hpp"
10 
11 #include <boost/container/flat_map.hpp>
12 
13 namespace chi {
72 struct Result {
74  struct ScopedContext {
78  ScopedContext(Result& res, int ctxId) : result{res}, contextId{ctxId} {}
79 
82 
85 
87  const int contextId;
88  };
89 
91  Result() : result_json(nlohmann::json::array()) {}
100  void addEntry(const char* ec, const char* overview, nlohmann::json data);
101 
107  int addContext(const nlohmann::json& data);
108 
123  ScopedContext addScopedContext(const nlohmann::json& data) {
124  return ScopedContext{*this, addContext(data)};
125  }
126 
129  void removeContext(int id);
130 
133  nlohmann::json contextJson() const;
134 
137  explicit operator bool() const { return success(); }
138 
141  bool success() const { return mSuccess; }
142 
145  bool operator!() const { return !success(); }
146 
149  std::string dump() const;
150 
152  nlohmann::json result_json;
153 
155  bool mSuccess = true;
156 
157 private:
158  boost::container::flat_map<int, nlohmann::json> mContexts;
159 };
160 
162 
165 
172 Result operator+(const Result& lhs, const Result& rhs);
173 
180 Result& operator+=(Result& lhs, const Result& rhs);
181 
187 inline std::ostream& operator<<(std::ostream& lhs, const Result& rhs) {
188  lhs << rhs.dump();
189 
190  return lhs;
191 }
192 
194 
195 } // namespace chi
196 
197 #endif // CHI_RESULT_HPP
std::string dump() const
Dump to a pretty-printed error message.
Definition: Result.cpp:65
void addEntry(const char *ec, const char *overview, nlohmann::json data)
Add a entry to the result, either a warning or an error.
Definition: Result.cpp:52
~ScopedContext()
Destructor–does the magic of remving itsself from the result.
Definition: Result.hpp:81
nlohmann::json contextJson() const
Get the JSON associated with the context that&#39;s been added.
Definition: Result.cpp:95
bool mSuccess
If it&#39;s successful.
Definition: Result.hpp:155
nlohmann::json result_json
The result JSON.
Definition: Result.hpp:152
const int contextId
The ID for this context.
Definition: Result.hpp:87
Result & result
The result object.
Definition: Result.hpp:84
ScopedContext(Result &res, int ctxId)
Create a scoped context object.
Definition: Result.hpp:78
ScopedContext addScopedContext(const nlohmann::json &data)
Add a context with a scope Example usage: chi::Result res; res.contextJson(); // returns {} { aut...
Definition: Result.hpp:123
Result operator+(const Result &lhs, const Result &rhs)
Append two Result objects.
Definition: Result.cpp:106
int addContext(const nlohmann::json &data)
Add some json that will ALWAYS be merged with entries that are added and when results are added to th...
Definition: Result.cpp:84
bool success() const
Success test.
Definition: Result.hpp:141
std::ostream & operator<<(std::ostream &lhs, const Result &rhs)
Stream operator.
Definition: Result.hpp:187
A helper object for contexts that should be removed at the end of a scope.
Definition: Result.hpp:74
Result & operator+=(Result &lhs, const Result &rhs)
Append one result to an existing one.
Definition: Result.cpp:127
The namespace where chigraph lives.
bool operator!() const
!Success test
Definition: Result.hpp:145
Result()
Default constructor; defaults to success.
Definition: Result.hpp:91
The result object, used for identifiying errors with good diagnostics.
Definition: Result.hpp:72
void removeContext(int id)
Removes a previously added context.
Definition: Result.cpp:93