Back to home page

EIC code displayed by LXR

 
 

    


Warning, /include/gsl/narrow is written in an unsupported language. File is not indexed.

0001 ///////////////////////////////////////////////////////////////////////////////
0002 //
0003 // Copyright (c) 2015 Microsoft Corporation. All rights reserved.
0004 //
0005 // This code is licensed under the MIT License (MIT).
0006 //
0007 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0008 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0009 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
0010 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0011 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
0012 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
0013 // THE SOFTWARE.
0014 //
0015 ///////////////////////////////////////////////////////////////////////////////
0016 
0017 #ifndef GSL_NARROW_H
0018 #define GSL_NARROW_H
0019 #include <gsl/assert> // for Expects
0020 #include <gsl/util>   // for narrow_cast
0021 namespace gsl
0022 {
0023 struct narrowing_error : public std::exception
0024 {
0025     const char* what() const noexcept override { return "narrowing_error"; }
0026 };
0027 
0028 // narrow() : a checked version of narrow_cast() that throws if the cast changed the value
0029 template <class T, class U, typename std::enable_if<std::is_arithmetic<T>::value>::type* = nullptr>
0030 // clang-format off
0031 GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
0032 GSL_SUPPRESS(f.6) // NO-FORMAT: attribute // TODO: MSVC /analyze does not recognise noexcept(false)
0033     // clang-format on
0034     constexpr T narrow(U u) noexcept(false)
0035 {
0036     constexpr const bool is_different_signedness =
0037         (std::is_signed<T>::value != std::is_signed<U>::value);
0038 
0039 GSL_SUPPRESS(es.103) // NO-FORMAT: attribute // don't overflow
0040 GSL_SUPPRESS(es.104) // NO-FORMAT: attribute // don't underflow
0041 GSL_SUPPRESS(p.2) // NO-FORMAT: attribute // don't rely on undefined behavior
0042     const T t = narrow_cast<T>(u); // While this is technically undefined behavior in some cases (i.e., if the source value is of floating-point type
0043                                    // and cannot fit into the destination integral type), the resultant behavior is benign on the platforms
0044                                    // that we target (i.e., no hardware trap representations are hit).
0045 
0046     if (static_cast<U>(t) != u || (is_different_signedness && ((t < T{}) != (u < U{}))))
0047     {
0048         throw narrowing_error{};
0049     }
0050 
0051     return t;
0052 }
0053 
0054 template <class T, class U, typename std::enable_if<!std::is_arithmetic<T>::value>::type* = nullptr>
0055 // clang-format off
0056 GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
0057 GSL_SUPPRESS(f.6) // NO-FORMAT: attribute // TODO: MSVC /analyze does not recognise noexcept(false)
0058     // clang-format on
0059     constexpr T narrow(U u) noexcept(false)
0060 {
0061     const T t = narrow_cast<T>(u);
0062 
0063     if (static_cast<U>(t) != u)
0064     {
0065         throw narrowing_error{};
0066     }
0067 
0068     return t;
0069 }
0070 } // namespace gsl
0071 #endif // GSL_NARROW_H