VirtualBox

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

Last change on this file since 92528 was 82968, checked in by vboxsync, 5 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: 1.8 KB
Line 
1/* $Id: shared_ptr.h 82968 2020-02-04 10:35:17Z vboxsync $ */
2/** @file
3 * Simplified shared pointer.
4 */
5
6/*
7 * Copyright (C) 2013-2020 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#ifndef VBOX_INCLUDED_SRC_NetLib_shared_ptr_h
19#define VBOX_INCLUDED_SRC_NetLib_shared_ptr_h
20#ifndef RT_WITHOUT_PRAGMA_ONCE
21# pragma once
22#endif
23
24#ifdef __cplusplus
25template<typename T>
26class SharedPtr
27{
28 struct imp
29 {
30 imp(T *pTrg = NULL, int cnt = 1): ptr(pTrg),refcnt(cnt){}
31 ~imp() { if (ptr) delete ptr;}
32
33 T *ptr;
34 int refcnt;
35 };
36
37
38 public:
39 SharedPtr(T *t = NULL):p(NULL)
40 {
41 p = new imp(t);
42 }
43
44 ~SharedPtr()
45 {
46 p->refcnt--;
47
48 if (p->refcnt == 0)
49 delete p;
50 }
51
52
53 SharedPtr(const SharedPtr& rhs)
54 {
55 p = rhs.p;
56 p->refcnt++;
57 }
58
59 const SharedPtr& operator= (const SharedPtr& rhs)
60 {
61 if (p == rhs.p) return *this;
62
63 p->refcnt--;
64 if (p->refcnt == 0)
65 delete p;
66
67 p = rhs.p;
68 p->refcnt++;
69
70 return *this;
71 }
72
73
74 T *get() const
75 {
76 return p->ptr;
77 }
78
79
80 T *operator->()
81 {
82 return p->ptr;
83 }
84
85
86 const T*operator->() const
87 {
88 return p->ptr;
89 }
90
91
92 int use_count()
93 {
94 return p->refcnt;
95 }
96
97 private:
98 imp *p;
99};
100#endif
101
102#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