Skip to content

Smart Pointers

Dusan Ciric edited this page Jan 19, 2018 · 7 revisions

Smart Pointers in RX Platform

#include "lib/rx_ptr.h"
using namespace rx::pointers 

RX Platform has custom implementation of smart pointers in a current version (Atom Ver 0.5.13). This implementation has the following properties:

  • Pointers are based on self contained reference counting. The basic principle can be seen on assignment operator of the smart pointer class:
template <class ptrT>
class reference : public basic_smart_ptr<ptrT>  
{

...

        // assignment operator
	reference<ptrT>& operator=(const reference<ptrT>& right)
	{
		if (this != &right)
		{
			if (this->_ptr)
				this->_ptr->release();
			this->_ptr = right._ptr;
			if (this->_ptr)
				this->_ptr->bind();
		}
		return *this;
	}

...

}
  • RX Platform smart pointer usage is based on the one counts the references principle in order to be able to easily move the referenced objects around different modules. This is a very elegant way of passing objects between threads because the threading infrastructure does have to know about every abstraction in advance. For example you can do something like this.
// declare forward
void accept_reference(reference<reference_object> ref);
// this function is not aware of class A
void send_object(reference<reference_object> ref)
{
	accept_reference(ref);
}

...

class A : public reference_object
{
};
// this function implementation knows definition of class A
void accept_reference(reference<reference_object> ref)
{
	reference<A> received = ref.cast_to<reference<A> >();
	if (received)
	{
	    ...
	}
}

// this function is knows definition of class A
void some_function()
{
	send_object(rx_create_reference<A>());
}