Mbed OS Reference
Loading...
Searching...
No Matches
Bounded.h
1/* mbed Microcontroller Library
2 * Copyright (c) 2006-2020 ARM Limited
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19#ifndef BLE_COMMON_BOUNDED_H_
20#define BLE_COMMON_BOUNDED_H_
21
22#include <cstdint>
23
24namespace ble {
25
26/**
27 * Restrict values of an integer type to a defined range.
28 *
29 * The range is a closed interval that includes its left-bound (Min) and
30 * right-bound value (Max).
31 *
32 * @tparam Rep The C++ integer type used to represent the values.
33 * @tparam Min Minimum value allowed.
34 * @tparam Max Maximum value allowed.
35 */
36template<typename Rep, Rep Min, Rep Max>
37struct Bounded {
38 /**
39 * Construct a bounded integer.
40 *
41 * If v is out of the range [Min : Max], then if it is less than Min, the
42 * value of the bounded integer will be Min. If it greater than Max, then
43 * the value of the bounded integer will be Max.
44 *
45 * @param v The value to store.
46 */
47 Bounded(Rep v) : _value(v)
48 {
49 if (v < Min) {
50 _value = Min;
51 } else if (v > Max) {
52 _value = Max;
53 }
54 }
55
56 /**
57 * Access the inner value.
58 *
59 * @return The current value.
60 */
61 Rep value() const
62 {
63 return _value;
64 }
65
66 /**
67 * The left-bound value.
68 *
69 * @return The lowest value that this type can represent
70 */
71 static Rep min()
72 {
73 return Min;
74 }
75
76 /**
77 * The right-bound value.
78 *
79 * @return The highest value that this type can represent
80 */
81 static Rep max()
82 {
83 return Max;
84 }
85
86 /**
87 * The left-bound value.
88 */
89 static const Rep MIN = Min;
90
91 /**
92 * The right-bound value.
93 */
94 static const Rep MAX = Max;
95
96private:
97 Rep _value;
98};
99
100/* ---------------------- Static variable initialization -------------------- */
101
102template<typename T, T Min, T Max>
104
105template<typename T, T Min, T Max>
107
108} // namespace ble
109
110#endif //BLE_COMMON_BOUNDED_H_
Entry namespace for all BLE API definitions.
Restrict values of an integer type to a defined range.
Definition: Bounded.h:37
static const Rep MAX
The right-bound value.
Definition: Bounded.h:94
static const Rep MIN
The left-bound value.
Definition: Bounded.h:89
static Rep min()
The left-bound value.
Definition: Bounded.h:71
Rep value() const
Access the inner value.
Definition: Bounded.h:61
static Rep max()
The right-bound value.
Definition: Bounded.h:81
Bounded(Rep v)
Construct a bounded integer.
Definition: Bounded.h:47