lxgui
gui_registry.cpp
1 #include "lxgui/gui_registry.hpp"
2 
3 #include "lxgui/gui_out.hpp"
4 #include "lxgui/gui_region.hpp"
5 #include "lxgui/utils_string.hpp"
6 
7 namespace lxgui::gui {
8 
9 bool registry::check_region_name(std::string_view name) const {
10  if (utils::has_no_content(name)) {
11  gui::out << gui::error << "gui::registry: "
12  << "Cannot create a region with a blank name." << std::endl;
13  return false;
14  }
15 
16  if (utils::is_number(name[0])) {
17  gui::out << gui::error << "gui::registry: "
18  << "A region's name cannot start by a number: \"" << name << "\" is forbidden."
19  << std::endl;
20  return false;
21  }
22 
23  std::size_t pos = name.find("$");
24  if (pos != name.npos && pos != 0) {
25  gui::out << gui::error << "gui::registry: "
26  << "A region's name cannot contain the character '$' except at the begining: \""
27  << name << "\" is forbidden." << std::endl;
28  return false;
29  }
30 
31  for (auto c : name) {
32  if ((std::isalnum(c) == 0) && c != '_' && c != '$') {
33  gui::out << gui::error << "gui::registry: "
34  << "A region's name can only contain alphanumeric symbols, or underscores: \""
35  << name << "\" is forbidden." << std::endl;
36  return false;
37  }
38  }
39 
40  return true;
41 }
42 
43 bool registry::add_region(utils::observer_ptr<region> obj) {
44  if (!obj) {
45  gui::out << gui::error << "gui::registry: Adding a null region." << std::endl;
46  return false;
47  }
48 
49  auto iter_named_obj = named_object_list_.find(obj->get_name());
50  if (iter_named_obj != named_object_list_.end()) {
51  gui::out << gui::warning << "gui::registry: "
52  << "A region with the name \"" << obj->get_name() << "\" already exists."
53  << std::endl;
54  return false;
55  }
56 
57  named_object_list_[obj->get_name()] = std::move(obj);
58 
59  return true;
60 }
61 
62 void registry::remove_region(const region& obj) {
63  named_object_list_.erase(obj.get_name());
64 }
65 
66 utils::observer_ptr<const region> registry::get_region_by_name(std::string_view name) const {
67  auto iter = named_object_list_.find(std::string{name});
68  if (iter != named_object_list_.end())
69  return iter->second;
70  else
71  return nullptr;
72 }
73 
74 } // namespace lxgui::gui
The base class of all elements in the GUI.
Definition: gui_region.hpp:161
const std::string & get_name() const
Returns this region's name.
Definition: gui_region.cpp:140
void remove_region(const region &obj)
Removes a region from this registry.
bool add_region(utils::observer_ptr< region > obj)
Adds a region to be handled by this registry.
bool check_region_name(std::string_view name) const
Checks the provided string is suitable for naming a region.
Definition: gui_registry.cpp:9
utils::observer_ptr< const region > get_region_by_name(std::string_view name) const
Returns the region associated with the given name.
std::ostream out
const std::string warning
Definition: gui_out.cpp:6
const std::string error
Definition: gui_out.cpp:7