ndn-cxx: NDN C++ Library 0.9.0-33-g832ea91d
Loading...
Searching...
No Matches
backports.hpp
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2013-2024 Regents of the University of California.
4 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
22#ifndef NDN_CXX_UTIL_BACKPORTS_HPP
23#define NDN_CXX_UTIL_BACKPORTS_HPP
24
25#include <type_traits>
26#include <utility>
27
28#include <boost/assert.hpp>
29#include <boost/predef/compiler/clang.h>
30#include <boost/predef/compiler/gcc.h>
31#include <boost/predef/compiler/visualc.h>
32
33namespace ndn {
34
35//
36// https://wg21.link/P1682
37// std::to_underlying() (C++23)
38//
39#if __cpp_lib_to_underlying >= 202102L
40using std::to_underlying;
41#else
42template<typename T>
43[[nodiscard]] constexpr std::underlying_type_t<T>
44to_underlying(T val) noexcept
45{
46 // instantiating underlying_type with a non-enum type is UB before C++20
47 static_assert(std::is_enum_v<T>);
48 return static_cast<std::underlying_type_t<T>>(val);
49}
50#endif // __cpp_lib_to_underlying
51
52//
53// https://wg21.link/P0627
54// std::unreachable() (C++23)
55//
56#ifndef NDEBUG
57# define NDN_CXX_UNREACHABLE BOOST_ASSERT(false)
58#elif __cpp_lib_unreachable >= 202202L
59# define NDN_CXX_UNREACHABLE std::unreachable()
60#else
61# define NDN_CXX_UNREACHABLE ::ndn::detail::unreachable()
62namespace detail {
63[[noreturn]] inline void
64unreachable()
65{
66#if BOOST_COMP_GNUC || BOOST_COMP_CLANG
67 __builtin_unreachable();
68#elif BOOST_COMP_MSVC
69 __assume(0);
70#endif
71} // unreachable()
72} // namespace detail
73#endif
74
75} // namespace ndn
76
77#endif // NDN_CXX_UTIL_BACKPORTS_HPP
Definition data.cpp:25
constexpr std::underlying_type_t< T > to_underlying(T val) noexcept
Definition backports.hpp:44