scc  2024.06
SystemC components library
cci_param_restricted.h
1 /*******************************************************************************
2  * Copyright 2024 MINRES Technologies GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *******************************************************************************/
16 #ifndef _SCC_CCI_PARAM_RESTRICTED_H_
17 #define _SCC_CCI_PARAM_RESTRICTED_H_
18 
19 #include <cci_configuration>
20 #include <unordered_set>
21 
22 namespace scc {
24 template <typename T> struct _min_max_restriction {
25  _min_max_restriction(T min, T max)
26  : min(min)
27  , max(max) {}
28 
29  bool operator()(cci::cci_param_write_event<T> const& ev) const { return ev.new_value >= min && ev.new_value <= max; }
30  T const min;
31  T const max;
32 };
33 
34 template <typename T> struct _min_max_excl_restriction {
35  _min_max_excl_restriction(T min, T max)
36  : min(min)
37  , max(max) {}
38 
39  bool operator()(cci::cci_param_write_event<T> const& ev) const { return ev.new_value > min && ev.new_value < max; }
40  T const min;
41  T const max;
42 };
43 
44 template <typename T> struct _min_restriction {
45  _min_restriction(T min)
46  : min(min) {}
47 
48  bool operator()(cci::cci_param_write_event<T> const& ev) const { return ev.new_value >= min; }
49  T const min;
50 };
51 
52 template <typename T> struct _min_excl_restriction {
53  _min_excl_restriction(T min)
54  : min(min) {}
55 
56  bool operator()(cci::cci_param_write_event<T> const& ev) const { return ev.new_value > min; }
57  T const min;
58 };
59 
60 template <typename T> struct _max_restriction {
61  _max_restriction(T max)
62  : max(max) {}
63 
64  bool operator()(cci::cci_param_write_event<T> const& ev) const { return ev.new_value <= max; }
65  T const max;
66 };
67 
68 template <typename T> struct _max_excl_restriction {
69  _max_excl_restriction(T max)
70  : max(max) {}
71 
72  bool operator()(cci::cci_param_write_event<T> const& ev) const { return ev.new_value < max; }
73  T const max;
74 };
75 
76 template <typename T> struct _discrete_restriction {
77  template <typename COLLECTION_TYPE>
78  _discrete_restriction(COLLECTION_TYPE values)
79  : values(std::begin(values), std::end(values)) {}
80 
81  bool operator()(cci::cci_param_write_event<T> const& ev) const { return values.count(ev.new_value) > 0; }
82  std::unordered_set<T> const values;
83 };
85 
93 template <typename T> inline _min_max_restriction<T> min_max_restriction(T min, T max) { return _min_max_restriction<T>(min, max); }
95 template <typename T> inline _min_max_restriction<T> gte_lte_restriction(T min, T max) { return _min_max_restriction<T>(min, max); }
104 template <typename T> inline _min_max_excl_restriction<T> min_max_excl_restriction(T min, T max) {
105  return _min_max_excl_restriction<T>(min, max);
106 }
108 template <typename T> inline _min_max_excl_restriction<T> gt_lt_restriction(T min, T max) { return _min_max_excl_restriction<T>(min, max); }
116 template <typename T> inline _min_restriction<T> min_restriction(T min) { return _min_restriction<T>(min); }
118 template <typename T> inline _min_restriction<T> gte_restriction(T min) { return _min_restriction<T>(min); }
126 template <typename T> inline _min_excl_restriction<T> min_excl_restriction(T min) { return _min_excl_restriction<T>(min); }
128 template <typename T> inline _min_excl_restriction<T> gt_restriction(T min) { return _min_excl_restriction<T>(min); }
136 template <typename T> inline _max_restriction<T> max_restriction(T max) { return _max_restriction<T>(max); }
138 template <typename T> inline _max_restriction<T> lte_restriction(T max) { return _max_restriction<T>(max); }
146 template <typename T> inline _max_excl_restriction<T> max_excl_restriction(T max) { return _max_excl_restriction<T>(max); }
148 template <typename T> inline _max_excl_restriction<T> lt_excl_restriction(T max) { return _max_excl_restriction<T>(max); }
156 template <typename T> inline _discrete_restriction<T> discrete_restriction(std::initializer_list<T> values) {
157  return _discrete_restriction<T>(values);
158 }
167 template <typename T, size_t SZ> inline _discrete_restriction<T> discrete_restriction(std::array<T, SZ> values) {
168  return _discrete_restriction<T>(values);
169 }
177 template <typename T> inline _discrete_restriction<T> discrete_restriction(std::vector<T> values) {
178  return _discrete_restriction<T>(values);
179 }
196 template <typename T, cci::cci_param_mutable_type TM = cci::CCI_MUTABLE_PARAM> struct cci_param_restricted : public cci::cci_param<T, TM> {
197 
211  template <typename RESTR>
212  cci_param_restricted(const std::string& name, const T& default_value, RESTR const& restr, const std::string& desc = "",
213  cci::cci_name_type name_type = cci::CCI_RELATIVE_NAME,
214  const cci::cci_originator& originator = sc_core::sc_get_current_object() ? cci::cci_originator()
215  : cci::cci_originator("sc_main"))
216  : cci::cci_param<T, TM>(name, default_value, desc, name_type, originator) {
217  this->register_pre_write_callback(restr);
218  this->reset();
219  }
220 
233  template <typename RESTR>
234  cci_param_restricted(const std::string& name, const T& default_value, RESTR const& restr, cci::cci_broker_handle private_broker,
235  const std::string& desc = "", cci::cci_name_type name_type = cci::CCI_RELATIVE_NAME,
236  const cci::cci_originator& originator = sc_core::sc_get_current_object() ? cci::cci_originator()
237  : cci::cci_originator("sc_main"))
238  : cci::cci_param<T, TM>(name, default_value, desc, name_type, originator) {
239  this->register_pre_write_callback(restr);
240  this->reset();
241  }
243 };
244 } // namespace scc
245 #endif /* _SCC_CCI_PARAM_RESTRICTED_H_ */
SCC TLM utilities.
_max_excl_restriction< T > max_excl_restriction(T max)
creates a maximum restriction excluding the maximum value
_max_restriction< T > max_restriction(T max)
creates a maximum restriction including the maximum value
_min_excl_restriction< T > gt_restriction(T min)
alias for min_excl_restriction(T min)
_min_restriction< T > gte_restriction(T min)
alias for min_restriction(T min)
_min_max_restriction< T > min_max_restriction(T min, T max)
creates a min/max restriction with including the limits
_discrete_restriction< T > discrete_restriction(std::initializer_list< T > values)
creates a restriction for a discrete values set
_min_restriction< T > min_restriction(T min)
creates a minimum restriction including the minimum value
_min_max_restriction< T > gte_lte_restriction(T min, T max)
alias for min_max_restriction(T min, T max)
_min_excl_restriction< T > min_excl_restriction(T min)
creates a minimum restriction excluding the minimum value
_max_excl_restriction< T > lt_excl_restriction(T max)
alias for max_excl_restriction(T max)
_max_restriction< T > lte_restriction(T max)
alias for max_restriction(T max)
_min_max_excl_restriction< T > min_max_excl_restriction(T min, T max)
creates a min/max restriction with excluding the limits
_min_max_excl_restriction< T > gt_lt_restriction(T min, T max)
alias for min_max_excl_restriction(T min, T max)
extension of cci_param<T, TM> which automatically registeres a callback to restrict the valid values ...
cci_param_restricted(const std::string &name, const T &default_value, RESTR const &restr, cci::cci_broker_handle private_broker, const std::string &desc="", cci::cci_name_type name_type=cci::CCI_RELATIVE_NAME, const cci::cci_originator &originator=sc_core::sc_get_current_object() ? cci::cci_originator() :cci::cci_originator("sc_main"))
cci_param_restricted(const std::string &name, const T &default_value, RESTR const &restr, const std::string &desc="", cci::cci_name_type name_type=cci::CCI_RELATIVE_NAME, const cci::cci_originator &originator=sc_core::sc_get_current_object() ? cci::cci_originator() :cci::cci_originator("sc_main"))