You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
3.5 KiB

// Copyright 2020 Siemens Digital Industries Software
// ==================================================
// Copyright 2013.
// Siemens Product Lifecycle Management Software Inc.
// All Rights Reserved.
// ==================================================
// Copyright 2020 Siemens Digital Industries Software
/**
@file
Base class for shared ptr reference counts.
@note This class should not be used as such.
<br>Use the class #SharedPtr instead.
*/
/*
Background:
Base implementation copied from NX. The code was copied as this class was available in
latest NX libraries which are currently not supported in Tc.
The implementation in NX uses code from Boost library, hence a reference to Boost license in included.
Changes were made to NX code to conform it to Tc. These changes include :
- namespace change
- using default heap instead of SM.
- remove support for custom allocator. This was NX specific extension, not present in std::tr1
- cosmetic changes like separating class declaration and definition for clarity.
*/
/* Boost license: as base implementation comes for Boost, including its license.
Copyright (c) 2001, 2002, 2003 Peter Dimov and Multi Media Ltd.
Copyright 2004-2005 Peter Dimov
Distributed under the Boost Software License, Version 1.0.
(See copy at http://www.boost.org/LICENSE_1_0.txt)
*/
#ifndef TEAMCENTER_BASE_UTILS_SHARED_PTR_COUNTED_BASE_HXX
#define TEAMCENTER_BASE_UTILS_SHARED_PTR_COUNTED_BASE_HXX
namespace Teamcenter
{
namespace Internal
{
class SharedPtrCountedBase
{
public:
inline SharedPtrCountedBase();
inline virtual ~SharedPtrCountedBase(); // nothrow
/// override this method to release the resources being managed
/// shouldn't throw exceptions.
virtual void dispose() = 0;
/// use this to release the reference count itself
inline virtual void destroy();
virtual void * get_deleter( std::type_info const & ti ) = 0;
inline void add_ref_copy();
inline bool add_ref_lock();
inline void release(); // nothrow
inline void weak_add_ref(); // nothrow
inline void weak_release(); // nothrow
inline long use_count() const; // nothrow
private:
SharedPtrCountedBase( SharedPtrCountedBase const & );
SharedPtrCountedBase & operator= ( SharedPtrCountedBase const & );
long use_count_; // #shared
long weak_count_; // #weak + (#shared != 0)
};
/// Implementation Details
inline SharedPtrCountedBase::SharedPtrCountedBase()
: use_count_( 1 ), weak_count_( 1 )
{
}
inline SharedPtrCountedBase::~SharedPtrCountedBase()
{
}
inline void SharedPtrCountedBase::destroy()
{
delete this;
}
inline void SharedPtrCountedBase::add_ref_copy()
{
++use_count_;
}
inline bool SharedPtrCountedBase::add_ref_lock()
{
if( use_count_ == 0 )
{
return false;
}
++use_count_;
return true;
}
inline void SharedPtrCountedBase::release()
{
if( --use_count_ == 0 )
{
dispose();
weak_release();
}
}
inline void SharedPtrCountedBase::weak_add_ref()
{
++weak_count_;
}
inline void SharedPtrCountedBase::weak_release()
{
if( --weak_count_ == 0 )
{
destroy();
}
}
inline long SharedPtrCountedBase::use_count() const
{
return use_count_;
}
} // end namespace Internal
} // end namespace Teamcenter
#endif // TEAMCENTER_BASE_UTILS_SHARED_PTR_COUNTED_BASE_HXX