STK++ 1.0
STK::Option Class Reference

the Option class allow to store the value of an option from a IPage. More...

#include <STK_Option.h>

Collaboration diagram for STK::Option:

List of all members.

Public Types

enum  TypeOption {
  unknown_ = 0, string_, real_, integer_,
  range_, lstring_, lreal_, linteger_,
  lrange_, page_
}
 A TypeOption is the kind of options that can read or write a program in a file. More...

Public Member Functions

 Option (String const &name, TypeOption type=string_, bool isOptional=true)
 constructor.
 Option (IPage const &page)
 Special constructor.
 Option (Option const &opt)
 Copy constructor.
 ~Option ()
 Destructor.
Optionoperator= (const Option &opt)
 Assignment operator.
String const & name () const
 name of the option.
TypeOption type () const
 type of the option.
bool isOptional () const
 check if the option is optional of the option.
void setListSeparator (Char const &sep)
 set the separator to use in the list of options.
void write (ostream &os) const
 write out the options in the output stream
void read (istream &is)
 read in the options from the input stream.
bool setValue (String const &str)
 Convert a string in a value.
void setPage (IPage const &value)
 set a value from a Page.
String const & get (String const &value) const
 get the option as a String
Real const & get (Real const &value) const
 get the option as a Real
Integer const & get (Integer const &value) const
 get the option as an Integer
Range const & get (Range const &value) const
 get the option as a Range
std::list< String > const & get (std::list< String > const &value) const
 get the option as a list of String
std::list< Real > const & get (std::list< Real > const &value) const
 get the option as a list of Real
std::list< Integer > const & get (std::list< Integer > const &value) const
 get the option as a list of Integer
std::list< Range > const & get (std::list< Range > const &value) const
 get the option as a list of Range
IPage const & get (IPage const &value) const
 get the option as a page

Protected Member Functions

void set (String const &value)
 set a value from string.
void set (Real const &value)
 set a value from a Real.
void set (Integer const &value)
 set a value from an Integer.
void set (Range const &value)
 set a value from a Range.
void set (std::list< String > const &value)
 set a value from a list of String.
void set (std::list< Real > const &value)
 set a value from a list of Real.
void set (std::list< Integer > const &value)
 set a value from a list of Integer.
void set (std::list< Range > const &value)
 set a value from a list of Range.

Private Member Functions

void deleteValue ()
 Remove the value of the option.
void setDefaultValue ()
 set a default value of the option.

Private Attributes

String name_
 name of the option
Char sep_
 Char used for the option list.
TypeOption type_
 type of the option
bool isOptional_
 true if the option is optional, false otherwise
bool isValued_
 true if the option is valued, false otherwise.
union {
   String *   p_String_
   Real *   p_Real_
   Integer *   p_Integer_
   Range *   p_Range_
   std::list< String > *   p_lString_
   std::list< Real > *   p_lReal_
   std::list< Integer > *   p_lInteger_
   std::list< Range > *   p_lRange_
   IPage *   p_Page_
}; 

Detailed Description

the Option class allow to store the value of an option from a IPage.

An Option is essentially a variant type. TODO add error message handling

Definition at line 63 of file STK_Option.h.


Constructor & Destructor Documentation

STK::Option::Option ( String const &  name,
TypeOption  type = string_,
bool  isOptional = true 
)

constructor.

Parameters:
namethe name of the Option
typethe type of the Option
isOptionaltrue if the parameter is optional, false otherwise

Definition at line 49 of file STK_Option.cpp.

References name_, setDefaultValue(), and STK::toUpperString().

Here is the call graph for this function:

STK::Option::Option ( IPage const &  page)

Special constructor.

This will construct an option as a sub-page. The name of the option will be the name of the page.

Parameters:
pagethe page to set as option

Definition at line 65 of file STK_Option.cpp.

References name_, and STK::toUpperString().

              : name_(page.name())
              , sep_(CHAR_SEP)
              , type_(page_)
              , isOptional_(page.isOptional())
              , isValued_(true)
              , p_Page_(page.clone())
{
  toUpperString(name_);
}

