-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathmemory_util.h
More file actions
61 lines (49 loc) · 981 Bytes
/
Copy pathmemory_util.h
File metadata and controls
61 lines (49 loc) · 981 Bytes
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright (C) 2022 Satya Das and CppParser contributors
// SPDX-License-Identifier: MIT
#ifndef B1A7BAF0_1D5C_4F41_9DF8_07895208C0BC
#define B1A7BAF0_1D5C_4F41_9DF8_07895208C0BC
#include <memory>
#include <stdexcept>
template <typename T>
auto Ptr(T* p)
{
return std::unique_ptr<T>(p);
}
template <typename T, typename = void>
class ToObjHelper
{
T* p_;
public:
ToObjHelper(T* p)
: p_(p)
{
}
T toObj()
{
if (p_ == nullptr)
throw std::runtime_error("nullptr cannot be converted to an object");
return std::move(*std::unique_ptr<T>(p_));
}
};
template <typename T>
class ToObjHelper<T, std::enable_if_t<std::is_default_constructible_v<T>>>
{
T* p_;
public:
ToObjHelper(T* p)
: p_(p)
{
}
T toObj()
{
if (p_ == nullptr)
return T();
return std::move(*std::unique_ptr<T>(p_));
}
};
template <typename T>
auto Obj(T* p)
{
return ToObjHelper(p).toObj();
}
#endif /* B1A7BAF0_1D5C_4F41_9DF8_07895208C0BC */