OpenMS
Loading...
Searching...
No Matches
CheckedCast.h
Go to the documentation of this file.
1// Copyright (c) 2002-present, OpenMS Inc. -- EKU Tuebingen, ETH Zurich, and FU Berlin
2// SPDX-License-Identifier: BSD-3-Clause
3//
4// --------------------------------------------------------------------------
5// $Maintainer: Timo Sachsenberg $
6// $Authors: Timo Sachsenberg $
7// --------------------------------------------------------------------------
8
9#pragma once
10
12#include <type_traits>
13#include <utility>
14
15namespace OpenMS
16{
18template<typename Target, typename Source>
19Target checkedCast(Source value)
20{
21 static_assert(std::is_integral_v<Target> && std::is_integral_v<Source>);
22 if (! std::in_range<Target>(value)) { throw Exception::OutOfRange(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION); }
23 return static_cast<Target>(value);
24}
25} // namespace OpenMS
Out of range exception.
Definition Exception.h:291
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
Target checkedCast(Source value)
Convert an integer without silently wrapping or truncating an out-of-range value.
Definition CheckedCast.h:19