/src/brpc/src/butil/ptr_container.h
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | // Date: Fri Sep 7 12:15:23 CST 2018 |
19 | | |
20 | | #ifndef BUTIL_PTR_CONTAINER_H |
21 | | #define BUTIL_PTR_CONTAINER_H |
22 | | |
23 | | namespace butil { |
24 | | |
25 | | // Manage lifetime of a pointer. The key difference between PtrContainer and |
26 | | // unique_ptr is that PtrContainer can be copied and the pointer inside is |
27 | | // deeply copied or constructed on-demand. |
28 | | template <typename T> |
29 | | class PtrContainer { |
30 | | public: |
31 | 0 | PtrContainer() : _ptr(NULL) {}Unexecuted instantiation: butil::PtrContainer<brpc::ServerSSLOptions>::PtrContainer() Unexecuted instantiation: butil::PtrContainer<brpc::ChannelSSLOptions>::PtrContainer() |
32 | | |
33 | | explicit PtrContainer(T* obj) : _ptr(obj) {} |
34 | | |
35 | 0 | ~PtrContainer() { |
36 | 0 | delete _ptr; |
37 | 0 | } Unexecuted instantiation: butil::PtrContainer<brpc::ServerSSLOptions>::~PtrContainer() Unexecuted instantiation: butil::PtrContainer<brpc::ChannelSSLOptions>::~PtrContainer() |
38 | | |
39 | | PtrContainer(const PtrContainer& rhs) |
40 | 0 | : _ptr(rhs._ptr ? new T(*rhs._ptr) : NULL) {} |
41 | | |
42 | 0 | void operator=(const PtrContainer& rhs) { |
43 | 0 | if (this == &rhs) { |
44 | 0 | return; |
45 | 0 | } |
46 | | |
47 | 0 | if (rhs._ptr) { |
48 | 0 | if (_ptr) { |
49 | 0 | *_ptr = *rhs._ptr; |
50 | 0 | } else { |
51 | 0 | _ptr = new T(*rhs._ptr); |
52 | 0 | } |
53 | 0 | } else { |
54 | 0 | delete _ptr; |
55 | 0 | _ptr = NULL; |
56 | 0 | } |
57 | 0 | } Unexecuted instantiation: butil::PtrContainer<brpc::ServerSSLOptions>::operator=(butil::PtrContainer<brpc::ServerSSLOptions> const&) Unexecuted instantiation: butil::PtrContainer<brpc::ChannelSSLOptions>::operator=(butil::PtrContainer<brpc::ChannelSSLOptions> const&) |
58 | | |
59 | 0 | T* get() const { return _ptr; }Unexecuted instantiation: butil::PtrContainer<brpc::ServerSSLOptions>::get() const Unexecuted instantiation: butil::PtrContainer<brpc::ChannelSSLOptions>::get() const |
60 | | |
61 | 0 | void reset(T* ptr) { |
62 | 0 | delete _ptr; |
63 | 0 | _ptr = ptr; |
64 | 0 | } Unexecuted instantiation: butil::PtrContainer<brpc::ServerSSLOptions>::reset(brpc::ServerSSLOptions*) Unexecuted instantiation: butil::PtrContainer<brpc::ChannelSSLOptions>::reset(brpc::ChannelSSLOptions*) |
65 | | |
66 | 0 | operator void*() const { return _ptr; }Unexecuted instantiation: butil::PtrContainer<brpc::ServerSSLOptions>::operator void*() const Unexecuted instantiation: butil::PtrContainer<brpc::ChannelSSLOptions>::operator void*() const |
67 | | |
68 | 0 | explicit operator bool() const { return get() != NULL; }Unexecuted instantiation: butil::PtrContainer<brpc::ServerSSLOptions>::operator bool() const Unexecuted instantiation: butil::PtrContainer<brpc::ChannelSSLOptions>::operator bool() const |
69 | | |
70 | 0 | T& operator*() const { return *get(); }Unexecuted instantiation: butil::PtrContainer<brpc::ServerSSLOptions>::operator*() const Unexecuted instantiation: butil::PtrContainer<brpc::ChannelSSLOptions>::operator*() const |
71 | | |
72 | | T* operator->() const { return get(); } |
73 | | |
74 | | private: |
75 | | T* _ptr; |
76 | | }; |
77 | | |
78 | | } // namespace butil |
79 | | |
80 | | #endif // BUTIL_PTR_CONTAINER_H |