DxLibEx
cast_if.hpp
Go to the documentation of this file.
1 /*=============================================================================
2  Copyright (C) 2015-2017 DxLibEx project
3  https://github.com/Nagarei/DxLibEx/
4 
5  Distributed under the Boost Software License, Version 1.0.
6  (See http://www.boost.org/LICENSE_1_0.txt)
7 =============================================================================*/
8 #ifndef DXLE_INC_ALGORITHM_CAST_IF_HPP_
9 #define DXLE_INC_ALGORITHM_CAST_IF_HPP_
10 #include "dxlibex/config/defines.h"
11 #include <utility>
12 namespace dxle {
13  namespace detail {
14 
15  template<typename from, typename to, bool do_cast,
16  bool is_nothrow_move_constructible =
17  std::is_nothrow_move_constructible<from>::value>
19  DXLE_CONSTEXPR to operator()(const from& n)
20  const DXLE_NOEXCEPT_IF_EXPR(static_cast<double>(std::declval<from>()))
21  {
22  return static_cast<double>(n);
23  }
24  };
25 
26  template<typename from, typename to>
27  struct static_cast_if_helper<to, from, false, false>{
28  DXLE_CONSTEXPR to operator()(const from& n)
29  const DXLE_NOEXCEPT_IF(std::is_nothrow_copy_constructible<from>::value)
30  {
31  return n;
32  }
33  };
34 
35  template<typename from, typename to>
36  struct static_cast_if_helper<to, from, false, true>{
37 
38  DXLE_CONSTEXPR to operator()(const from& n)
39  const DXLE_NOEXCEPT_IF(std::is_nothrow_copy_constructible<from>::value)
40  {
41  return n;
42  }
45  {
46  return std::move(n);
47  }
48  };
49  }//namespace detail
50 
51  template<typename from, typename to, bool do_cast>
52  DXLE_CONSTEXPR to static_cast_if(const from& n)
55  std::move(std::declval<from>())
56  )
57  ))
58  {
60  }
61 
62  template<typename from, typename to, bool do_cast>
65  dxle::detail::static_cast_if_helper<to, from, do_cast>()(
66  std::move(std::declval<from>())
67  )
68  ))
69  {
71  }
72 }//namespace dxle
73 #endif //DXLE_INC_ALGORITHM_CAST_IF_HPP_
#define DXLE_CONSTEXPR
Definition: suffix.hpp:21
DXLE_CONSTEXPR to operator()(const from &n) const DXLE_NOEXCEPT_IF_EXPR(static_cast< double >(std
Definition: cast_if.hpp:19
Definition: point2d.hpp:672
Definition: cast_if.hpp:12
DXLE_CONSTEXPR to operator()(from &&n) const DXLE_NOEXCEPT_OR_NOTHROW
Definition: cast_if.hpp:43
#define DXLE_NOEXCEPT_IF_EXPR(EXPR)
Definition: suffix.hpp:110
DXLE_CONSTEXPR to operator()(const from &n) const DXLE_NOEXCEPT_IF(std
Definition: cast_if.hpp:38
#define DXLE_NOEXCEPT_IF(COND)
Definition: suffix.hpp:107
#define DXLE_NOEXCEPT_OR_NOTHROW
Definition: suffix.hpp:94
DXLE_CONSTEXPR to operator()(const from &n) const DXLE_NOEXCEPT_IF(std
Definition: cast_if.hpp:28
DXLE_CONSTEXPR to static_cast_if(const from &n) DXLE_NOEXCEPT_IF_EXPR((dxle
Definition: cast_if.hpp:52