Here is the call graph for this function:

STK::Option::Option ( Option const &  opt)

Copy constructor.

Parameters:
optthe Option to copy

Definition at line 79 of file STK_Option.cpp.

References integer_, linteger_, lrange_, lreal_, lstring_, p_Integer_, p_lInteger_, p_lRange_, p_lReal_, p_lString_, p_Page_, p_Range_, p_Real_, p_String_, page_, range_, real_, setPage(), string_, type_, and unknown_.

              : name_(opt.name_)
              , sep_(opt.sep_)
              , type_(opt.type_)
              , isOptional_(opt.isOptional_)
              , isValued_(opt.isOptional_)
              , p_String_(0)
{
  // copy rhs value
  switch (type_)
  {
    case string_:
      if (opt.p_String_) set(*(opt.p_String_));
      else p_String_ = 0;
      break;
    case real_:
      if (opt.p_Real_) set(*(opt.p_Real_));
      else p_Real_ = 0;
      break;
    case integer_:
      if (opt.p_Integer_) set(*(opt.p_Integer_));
      else p_Integer_ = 0;
      break;
    case range_:
      if (opt.p_Range_) set(*(opt.p_Range_));
      else p_Range_ = 0;
      break;
    case lstring_:
      if (opt.p_lString_) set(*(opt.p_lString_));
      else p_lString_ = 0;
      break;
    case lreal_:
      if (opt.p_lReal_) set(*(opt.p_lReal_));
      else p_lReal_ = 0;
      break;
    case linteger_:
      if (opt.p_lInteger_) set(*(opt.p_lInteger_));
      else p_lInteger_ = 0;
      break;
    case lrange_:
      if (opt.p_lRange_) set(*(opt.p_lRange_));
      else p_lRange_ = 0;
      break;
    case page_:
      if (opt.p_Page_) setPage(*(opt.p_Page_));
      else p_Page_ = 0;
      break;
    case unknown_:
      runtime_error("Error in Option(opt). Unknown type option.\n");
  };
}

Here is the call graph for this function:

STK::Option::~Option ( )

Destructor.

Definition at line 134 of file STK_Option.cpp.

References deleteValue().

Here is the call graph for this function:


Member Function Documentation

Option & STK::Option::operator= ( const Option opt)

Assignment operator.

We need to overload this operator otherwise the variant union containing the value of the Option will not be copied correctly.

Parameters:
optthe Option to copy
Returns:
a reference o this

Definition at line 140 of file STK_Option.cpp.

References integer_, isOptional_, isValued_, linteger_, lrange_, lreal_, lstring_, name_, p_Integer_, p_lInteger_, p_lRange_, p_lReal_, p_lString_, p_Page_, p_Range_, p_Real_, p_String_, page_, range_, real_, sep_, setPage(), string_, type_, and unknown_.

{
  // Do the assignment operation of the members
  name_ = opt.name_;
  sep_ = opt.sep_;
  type_ = opt.type_;
  isOptional_ = opt.isOptional_;
  isValued_ = opt.isValued_;
  // copy rhs value
  switch (type_)
  {
    case string_:
      if (opt.p_String_) set(*(opt.p_String_));
      else p_String_ = 0;
      break;
    case real_:
      if (opt.p_Real_) set(*(opt.p_Real_));
      else p_Real_ = 0;
      break;
    case integer_:
      if (opt.p_Integer_) set(*(opt.p_Integer_));
      else p_Integer_ = 0;
      break;
    case range_:
      if (opt.p_Range_) set(*(opt.p_Range_));
      else p_Range_ = 0;
      break;
    case lstring_:
      if (opt.p_lString_) set(*(opt.p_lString_));
      else p_lString_ = 0;
      break;
    case lreal_:
      if (opt.p_lReal_) set(*(opt.p_lReal_));
      else p_lReal_ = 0;
      break;
    case linteger_:
      if (opt.p_lInteger_) set(*(opt.p_lInteger_));
      else p_lInteger_ = 0;
      break;
    case lrange_:
      if (opt.p_lRange_) set(*(opt.p_lRange_));
      else p_lRange_ = 0;
      break;
    case page_:
      if (opt.p_Page_) setPage(*(opt.p_Page_));
      else p_Page_ = 0;
      break;
    case unknown_:
      runtime_error("Error in Option::operator=(opt). Unknown type option.\n");
  };
  // return this
  return *this;  // Return a reference to myself.
}

