wxMaxima
Loading...
Searching...
No Matches
unique_cast.hpp
1// -*- mode: c++; c-file-style: "linux"; c-basic-offset: 2; indent-tabs-mode: nil -*-
2//
3// Copyright (C) 2020 Kuba Ober <kuba@mareimbrium.org>
4//
5// Use, modification, and distribution is subject to the Boost Software
6// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7// http://www.boost.org/LICENSE_1_0.txt)
8//
9// SPDX-License-Identifier: BSL-1.0
10
11#ifndef STX_UNIQUE_CAST_HPP_INCLUDED
12#define STX_UNIQUE_CAST_HPP_INCLUDED
13
14#include <memory>
15
16namespace stx {
17
18// std::make_unique for non-array types is available since C++14
19#if 0
20template <typename T, typename... Args>
21std::unique_ptr<T> make_unique(Args &&... args) {
22 return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
23}
24#endif
25
28template <typename Derived, typename Base>
29std::unique_ptr<Derived> static_unique_ptr_cast(std::unique_ptr<Base> &&p) {
30 auto d = static_cast<Derived *>(p.release());
31 return std::unique_ptr<Derived>(d);
32 // Note: We don't move the deleter, since it's not special.
33}
34
37template <typename Derived, typename Base>
38std::unique_ptr<Derived> dynamic_unique_ptr_cast(std::unique_ptr<Base> &&p) {
39 auto d = dynamic_cast<Derived *>(p.get());
40 if (d)
41 p.release();
42 return std::unique_ptr<Derived>(d);
43 // Note: We don't move the deleter, since it's not special.
44}
45
46}
47
48#endif
std::unique_ptr< Derived > static_unique_ptr_cast(std::unique_ptr< Base > &&p) noexcept
A cast for unique pointers, used to downcast to a derived type iff we're certain the cell is indeed o...
Definition: CellPtr.h:657
std::unique_ptr< Derived > dynamic_unique_ptr_cast(std::unique_ptr< Base > &&p) noexcept
A cast for unique pointers, used to downcast to a derived type in a type-safe manner.
Definition: CellPtr.h:667