HAL  v4.5.0-254-g8f9f6a423
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
data_container.cpp
Go to the documentation of this file.
2 
3 namespace hal
4 {
6  {
7  py::class_<DataContainer> py_data_container(m, "DataContainer", R"(
8  Container to hold data that is associated with an entity.
9  )");
10 
11  py_data_container.def(py::init<>(), R"(
12  Construct a new data container.
13  )");
14 
15  py_data_container.def("set_data", &DataContainer::set_data, py::arg("category"), py::arg("key"), py::arg("data_type"), py::arg("value"), py::arg("log_with_info_level") = false, R"(
16  Add a data entry.<br>
17  May overwrite an existing entry.
18 
19  :param str category: The data category.
20  :param str key: The data key.
21  :param str data_type: The data type.
22  :param str value: The data value.
23  :param bool log_with_info_level: Force explicit logging channel 'netlist' with log level 'info' to trace GUI events.
24  :returns: ``True`` on success, ``False`` otherwise.
25  :rtype: bool
26  )");
27 
28  py_data_container.def("has_data", &DataContainer::has_data, py::arg("category"), py::arg("key"), R"(
29  Determine whether an entry of given category and key exists.
30 
31  :param str category: The data category.
32  :param str key: The data key.
33  :returns: ``True`` if the entry exists, ``False`` otherwise.
34  :rtype: bool
35  )");
36 
37  py_data_container.def("get_data", &DataContainer::get_data, py::arg("category"), py::arg("key"), R"(
38  Get the data entry specified by the given category and key.
39 
40  :param str category: The data category.
41  :param str key: The data key.
42  :returns: The tuple ((1) type, (2) value).
43  :rtype: tuple(str,str)
44  )");
45 
46  py_data_container.def("delete_data", &DataContainer::delete_data, py::arg("category"), py::arg("key"), py::arg("log_with_info_level") = false, R"(
47  Delete a data entry.
48 
49  :param str category: The data category.
50  :param str key: The data key.
51  :param bool log_with_info_level: Force explicit logging channel 'netlist' with log level 'info' to trace GUI events.
52  :returns: ``True`` on success, ``False`` otherwise.
53  :rtype: bool
54  )");
55 
56  py_data_container.def_property_readonly("data", &DataContainer::get_data_map, R"(
57  A dict from ((1) category, (2) key) to ((1) type, (2) value) containing all stored data entries.
58 
59  :type: dict[tuple(str,str),tuple(str,str)]
60  )");
61 
62  py_data_container.def("get_data_map", &DataContainer::get_data_map, R"(
63  Get a dict from ((1) category, (2) key) to ((1) type, (2) value) containing all stored data entries.
64 
65  :returns: The stored data as a dict.
66  :rtype: dict[tuple(str,str),tuple(str,str)]
67  )");
68 
69  py_data_container.def("set_parameter", [](DataContainer& self, const Parameter& param, const std::string& value) -> bool {
70  auto res = self.set_parameter(param, value);
71  if (res.is_ok())
72  {
73  return true;
74  }
75  else
76  {
77  log_error("python_context", "{}", res.get_error().get());
78  return false;
79  }
80  }, py::arg("param"), py::arg("value"), R"(
81  Set (or overwrite) the value of a typed parameter as a string. The value is validated, normalized, and stored; any existing entry with the same name is replaced.
82 
83  :param hal_py.Parameter param: The parameter declaration.
84  :param str value: The value to store as a string.
85  :returns: ``True`` on success, ``False`` otherwise.
86  :rtype: bool
87  )");
88 
89  py_data_container.def("get_parameter_value", [](DataContainer& self, const std::string& name) -> std::optional<std::string> {
90  auto res = self.get_parameter_value(name);
91  if (res.is_ok())
92  {
93  return res.get();
94  }
95  else
96  {
97  log_error("python_context", "{}", res.get_error().get());
98  return std::nullopt;
99  }
100  }, py::arg("name"), R"(
101  Get the value of a typed parameter. Returns the explicitly-stored value or ``None`` if the parameter does not exist.
102 
103  :param str name: The parameter name.
104  :returns: The value string on success, ``None`` otherwise.
105  :rtype: str or None
106  )");
107 
108  py_data_container.def("get_parameter_value", [](DataContainer& self, const Parameter& param) -> std::optional<std::string> {
109  auto res = self.get_parameter_value(param);
110  if (res.is_ok())
111  {
112  return res.get();
113  }
114  else
115  {
116  log_error("python_context", "{}", res.get_error().get());
117  return std::nullopt;
118  }
119  }, py::arg("name"), R"(
120  Get the value of a typed parameter. Returns the explicitly-stored value or ``None`` if the parameter does not exist.
121 
122  :param hal_py.Parameter param: The parameter.
123  :returns: The value string on success, ``None`` otherwise.
124  :rtype: str or None
125  )");
126 
127  py_data_container.def("get_parameter_declaration", [](DataContainer& self, const std::string& name) -> std::optional<Parameter> {
128  auto res = self.get_parameter_declaration(name);
129  if (res.is_ok())
130  {
131  return res.get();
132  }
133  else
134  {
135  log_error("python_context", "{}", res.get_error().get());
136  return std::nullopt;
137  }
138  }, py::arg("name"), R"(
139  Get the full declaration of a typed parameter.
140 
141  :param str name: The parameter name.
142  :returns: The parameter declaration on success, ``None`` otherwise.
143  :rtype: hal_py.Parameter or None
144  )");
145 
146  py_data_container.def("has_parameter", py::overload_cast<const std::string&>(&DataContainer::has_parameter, py::const_), py::arg("name"), R"(
147  Check whether a parameter with the given name exists and has an explicitly stored value.
148 
149  :param str name: The parameter name.
150  :returns: ``True`` if the parameter is explicitly set, ``False`` otherwise.
151  :rtype: bool
152  )");
153 
154  py_data_container.def("has_parameter", py::overload_cast<const Parameter&>(&DataContainer::has_parameter, py::const_), py::arg("param"), R"(
155  Check whether a parameter exists and has an explicitly stored value.
156 
157  :param hal_py.Parameter param: The parameter.
158  :returns: ``True`` if the parameter is explicitly set, ``False`` otherwise.
159  :rtype: bool
160  )");
161 
162  py_data_container.def("delete_parameter", &DataContainer::delete_parameter, py::arg("name"), R"(
163  Delete an explicitly stored parameter value, if any.
164 
165  :param str name: The parameter name.
166  :returns: ``True`` if a value was deleted, ``False`` otherwise.
167  :rtype: bool
168  )");
169 
170  py_data_container.def_property_readonly("parameters", &DataContainer::get_parameters, R"(
171  All explicitly stored parameter values as a dict from ``name`` to `(declaration, value)`.
172 
173  :type: dict[str,tuple(hal_py.Parameter,str)]
174  )");
175 
176  py_data_container.def("get_parameters", &DataContainer::get_parameters, R"(
177  Get all explicitly stored parameter values as a dict from ``name`` to `(declaration, value)`.
178 
179  :returns: The parameter dict.
180  :rtype: dict[str,tuple(hal_py.Parameter,str)]
181  )");
182  }
183 } // namespace hal
const std::map< std::tuple< std::string, std::string >, std::tuple< std::string, std::string > > & get_data_map() const
std::tuple< std::string, std::string > get_data(const std::string &category, const std::string &key) const
bool has_parameter(const Parameter &param) const
const std::unordered_map< std::string, std::pair< Parameter, std::string > > & get_parameters() const
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)
bool delete_parameter(const std::string &name)
void data_container_init(py::module &m)
#define log_error(channel,...)
Definition: log.h:78
const Module * module(const Gate *g, const NodeBoxes &boxes)
Definition: defines.h:45
std::string name