Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

cfg::Param Class Reference

A single parameter stored in a configuration. More...

#include <Param.h>

List of all members.

Public Member Functions

 Param ()
 Param (const Param &p)
 Param (const char *name, const char *comment)
template<class T>
 Param (const char *name, const T &d, const char *comment)
 ~Param ()
const char * GetName () const
const char * GetComment () const
const char * XMLTag () const
void Print (std::ostream &os) const
template<typename T>
void Get (T &t) const
template<class T>
void Set (const T &t)
Paramoperator= (const Param &rhs)

Private Attributes

std::string fName
 Name of parameter.
std::string fComment
 Explanation of what parameter is used for.
void * fData
 Data values held by parameter.
const std::type_info &(* fDataType )(void)
const std::type_info &(* fVectorType )(void)
void(* fDataDelete )(void *)
void(* fDataCopyCons )(void **dest, const void *src)
void(* fDataCopy )(void *dest, const void *src)
void(* fDataPrint )(std::ostream &os, const void *d)

Friends

std::ostream & operator<< (std::ostream &os, const Param &p)


Detailed Description

A single parameter stored in a configuration.

A parameter consists of a name, and explanation ("comment") and a collection of data values all of which must be of a single type

Definition at line 23 of file Param.h.


Constructor & Destructor Documentation

cfg::Param::Param  ) 
 

Definition at line 95 of file Param.cxx.

00095                 :
00096   fName         ( "<null>" ),
00097   fComment      ( "" ),
00098   fData         ( 0 ),
00099   fDataType     ( 0 ),
00100   fVectorType   ( 0 ),
00101   fDataDelete   ( 0 ),
00102   fDataCopyCons ( 0 ),
00103   fDataCopy     ( 0 ),
00104   fDataPrint    ( 0 )
00105 { }

cfg::Param::Param const Param p  ) 
 

Definition at line 109 of file Param.cxx.

References fData, and fDataCopyCons.

00109                                  :
00110   fName         ( p.fName    ),
00111   fComment      ( p.fComment ),
00112   fData         ( 0 ),
00113   fDataType     ( p.fDataType     ),
00114   fVectorType   ( p.fVectorType   ),
00115   fDataDelete   ( p.fDataDelete   ),
00116   fDataCopyCons ( p.fDataCopyCons ),
00117   fDataCopy     ( p.fDataCopy     ),
00118   fDataPrint    ( p.fDataPrint    )
00119 {
00120   fDataCopyCons(&fData, p.fData);
00121 }

cfg::Param::Param const char *  name,
const char *  comment
 

Definition at line 125 of file Param.cxx.

00125                                                      :
00126   fName         ( name ),
00127   fComment      ( comment ),
00128   fData         ( 0 ),
00129   fDataType     ( 0 ),
00130   fVectorType   ( 0 ),
00131   fDataDelete   ( 0 ),
00132   fDataCopyCons ( 0 ),
00133   fDataCopy     ( 0 ),
00134   fDataPrint    ( 0 )
00135 { }

template<class T>
cfg::Param::Param const char *  name,
const T &  d,
const char *  comment
 

Construct a parameter from a name, comment, and data

Definition at line 177 of file Param.h.

References Set().

00177                                                                   :
00178     fName        ( name    ),
00179     fComment     ( comment ),
00180     fData        ( 0 ),
00181     fDataType    ( 0 ),
00182     fVectorType  ( 0 ),
00183     fDataDelete  ( 0 ),
00184     fDataCopyCons( 0 ),
00185     fDataCopy    ( 0 ),
00186     fDataPrint   ( 0 )
00187     {
00188       this->Set(d);
00189     }
  

cfg::Param::~Param  ) 
 

Definition at line 139 of file Param.cxx.

References fData, and fDataDelete.

00140 { 
00141   if (fData) {
00142     fDataDelete(fData); 
00143     fData = 0;
00144   }
00145 }


Member Function Documentation

template<typename T>
void cfg::Param::Get T &  item  )  const
 

Retrieve the data held by the parameter into item. Note that Param objects always hold vectors, even if that vector has only one item in it (see Config/xml/paramBuilder.cxx).

Definition at line 199 of file Param.h.

References fDataType, fVectorType, and GetName().

Referenced by cfg::operator<<(), and testGet().

