Commit 02ac4e77 authored by berenger-bramas's avatar berenger-bramas

Add comments to the smart pointer class.

git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/scalfmm/scalfmm/trunk@382 2616d619-271b-44dc-8df4-d4a8f33a7222
parent 00014c51
#ifndef FSMARTPOINTER_HPP #ifndef FSMARTPOINTER_HPP
#define FSMARTPOINTER_HPP #define FSMARTPOINTER_HPP
/** This class is a basic smart pointer class
*
*/
template <class ClassType> template <class ClassType>
class FSmartPointer { class FSmartPointer {
ClassType* pointer; ClassType* pointer; //< The pointer to the memory area
int* counter; int* counter; //< Reference counter
public: public:
// The type of data managed by the pointer
typedef ClassType ValueType; typedef ClassType ValueType;
/** Empty constructor */
FSmartPointer() : pointer(0), counter(0) { FSmartPointer() : pointer(0), counter(0) {
} }
/** Constructor from the memory pointer */
FSmartPointer(ClassType* const inPointer) : pointer(0), counter(0) { FSmartPointer(ClassType* const inPointer) : pointer(0), counter(0) {
assign(inPointer); assign(inPointer);
} }
/** Constructor from a smart pointer */
FSmartPointer(const FSmartPointer& inPointer) : pointer(0), counter(0) { FSmartPointer(const FSmartPointer& inPointer) : pointer(0), counter(0) {
assign(inPointer); assign(inPointer);
} }
/** Destructor, same as release */
~FSmartPointer(){ ~FSmartPointer(){
release(); release();
} }
/** Assign operator, same as assign */
void operator=(ClassType* const inPointer){ void operator=(ClassType* const inPointer){
assign(inPointer); assign(inPointer);
} }
/** Assign operator, same as assign */
void operator=(const FSmartPointer& inPointer){ void operator=(const FSmartPointer& inPointer){
assign(inPointer); assign(inPointer);
} }
/** Point to a new pointer, release if needed */
void assign(ClassType* const inPointer){ void assign(ClassType* const inPointer){
release(); release();
pointer = inPointer; pointer = inPointer;
...@@ -39,6 +50,7 @@ public: ...@@ -39,6 +50,7 @@ public:
(*counter) = 1; (*counter) = 1;
} }
/** Point to a new pointer, release if needed */
void assign(const FSmartPointer& inPointer){ void assign(const FSmartPointer& inPointer){
release(); release();
pointer = inPointer.pointer; pointer = inPointer.pointer;
...@@ -46,6 +58,7 @@ public: ...@@ -46,6 +58,7 @@ public:
if(counter) (*counter) = (*counter) + 1; if(counter) (*counter) = (*counter) + 1;
} }
/** Dec counter and Release the memory last */
void release(){ void release(){
if(counter){ if(counter){
(*counter) = (*counter) - 1; (*counter) = (*counter) - 1;
...@@ -57,50 +70,62 @@ public: ...@@ -57,50 +70,62 @@ public:
} }
} }
/** To know if the smart pointer is pointing to a memory */
bool isAssigned() const{ bool isAssigned() const{
return pointer != 0; return pointer != 0;
} }
/** Is last => counter == 0 */
bool isLast() const{ bool isLast() const{
return counter && (*counter) == 1; return counter && (*counter) == 1;
} }
/** Get the direct pointer */
ClassType* getPtr(){ ClassType* getPtr(){
return pointer; return pointer;
} }
/** Get the direct pointer */
const ClassType* getPtr() const { const ClassType* getPtr() const {
return pointer; return pointer;
} }
/** Operator [] */
ClassType& operator[](const int& index){ ClassType& operator[](const int& index){
return pointer[index]; return pointer[index];
} }
/** Operator [] */
const ClassType& operator[](const int& index) const { const ClassType& operator[](const int& index) const {
return pointer[index]; return pointer[index];
} }
/** Operator * return a reference to the pointer object */
ClassType& operator*(){ ClassType& operator*(){
return (*pointer); return (*pointer);
} }
/** Operator * return a reference to the pointer object */
const ClassType& operator*() const { const ClassType& operator*() const {
return (*pointer); return (*pointer);
} }
/** Return the pointing address */
ClassType* operator->(){ ClassType* operator->(){
return pointer; return pointer;
} }
/** Return the pointing address */
const ClassType* operator->() const { const ClassType* operator->() const {
return pointer; return pointer;
} }
/** To cast to class type pointer */
operator const ClassType*() const { operator const ClassType*() const {
return pointer; return pointer;
} }
/** To cast to class type pointer */
operator ClassType*() { operator ClassType*() {
return pointer; return pointer;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment