Empirical
errors.h
Go to the documentation of this file.
1 
39 #ifndef EMP_ERRORS_H
40 #define EMP_ERRORS_H
41 
42 #include <iostream>
43 #include <map>
44 #include <sstream>
45 #include <string>
46 
48 #ifdef EMSCRIPTEN
49 #include <emscripten.h>
50 #endif
51 
52 namespace emp {
53 
55  struct ExceptInfo {
56  std::string id;
57  std::string desc;
59  };
60 
62  static const ExceptInfo & GetEmptyExcept() {
63  static ExceptInfo fail_info{"","",false};
64  return fail_info;
65  }
66 
68  static std::multimap<std::string, ExceptInfo> & GetExceptMap() {
69  static std::multimap<std::string, ExceptInfo> except_map;
70  return except_map;
71  }
72 
74  inline void TriggerExcept(const std::string & in_id, const std::string & in_desc, bool in_error=true) {
75  GetExceptMap().emplace(in_id, ExceptInfo({in_id, in_desc, in_error}));
76  }
77 
79  inline const ExceptInfo & GetExcept(const std::string & id) {
80  auto & fail_map = GetExceptMap();
81  auto it = fail_map.find(id);
82  if (it != fail_map.end()) return it->second;
83  return GetEmptyExcept();
84  }
85 
87  inline ExceptInfo PopExcept(const std::string & id) {
88  auto & fail_map = GetExceptMap();
89  auto it = fail_map.find(id);
90  auto out = GetEmptyExcept();
91  if (it != fail_map.end()) { out = it->second; fail_map.erase(it); }
92  return out;
93  }
94 
96  inline size_t CountExcepts() { return GetExceptMap().size(); }
97 
99  inline bool HasExcept() { return CountExcepts(); }
100 
102  inline bool HasExcept(const std::string & id) { return GetExceptMap().count(id); }
103 
105  inline void ClearExcepts() { GetExceptMap().clear(); }
106 
108  inline void ClearExcept(const std::string & id) {
109  auto & fail_map = GetExceptMap();
110  auto it = fail_map.find(id);
111  if (it != fail_map.end()) fail_map.erase(it);
112  }
113 
114  namespace {
115  // Copy all of the args into the stringstream.
116  // Base case
117  void Notify_impl(std::stringstream &) { ; }
118 
119  // For each arg, copy it into the provided stringstream and recurse to do the rest.
120  template <typename T, typename... Ts>
121  void Notify_impl(std::stringstream & ss, T && arg1, Ts &&... args) {
122  ss << std::forward<T>(arg1);
123  Notify_impl(ss, std::forward<Ts>(args)...);
124  }
125  }
126 
128  template <typename... Ts>
129  void Notify(Ts &&... args) {
130  std::stringstream ss;
131  Notify_impl(ss, std::forward<Ts>(args)...);
132 #ifdef EMSCRIPTEN
133  EM_ASM_ARGS({ msg = Pointer_stringify($0); alert(msg); }, ss.str().c_str());
134 #else
135  std::cerr << ss.str() << std::endl;
136 #endif
137  }
138 
140  template <typename... Ts>
141  void NotifyWarning(Ts &&... msg) { Notify("WARNING: ", std::forward<Ts>(msg)...); }
142 
144  template <typename... Ts>
145  void NotifyError(Ts &&... msg) { Notify("ERROR: ", std::forward<Ts>(msg)...); }
146 
148  template <typename... Ts>
149  void LibraryWarning(Ts &&... msg) { Notify("EMPIRICAL USE WARNING: ", std::forward<Ts>(msg)...); }
150 
152  template <typename... Ts>
153  void LibraryError(Ts &&... msg) { Notify("EMPIRICAL USE ERROR: ", std::forward<Ts>(msg)...); }
154 
156  template <typename... Ts>
157  void InternalError(Ts &&... msg) { Notify("INTERNAL EMPIRICAL ERROR: ", std::forward<Ts>(msg)...); }
158 
159 }
160 
161 
162 #endif
const ExceptInfo & GetExcept(const std::string &id)
Get the first waiting exception.
Definition: errors.h:79
void ClearExcept(const std::string &id)
Remove all waiting exceptions of the desginated type.
Definition: errors.h:108
Information about an exception that has occured.
Definition: errors.h:55
bool HasExcept()
Are any exceptions waiting?
Definition: errors.h:99
size_t CountExcepts()
How many exceptions are waiting to be dealt with?
Definition: errors.h:96
void InternalError(Ts &&...msg)
Original library implementers must have made an error.
Definition: errors.h:157
void ClearExcepts()
Remove all waiting exceptions.
Definition: errors.h:105
void Notify(Ts &&...args)
Send information to a program user (via standard error in native mode, or alter in Emscripten) ...
Definition: errors.h:129
std::string desc
A detailed description of thie exception.
Definition: errors.h:57
void NotifyError(Ts &&...msg)
End user has done something resulting in an non-recoverable problem.
Definition: errors.h:145
static const ExceptInfo & GetEmptyExcept()
Function to generate an empty exception (returned when an exception is checked, but none exist...
Definition: errors.h:62
ExceptInfo PopExcept(const std::string &id)
Get and remove a waiting exception.
Definition: errors.h:87
static const PrintStr endl("<br>")
Pre-define emp::endl to insert a "<br>" and thus acting like a newline.
void TriggerExcept(const std::string &in_id, const std::string &in_desc, bool in_error=true)
Provide information about an exception that needs to be triggered.
Definition: errors.h:74
std::string id
A unique string ID for this exception type.
Definition: errors.h:56
If we are in emscripten, make sure to include the header.
Definition: array.h:37
void NotifyWarning(Ts &&...msg)
End user has done something possibly a problem.
Definition: errors.h:141
static std::multimap< std::string, ExceptInfo > & GetExceptMap()
A map of all exceptions that have occurred and are awaiting to be dealt with.
Definition: errors.h:68
void LibraryError(Ts &&...msg)
Library user has made an error in how they are using the library.
Definition: errors.h:153
bool default_to_error
Should we default to an error (or a warning) if not resolved?
Definition: errors.h:58
void LibraryWarning(Ts &&...msg)
Library user has made an error in how they are using the library.
Definition: errors.h:149