Resource< T, SourceTrait > Class Template Reference

This is the basic template that underlies all of the Resource capability. More...

#include <Resource.h>

Inheritance diagram for Resource< T, SourceTrait >:

Inheritance graph

List of all members.

Public Member Functions

 Resource (const Args &args=Args())
 Resource (T *pObject, const Args &args=Args())
 Resource (const Resource< T, SourceTrait > &source)
Resource< T, SourceTrait > & operator= (const Resource< T, SourceTrait > &source)
virtual ~Resource ()
const ArgsgetArgs () const
ArgsgetArgs ()
const T * get () const
T * get ()
const T * operator-> () const
T * operator-> ()
const T & operator* () const
T & operator* ()
const T & operator[] (int index) const
T & operator[] (int index)
const T * release () const
T * release ()

Protected Types

typedef SourceTrait::Args Args

Protected Member Functions

void destroyIfOwned ()


Detailed Description

template<class T, class SourceTrait = MemoryObject<T>>
class Resource< T, SourceTrait >

This is the basic template that underlies all of the Resource capability.

The Resource template is effectively an auto_ptr with a trait describing how to obtain and release the resource. The default trait is MemoryObject, which results in identical syntax and functionality to an auto_ptr. It can also be used to manage FILE *, objects and vectors created via the Object Factory and spectral elements created via ModelServices. The following examples show creation (and upon exit from the code block - destruction) of each of these types.

 {
    // Allocate a PassThru object from the heap. The PassThru object will be deleted on destruction of the Resource
    Resource<struct PassThru> pMyObject;
    Resource<struct PassThru> pMyObject2(new PassThru); // alternate syntax to the above line

    // Open a file for reading. On destruction of the FileResource, the file will be closed
    Resource<FILE,FileObject> pMyFile(FileObject::Args("e:\\application\\configure.in", "r"));
    FileResource pMyFile2(FileObject::Args("e:\\application\\configure", "r")); // alternate syntax to the above line
    FileResource pMyFile3("e:\\application\\configure.out", "r"); // alternate syntax to the above line
 
    // Create a Filename object via the ObjectFactory. On destruction of the FactoryResource, the 
    // object will be destroyed via the ObjectFactory.
    Resource<Filename,FactoryObject> pMyFilename(FactoryArgs("Filename"));
    FactoryResource<Filename> pMyFilename2(); // alternate syntax to the above line
 
    // Create an empty vector of Filename objects via the ObjectFactory.
    Resource<vector<Filename*>,FactoryVector> pMyVector(FactoryArgs("Filename"));
 
    // Create a DataElement via ModelServices. On destruction of the ModelResource, the 
    // object will be destroyed via ModelServices.
    ModelResource<AoiElement> pMyAoi2("MyAoi", mpRasterElement);
 } // closes all 3 files and deletes the Filename, vector, AOI, and PassThru objects

Additional traits can be defined as needed. The requirements for a traits class are as follows. 1) It must have a default constructor 2) It must define a nested struct/class/typedef called Args 3) It must define a method called obtainResource taking an Args object as its only non-default argument and returning a value that can be cast to a (T*). If this call fails, the obtainResource method should throw an exception to prevent the creation of an invalid Resource object. 4) It must define a method called releaseResource taking an Args object and a T* as its only non-default arguments The only requirement on the nested 'Args' struct/class is that it have a copy constructor.

Parameters:
T The type of the object the Resource wraps
SourceTrait The type of object to use for obtaining and releasing objects of type T

Definition at line 85 of file Resource.h.


Member Typedef Documentation

template<class T, class SourceTrait = MemoryObject<T>>
typedef SourceTrait::Args Resource< T, SourceTrait >::Args [protected]

Definition at line 88 of file Resource.h.


Constructor & Destructor Documentation

template<class T, class SourceTrait = MemoryObject<T>>
Resource< T, SourceTrait >::Resource ( const Args args = Args()  )  [explicit]

Constructs the Resource object.

Constructs the Resource object by storing the args and by calling obtainResource on the SourceTrait. If no argument is provided, it will construct the Resource using the SourceTrait's default Args object.

Parameters:
args The arguments that will be provided to the obtainResource method of the SourceTrait. The type of this argument is dependent on the SourceTrait.

Definition at line 102 of file Resource.h.

template<class T, class SourceTrait = MemoryObject<T>>
Resource< T, SourceTrait >::Resource ( T *  pObject,
const Args args = Args() 
) [explicit]

Constructs the Resource object.

Constructs the Resource object based on an existing object. Essentially, this wraps the object in the Resource and assigns the Resource object the responsibility for freeing the underlying object.

Parameters:
pObject The object to wrap in the Resource.
args The arguments that would have been provided to the obtainResource method of the SourceTrait. The type of this argument is dependent on the SourceTrait.

Definition at line 119 of file Resource.h.

template<class T, class SourceTrait = MemoryObject<T>>
Resource< T, SourceTrait >::Resource ( const Resource< T, SourceTrait > &  source  ) 

Copy-constructs the Resource object.

Copy-constructs a Resource object from an existing Resource object. The new Resource object takes ownership of the wrapped object from the source object.

