3 /******************************************************************************
5 * Copyright (c) 2019 AT&T Intellectual Property.
6 * Copyright (c) 2018-2019 Nokia.
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
21 ******************************************************************************/
23 // Standard Includes: ANSI C/C++, MSA, and Third-Party Libraries
25 #include <type_traits>
29 // Local Includes: Application specific classes, functions, and libraries
33 using bound_t = int64_t;
35 enum class constraint_type : uint8_t
41 SEMICONSTRAINED_EXTENDED
44 template <bound_t LB, bound_t UB>
47 static_assert(UB >= LB, "UPPER >= LOWER");
48 static constexpr bound_t lower_bound = LB;
49 static constexpr bound_t upper_bound = UB;
59 template <bound_t VALUE>
60 struct one : span<VALUE, VALUE> {};
62 struct max : one<std::numeric_limits<bound_t>::max()> {};
63 struct min : one<std::numeric_limits<bound_t>::min()> {};
65 static constexpr bound_t MAX = std::numeric_limits<bound_t>::max();
66 static constexpr bound_t MIN = std::numeric_limits<bound_t>::min();
68 template <bool Extended, class... RANGE>
71 static constexpr bool extended = Extended;
72 static constexpr bound_t lower_bound = std::min({RANGE::lower_bound...});
73 static constexpr bound_t upper_bound = std::max({RANGE::upper_bound...});
75 static constexpr constraint_type type =
76 (Extended && lower_bound > min::lower_bound && upper_bound < max::upper_bound) ? constraint_type::CONSTRAINED_EXTENDED :
77 (!Extended && lower_bound > min::lower_bound && upper_bound < max::upper_bound) ? constraint_type::CONSTRAINED :
78 (Extended && lower_bound > min::lower_bound && upper_bound == max::upper_bound) ? constraint_type::SEMICONSTRAINED_EXTENDED :
79 (!Extended && lower_bound == min::lower_bound && upper_bound < max::upper_bound) ? constraint_type::SEMICONSTRAINED :
80 (Extended && lower_bound == min::lower_bound && upper_bound < max::upper_bound) ? constraint_type::SEMICONSTRAINED_EXTENDED :
81 (!Extended && lower_bound > min::lower_bound && upper_bound == max::upper_bound) ? constraint_type::SEMICONSTRAINED : constraint_type::UNCONSTRAINED;
83 static constexpr bool is_signed = lower_bound < 0;
85 static constexpr bound_t num_spans = static_cast<bound_t>(sizeof...(RANGE));
86 static constexpr pair<bound_t> bounds[] = {{RANGE::lower_bound, RANGE::upper_bound}...};
88 using boundary_type = bound_t;
90 static constexpr bool is_extended(bound_t val)
92 for (bound_t i = 0; i < num_spans; ++i)
94 auto const& p = bounds[i];
95 if (val <= p.upper_bound)
97 if(val < p.lower_bound)
106 template <bool Extended, class... RANGE>
107 constexpr pair<bound_t> constraints<Extended, RANGE...>::bounds[];
109 template <bool Extended>
110 struct constraints<Extended>
112 static constexpr bool extended = Extended;
113 static constexpr constraint_type type = constraint_type::UNCONSTRAINED;
114 static constexpr bound_t lower_bound = std::numeric_limits<bound_t>::min();
115 static constexpr bound_t upper_bound = std::numeric_limits<bound_t>::max();
117 static constexpr bool is_extended(bound_t val) {return true;}
120 /***************************************************************************************
121 * RANGE for sequences
122 ***************************************************************************************/
123 template<int TotalNumEntries, int NumExtEntries, bool Extended>
126 static_assert(Extended || TotalNumEntries > 0, "TotalNumEntries must be > 0");
127 static_assert(NumExtEntries <= TotalNumEntries, "NumExtEntries must be <= TotalNumEntries");
129 static constexpr constraint_type type = Extended ? constraint_type::CONSTRAINED_EXTENDED : constraint_type::CONSTRAINED;
130 static constexpr bool extended = Extended;
131 static constexpr bound_t lower_bound = 0;
132 static constexpr bound_t upper_bound = TotalNumEntries - NumExtEntries - 1;
133 static constexpr bound_t default_value = lower_bound;
134 static constexpr bound_t total_num_entries = TotalNumEntries;
136 using boundary_type = bound_t;
137 using value_type = uint32_t;