Mistake on this page?
Report an issue in GitHub or email us
Bounded.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2018 ARM Limited
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 
17 #ifndef BLE_COMMON_BOUNDED_H_
18 #define BLE_COMMON_BOUNDED_H_
19 
20 #include <stdint.h>
21 
22 namespace ble {
23 
24 /**
25  * Restrict values of an integer type to a defined range.
26  *
27  * The range is a closed interval that includes its left-bound (Min) and
28  * right-bound value (Max).
29  *
30  * @tparam Rep The C++ integer type used to represent the values.
31  * @tparam Min Minimum value allowed.
32  * @tparam Max Maximum value allowed.
33  */
34 template<typename Rep, Rep Min, Rep Max>
35 struct Bounded {
36  /**
37  * Construct a bounded integer.
38  *
39  * If v is out of the range [Min : Max], then if it is less than Min, the
40  * value of the bounded integer will be Min. If it greater than Max, then
41  * the value of the bounded integer will be Max.
42  *
43  * @param v The value to store.
44  */
45  Bounded(Rep v) : _value(v)
46  {
47  if (v < Min) {
48  _value = v;
49  } else if (v > Max) {
50  _value = v;
51  }
52  }
53 
54  /**
55  * Access the inner value.
56  *
57  * @return The current value.
58  */
59  Rep value() const
60  {
61  return _value;
62  }
63 
64  /**
65  * The left-bound value.
66  *
67  * @return The lowest value that this type can represent
68  */
69  static Rep min()
70  {
71  return Min;
72  }
73 
74  /**
75  * The right-bound value.
76  *
77  * @return The highest value that this type can represent
78  */
79  static Rep max()
80  {
81  return Max;
82  }
83 
84  /**
85  * The left-bound value.
86  */
87  static const Rep MIN = Min;
88 
89  /**
90  * The right-bound value.
91  */
92  static const Rep MAX = Max;
93 
94 private:
95  Rep _value;
96 };
97 
98 /* ---------------------- Static variable initialization -------------------- */
99 
100 template<typename T, T Min, T Max>
102 
103 template<typename T, T Min, T Max>
105 
106 } // namespace ble
107 
108 #endif //BLE_COMMON_BOUNDED_H_
static const Rep MIN
The left-bound value.
Definition: Bounded.h:87
Bounded(Rep v)
Construct a bounded integer.
Definition: Bounded.h:45
static Rep max()
The right-bound value.
Definition: Bounded.h:79
Restrict values of an integer type to a defined range.
Definition: Bounded.h:35
Rep value() const
Access the inner value.
Definition: Bounded.h:59
static const Rep MAX
The right-bound value.
Definition: Bounded.h:92
static Rep min()
The left-bound value.
Definition: Bounded.h:69
Entry namespace for all BLE API definitions.
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.