Parameters:
source The Resource object to construct from. After this call, the source no longer owns the wrapped object.

Definition at line 133 of file Resource.h.

template<class T, class SourceTrait = MemoryObject<T>>
virtual Resource< T, SourceTrait >::~Resource (  )  [virtual]

Destructs a Resource object.

If the Resource object owns the wrapped object, the wrapped object will be freed via the SourceTrait's releaseResource method.

Definition at line 172 of file Resource.h.


Member Function Documentation

template<class T, class SourceTrait = MemoryObject<T>>
Resource<T, SourceTrait>& Resource< T, SourceTrait >::operator= ( const Resource< T, SourceTrait > &  source  ) 

Sets a Resource object to another one.

The assigned object takes ownership of the wrapped object from the source object.

Parameters:
source The Resource object to assign from. After this call, the source no longer owns the wrapped object.

Definition at line 147 of file Resource.h.

template<class T, class SourceTrait = MemoryObject<T>>
const Args& Resource< T, SourceTrait >::getArgs (  )  const

Gets the args that were used when the Resource was created.

Returns:
The args that were used when the Resource was created.

Definition at line 182 of file Resource.h.

template<class T, class SourceTrait = MemoryObject<T>>
Args& Resource< T, SourceTrait >::getArgs (  ) 

Gets the args that were used when the Resource was created.

Returns:
The args that were used when the Resource was created.

Definition at line 192 of file Resource.h.

template<class T, class SourceTrait = MemoryObject<T>>
const T* Resource< T, SourceTrait >::get (  )  const

Gets a pointer to the underlying object.

Returns:
A pointer to the underlying object.

Definition at line 202 of file Resource.h.

template<class T, class SourceTrait = MemoryObject<T>>
T* Resource< T, SourceTrait >::get (  ) 

Gets a pointer to the underlying object.

Returns:
A pointer to the underlying object.

Definition at line 212 of file Resource.h.

template<class T, class SourceTrait = MemoryObject<T>>
const T* Resource< T, SourceTrait >::operator-> (  )  const

Gets a pointer to the underlying object.

Returns:
A pointer to the underlying object.

Definition at line 222 of file Resource.h.

template<class T, class SourceTrait = MemoryObject<T>>
T* Resource< T, SourceTrait >::operator-> (  ) 

Gets a pointer to the underlying object.

Returns:
A pointer to the underlying object.

Definition at line 232 of file Resource.h.

template<class T, class SourceTrait = MemoryObject<T>>
const T& Resource< T, SourceTrait >::operator* (  )  const

Gets a reference to the underlying object.

Returns:
A reference to the underlying object.

Definition at line 242 of file Resource.h.

template<class T, class SourceTrait = MemoryObject<T>>
T& Resource< T, SourceTrait >::operator* (  ) 

Gets a reference to the underlying object.

Returns:
A reference to the underlying object.

Definition at line 252 of file Resource.h.

template<class T, class SourceTrait = MemoryObject<T>>
const T& Resource< T, SourceTrait >::operator[] ( int  index  )  const

Returns a reference to the indexed object.

Indexes into the underlying array and returns a reference to the indexed object. This is only useful if the SourceTrait's obtainResource and releaseResource work with arrays of objects. In other cases, calling this method with an index of other than 0 will cause undefined behavior.

Parameters:
index The index of the object in the underlying array.
Returns:
A reference to the underlying indexed object.

Definition at line 272 of file Resource.h.

template<class T, class SourceTrait = MemoryObject<T>>
T& Resource< T, SourceTrait >::operator[] ( int  index  ) 

Returns a reference to the indexed object.

Indexes into the underlying array and returns a reference to the indexed object. This is only useful if the SourceTrait's obtainResource and releaseResource work with arrays of objects. In other cases, calling this method with an index of other than 0 will cause undefined behavior.

Parameters:
index The index of the object in the underlying array.
Returns:
A reference to the underlying indexed object.

Definition at line 292 of file Resource.h.

template<class T, class SourceTrait = MemoryObject<T>>
const T* Resource< T, SourceTrait >::release (  )  const

Removes ownership of the underlying resource and returns it.

Gets a pointer to the underlying object and removes ownership of the underlying object. After this call, the Resource object no longer owns the underlying object.

Returns:
A pointer to the underlying object.

Definition at line 306 of file Resource.h.

template<class T, class SourceTrait = MemoryObject<T>>
T* Resource< T, SourceTrait >::release (  ) 

Removes ownership of the underlying resource and returns it.

Gets a pointer to the underlying object and removes ownership of the underlying object. After this call, the Resource object no longer owns the underlying object.

Returns:
A pointer to the underlying object.

Definition at line 321 of file Resource.h.

template<class T, class SourceTrait = MemoryObject<T>>
void Resource< T, SourceTrait >::destroyIfOwned (  )  [protected]

Destroys the underlying resource.

If the underlying object is owned by the Resource, this method destroys it via the SourceTrait's releaseResource method. After destroying it, the Resource object is empty.

Definition at line 335 of file Resource.h.


Software Development Kit - Opticks 4.9.0 Build 16218