20 #if !defined(__MITSUBA_CORE_REF_H_)
21 #define __MITSUBA_CORE_REF_H_
40 template <
typename T>
class ref {
43 ref() : m_ptr(NULL) { }
46 ref(T *ptr) : m_ptr(ptr) {
if (m_ptr) ((
Object *) m_ptr)->incRef(); }
49 ref(
const ref &pRef) : m_ptr(pRef.m_ptr) {
if (m_ptr) ((
Object *) m_ptr)->incRef(); }
59 ((
Object *) m_ptr)->decRef();
62 ((
Object *) m_ptr)->incRef();
71 ((
Object *) m_ptr)->decRef();
74 ((
Object *) m_ptr)->incRef();
79 inline bool operator== (
const ref &pref)
const {
return (m_ptr == pref.m_ptr); }
82 inline bool operator!= (
const ref &pref)
const {
return (m_ptr != pref.m_ptr); }
85 inline bool operator== (
const T* ptr)
const {
return (m_ptr == ptr); }
88 inline bool operator!= (
const T* ptr)
const {
return (m_ptr != ptr); }
91 inline bool operator!()
const {
return (m_ptr == NULL); }
106 inline operator T* () {
return m_ptr; }
109 inline T*
get() {
return m_ptr; }
112 inline const T*
get()
const {
return m_ptr; }
120 T::m_theClass->getName().c_str());
123 m_ptr->getClass()->getName().c_str(),
124 m_ptr->getRefCount(), m_ptr->toString().c_str());
133 template <
typename T>
struct ref_comparator {
134 bool operator() (
const ref<T>& lhs,
const ref<T>& rhs)
const {
135 return lhs.
get() < rhs.
get();
144 template <
typename T>
class ref_vector :
public std::vector< ref<T> > {
156 typename parent_type::iterator it1 = this->begin(), it2 = this->begin();
157 for (; it1 < this->end(); ++it1) {
158 if (seen.find(it1->get()) != seen.end())
160 seen.insert(it1->get());
166 this->erase(it2, this->end());
171 for (
typename parent_type::const_iterator it = this->begin();
172 it != this->end(); ++it)
173 if (it->get() == ptr)
MTS_EXPORT_CORE std::string formatString(const char *pFmt,...)
Wrapped snprintf.
void ensureUnique()
Remove all duplicates without changing the order.
Definition: ref.h:153
~ref()
Destroy this reference.
Definition: ref.h:52
ref & operator=(const ref &r)
Overwrite this reference with another reference.
Definition: ref.h:55
std::vector< ref< T > > parent_type
Definition: ref.h:146
ref_vector()
Definition: ref.h:148
Simple reference-counted vector container based on std::vector and ref.
Definition: ref.h:144
ref_vector(size_t size)
Definition: ref.h:149
bool contains(const T *ptr) const
Check if a certain pointer is contained in the vector.
Definition: ref.h:170
T & operator*()
Return a C++ reference to the referenced object.
Definition: ref.h:100
T * operator->()
Access the object referenced by this reference.
Definition: ref.h:94
ref()
Create a NULL reference.
Definition: ref.h:43
std::string toString() const
Definition: ref.h:117
ref_vector(const ref_vector &vec)
Definition: ref.h:150
const T & operator*() const
Return a C++ reference to the referenced object.
Definition: ref.h:103
bool operator==(const ref &pref) const
Compare this reference with another reference.
Definition: ref.h:79
Reference counting helper.
Definition: ref.h:40
ref(T *ptr)
Construct a reference from a pointer.
Definition: ref.h:46
T * get()
Return a pointer to the referenced object.
Definition: ref.h:109
bool operator!=(const ref &pref) const
Compare this reference with another reference.
Definition: ref.h:82
Parent of all Mitsuba classes.
Definition: object.h:38
ref(const ref &pRef)
Copy-constructor.
Definition: ref.h:49
bool operator!() const
Check whether this is a NULL reference.
Definition: ref.h:91