VirtualBox

source: vbox/trunk/src/VBox/NetworkServices/NetLib/shared_ptr.h@ 101505

Last change on this file since 101505 was 98103, checked in by vboxsync, 2 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.1 KB
Line 
1/* $Id: shared_ptr.h 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * Simplified shared pointer.
4 */
5
6/*
7 * Copyright (C) 2013-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28#ifndef VBOX_INCLUDED_SRC_NetLib_shared_ptr_h
29#define VBOX_INCLUDED_SRC_NetLib_shared_ptr_h
30#ifndef RT_WITHOUT_PRAGMA_ONCE
31# pragma once
32#endif
33
34#ifdef __cplusplus
35template<typename T>
36class SharedPtr
37{
38 struct imp
39 {
40 imp(T *pTrg = NULL, int cnt = 1): ptr(pTrg),refcnt(cnt){}
41 ~imp() { if (ptr) delete ptr;}
42
43 T *ptr;
44 int refcnt;
45 };
46
47
48 public:
49 SharedPtr(T *t = NULL):p(NULL)
50 {
51 p = new imp(t);
52 }
53
54 ~SharedPtr()
55 {
56 p->refcnt--;
57
58 if (p->refcnt == 0)
59 delete p;
60 }
61
62
63 SharedPtr(const SharedPtr& rhs)
64 {
65 p = rhs.p;
66 p->refcnt++;
67 }
68
69 const SharedPtr& operator= (const SharedPtr& rhs)
70 {
71 if (p == rhs.p) return *this;
72
73 p->refcnt--;
74 if (p->refcnt == 0)
75 delete p;
76
77 p = rhs.p;
78 p->refcnt++;
79
80 return *this;
81 }
82
83
84 T *get() const
85 {
86 return p->ptr;
87 }
88
89
90 T *operator->()
91 {
92 return p->ptr;
93 }
94
95
96 const T*operator->() const
97 {
98 return p->ptr;
99 }
100
101
102 int use_count()
103 {
104 return p->refcnt;
105 }
106
107 private:
108 imp *p;
109};
110#endif
111
112#endif /* !VBOX_INCLUDED_SRC_NetLib_shared_ptr_h */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette