shared_ptr.h revision 168c8d6c9ebf206dd762e5909178774e4e6c68e2
168c8d6c9ebf206dd762e5909178774e4e6c68e2vboxsync/* $Id$ */
168c8d6c9ebf206dd762e5909178774e4e6c68e2vboxsync/** @file
168c8d6c9ebf206dd762e5909178774e4e6c68e2vboxsync * Simplified shared pointer.
168c8d6c9ebf206dd762e5909178774e4e6c68e2vboxsync */
168c8d6c9ebf206dd762e5909178774e4e6c68e2vboxsync
168c8d6c9ebf206dd762e5909178774e4e6c68e2vboxsync/*
168c8d6c9ebf206dd762e5909178774e4e6c68e2vboxsync * Copyright (C) 2013 Oracle Corporation
168c8d6c9ebf206dd762e5909178774e4e6c68e2vboxsync *
168c8d6c9ebf206dd762e5909178774e4e6c68e2vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
168c8d6c9ebf206dd762e5909178774e4e6c68e2vboxsync * available from http://www.virtualbox.org. This file is free software;
168c8d6c9ebf206dd762e5909178774e4e6c68e2vboxsync * you can redistribute it and/or modify it under the terms of the GNU
168c8d6c9ebf206dd762e5909178774e4e6c68e2vboxsync * General Public License (GPL) as published by the Free Software
168c8d6c9ebf206dd762e5909178774e4e6c68e2vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
168c8d6c9ebf206dd762e5909178774e4e6c68e2vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
168c8d6c9ebf206dd762e5909178774e4e6c68e2vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
168c8d6c9ebf206dd762e5909178774e4e6c68e2vboxsync */
168c8d6c9ebf206dd762e5909178774e4e6c68e2vboxsync
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync#ifndef __SHARED_PTR_H__
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync#define __SHARED_PTR_H__
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync#ifdef __cplusplus
0d49a2fbc9857ad8dd29542de7fb37202f1a283fvboxsynctemplate<typename T>
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsyncclass SharedPtr
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync{
0d49a2fbc9857ad8dd29542de7fb37202f1a283fvboxsync struct imp
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync {
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync imp(T *pTrg = NULL, int cnt = 1): ptr(pTrg),refcnt(cnt){}
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync ~imp() { if (ptr) delete ptr;}
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync T *ptr;
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync int refcnt;
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync };
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync
0d49a2fbc9857ad8dd29542de7fb37202f1a283fvboxsync
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync public:
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync SharedPtr(T *t = NULL):p(NULL)
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync {
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync p = new imp(t);
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync }
0d49a2fbc9857ad8dd29542de7fb37202f1a283fvboxsync
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync ~SharedPtr()
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync {
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync p->refcnt--;
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync if (p->refcnt == 0)
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync delete p;
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync }
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync SharedPtr(const SharedPtr& rhs)
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync {
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync p = rhs.p;
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync p->refcnt++;
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync }
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync const SharedPtr& operator= (const SharedPtr& rhs)
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync {
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync if (p == rhs.p) return *this;
0d49a2fbc9857ad8dd29542de7fb37202f1a283fvboxsync
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync p->refcnt--;
0d49a2fbc9857ad8dd29542de7fb37202f1a283fvboxsync if (p->refcnt == 0)
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync delete p;
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync p = rhs.p;
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync p->refcnt++;
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync return *this;
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync }
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync T *get() const
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync {
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync return p->ptr;
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync }
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync
0d49a2fbc9857ad8dd29542de7fb37202f1a283fvboxsync
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync T *operator->()
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync {
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync return p->ptr;
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync }
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync
0d49a2fbc9857ad8dd29542de7fb37202f1a283fvboxsync
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync const T*operator->() const
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync {
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync return p->ptr;
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync }
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync int use_count()
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync {
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync return p->refcnt;
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync }
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync private:
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync imp *p;
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync};
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync#endif
0d49a2fbc9857ad8dd29542de7fb37202f1a283fvboxsync
66a09bb37f8bc24ce62920cd480f9b4d13347aadvboxsync#endif