00200   {
00201     // Basic check: The types match exactly.  In that case, just
00202     // make a copy and return it.
00203     const std::type_info& typeInfo = typeid( T );
00204     if ( typeInfo == this->fDataType() ) 
00205       {
00206         // Copy the parameter into the return object.
00207         item = (*(T*)fData);
00208         return;
00209       }
00210     else
00211       {
00212         // Perhaps the user has asked for a simple type.  This is
00213         // fine, as long this parameter is vector of that type and
00214         // only has a single value.
00215         if ( typeInfo == this->fVectorType() )
00216           {
00217             // The parameter is a vector of the type that the user requests.
00218             std::vector<T>& param = (*(std::vector<T>*)fData);
00219             // Does this vector have exactly one element in it?
00220             if ( param.size() == 1 )
00221               {
00222                 // Yes, so return that item.
00223                 item = param.front();
00224                 return;
00225               }
00226             else
00227               {
00228                 std::ostringstream os;
00229                 os << "Parameter " << this->GetName() << " has multiple values of type "
00230                    << typeInfo.name();
00231                 throw cfg::Exception(Exception::kTypeMismatch,
00232                                      os.str().c_str(),
00233                                      __FILE__,
00234                                      __LINE__);
00235               }
00236           }
00237       }
00238     // If we get here, we don't know how to handle the
00239     // difference between the type the user has requested
00240     // and the type of this parameter.  Throw an exception.
00241     std::ostringstream os;
00242     os << "Attempt to get data of type " << typeInfo.name()
00243        << " from parameter " << this->GetName() << " which is of type "
00244        << this->fDataType().name() << " (a vector of " 
00245        << this->fVectorType().name() << ")";
00246     throw cfg::Exception(Exception::kTypeMismatch,
00247                          os.str().c_str(),
00248                          __FILE__,
00249                          __LINE__);
00250   }

const char * cfg::Param::GetComment  )  const
 

Definition at line 153 of file Param.cxx.

References fComment.

Referenced by cfg::Config::AsXML(), and evdb::ParamFrame::ParamFrame().

00153 { return fComment.c_str(); }

const char * cfg::Param::GetName  )  const
 

Definition at line 149 of file Param.cxx.

References fName.

Referenced by cfg::Config::AdoptParam(), cfg::Config::AsXML(), Get(), and evdb::ParamFrame::ParamFrame().

00149 { return fName.c_str(); }

cfg::Param & cfg::Param::operator= const Param rhs  ) 
 

Definition at line 161 of file Param.cxx.

References fComment, fData, fDataCopy, fDataCopyCons, fDataDelete, fDataPrint, fDataType, fName, and fVectorType.

00162 {
00163 //======================================================================
00164 // Copy operation
00165 //======================================================================
00166   // Avoid self-copy
00167   if (&rhs == this) return *this;
00168   
00169   // Copy the name and comments over
00170   fName    = rhs.fName;
00171   fComment = rhs.fComment;
00172   
00173   // Copy the data handlers
00174   fDataType     = rhs.fDataType;
00175   fVectorType   = rhs.fVectorType;
00176   fDataDelete   = rhs.fDataDelete;
00177   fDataCopyCons = rhs.fDataCopyCons;
00178   fDataCopy     = rhs.fDataCopy;
00179   fDataPrint    = rhs.fDataPrint;
00180   
00181   // Delete the data from the left-hand side and reallocate it using the
00182   // copy constructor
00183   if (fData) { fDataDelete(fData); fData = 0; }
00184   fDataCopyCons(&fData, rhs.fData);
00185 
00186   return *this;
00187 }

void cfg::Param::Print std::ostream &  os  )  const
 

Definition at line 157 of file Param.cxx.

References fData, and fDataPrint.

Referenced by cfg::operator<<().

00157 { fDataPrint(os, fData); }

template<class T>
void cfg::Param::Set const T &  t  ) 
 

Set the value of the data held by the parameter to t

Definition at line 258 of file Param.h.

References fData, fDataCopy, fDataCopyCons, fDataDelete, fDataPrint, fDataType, and fVectorType.

Referenced by Param(), and testSet().

00259     {
00260       // Delete the data currently held
00261       if (fData) {
00262         this->fDataDelete(fData);
00263         fData = 0;
00264       }
00265       
00266       // Setup the methods for handling this datum
00267       fDataType     = gsDataType<T>;
00268       fVectorType   = gsVectorType<T>;
00269       fDataDelete   = gsDataDelete<T>;
00270       fDataCopyCons = gsDataCopyCons<T>;
00271       fDataCopy     = gsDataCopy<T>;
00272       fDataPrint    = gsDataPrint<T>;
00273       
00274       // Create the data from the data passed in
00275       fDataCopyCons(&fData, &t);
00276     }

const char * cfg::Param::XMLTag  )  const
 

Definition at line 36 of file Param.cxx.

References fDataType.

Referenced by cfg::Config::AsXML(), cfg::operator<<(), and evdb::ParamFrame::ParamFrame().

