Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Bounded.h Source File

Bounded.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2018 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef BLE_COMMON_BOUNDED_H_
00018 #define BLE_COMMON_BOUNDED_H_
00019 
00020 #include <stdint.h>
00021 
00022 namespace ble {
00023 
00024 /**
00025  * Restrict values of an integer type to a defined range.
00026  *
00027  * The range is a closed interval that includes its left-bound (Min) and
00028  * right-bound value (Max).
00029  *
00030  * @tparam Rep The C++ integer type used to represent the values.
00031  * @tparam Min Minimum value allowed.
00032  * @tparam Max Maximum value allowed.
00033  */
00034 template<typename Rep, Rep Min, Rep Max>
00035 struct Bounded {
00036     /**
00037      * Construct a bounded integer.
00038      *
00039      * If v is out of the range [Min : Max], then if it is less than Min, the
00040      * value of the bounded integer will be Min. If it greater than Max, then
00041      * the value of the bounded integer will be Max.
00042      *
00043      * @param v The value to store.
00044      */
00045     Bounded(Rep v) : _value(v)
00046     {
00047         if (v < Min) {
00048             _value = v;
00049         } else if (v > Max) {
00050             _value = v;
00051         }
00052     }
00053 
00054     /**
00055      * Access the inner value.
00056      *
00057      * @return The current value.
00058      */
00059     Rep value() const
00060     {
00061         return _value;
00062     }
00063 
00064     /**
00065      * The left-bound value.
00066      *
00067      * @return The lowest value that this type can represent
00068      */
00069     static Rep min()
00070     {
00071         return Min;
00072     }
00073 
00074     /**
00075      * The right-bound value.
00076      *
00077      * @return The highest value that this type can represent
00078      */
00079     static Rep max()
00080     {
00081         return Max;
00082     }
00083 
00084     /**
00085      * The left-bound value.
00086      */
00087     static const Rep MIN = Min;
00088 
00089     /**
00090      * The right-bound value.
00091      */
00092     static const Rep MAX = Max;
00093 
00094 private:
00095     Rep _value;
00096 };
00097 
00098 /* ---------------------- Static variable initialization -------------------- */
00099 
00100 template<typename T, T Min, T Max>
00101 const T Bounded<T, Min, Max>::MIN;
00102 
00103 template<typename T, T Min, T Max>
00104 const T Bounded<T, Min, Max>::MAX;
00105 
00106 } // namespace ble
00107 
00108 #endif //BLE_COMMON_BOUNDED_H_