Here is the call graph for this function:

String const& STK::Option::name ( ) const [inline]

name of the option.

If the option is a IPage, the name have to be the keyword.

Returns:
the name of the option

Definition at line 124 of file STK_Option.h.

References name_.

{ return name_;}
TypeOption STK::Option::type ( ) const [inline]

type of the option.

Returns:
the type of the option

Definition at line 128 of file STK_Option.h.

References type_.

{ return type_;}
bool STK::Option::isOptional ( ) const [inline]

check if the option is optional of the option.

Returns:
ture if the option s optional, false otherwise

Definition at line 132 of file STK_Option.h.

References isOptional_.

{ return isOptional_;}
void STK::Option::setListSeparator ( Char const &  sep) [inline]

set the separator to use in the list of options.

Parameters:
septhe separator to use

Definition at line 137 of file STK_Option.h.

References sep_.

    { sep_ = sep;}
void STK::Option::write ( ostream os) const

write out the options in the output stream

Parameters:
osoutput stream

Definition at line 272 of file STK_Option.cpp.

References STK::CHAR_EQUAL, integer_, linteger_, lrange_, lreal_, lstring_, name_, p_Integer_, p_lInteger_, p_lRange_, p_lReal_, p_lString_, p_Page_, p_Range_, p_Real_, p_String_, page_, range_, real_, sep_, string_, STK::STRING_BLANK, type_, unknown_, STK::IPage::write(), and STK::DManager::writeList().

{
  // write option name if it's not a page
  if (type_ != page_)
  {  // write name and " = "
    os << name_ << STRING_BLANK << CHAR_EQUAL << STRING_BLANK;
  }
  // write option value
  switch (type_)
  {
    case string_:
      if (p_String_)
        os << *p_String_;
      break;
    case real_:
      if (p_Real_)
        os << *p_Real_;
      break;
    case integer_:
      if (p_Integer_)
        os << *p_Integer_;
      break;
    case range_:
      if (p_Range_)
        os << *p_Range_;
      break;
    case lstring_:
      if (p_lString_)
        DManager::writeList(os, *p_lString_, sep_);
      break;
    case lreal_:
      if (p_lReal_)
      DManager::writeList(os, *p_lReal_, sep_);
      break;
    case linteger_:
      if (p_lInteger_)
        DManager::writeList(os, *p_lInteger_, sep_);
      break;
    case lrange_:
      if (p_lRange_)
        DManager::writeList(os, *p_lRange_, sep_);
      break;
    case page_:
      if (p_Page_)
        p_Page_->write(os);
      break;
    case unknown_:
      runtime_error("Error in Option::setValue(value). Unknown type option.\n");

  };
}

Here is the call graph for this function:

void STK::Option::read ( istream is)

read in the options from the input stream.

This method is only used for reading the pages options. All the other options are read outside and set using the set and setValue methods.

Parameters:
isthe input stream to read

Definition at line 327 of file STK_Option.cpp.

References p_Page_, page_, STK::IPage::read(), type_, and STK::IPage::validate().

{
  // read option value
  if (type_ == page_)
  {
      if (p_Page_)
      {
        p_Page_->read(is);
        if (!p_Page_->validate())
        {
          throw runtime_error("Error in Option::read(is) <A sub-page is not validated>.\n");
        }
      }
  };
}

Here is the call graph for this function:

bool STK::Option::setValue ( String const &  str)

