Empirical
Attributes.h
Go to the documentation of this file.
1 
10 #ifndef EMP_WEB_ATTRIBUTES_H
11 #define EMP_WEB_ATTRIBUTES_H
12 
13 
14 #ifdef EMSCRIPTEN
15 #include <emscripten.h>
16 #endif
17 
18 #include "../tools/string_utils.h"
19 
20 #include <map>
21 #include <string>
22 
23 namespace emp {
24 namespace web {
25 
28 
29  class Attributes {
30  private:
31  std::map<std::string, std::string> settings;
32 
33  public:
34  Attributes() { ; }
35  Attributes(const Attributes &) = default;
36  Attributes & operator=(const Attributes &) = default;
37 
39  int GetSize() const { return (int) settings.size(); }
40 
41  Attributes & DoSet(const std::string & in_set, const std::string & in_val) {
42  settings[in_set] = in_val;
43  return *this;
44  }
45 
47  template <typename SET_TYPE>
48  Attributes & Set(const std::string & s, SET_TYPE v) {
49  return DoSet(s, emp::to_string(v));
50  }
51 
53  Attributes & Insert(const Attributes & in_attr) {
54  settings.insert(in_attr.settings.begin(), in_attr.settings.end());
55  return *this;
56  }
57 
59  bool Has(const std::string & setting) const {
60  return settings.find(setting) != settings.end();
61  }
62 
65  const std::string & Get(const std::string & setting) {
66  // Note: if setting did not exist, this does create an empty entry.
67  return settings[setting];
68  }
69 
70  const std::map<std::string, std::string> & GetMap() const {
71  return settings;
72  }
73 
74  void Remove(const std::string & setting) {
75  settings.erase(setting);
76  }
77 
79  void Clear() { settings.clear(); }
80 
82  void Apply(const std::string & widget_id) {
83  // Stop immediately if nothing to set.
84  if (settings.size() == 0) return;
85 
86  // Find the current object only once.
87 #ifdef EMSCRIPTEN
88  EM_ASM_ARGS({
89  var id = Pointer_stringify($0);
90  emp_i.cur_obj = $( '#' + id );
91  }, widget_id.c_str());
92 #endif
93 
94  for (auto attr_pair : settings) {
95  if (attr_pair.second == "") continue; // Ignore empty entries.
96 #ifdef EMSCRIPTEN
97  EM_ASM_ARGS({
98  var name = Pointer_stringify($0);
99  var value = Pointer_stringify($1);
100  emp_i.cur_obj.attr( name, value);
101  }, attr_pair.first.c_str(), attr_pair.second.c_str());
102 #else
103  std::cout << "Setting '" << widget_id << "' attribute '" << attr_pair.first
104  << "' to '" << attr_pair.second << "'.";
105 #endif
106  }
107  }
108 
110  void Apply(const std::string & widget_id, const std::string & setting) {
111  emp_assert(Has(setting));
112 
113 #ifdef EMSCRIPTEN
114  EM_ASM_ARGS({
115  var id = Pointer_stringify($0);
116  var setting = Pointer_stringify($1);
117  var value = Pointer_stringify($2);
118  $( '#' + id ).attr( setting, value);
119  }, widget_id.c_str(), setting.c_str(), settings[setting].c_str());
120 #else
121  std::cout << "Setting '" << widget_id << "' attribute '" << setting
122  << "' to '" << settings[setting] << "'.";
123 #endif
124  }
125 
127  static void Apply(const std::string & widget_id, const std::string & setting,
128  const std::string & value) {
129 #ifdef EMSCRIPTEN
130  EM_ASM_ARGS({
131  var id = Pointer_stringify($0);
132  var setting = Pointer_stringify($1);
133  var value = Pointer_stringify($2);
134  $( '#' + id ).attr( setting, value);
135  }, widget_id.c_str(), setting.c_str(), value.c_str());
136 #else
137  std::cout << "Setting '" << widget_id << "' attribute '" << setting
138  << "' to '" << value << "'.";
139 #endif
140  }
141 
143  operator bool() const { return (bool) settings.size(); }
144  };
145 
146 
147 }
148 }
149 
150 
151 #endif
void Clear()
Remove all setting values.
Definition: Attributes.h:79
Attributes & Set(const std::string &s, SET_TYPE v)
Record that attribute "a" is set to value "v" (converted to string) and return this object...
Definition: Attributes.h:48
Attributes()
Definition: Attributes.h:34
int GetSize() const
Return a count of the number of attributes that have been set.
Definition: Attributes.h:39
const std::map< std::string, std::string > & GetMap() const
Definition: Attributes.h:70
std::string to_string(ALL_TYPES &&...all_values)
Definition: string_utils.h:511
const std::string & Get(const std::string &setting)
Definition: Attributes.h:65
Attributes & operator=(const Attributes &)=default
Maintains a map of attribute names to values for use in JavaScript Closely related to Style...
Definition: Attributes.h:29
bool Has(const std::string &setting) const
Return true/false based on whether "setting" has been given a value in this Attributes obj...
Definition: Attributes.h:59
static void Apply(const std::string &widget_id, const std::string &setting, const std::string &value)
Apply onlay a SPECIFIC attributes setting with a specifid value!
Definition: Attributes.h:127
void Apply(const std::string &widget_id)
Apply ALL of the Attribute&#39;s settings to dom element "widget_id".
Definition: Attributes.h:82
Attributes & Insert(const Attributes &in_attr)
Set all values from in_attr here as well. Return this object.
Definition: Attributes.h:53
Attributes & DoSet(const std::string &in_set, const std::string &in_val)
Definition: Attributes.h:41
If we are in emscripten, make sure to include the header.
Definition: array.h:37
void Remove(const std::string &setting)
Definition: Attributes.h:74
#define emp_assert(...)
Definition: assert.h:199
void Apply(const std::string &widget_id, const std::string &setting)
Apply onlay a SPECIFIC attributes setting from the setting library to widget_id.
Definition: Attributes.h:110