RDBColumn.h

00001 #ifndef RDBColumn_h
00002 #define RDBColumn_h
00003 
00004 /* --8<--8<--8<--8<--
00005  *
00006  * Copyright (C) 2006 Smithsonian Astrophysical Observatory
00007  *
00008  * This file is part of RDB
00009  *
00010  * RDB is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU General Public License
00012  * as published by the Free Software Foundation; either version 2
00013  * of the License, or (at your option) any later version.
00014  *
00015  * RDB is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program; if not, write to the 
00022  *       Free Software Foundation, Inc. 
00023  *       51 Franklin Street, Fifth Floor
00024  *       Boston, MA  02110-1301, USA
00025  *
00026  * -->8-->8-->8-->8-- */
00027 
00028 #include <cerrno>
00029 #include <cstdio>
00030 #include <cstring>
00031 #include <float.h>
00032 #include <iomanip>
00033 #include <iostream>
00034 #include <limits>
00035 #include <sstream>
00036 #include <string>
00037 
00038 #include <rdbxx/RDBErr.h>
00039 
00040 using namespace std;
00041 
00043 class RDBColumn {
00045 
00046 
00047   friend istream& operator>>( istream& is, RDBColumn& col ) throw ( RDBErr );
00049   friend istream& operator>>( istream& is, RDBColumn* col ) throw ( RDBErr );
00051   friend ostream& operator<<( ostream& os, const RDBColumn& col );
00053   friend ostream& operator<<( ostream& os, const RDBColumn* col );
00055   
00056  public:
00058 
00059 
00060   enum Just { LEFT='<', RIGHT='>' };
00062   enum Type { MONTH='M', NUMERIC='N', STRING='S' };
00064   enum Err  { NOERR=0, LOSTPRECISION=EDOM, NONNUMERIC=EINVAL, OUTOFRANGE=ERANGE, NODATA=ENODATA };
00065   enum Status { CMOG=0x01, CEOG=0x02, CBOG=0x04 };
00067  
00068   
00070 
00071 
00072   RDBColumn( const string& name="", const string& def="" ) throw ( RDBErr );
00074   RDBColumn( const RDBColumn& col );
00076   virtual ~RDBColumn( void );
00077 
00079   RDBColumn& operator=( const RDBColumn& col );
00081   virtual RDBColumn& operator=( const double& data ) throw ( RDBErr ) = 0;
00083   virtual RDBColumn& operator=( const long& data ) throw ( RDBErr ) = 0;
00085   virtual RDBColumn& operator=( const string& data ) throw ( RDBErr ) = 0;
00087 
00089 
00090 
00091   virtual void advanceIdx( void ) = 0;
00093   virtual void rewind( void ) = 0;
00095 
00097 
00098 
00099   virtual void setGroup( bool group );
00102   bool getGroup( void ) const;
00104   virtual void setGroupValue( void ) = 0;
00106   virtual int newGroup( void ) = 0;
00108   
00110 
00111 
00112   void setName( const string& name );
00114   void setDef( const string& def ) throw ( RDBErr );
00116   void setWidth( const long width );
00118   void setType( const RDBColumn::Type type );
00120   void setJust( const RDBColumn::Just just );
00122   void setDesc( const string& desc );
00124   void setPrecision( const int precision );
00126   void setThrow( const bool t=true );
00128   void setErrNo( const int no=0 );  
00130   virtual bool setData( const double& data ) throw ( RDBErr ) = 0;
00132   virtual bool setData( const long& data ) throw ( RDBErr ) = 0;
00134   virtual bool setData( const string& data ) throw ( RDBErr ) = 0;
00136 
00138 
00139 
00140   virtual void mapData( double data[], const size_t nelems=1 ) throw ( RDBErr );
00142   virtual void mapData( long data[], const size_t nelems=1 ) throw ( RDBErr );
00144   virtual void mapData( string data[], const size_t nelems=1 ) throw ( RDBErr );
00146 
00148 
00149 
00150   string getName( void ) const;
00152   string getDef( void ); // Non-constant, because it has to
00153                         //   reconstruct the definition if any field
00154                         //   has changed... 
00156   long getWidth( void ) const;
00158   RDBColumn::Type getType( void ) const;
00160   RDBColumn::Just getJust( void ) const;
00162   string getDesc( void ) const;
00164   int getPrecision( void ) const;
00166   bool getThrow( void ) const;
00168   char* getErr( void ) const;
00170   int getErrNo( void ) const;
00172   virtual void* getData( void ) = 0;
00174   virtual bool getData( double& data ) throw( RDBErr ) = 0;
00176   virtual bool getData( long& data ) throw( RDBErr ) = 0;
00178   virtual bool getData( string& data ) throw( RDBErr ) = 0;
00180   virtual double getDataDouble( void ) throw ( RDBErr ) = 0;
00182   virtual long getDataLong( void ) throw ( RDBErr ) = 0;
00184   virtual string getDataString( void ) throw ( RDBErr ) = 0;
00186   
00187  protected:
00189   void convert( const double& idata, double& odata ) throw ( RDBErr ); 
00191   void convert( const double& idata, long& odata ) throw ( RDBErr );
00193   void convert( const double& idata, string& odata ) throw ( RDBErr );
00195   void convert( const long& idata, double& odata ) throw ( RDBErr );
00197   void convert( const long& idata, long& odata ) throw ( RDBErr );
00199   void convert( const long& idata, string& odata ) throw ( RDBErr );
00201   void convert( const string& idata, double& odata ) throw ( RDBErr ); 
00203   void convert( const string& idata, long& odata ) throw ( RDBErr ); 
00205   void convert( const string& idata, string& odata ) throw ( RDBErr );
00206 
00208   virtual istream& read( istream& is ) throw ( RDBErr ) = 0;
00210   virtual istream& extract( istream& is, double& data ) throw ( RDBErr );
00212   virtual istream& extract( istream& is, long& data ) throw ( RDBErr );
00214   virtual istream& extract( istream& is, string& data ) throw ( RDBErr );
00216   virtual ostream& write( ostream& os ) const = 0;
00218   virtual ostream& insert( ostream& os, double& data ) const;
00220   virtual ostream& insert( ostream& os, long& data ) const;
00222   virtual ostream& insert( ostream& os, string& data ) const;
00223 
00225   string _name;
00227   string _def;
00229   long _width;
00231   RDBColumn::Type _type;
00233   RDBColumn::Just _just;
00235   string _desc;
00237   bool _changed;
00238   
00240   bool _throw;
00242   int _errno;
00244   int _precision;
00246   stringstream _strstrm;
00248   bool _group;
00250   bool _initgroup;
00251   
00252 };
00253 
00254 #endif

Generated on Tue Sep 15 11:07:16 2009 for rdbxx by  doxygen 1.5.6