Loading...
Searching...
No Matches
Pointer.hpp
1#pragma once
2#include <score/tools/Debug.hpp>
3
4#include <memory>
5
6#ifdef SCORE_DEBUG
7template <typename Derived, typename Base, typename Del>
8std::unique_ptr<Derived> dynamic_unique_ptr_cast(std::unique_ptr<Base, Del>&& p)
9{
10 if(Derived* result = dynamic_cast<Derived*>(p.get()))
11 {
12 p.release();
13 return std::unique_ptr<Derived>(result);
14 }
15 return nullptr;
16}
17
18template <typename T, typename U>
19auto safe_unique_ptr_cast(std::unique_ptr<U> other)
20{
21 auto res = dynamic_unique_ptr_cast<T>(other);
22 SCORE_ASSERT(res);
23 return res;
24}
25#else
26// http://stackoverflow.com/a/21174979/1495627
27template <typename Derived, typename Base, typename Del>
28std::unique_ptr<Derived> static_unique_ptr_cast(std::unique_ptr<Base, Del>&& p)
29{
30 return std::unique_ptr<Derived>(static_cast<Derived*>(p.release()));
31}
32
33#define safe_unique_ptr_cast static_unique_ptr_cast
34#endif