HAL
data_container.cpp
Go to the documentation of this file.
2 
4 
5 namespace hal
6 {
7  bool DataContainer::operator==(const DataContainer& other) const
8  {
9  return m_data == other.get_data_map();
10  }
11 
12  bool DataContainer::operator!=(const DataContainer& other) const
13  {
14  return !operator==(other);
15  }
16 
17  bool DataContainer::set_data(const std::string& category, const std::string& key, const std::string& value_data_type, const std::string& value, const bool log_with_info_level)
18  {
19  if (category.empty() || key.empty())
20  {
21  log_error("netlist", "key category or key is empty.");
22  return false;
23  }
24 
25  m_data[std::make_tuple(category, key)] = std::make_tuple(value_data_type, value);
26 
27  //notify_updated();
28 
29  if (log_with_info_level)
30  {
31  log_info("netlist", "added {} data '{}' ({}, {}).", value_data_type, value, category, key);
32  }
33  else
34  {
35  log_debug("netlist", "added {} data '{}' ({}, {}).", value_data_type, value, category, key);
36  }
37 
38  return true;
39  }
40 
41  bool DataContainer::delete_data(const std::string& category, const std::string& key, const bool log_with_info_level)
42  {
43  if (category.empty() || key.empty())
44  {
45  log_error("netlist", "key category or key is empty.");
46  return false;
47  }
48 
49  auto it = m_data.find(std::make_tuple(category, key));
50  if (it == m_data.end())
51  {
52  log_debug("netlist", "no key ('{}', '{}') found.", category, key);
53  return true;
54  }
55 
56  auto deleted_value = std::get<1>(it->second);
57  m_data.erase(it);
58 
59  //notify_updated();
60 
61  if (log_with_info_level)
62  {
63  log_info("netlist", "removed data '{}' ({}, {}).", deleted_value, category, key);
64  }
65  else
66  {
67  log_debug("netlist", "removed data '{}' ({}, {}).", deleted_value, category, key);
68  }
69  return true;
70  }
71 
72  const std::map<std::tuple<std::string, std::string>, std::tuple<std::string, std::string>>& DataContainer::get_data_map() const
73  {
74  return m_data;
75  }
76 
77  void DataContainer::set_data_map(const std::map<std::tuple<std::string, std::string>, std::tuple<std::string, std::string>>& map)
78  {
79  m_data = map;
80  }
81 
82  bool DataContainer::has_data(const std::string& category, const std::string& key) const
83  {
84  if (category.empty() || key.empty())
85  {
86  return false;
87  }
88 
89  if (auto it = m_data.find(std::make_tuple(category, key)); it == m_data.end())
90  {
91  return false;
92  }
93 
94  return true;
95  }
96 
97  std::tuple<std::string, std::string> DataContainer::get_data(const std::string& category, const std::string& key) const
98  {
99  if (category.empty() || key.empty())
100  {
101  log_error("netlist", "key category or key is empty.");
102  return std::make_tuple("", "");
103  }
104 
105  auto it = m_data.find(std::make_tuple(category, key));
106  if (it == m_data.end())
107  {
108  log_debug("netlist", "no value stored for key ('{}', '{}').", category, key);
109  return std::make_tuple("", "");
110  }
111  return it->second;
112  }
113 
114 } // namespace hal
const std::map< std::tuple< std::string, std::string >, std::tuple< std::string, std::string > > & get_data_map() const
bool operator==(const DataContainer &other) const
std::tuple< std::string, std::string > get_data(const std::string &category, const std::string &key) const
bool operator!=(const DataContainer &other) const
std::map< std::tuple< std::string, std::string >, std::tuple< std::string, std::string > > m_data
bool set_data(const std::string &category, const std::string &key, const std::string &data_type, const std::string &value, const bool log_with_info_level=false)
bool has_data(const std::string &category, const std::string &key) const
bool delete_data(const std::string &category, const std::string &key, const bool log_with_info_level=false)
void set_data_map(const std::map< std::tuple< std::string, std::string >, std::tuple< std::string, std::string >> &map)
#define log_error(channel,...)
Definition: log.h:78
#define log_debug(channel,...)
Definition: log.h:74
#define log_info(channel,...)
Definition: log.h:70