scc  2022.4.0
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_cfg/cci_param_typed.h>
20 #include <unordered_set>
21 
22 namespace scc {
23 template <typename T> struct _min_max_restriction {
24  _min_max_restriction(T min, T max)
25  : min(min)
26  , max(max) {}
27 
28  bool operator()(cci::cci_param_write_event<T> const& ev) const { return ev.new_value >= min && ev.new_value <= max; }
29  T const min;
30  T const max;
31 };
32 
33 template <typename T> struct _min_max_excl_restriction {
34  _min_max_excl_restriction(T min, T max)
35  : min(min)
36  , max(max) {}
37 
38  bool operator()(cci::cci_param_write_event<T> const& ev) const { return ev.new_value > min && ev.new_value < max; }
39  T const min;
40  T const max;
41 };
42 
43 template <typename T> struct _min_restriction {
44  _min_restriction(T min)
45  : min(min) {}
46 
47  bool operator()(cci::cci_param_write_event<T> const& ev) const { return ev.new_value >= min; }
48  T const min;
49 };
50 
51 template <typename T> struct _min_excl_restriction {
53  : min(min) {}
54 
55  bool operator()(cci::cci_param_write_event<T> const& ev) const { return ev.new_value > min; }
56  T const min;
57 };
58 
59 template <typename T> struct _max_restriction {
60  _max_restriction(T max)
61  : max(max) {}
62 
63  bool operator()(cci::cci_param_write_event<T> const& ev) const { return ev.new_value <= max; }
64  T const max;
65 };
66 
67 template <typename T> struct _max_excl_restriction {
69  : max(max) {}
70 
71  bool operator()(cci::cci_param_write_event<T> const& ev) const { return ev.new_value < max; }
72  T const max;
73 };
74 
75 template <typename T> struct _discrete_restriction {
76  template <typename COLLECTION_TYPE>
77  _discrete_restriction(COLLECTION_TYPE values)
78  : values(std::begin(values), std::end(values)) {}
79 
80  bool operator()(cci::cci_param_write_event<T> const& ev) const { return values.count(ev.new_value) > 0; }
81  std::unordered_set<T> const values;
82 };
91 template <typename T> inline _min_max_restriction<T> min_max_restriction(T min, T max) { return _min_max_restriction<T>(min, max); }
100 template <typename T> inline _min_max_excl_restriction<T> min_max_excl_restriction(T min, T max) {
101  return _min_max_excl_restriction<T>(min, max);
102 }
110 template <typename T> inline _min_restriction<T> min_restriction(T min) { return _min_restriction<T>(min); }
112 template <typename T> inline _min_restriction<T> gte_restriction(T min) { return _min_restriction<T>(min); }
120 template <typename T> inline _min_excl_restriction<T> min_excl_restriction(T min) { return _min_excl_restriction<T>(min); }
122 template <typename T> inline _min_excl_restriction<T> gt_excl_restriction(T min) { return _min_excl_restriction<T>(min); }
130 template <typename T> inline _max_restriction<T> max_restriction(T max) { return _max_restriction<T>(max); }
132 template <typename T> inline _max_restriction<T> lte_restriction(T max) { return _max_restriction<T>(max); }
140 template <typename T> inline _max_excl_restriction<T> max_excl_restriction(T max) { return _max_excl_restriction<T>(max); }
142 template <typename T> inline _max_excl_restriction<T> lt_excl_restriction(T max) { return _max_excl_restriction<T>(max); }
150 template <typename T> inline _discrete_restriction<T> discrete_restriction(std::initializer_list<T> values) {
151  return _discrete_restriction<T>(values);
152 }
161 template <typename T, size_t SZ> inline _discrete_restriction<T> discrete_restriction(std::array<T, SZ> values) {
162  return _discrete_restriction<T>(values);
163 }
171 template <typename T> inline _discrete_restriction<T> discrete_restriction(std::vector<T> values) {
172  return _discrete_restriction<T>(values);
173 }
180 template <typename T, cci::cci_param_mutable_type TM = cci::CCI_MUTABLE_PARAM> struct cci_param_restricted : public cci::cci_param<T, TM> {
181 
195  template <typename RESTR>
196  cci_param_restricted(const std::string& name, const T& default_value, RESTR const& restr, const std::string& desc = "",
197  cci::cci_name_type name_type = cci::CCI_RELATIVE_NAME,
198  const cci::cci_originator& originator = cci::cci_originator())
199  : cci::cci_param<T, TM>(name, default_value, desc, name_type, originator) {
200  this->template register_pre_write_callback(restr);
201  this->template reset();
202  }
203 
216  template <typename RESTR>
217  cci_param_restricted(const std::string& name, const T& default_value, RESTR const& restr, cci::cci_broker_handle private_broker,
218  const std::string& desc = "", cci::cci_name_type name_type = cci::CCI_RELATIVE_NAME,
219  const cci::cci_originator& originator = cci::cci_originator())
220  : cci::cci_param<T, TM>(name, default_value, desc, name_type, originator) {
221  this->template register_pre_write_callback(restr);
222  this->template reset();
223  }
225 };
226 } // namespace scc
227 #endif /* _SCC_CCI_PARAM_RESTRICTED_H_ */
SCC SystemC 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_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
_min_excl_restriction< T > gt_excl_restriction(T min)
alias for min_excl_restriction(T min)
_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_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
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, const std::string &desc="", cci::cci_name_type name_type=cci::CCI_RELATIVE_NAME, const cci::cci_originator &originator=cci::cci_originator())
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=cci::cci_originator())