Empirical
alert.h
Go to the documentation of this file.
1 
11 #ifndef EMP_ALERT_H
12 #define EMP_ALERT_H
13 
14 #include "string_utils.h"
15 
16 // If we are in emscripten, make sure to include the header.
17 #ifdef EMSCRIPTEN
18 #include <emscripten.h>
19 #endif
20 
21 
22 namespace emp {
23 #ifdef EMSCRIPTEN
24  void Alert(const std::string & msg) { EM_ASM_ARGS({ msg = Pointer_stringify($0); alert(msg); }, msg.c_str()); }
25 #else
26  void Alert(const std::string & msg) { std::cerr << msg << std::endl; }
30 #endif
31  template <typename... TYPE_SET>
33  void Alert(TYPE_SET... inputs) { Alert(emp::to_string(inputs...)); }
35 
37  template <typename... TYPE_SET>
38  static void CappedAlert(size_t cap, TYPE_SET... inputs) {
39  static size_t cur_count = 0;
40  if (cur_count++ < cap) Alert(emp::to_string(inputs...));
41  }
42 
45  struct AlertObj {
46  std::string msg;
47  bool on_construct;
48  bool on_destruct;
49 
50  AlertObj(const std::string & _msg, bool _on_c=true, bool _on_d=false)
51  : msg(_msg), on_construct(_on_c), on_destruct(_on_d) { if (on_construct) emp::Alert(msg); }
52  ~AlertObj() { if (on_destruct) emp::Alert(msg); }
53 
54  void Trigger() { emp::Alert(msg); }
55  void SetMessage(const std::string & _msg) { msg = _msg; }
56  };
57 }
58 
59 
60 #endif
void Trigger()
Definition: alert.h:54
std::string to_string(ALL_TYPES &&...all_values)
Definition: string_utils.h:511
bool on_destruct
Should the message print automatically during destruction?
Definition: alert.h:48
static void CappedAlert(size_t cap, TYPE_SET...inputs)
A version of Alert that will cap how many times it can go off.
Definition: alert.h:38
Simple functions to manipulate strings.
void SetMessage(const std::string &_msg)
Definition: alert.h:55
static const PrintStr endl("<br>")
Pre-define emp::endl to insert a "<br>" and thus acting like a newline.
std::string msg
Message for the alert to print.
Definition: alert.h:46
void Alert(const std::string &msg)
Definition: alert.h:29
If we are in emscripten, make sure to include the header.
Definition: array.h:37
~AlertObj()
Definition: alert.h:52
AlertObj(const std::string &_msg, bool _on_c=true, bool _on_d=false)
Definition: alert.h:50
bool on_construct
Should the message print automatically during construction?
Definition: alert.h:47
Definition: alert.h:45