QVdt.hh

00001 /*
00002  * @file QVdt.hh
00003  * @class QVdt
00004  * @author A. Razeto
00005  * @author M. Vignati (mantainer)
00006  * @brief Variable Data Type
00007  *
00008  * Variable Data Type is an object which is constructed from
00009  * a string and holds the type of the assigne value.
00010  * QVdt("1000") creates a QVdt obj holding an integer with value
00011  * 1000. Later the obj value can be fetced only if the query
00012  * matches the QVdt type, else an exception is generated.
00013  * An assignement operator is present, which does not allow
00014  * runtime type change (except float/int).
00015  *        
00016  */
00017 #ifndef _Q_VDT_H
00018 #define _Q_VDT_H
00019 #include <string>
00020 #include <iostream>
00021 #include <vector>
00022 
00023 class QVdt {
00024 
00025   public:
00026       
00027     enum QVdt_type {
00028       Int_QVdt = 'I',
00029       Double_QVdt = 'F',
00030       String_QVdt = 'S',
00031       Vector_QVdt = 'V',
00032       Unassigned_QVdt = 'U'
00033     };
00034 
00035     typedef std::vector<QVdt> QVdt_vector;
00036 
00037     QVdt (): type_internal(Unassigned_QVdt), name_s("") { }
00038     
00039     QVdt (const std::string& value, const std::string& name = "", QVdt_type type = Unassigned_QVdt);
00040     
00041     QVdt (long int value): name_s("") {  value_i = value; type_internal = Int_QVdt; fBuiltFromString = false;}
00042     
00043     QVdt (double value): name_s("") {  value_f = value; type_internal = Double_QVdt; fBuiltFromString = false;}
00044     
00045     template<typename V> QVdt (const std::vector<V, std::allocator<V> >& value): name_s("") { // This should stay in .cc file, but
00046                                                                                              // gcc does not have export for templates
00047       
00048       type_internal = Vector_QVdt;
00049       std::back_insert_iterator<std::vector<QVdt> > inserter(value_v);
00050       std::copy (value.begin (), value.end (), inserter);
00051       fBuiltFromString = false;
00052     }
00053       // Since cast is not called in some onstructor operator these methods are necessary
00054     QVdt (int value): name_s("") { value_i = value; type_internal = Int_QVdt; fBuiltFromString = false;}
00055     
00056     QVdt (short value): name_s("") { value_i = value; type_internal = Int_QVdt; fBuiltFromString = false;}
00057     
00058     QVdt (char value): name_s("") { value_i = value; type_internal = Int_QVdt; fBuiltFromString = false;}
00059     QVdt (unsigned long value): name_s("") { value_i = value; type_internal = Int_QVdt; fBuiltFromString = false;}
00060     QVdt (unsigned short value): name_s("") { value_i = value; type_internal = Int_QVdt; fBuiltFromString = false;}
00061     QVdt (unsigned char value): name_s("") { value_i = value; type_internal = Int_QVdt; fBuiltFromString = false;}
00062     QVdt (float value): name_s("") { value_f = value; type_internal = Double_QVdt; fBuiltFromString = false;}
00063 
00064     void SetName (const std::string& name) { name_s = name; }
00065     std::string GetName () const { return name_s; }
00066 
00067     const QVdt& operator= (const std::string& value); 
00068     const QVdt& operator= (long int value);
00069     const QVdt& operator= (double value);
00070 
00071     template<typename V> const QVdt& operator= (const std::vector<V, std::allocator<V> >& value) { // This should stay in .cc file, but
00072                                                                                                   // gcc does not have export for templates
00073       if (type_internal != Unassigned_QVdt) CheckType (QVdt::Vector_QVdt);
00074       type_internal = Vector_QVdt;
00075       std::back_insert_iterator<std::vector<QVdt> > inserter(value_v);
00076       std::copy (value.begin (), value.end (), inserter);
00077       fBuiltFromString = false;
00078       return *this;
00079     }
00080     
00081       // Since cast is not called in assignement operator these methods are necessary
00082     const QVdt& operator = (int value)      { return (*this) = (long int)(value); }
00083     const QVdt& operator = (short value)          { return (*this) = (long int)(value); }
00084     const QVdt& operator = (char value)     { return (*this) = (long int)(value); }
00085     const QVdt& operator = (unsigned long value) { return (*this) = (long int)(value); }
00086     const QVdt& operator = (unsigned short value) { return (*this) = (long int)(value); }
00087     const QVdt& operator = (unsigned char value) { return (*this) = (long int)(value); }
00088     const QVdt& operator = (float value)    { return (*this) = double(value); }
00089     const QVdt& operator = (const QVdt& other);
00090 
00091     bool is_valid ()     const  { return (type_internal == Unassigned_QVdt) ? 0 : 1; }
00092     
00093     QVdt_type GetType () const  { return type_internal; }
00094     long int GetInt () const;
00095     double GetDouble () const;
00096     const std::string& GetString () const;
00097     const QVdt_vector& GetVector () const;
00098     bool GetBool () const { return bool (GetInt ()); }
00099     
00100   private:
00101     QVdt_type type_internal;
00102     
00103     long int value_i;
00104     double value_f;
00105     mutable std::string value_s;
00106     QVdt_vector value_v;
00107     
00108     std::string name_s;
00109     bool fBuiltFromString;
00110     void CheckType (QVdt_type new_type) const;  // Throws an exception if the internal type 
00111     std::string ToString() const;
00112                                                   // is different from the required type
00113     QVdt_type SearchType (const std::string& str);
00114     void AssignValue (const std::string& str);
00115 };
00116 
00117 std::ostream& operator<< (std::ostream&, const QVdt&);
00118 #endif

Generated on Tue Nov 16 10:49:55 2010 for CUORE Software by  doxygen 1.5.6