summaryrefslogtreecommitdiffstats
path: root/Utilities/std/cmext/memory
blob: 3681d975239e9c50e9602ce2dc592bbf591d59e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// -*-c++-*-
// vim: set ft=cpp:

/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing for details.  */
#pragma once

#include <typeinfo>

#include <cm/type_traits>

namespace cm {

template <
  typename T, typename O,
  cm::enable_if_t<std::is_pointer<decltype(std::declval<O>().get())>::value,
                  int> = 0>
T& static_reference_cast(O& item)
{
  return *(static_cast<T*>(item.get()));
}

template <
  typename T, typename O,
  cm::enable_if_t<std::is_pointer<decltype(std::declval<O>().get())>::value,
                  int> = 0>
T& dynamic_reference_cast(O& item)
{
  auto p = dynamic_cast<T*>(item.get());

  if (p == nullptr) {
    throw std::bad_cast();
  }

  return *p;
}

} // namespace cm