Convert a string in a value.

The conversion is done using the field type_ of the class.

Parameters:
strthe string to convert
Returns:
true if the conversion success, false otherwise

Definition at line 197 of file STK_Option.cpp.

References integer_, linteger_, lrange_, lreal_, lstring_, page_, range_, STK::DManager::readList(), real_, string_, STK::stringToType(), type_, and unknown_.

{
  // list of values
  Range rangeValue;
  std::list<String> lStringValue;
  std::list<Real> lRealValue;
  std::list<Integer> lIntegerValue;
  std::list<Range> lRangeValue;
  // choose type
  switch (type_)
  {
    case string_:
      set(str);
      break;
    case real_:
      Real realValue;
      if (stringToType(realValue, str))
        set(realValue);
      else
        runtime_error("Error in Option::setValue(value). Conversion failed.\n");
      break;
    case integer_:
      Integer integerValue;
      if (stringToType(integerValue, str))
        set(integerValue);
      else
        runtime_error("Error in Option::setValue(value). Conversion failed.\n");
      break;
    case range_:
      if (stringToType(rangeValue, str))
      { set(rangeValue);}
      else
        runtime_error("Error in Option::setValue(value). Conversion failed.\n");
      break;
    case lstring_:
      DManager::readList(str, lStringValue);
      set(lStringValue);
      break;
    case lreal_:
      DManager::readList(str, lRealValue);
      set(lRealValue);
      break;
    case linteger_:
      DManager::readList(str, lIntegerValue);
      set(lIntegerValue);
      break;
    case lrange_:
      DManager::readList(str, lRangeValue);
      set(lRangeValue);
      break;
    case page_:
      runtime_error("Error in Option::setValue(value). Page option.\n");
      break;
    case unknown_:
      runtime_error("Error in Option::setValue(value). Unknown type option.\n");
  };
  // error if an error occur in readList ?
  return true;
}

Here is the call graph for this function:

void STK::Option::setPage ( IPage const &  value)

set a value from a Page.

The field type_ of the class is set to DManager::page_.

Parameters:
valuethe Page to set

Definition at line 260 of file STK_Option.cpp.

References STK::IPage::clone(), deleteValue(), isValued_, p_Page_, page_, and type_.

Referenced by operator=(), and Option().

{
  deleteValue();
  p_Page_ = value.clone();
  isValued_ = true;
  type_ = page_;
}

Here is the call graph for this function:

String const& STK::Option::get ( String const &  value) const [inline]

get the option as a String

Parameters:
valueany String (will not be used)

Definition at line 168 of file STK_Option.h.

References p_String_.

    { return *p_String_;}
Real const& STK::Option::get ( Real const &  value) const [inline]

get the option as a Real

Parameters:
valueany Real (will not be used)

Definition at line 173 of file STK_Option.h.

References p_Real_.

    { return *p_Real_;}
Integer const& STK::Option::get ( Integer const &  value) const [inline]

get the option as an Integer

Parameters:
valueany Integer (will not be used)

Definition at line 178 of file STK_Option.h.

References p_Integer_.

    { return *p_Integer_;}
Range const& STK::Option::get ( Range const &  value) const [inline]

get the option as a Range

Parameters:
valueany Range (will not be used)

Definition at line 183 of file STK_Option.h.

References p_Range_.

    { return *p_Range_;}
std::list<String> const& STK::Option::get ( std::list< String > const &  value) const [inline]

get the option as a list of String

Parameters:
valueany list of String (will not be used)

Definition at line 188 of file STK_Option.h.

References p_lString_.

    { return *p_lString_;}
std::list<Real> const& STK::Option::get ( std::list< Real > const &  value) const [inline]

get the option as a list of Real

Parameters:
valueany list of Real (will not be used)

Definition at line 193 of file STK_Option.h.

References p_lReal_.

    { return *p_lReal_;}
std::list<Integer> const& STK::Option::get ( std::list< Integer > const &  value) const [inline]

get the option as a list of Integer