00037 {
00038 //
00039 // Return a recommended xml tag for this data type
00040 //
00041   static const std::type_info& bool_id   = typeid(bool);
00042   static const std::type_info& char_id   = typeid(char);
00043   static const std::type_info& short_id  = typeid(short);
00044   static const std::type_info& int_id     =typeid(int);
00045   static const std::type_info& uchar_id   =typeid(unsigned char);
00046   static const std::type_info& ushort_id  =typeid(unsigned short);
00047   static const std::type_info& uint_id    =typeid(unsigned int);
00048   static const std::type_info& float_id   =typeid(float);
00049   static const std::type_info& double_id  =typeid(double);
00050   static const std::type_info& string_id  =typeid(std::string);
00051   static const std::type_info& time_id    =typeid(time_t);
00052   static const std::type_info& boolv_id   =typeid(std::vector<bool>);
00053   static const std::type_info& charv_id   =typeid(std::vector<char>);
00054   static const std::type_info& shortv_id  =typeid(std::vector<short>);
00055   static const std::type_info& intv_id    =typeid(std::vector<int>);
00056   static const std::type_info& ucharv_id  =typeid(std::vector<unsigned char>);
00057   static const std::type_info& ushortv_id =typeid(std::vector<unsigned short>);
00058   static const std::type_info& uintv_id   =typeid(std::vector<unsigned int>);
00059   static const std::type_info& floatv_id  =typeid(std::vector<float>);
00060   static const std::type_info& doublev_id =typeid(std::vector<double>);
00061   static const std::type_info& stringv_id =typeid(std::vector<std::string>);
00062   static const std::type_info& timev_id   =typeid(std::vector<time_t>);
00063 
00064   if (bool_id   == this->fDataType()) return "bool";
00065   if (char_id   == this->fDataType()) return "char";
00066   if (short_id  == this->fDataType()) return "short";
00067   if (int_id    == this->fDataType()) return "int";
00068   if (uint_id   == this->fDataType()) return "uint";
00069   if (uchar_id  == this->fDataType()) return "uchar";
00070   if (ushort_id == this->fDataType()) return "ushort";
00071   if (uint_id   == this->fDataType()) return "uint";
00072   if (float_id  == this->fDataType()) return "float";
00073   if (double_id == this->fDataType()) return "double";
00074   if (string_id == this->fDataType()) return "string";
00075   if (time_id   == this->fDataType()) return "time_t";
00076 
00077   if (boolv_id   == this->fDataType()) return "bool";
00078   if (charv_id   == this->fDataType()) return "char";
00079   if (shortv_id  == this->fDataType()) return "short";
00080   if (intv_id    == this->fDataType()) return "int";
00081   if (uintv_id   == this->fDataType()) return "uint";
00082   if (ucharv_id  == this->fDataType()) return "uchar";
00083   if (ushortv_id == this->fDataType()) return "ushort";
00084   if (uintv_id   == this->fDataType()) return "uint";
00085   if (floatv_id  == this->fDataType()) return "float";
00086   if (doublev_id == this->fDataType()) return "double";
00087   if (stringv_id == this->fDataType()) return "string";
00088   if (timev_id   == this->fDataType()) return "time_t";
00089   
00090   return "?";
00091 }


Friends And Related Function Documentation

std::ostream& operator<< std::ostream &  os,
const Param p
[friend]
 

Definition at line 16 of file Param.cxx.

00017   {
00018     if (std::string(p.XMLTag())=="time_t") {
00019       time_t t;
00020       p.Get(t);
00021       struct tm* ts = gmtime(&t);
00022       os << ts->tm_year+1900 << "-" << ts->tm_mon+1 << "-" 
00023          << ts->tm_mday << " " << ts->tm_hour << ":" << ts->tm_min << ":" 
00024          << ts->tm_sec;
00025       return os;
00026     }
00027     else {
00028       p.Print(os); 
00029       return os;
00030     }
00031   }


Member Data Documentation

std::string cfg::Param::fComment [private]
 

Explanation of what parameter is used for.

Definition at line 50 of file Param.h.

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

void* cfg::Param::fData [private]
 

Data values held by parameter.

Definition at line 51 of file Param.h.

Referenced by operator=(), Param(), Print(), Set(), and ~Param().

void(* cfg::Param::fDataCopy)(void *dest, const void *src) [private]
 

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

void(* cfg::Param::fDataCopyCons)(void **dest, const void *src) [private]
 

Referenced by operator=(), Param(), and Set().

void(* cfg::Param::fDataDelete)(void *) [private]
 

Referenced by operator=(), Set(), and ~Param().

void(* cfg::Param::fDataPrint)(std::ostream &os, const void *d) [private]
 

Referenced by operator=(), Print(), and Set().

const std::type_info&(* cfg::Param::fDataType)(void) [private]
 

Referenced by Get(), operator=(), Set(), and XMLTag().

std::string cfg::Param::fName [private]
 

Name of parameter.

Definition at line 49 of file Param.h.

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

const std::type_info&(* cfg::Param::fVectorType)(void) [private]
 

Referenced by Get(), operator=(), and Set().


The documentation for this class was generated from the following files:
Generated on Sat Nov 7 04:47:02 2009 for NOvA Offline by  doxygen 1.3.9.1