NAP
rtticast.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4 
5 #pragma once
6 
12 template<typename T, typename Arg>
13 T* rtti_cast(Arg object)
14 {
15  return object && object->get_type().template is_derived_from<T>() ?
16  reinterpret_cast<T*>(object) : nullptr;
17 };
18 
19 
26 template<typename Derived, typename Base>
27 std::unique_ptr<Derived> rtti_cast(std::unique_ptr<Base>& pointer)
28 {
29  if (Derived *result = rtti_cast<Derived>(pointer.get()))
30  {
31  pointer.release();
32  return std::unique_ptr<Derived>(result);
33  }
34  return std::unique_ptr<Derived>(nullptr);
35 }