VirtualBox

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

Last change on this file since 70905 was 69500, checked in by vboxsync, 7 years ago

*: scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 1.6 KB
Line 
1/* $Id: shared_ptr.h 69500 2017-10-28 15:14:05Z vboxsync $ */
2/** @file
3 * Simplified shared pointer.
4 */
5
6/*
7 * Copyright (C) 2013-2017 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 __SHARED_PTR_H__
19#define __SHARED_PTR_H__
20
21#ifdef __cplusplus
22template<typename T>
23class SharedPtr
24{
25 struct imp
26 {
27 imp(T *pTrg = NULL, int cnt = 1): ptr(pTrg),refcnt(cnt){}
28 ~imp() { if (ptr) delete ptr;}
29
30 T *ptr;
31 int refcnt;
32 };
33
34
35 public:
36 SharedPtr(T *t = NULL):p(NULL)
37 {
38 p = new imp(t);
39 }
40
41 ~SharedPtr()
42 {
43 p->refcnt--;
44
45 if (p->refcnt == 0)
46 delete p;
47 }
48
49
50 SharedPtr(const SharedPtr& rhs)
51 {
52 p = rhs.p;
53 p->refcnt++;
54 }
55
56 const SharedPtr& operator= (const SharedPtr& rhs)
57 {
58 if (p == rhs.p) return *this;
59
60 p->refcnt--;
61 if (p->refcnt == 0)
62 delete p;
63
64 p = rhs.p;
65 p->refcnt++;
66
67 return *this;
68 }
69
70
71 T *get() const
72 {
73 return p->ptr;
74 }
75
76
77 T *operator->()
78 {
79 return p->ptr;
80 }
81
82
83 const T*operator->() const
84 {
85 return p->ptr;
86 }
87
88
89 int use_count()
90 {
91 return p->refcnt;
92 }
93
94 private:
95 imp *p;
96};
97#endif
98
99#endif
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