Parameters:
valueany list of Integer (will not be used)

Definition at line 198 of file STK_Option.h.

References p_lInteger_.

    { return *p_lInteger_;}
std::list<Range> const& STK::Option::get ( std::list< Range > const &  value) const [inline]

get the option as a list of Range

Parameters:
valueany list of Range (will not be used)

Definition at line 203 of file STK_Option.h.

References p_lRange_.

    { return *p_lRange_;}
IPage const& STK::Option::get ( IPage const &  value) const [inline]

get the option as a page

Parameters:
valueany page (will not be used)

Definition at line 208 of file STK_Option.h.

References p_Page_.

    { return *p_Page_;}
void STK::Option::set ( String const &  value) [protected]

set a value from string.

Parameters:
valuethe string value to set

Definition at line 346 of file STK_Option.cpp.

References deleteValue(), isValued_, and p_String_.

{
  deleteValue();
  p_String_ = new String(value);
  isValued_ = true;
}

Here is the call graph for this function:

void STK::Option::set ( Real const &  value) [protected]

set a value from a Real.

Parameters:
valuethe Real value to set

Definition at line 356 of file STK_Option.cpp.

References deleteValue(), isValued_, and p_Real_.

{
  deleteValue();
  p_Real_ = new Real(value);
  isValued_ = true;
}

Here is the call graph for this function:

void STK::Option::set ( Integer const &  value) [protected]

set a value from an Integer.

Parameters:
valuethe Integer value to set

Definition at line 366 of file STK_Option.cpp.

References deleteValue(), isValued_, and p_Integer_.

{
  deleteValue();
  p_Integer_ = new Integer(value);
  isValued_ = true;
}

Here is the call graph for this function:

void STK::Option::set ( Range const &  value) [protected]

set a value from a Range.

Parameters:
valuethe Range value to set

Definition at line 376 of file STK_Option.cpp.

References deleteValue(), isValued_, and p_Range_.

{
  deleteValue();
  p_Range_ = new Range(value);
  isValued_ = true;
}

Here is the call graph for this function:

void STK::Option::set ( std::list< String > const &  value) [protected]

set a value from a list of String.

Parameters:
valuethe list of string values to set

Definition at line 386 of file STK_Option.cpp.

References deleteValue(), isValued_, and p_lString_.

{
  deleteValue();
  p_lString_ = new std::list<String>(value);
  isValued_ = true;
}

Here is the call graph for this function:

void STK::Option::set ( std::list< Real > const &  value) [protected]

set a value from a list of Real.

Parameters:
valuethe list of Real values to set

Definition at line 396 of file STK_Option.cpp.

References deleteValue(), isValued_, and p_lReal_.

{
  deleteValue();
  p_lReal_ = new std::list<Real>(value);
  isValued_ = true;
}

Here is the call graph for this function:

void STK::Option::set ( std::list< Integer > const &  value) [protected]

set a value from a list of Integer.

Parameters:
valuethe list of Integer values to set

Definition at line 406 of file STK_Option.cpp.

References deleteValue(), isValued_, and p_lInteger_.

{
  deleteValue();
  p_lInteger_ = new std::list<Integer>(value);
  isValued_ = true;
}

Here is the call graph for this function:

void STK::Option::set ( std::list< Range > const &  value) [protected]

set a value from a list of Range.

Parameters:
valuethe list of Range values to set

Definition at line 415 of file STK_Option.cpp.

References deleteValue(), isValued_, and p_lRange_.

{
  deleteValue();
  p_lRange_ = new std::list<Range>(value);
  isValued_ = true;
}

Here is the call graph for this function:

void STK::Option::deleteValue ( ) [private]

Remove the value of the option.

Definition at line 423 of file STK_Option.cpp.

References integer_, isValued_, linteger_, lrange_, lreal_, lstring_, p_Integer_, p_lInteger_, p_lRange_, p_lReal_, p_lString_, p_Page_, p_Range_, p_Real_, p_String_, page_, range_, real_, string_, and type_.

