-
Notifications
You must be signed in to change notification settings - Fork 171
Expand file tree
/
Copy pathoptional_ptr.h
More file actions
88 lines (71 loc) · 2.04 KB
/
Copy pathoptional_ptr.h
File metadata and controls
88 lines (71 loc) · 2.04 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//
// Copyright 2018 The Simons Foundation, Inc. - All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#ifndef __ITENSOR_OPTIONAL_PTR_H
#define __ITENSOR_OPTIONAL_PTR_H 1
#include <memory>
#include <type_traits>
namespace itensor {
/**
optional_ptr<T> functions either as a raw unmanaged pointer
or as a smart pointer of type managed_ptr depending on whether
it is initialized through the set_external method or
the set_managed method
*/
template <typename T, typename managed_ptr = std::unique_ptr<T>>
class optional_ptr
{
public:
optional_ptr() : p_(nullptr) { }
optional_ptr(const optional_ptr& other,
typename std::enable_if<std::is_copy_constructible<managed_ptr>::value>::type* = 0)
:
p_(other.p_),
mp_(other.mp_)
{ }
optional_ptr(optional_ptr&& other)
:
p_(other.p_),
mp_(std::move(other.mp_))
{ }
template <typename std::enable_if<std::is_copy_constructible<managed_ptr>::value>::type* = nullptr>
optional_ptr&
operator=(const optional_ptr& other)
{
p_ = other.p_;
mp_ = other.mp_;
}
T&
operator*() const { return *p_; }
T*
operator->() const { return p_; }
void
set_managed(T* new_p)
{
mp_ = std::move(managed_ptr(new_p));
p_ = mp_.get();
}
void
set_external(T* ext_p)
{
p_ = ext_p;
mp_.reset();
}
private:
T* p_;
managed_ptr mp_;
};
} // namespace itensor
#endif