Leosac  0.8.0
Open Source Access Control
AssertCast.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Leosac is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #pragma once
21 
22 #include "tools/log.hpp"
23 #include <memory>
24 
25 namespace Leosac
26 {
27 
28 template <typename T>
29 struct is_shared_ptr : public std::false_type
30 {
31 };
32 
33 template <typename T>
34 struct is_shared_ptr<std::shared_ptr<T>> : public std::true_type
35 {
36 };
37 
38 template <typename T>
39 struct is_shared_ptr<const std::shared_ptr<T>> : public std::true_type
40 {
41 };
42 
43 template <typename T>
44 struct is_shared_ptr<std::shared_ptr<T> &> : public std::true_type
45 {
46 };
47 
48 template <typename T>
49 struct is_shared_ptr<const std::shared_ptr<T> &> : public std::true_type
50 {
51 };
52 
53 template <typename T>
55 
56 template <typename Out, typename In>
57 std::enable_if_t<is_shared_ptr_v<Out>, Out>
58 assert_cast(const std::shared_ptr<In> &in)
59 {
60  bool is_null = !in;
61 
62  if (auto p = dynamic_cast<typename Out::element_type *>(in.get()))
63  {
64  return Out(in, p);
65  }
66  else if (!is_null)
67  {
68  ASSERT_LOG(0, "Pointer is null after assert_cast");
69  }
70  return nullptr;
71 };
72 
73 template <typename Out, typename In>
74 std::enable_if_t<!is_shared_ptr_v<Out> && !std::is_pointer<Out>::value, Out &&>
75 assert_cast(In &&in)
76 {
77  try
78  {
79  Out &&tmp = dynamic_cast<Out &&>(in);
80  return std::forward<Out>(tmp);
81  }
82  catch (const std::bad_cast &)
83  {
84  ASSERT_LOG(false, "Cast failed.");
85  }
86  std::terminate();
87 };
88 
89 template <typename Out, typename In>
90 std::enable_if_t<std::is_pointer<Out>::value && std::is_pointer<In>::value, Out>
91 assert_cast(In &&in)
92 {
93  bool is_null = !in;
94 
95  if (auto p = dynamic_cast<Out>(in))
96  {
97  return p;
98  }
99  else if (!is_null)
100  {
101  ASSERT_LOG(0, "Pointer is null after assert_cast");
102  }
103  return nullptr;
104 };
105 }
Leosac::is_shared_ptr_v
constexpr bool is_shared_ptr_v
Definition: AssertCast.hpp:54
Leosac::is_shared_ptr
Definition: AssertCast.hpp:29
ASSERT_LOG
#define ASSERT_LOG(cond, msg)
Definition: log.hpp:190
Leosac::assert_cast
std::enable_if_t< is_shared_ptr_v< Out >, Out > assert_cast(const std::shared_ptr< In > &in)
Definition: AssertCast.hpp:58
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
log.hpp