Return to SharedPtr.h CVS log | Up to [Pegasus] / pegasus / src / Pegasus / Common |
File: [Pegasus] / pegasus / src / Pegasus / Common / SharedPtr.h
(download)
Revision: 1.5, Tue Dec 16 18:56:00 2008 UTC (15 years, 6 months ago) by kumpf Branch: MAIN CVS Tags: preBug9676, postBug9676, TASK_PEP317_1JUNE_2013, TASK-TASK_PEP362_RestfulService_branch-root, TASK-TASK_PEP362_RestfulService_branch-merged_out_from_trunk, TASK-TASK_PEP362_RestfulService_branch-merged_in_to_trunk, TASK-TASK_PEP362_RestfulService_branch-merged_in_from_branch, TASK-TASK_PEP362_RestfulService_branch-branch, TASK-PEP362_RestfulService-root, TASK-PEP362_RestfulService-merged_out_to_branch, TASK-PEP362_RestfulService-merged_out_from_trunk, TASK-PEP362_RestfulService-merged_in_to_trunk, TASK-PEP362_RestfulService-merged_in_from_branch, TASK-PEP362_RestfulService-branch, TASK-PEP348_SCMO-root, TASK-PEP348_SCMO-merged_out_to_branch, TASK-PEP348_SCMO-merged_out_from_trunk, TASK-PEP348_SCMO-merged_in_to_trunk, TASK-PEP348_SCMO-merged_in_from_branch, TASK-PEP348_SCMO-branch, TASK-PEP317_pullop-root, TASK-PEP317_pullop-merged_out_to_branch, TASK-PEP317_pullop-merged_out_from_trunk, TASK-PEP317_pullop-merged_in_to_trunk, TASK-PEP317_pullop-merged_in_from_branch, TASK-PEP317_pullop-branch, RELEASE_2_9_2-RC2, RELEASE_2_9_2-RC1, RELEASE_2_9_2, RELEASE_2_9_1-RC1, RELEASE_2_9_1, RELEASE_2_9_0-RC1, RELEASE_2_9_0, RELEASE_2_9-root, RELEASE_2_9-branch, RELEASE_2_14_1, RELEASE_2_14_0-RC2, RELEASE_2_14_0-RC1, RELEASE_2_14_0, RELEASE_2_14-root, RELEASE_2_14-branch, RELEASE_2_13_0-RC2, RELEASE_2_13_0-RC1, RELEASE_2_13_0-FC, RELEASE_2_13_0, RELEASE_2_13-root, RELEASE_2_13-branch, RELEASE_2_12_1-RC1, RELEASE_2_12_1, RELEASE_2_12_0-RC1, RELEASE_2_12_0-FC, RELEASE_2_12_0, RELEASE_2_12-root, RELEASE_2_12-branch, RELEASE_2_11_2-RC1, RELEASE_2_11_2, RELEASE_2_11_1-RC1, RELEASE_2_11_1, RELEASE_2_11_0-RC1, RELEASE_2_11_0-FC, RELEASE_2_11_0, RELEASE_2_11-root, RELEASE_2_11-branch, RELEASE_2_10_1-RC1, RELEASE_2_10_1, RELEASE_2_10_0-RC2, RELEASE_2_10_0-RC1, RELEASE_2_10_0, RELEASE_2_10-root, RELEASE_2_10-branch, PREAUG25UPDATE, POSTAUG25UPDATE, HPUX_TEST, HEAD, CIMRS_WORK_20130824, BeforeUpdateToHeadOct82011 Changes since 1.4: +18 -18 lines BUG#: 8273 TITLE: Remove trailing space characters DESCRIPTION: Remove meaningless whitespace. |
//%LICENSE//////////////////////////////////////////////////////////////// // // Licensed to The Open Group (TOG) under one or more contributor license // agreements. Refer to the OpenPegasusNOTICE.txt file distributed with // this work for additional information regarding copyright ownership. // Each contributor licenses this file to you under the OpenPegasus Open // Source License; you may not use this file except in compliance with the // License. // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the "Software"), // to deal in the Software without restriction, including without limitation // the rights to use, copy, modify, merge, publish, distribute, sublicense, // and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included // in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // ////////////////////////////////////////////////////////////////////////// // //%///////////////////////////////////////////////////////////////////////////// #ifndef Pegasus_SharedPtr_h #define Pegasus_SharedPtr_h #include <Pegasus/Common/Config.h> #include <Pegasus/Common/AtomicInt.h> #include <Pegasus/Common/AutoPtr.h> PEGASUS_NAMESPACE_BEGIN /** Rep class for SharedPtr<> and SharedArrayPtr<>. */ template<class T, class D> class SharedPtrRep { public: SharedPtrRep() { _impl = new Impl(0); } SharedPtrRep(const SharedPtrRep<T, D>& x) { Impl::ref(_impl = x._impl); } explicit SharedPtrRep(T* ptr) { _impl = new Impl(ptr); } ~SharedPtrRep() { Impl::unref(_impl); } void assign(const SharedPtrRep<T, D>& x) { if (_impl != x._impl) { Impl::unref(_impl); Impl::ref(_impl = x._impl); } } void reset(T* ptr = 0) { if (ptr == _impl->ptr) return; if (_impl->refs.get() == 1) { _impl->d(_impl->ptr); _impl->ptr = ptr; } else { Impl::unref(_impl); _impl = new Impl(ptr); } } T* get() const { return _impl->ptr; } size_t count() const { return _impl->refs.get(); } private: struct Impl { D d; AtomicInt refs; T* ptr; Impl(T* ptr_) : refs(1), ptr(ptr_) { } ~Impl() { d(ptr); } static void ref(Impl* impl_) { Impl* impl = (Impl*)(impl_); if (impl) impl->refs++; } static void unref(Impl* impl_) { Impl* impl = (Impl*)(impl_); if (impl && impl->refs.decAndTestIfZero()) delete impl; } }; Impl* _impl; }; /** This class implements a shared pointer, similar to the one found in BOOST (and in countless other libraries). The implementation maintains a pointer to an intermediate object that contains the pointer to the target object as well as a reference counter. Because the target object need not have a reference counter of its own, we say that this implementation is non-invasive (not imposing requirements on the target class). The instance diagram below depicts two SharedPtr objects sharing a target instance (of the class Monkey). \verbatim +-----------+ | SharedPtr | +-----------+ +---------------+ | _impl |--->| SharedPtrImpl | +-----------+ +---------------+ +--------+ | ptr |--->| Monkey | (target instance) +-----------+ +---------------+ +--------+ | SharedPtr |--->| refs = 2 | +-----------+ +---------------+ + _impl | +-----------+ \endverbatim The code snippet below constructs this instance diagram. \code SharedPtr p1(new Monkey); SharedPtr p2 = p1; \endcode The target instance is deleted when all shared pointers that refer to it are destroyed (or reset). A pointer may be reset, which causes the previous referent to be released. For example: \code SharedPtr p(new Monkey); // Release the old monkey (destroying it in this case). p.reset(new Monkey); \endcode SharedPtr implements pointer semantics by overloading the related operators. For example: \code SharedPtr p(new Monkey); if (p) { // Print the monkey object (print() is a member of the Monkey class). p->print(); } \endcode Shared pointers may be dereferenced but beware that doing so to a null pointer is an error and will result in a crash. \code SharedPtr p(0); // Core dump! (dereferencing a null pointer). Monkey& monkey = *p; // Core dump! (dereferencing a null pointer). p->print(); \endcode The get() method obtains the target pointer but beware that constructing a new SharedPtr with this pointer will result in double deletes (that is why the SharedPtr(T*) constructor is explicit). For example, avoid this: \code SharedPtr p1(new Monkey); SharedPtr p2(p1.get()); \endcode */ template<class T, class D = DeletePtr<T> > class SharedPtr { public: typedef SharedPtrRep<T, D> Rep; SharedPtr() { } SharedPtr(const SharedPtr<T, D>& x) : _rep(x._rep) { } explicit SharedPtr(T* ptr) : _rep(ptr) { } ~SharedPtr() { } SharedPtr<T, D>& operator=(const SharedPtr<T, D>& x) { _rep.assign(x._rep); return *this; } void reset(T* ptr = 0) { _rep.reset(ptr); } T& operator*() const { return *get(); } T* operator->() const { return get(); } T* get() const { return _rep.get(); } operator bool() const { return get() != 0; } size_t count() const { return _rep.count(); } protected: Rep _rep; }; /** SharedArrayPtr<> has the same interface as SharedPtr<> but works on arrays. */ template<class T, class D = DeleteArrayPtr<T> > class SharedArrayPtr : public SharedPtr<T, D> { public: SharedArrayPtr() { } explicit SharedArrayPtr(T* ptr) : SharedPtr<T, D>(ptr) { } T& operator[](size_t i) const { return this->_rep.get()[i]; } }; PEGASUS_NAMESPACE_END #endif /* Pegasus_SharedPtr_h */
No CVS admin address has been configured |
Powered by ViewCVS 0.9.2 |