NOMAD Source  Version 4.0.0 Beta
StopReason< T > Class Template Reference

Template class for the stop reason of a stop type. More...

#include <StopReason.hpp>

Public Member Functions

 StopReason ()
 Constructor. More...
 
virtual ~StopReason ()
 Destructor. More...
 
get () const
 The stop reason. More...
 
void set (T s)
 Set the stop reason if it is listed in dictionnary. More...
 
void setStarted ()
 Reset the stop reason to the default STARTED state. More...
 
bool isStarted () const
 Check if it is in STARTED state. More...
 
std::string getStopReasonAsString () const
 Translate the stop reason into a string for display. More...
 
bool checkTerminate () const
 Check if the stop reason requires a termination. More...
 

Private Member Functions

std::map< T, std::string > & dict () const
 Dictionnary to translate a stop type into a string. More...
 
void testValidity () const
 Helper for constructor (check sanity) More...
 

Private Attributes

_stopReason
 The stop reason stored as a stop type. More...
 

Detailed Description

template<typename T>
class StopReason< T >

Template class for the stop reason of a stop type.

The possible stop types can be generic: IterStopType, EvalGlobalStopType, EvalMainThreadStopType, BaseStopType, or specific to an algorithm: MadsStopType, PhaseOneStopType, NMStopType ,....

The default stop type is STARTED (no stop). A stop reason different than STARTED indicates what is the cause of termination. Some stop reasons indicate a normal termination that do not need propagation and others must be propagated to stop an algorithm (see StopReason::checkTerminate()).

Definition at line 195 of file StopReason.hpp.

Constructor & Destructor Documentation

◆ StopReason()

template<typename T >
StopReason< T >::StopReason ( )
inlineexplicit

Constructor.

Upon construction, the validity of the stop reason is verified (sanity check).
By default the stop reason is set to STARTED.

Definition at line 246 of file StopReason.hpp.

247  {
248  testValidity();
249 
250  _stopReason = T::STARTED;
251 
252  }

◆ ~StopReason()

template<typename T >
virtual StopReason< T >::~StopReason ( )
inlinevirtual

Destructor.

Definition at line 255 of file StopReason.hpp.

255 {}

Member Function Documentation

◆ checkTerminate()

template<typename T >
bool StopReason< T >::checkTerminate ( ) const

Check if the stop reason requires a termination.

This is implemented for each stop type (template specialization of the function).
Except for EvalMainThreadStopType, a stop reason different than STARTED indicates that an algorithm or a sub-algorithm must terminate.

Returns
true if a termination is required, false otherwise.

◆ dict()

template<typename T >
std::map<T,std::string>& StopReason< T >::dict ( ) const
private

Dictionnary to translate a stop type into a string.

We have template specializations of this function for each stop type. This function is called to display the stop reason.

◆ get()

template<typename T >
T StopReason< T >::get ( ) const
inline

The stop reason.

Definition at line 258 of file StopReason.hpp.

259  {
260  return _stopReason;
261  }

◆ getStopReasonAsString()

template<typename T >
std::string StopReason< T >::getStopReasonAsString ( ) const
inline

Translate the stop reason into a string for display.

Definition at line 287 of file StopReason.hpp.

288  {
289  typename std::map<T,std::string>::iterator it = dict().find( _stopReason );
290  return it->second;
291  }

◆ isStarted()

template<typename T >
bool StopReason< T >::isStarted ( ) const
inline

Check if it is in STARTED state.

Definition at line 281 of file StopReason.hpp.

282  {
283  return ( _stopReason == T::STARTED );
284  }

◆ set()

template<typename T >
void StopReason< T >::set ( s)
inline

Set the stop reason if it is listed in dictionnary.

Definition at line 264 of file StopReason.hpp.

265  {
266  typename std::map<T,std::string>::iterator it = dict().find(s);
267 
268  if ( it==dict().end() )
269  throw Exception(__FILE__,__LINE__,"Stop reason not found.");
270 
271  _stopReason = s;
272  }

◆ setStarted()

template<typename T >
void StopReason< T >::setStarted ( )
inline

Reset the stop reason to the default STARTED state.

Definition at line 275 of file StopReason.hpp.

276  {
277  _stopReason = T::STARTED;
278  }

◆ testValidity()

template<typename T >
void StopReason< T >::testValidity ( ) const
inlineprivate

Helper for constructor (check sanity)

Definition at line 210 of file StopReason.hpp.

211  {
212  if ( ! std::is_enum<T>::value )
213  throw Exception(__FILE__,__LINE__,"The templated stop reason is not an enum.");
214 
215  if ( dict().size() == 0 )
216  throw Exception(__FILE__,__LINE__,"Dictionary not filled.");
217 
218 
219  if ( (int) T::STARTED != 0 )
220  throw Exception(__FILE__,__LINE__,"First StopType in enum must be STARTED.");
221 
222  if ( (int) T::LAST != dict().size() )
223  {
224  std::string s = "Not enough elements in enum dictionary (";
225  s += std::to_string(dict().size()) + "), expecting " + std::to_string((int)T::LAST);
226  throw Exception(__FILE__,__LINE__,s);
227  }
228 
229  for (int i = (int) T::STARTED; i < (int ) T::LAST; i++)
230  {
231  typename std::map<T,std::string>::iterator it = dict().find( (T) i );
232 
233  if ( it== dict().end() )
234  throw Exception(__FILE__,__LINE__,"All enum elements must be in dictionary.");
235 
236  }
237 
238  }

Member Data Documentation

◆ _stopReason

template<typename T >
T StopReason< T >::_stopReason
private

The stop reason stored as a stop type.

Definition at line 200 of file StopReason.hpp.


The documentation for this class was generated from the following file:
StopReason::dict
std::map< T, std::string > & dict() const
Dictionnary to translate a stop type into a string.
StopReason::testValidity
void testValidity() const
Helper for constructor (check sanity)
Definition: StopReason.hpp:210
Exception
Exception utility.
Definition: WriteAttributeDefinitionFile.cpp:86
StopReason::_stopReason
T _stopReason
The stop reason stored as a stop type.
Definition: StopReason.hpp:200