Referenced by set(), setPage(), and ~Option().

{
  switch (type_)
  {
    case string_:
      if (p_String_) delete p_String_;
      p_String_ = 0;
      break;
    case real_:
      if (p_Real_) delete p_Real_;
      p_Real_ = 0;
      break;
    case integer_:
      if (p_Integer_) delete p_Integer_;
      p_Integer_ = 0;
      break;
    case range_:
      if (p_Range_) delete p_Range_;
      p_Range_ = 0;
      break;
    case lstring_:
      if (p_lString_) delete p_lString_;
      p_lString_ = 0;
      break;
    case lreal_:
      if (p_lReal_) delete p_lReal_;
      p_lReal_ = 0;
      break;
    case linteger_:
      if (p_lInteger_) delete p_lInteger_;
      p_lInteger_ = 0;
      break;
    case lrange_:
      if (p_lRange_) delete p_lRange_;
      p_lRange_ = 0;
      break;
    case page_:
      if (p_Page_) delete p_Page_;
      p_Page_ = 0;
      break;
    default:
      break;
  };
  isValued_ = false;
}
void STK::Option::setDefaultValue ( ) [private]

set a default value of the option.

This method is used only once when the object is created.

Definition at line 471 of file STK_Option.cpp.

References integer_, linteger_, lrange_, lreal_, lstring_, p_Integer_, p_lInteger_, p_lRange_, p_lReal_, p_lString_, p_Page_, p_Range_, p_Real_, p_String_, page_, range_, real_, string_, STK::STRING_NA, and type_.

Referenced by Option().

{
  //deleteValue();
  switch (type_)
  {
    case string_:
      p_String_ = new String;
      *p_String_ = STRING_NA;
      break;
    case real_:
      p_Real_ = new Real;
      *p_Real_ = Arithmetic<Real>::NA();
      break;
    case integer_:
      p_Integer_ = new Integer;
      *p_Integer_ = Arithmetic<Integer>::NA();
      break;
    case range_:
      p_Range_ = new Range;
      break;
    case lstring_:
      p_lString_ = new std::list<String>;
      break;
    case lreal_:
      p_lReal_ = new std::list<Real>;
      break;
    case linteger_:
      p_lInteger_ = new std::list<Integer>;
      break;
    case lrange_:
      p_lRange_ = new std::list<Range>;
      break;
    case page_:
      p_Page_ = 0;
      break;
    default:
      break;
  };
}

Member Data Documentation

name of the option

Definition at line 247 of file STK_Option.h.

Referenced by name(), operator=(), Option(), and write().

Char used for the option list.

Definition at line 249 of file STK_Option.h.

Referenced by operator=(), setListSeparator(), and write().

type of the option

Definition at line 251 of file STK_Option.h.

Referenced by deleteValue(), operator=(), Option(), read(), setDefaultValue(), setPage(), setValue(), type(), and write().

bool STK::Option::isOptional_ [private]

true if the option is optional, false otherwise

Definition at line 253 of file STK_Option.h.

Referenced by isOptional(), and operator=().

bool STK::Option::isValued_ [private]

true if the option is valued, false otherwise.

A default value is settled when the Option is created as the type is fixed.

Definition at line 256 of file STK_Option.h.

Referenced by deleteValue(), operator=(), set(), and setPage().

Definition at line 264 of file STK_Option.h.

Referenced by deleteValue(), get(), operator=(), Option(), set(), setDefaultValue(), and write().

Definition at line 265 of file STK_Option.h.

Referenced by deleteValue(), get(), operator=(), Option(), set(), setDefaultValue(), and write().

Definition at line 266 of file STK_Option.h.

Referenced by deleteValue(), get(), operator=(), Option(), set(), setDefaultValue(), and write().

Definition at line 267 of file STK_Option.h.

Referenced by deleteValue(), get(), operator=(), Option(), set(), setDefaultValue(), and write().

union { ... } [private]

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