IterationPack: General framework for building iterative algorithms  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Public Member Functions | List of all members
IterationPack::CastIQMember< T > Class Template Reference

Template class to be used to lookup an interation quantity, cast it to an IterQuantityAccess<T> object and cache the iq_id for fast access later. More...

#include <IterationPack_CastIQMember.hpp>

Inheritance diagram for IterationPack::CastIQMember< T >:
Inheritance graph
[legend]

Public Member Functions

 CastIQMember (const std::string iq_name)
 Construct with the name of an iteration quantity. More...
 
IterQuantityAccess< T > & operator() (AlgorithmState &s) const
 Get the iteration quantity from an AlgorithmState object. More...
 
const IterQuantityAccess< T > & operator() (const AlgorithmState &s) const
 
- Public Member Functions inherited from IterationPack::CastIQMemberBase
const std::string & iq_name () const
 Name returns the name of the iteration quantity. More...
 
bool exists_in (const AlgorithmState &s) const
 Returns if the iteration quantity exists in the state object. More...
 

Additional Inherited Members

- Protected Member Functions inherited from IterationPack::CastIQMemberBase
 CastIQMemberBase (const std::string iq_name)
 
void cache_iq_id (const AlgorithmState &s) const
 
void throw_cast_error (const AlgorithmState::iq_id_type iq_id, const std::string &iqa_name) const
 
- Protected Attributes inherited from IterationPack::CastIQMemberBase
const std::string iq_name_
 
AlgorithmState::iq_id_type iq_id_
 

Detailed Description

template<class T>
class IterationPack::CastIQMember< T >

Template class to be used to lookup an interation quantity, cast it to an IterQuantityAccess<T> object and cache the iq_id for fast access later.

The idea is that a Step class can create a data member of this type and then access and interation quantity and have the iq_id looked up the first time.

The best way to use this class to access an iteration quantity is for a header file to be created for the iteration quantity (or several iteration quantities) that contains a new class. For example, suppose we have an double object that we want to add as an iteration quantity with the name "x_step". For this we might create an header file like:

// /////////////////////////////////////////////////////////////////
// x_step_iter_quantity.h
#include "IterationPack_CastIQMember.hpp"
class x_step_iq_member : public CastIQMember<double> {
public:
x_step_iq_member() : CastIQMember<double>("x_step") {}
}

Now lets suppose we have two step classes that need to access this iteration quantity. These step classes would each include a data member of this new class. For example, these step classes might be implemented as:

// /////////////////////////////////////////////////////////////////
// MyStep1.h
#include "x_step_iter_quantity.h"
class MyStep1 : public AlgorithmStep {
public:
bool do_step( algo, ... )
{
AlgorithmState &s = algo.state();
x_step_(s).set_k(0) = 5.0;
}
private:
x_step_iq_member x_step_;
}
// /////////////////////////////////////////////////////////////////
// MyStep2.h
#include "x_step_iter_quantity.h"
class MyStep2 : public AlgorithmStep {
public:
bool do_step( algo, ... )
{
AlgorithmState &s = algo.state();
double x_step = x_step_(s).get_k(0);
cout << "\nx_step = " << x_step << std::endl;
}
private:
x_step_iq_member x_step_;
}

In the above example, an O(s.num_iter_quantities()) search for the iq_id would only be performed the first time x_step_(s) was called by each step object. In later iterations, the cached iq_id would be used to access the iteration quantity and the only price one would pay above a few O(1) function calls in is an O(1) dynamic cast.

The default constructor is not allowed by the default copy constructors and assignment operators are allowed since they have the correct semantics.

Definition at line 154 of file IterationPack_CastIQMember.hpp.

Constructor & Destructor Documentation

template<class T >
IterationPack::CastIQMember< T >::CastIQMember ( const std::string  iq_name)

Construct with the name of an iteration quantity.

Definition at line 178 of file IterationPack_CastIQMember.hpp.

Member Function Documentation

template<class T >
IterQuantityAccess< T > & IterationPack::CastIQMember< T >::operator() ( AlgorithmState s) const

Get the iteration quantity from an AlgorithmState object.

If the iteration quantity of the name iq_namt does not exist then a AlgorithmState::DoesNotExist exception will be thrown. If the type of the iteration quantity is not of the type IterQuantityAcess<T> (as determined by dynamic_cast<T>) then the exception InvalidTypeCastException: will be thrown with a helpful error message.

Definition at line 184 of file IterationPack_CastIQMember.hpp.

template<class T >
const IterQuantityAccess< T > & IterationPack::CastIQMember< T >::operator() ( const AlgorithmState s) const

Definition at line 198 of file IterationPack_CastIQMember.hpp.


The documentation for this class was generated from the following file: