CExtrusivePtr.h
Go to the documentation of this file.
// -*- c++ -*-
//*****************************************************************************
//*****************************************************************************
#ifndef polybori_common_CExtrusivePtr_h_
#define polybori_common_CExtrusivePtr_h_
// include basic definitions
#include <polybori/pbori_defs.h>
#include <algorithm> // std::swap
BEGIN_NAMESPACE_PBORI
template <class DataType, class ValueType>
class CExtrusivePtr {
typedef CExtrusivePtr self;
public:
typedef DataType data_type;
typedef ValueType value_type;
CExtrusivePtr(const data_type& data, value_type* ptr):
m_data(data), p_ptr(ptr) { lock(); }
CExtrusivePtr(const self& rhs):
m_data(rhs.m_data), p_ptr(rhs.p_ptr) { lock(); }
CExtrusivePtr():
m_data(), p_ptr(NULL) { }
~CExtrusivePtr() { release(); }
self& operator=(const self& rhs) {
self(rhs).swap(*this);
return *this;
}
const data_type& data() const { return m_data; }
value_type* get() const {
return p_ptr;
}
const value_type & operator*() const {
PBORI_ASSERT(p_ptr != NULL);
return *p_ptr;
}
value_type & operator*() {
PBORI_ASSERT(p_ptr != NULL);
return *p_ptr;
}
value_type* operator->() const {
PBORI_ASSERT(p_ptr != NULL);
return p_ptr;
}
void swap(self & rhs) {
std::swap(m_data, rhs.m_data);
std::swap(p_ptr, rhs.p_ptr);
}
protected:
void lock() {
extrusive_ptr_add_ref(data(), get());
}
void release() {
extrusive_ptr_release(data(), get());
}
data_type m_data;
value_type* p_ptr;
};
template <class Data1, class Type1, class Data2, class Type2>
inline bool
operator==(const CExtrusivePtr<Data1, Type1> & lhs,
const CExtrusivePtr<Data2, Type2> & rhs) {
return lhs.get() == rhs.get();
}
template <class Data1, class Type1, class Data2, class Type2>
inline bool
operator!=(const CExtrusivePtr<Data1, Type1> & lhs,
const CExtrusivePtr<Data2, Type2> & rhs) {
return lhs.get() != rhs.get();
}
template <class Data1, class Type1, class Type2>
inline bool
operator==(const CExtrusivePtr<Data1, Type1> & lhs, Type2 * rhs) {
return lhs.get() == rhs;
}
template <class Data1, class Type1, class Type2>
inline bool
operator!=(const CExtrusivePtr<Data1, Type1> & lhs, Type2* rhs) {
return lhs.get() != rhs;
}
template <class Type1, class Data2, class Type2>
inline bool
operator==(Type1* lhs, const CExtrusivePtr<Data2, Type2> & rhs) {
return lhs == rhs.get();
}
template <class Type1, class Data2, class Type2>
inline bool
operator!=(Type1* lhs, const CExtrusivePtr<Data2, Type2> & rhs) {
return lhs != rhs.get();
}
END_NAMESPACE_PBORI
#endif