base.h

Go to the documentation of this file.
00001 /**********************************************************************
00002 base.h - Base class for OpenBabel objects
00003  
00004 Copyright (C) 1998-2001 by OpenEye Scientific Software, Inc.
00005 Some portions Copyright (C) 2001-2006 by Geoffrey R. Hutchison
00006  
00007 This file is part of the Open Babel project.
00008 For more information, see <http://openbabel.sourceforge.net/>
00009  
00010 This program is free software; you can redistribute it and/or modify
00011 it under the terms of the GNU General Public License as published by
00012 the Free Software Foundation version 2 of the License.
00013  
00014 This program is distributed in the hope that it will be useful,
00015 but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 GNU General Public License for more details.
00018 ***********************************************************************/
00019 
00020 #ifndef OB_BASE_H
00021 #define OB_BASE_H
00022 
00023 #include <openbabel/babelconfig.h>
00024 
00025 #include <vector>
00026 #include <map>
00027 #include <string>
00028 #include <iostream>
00029 #include <openbabel/tokenst.h>
00030 
00031 #ifndef UNUSED
00032 #define UNUSED(expr) { (void)(expr); }
00033 #endif
00034 
00035 namespace OpenBabel
00036 {
00037 
00038   //Forward declaration of the base class for OBMol OBReaction, OBAtom, etc.
00039   //Declaration later in this file.
00040 class OBBase;
00041 class OBConversion; //used only as pointer
00042 
00054   namespace OBGenericDataType
00055   {
00056     enum
00057     {
00059       UndefinedData =      0,
00060 
00062       PairData      =      1,
00063 
00065       EnergyData    =      2,
00066 
00068       CommentData   =      3,
00069 
00071       ConformerData =      4,
00072 
00074       ExternalBondData =   5,
00075 
00077       RotamerList =        6,
00078 
00080       VirtualBondData =    7,
00081 
00083       RingData =           8,
00084 
00086       TorsionData =        9,
00087 
00089       AngleData =         10,
00090 
00092       SerialNums =        11,
00093 
00095       UnitCell =          12,
00096 
00098       SpinData =          13,
00099 
00101       ChargeData =        14,
00102 
00104       SymmetryData =      15,
00105 
00107       ChiralData =        16,
00108 
00110       OccupationData =    17,
00111 
00113       DensityData =       18,
00114 
00116       ElectronicData =    19,
00117 
00119       VibrationData =     20,
00120 
00122       RotationData =      21,
00123 
00125       NuclearData =       22,
00126 
00128       SetData =           23,
00129 
00131       GridData =          24,
00132 
00134       VectorData =        25,
00135       
00137       MatrixData =        26,
00138 
00140       StereoData =        27,
00141 
00143       DOSData =           28,
00144 
00146       ElectronicTransitionData =
00147                           29,
00148 
00149       // space for up to 2^14 more entries...
00150 
00152       CustomData0 = 16384,
00153       CustomData1 = 16385,
00154       CustomData2 = 16386,
00155       CustomData3 = 16387,
00156       CustomData4 = 16388,
00157       CustomData5 = 16389,
00158       CustomData6 = 16390,
00159       CustomData7 = 16391,
00160       CustomData8 = 16392,
00161       CustomData9 = 16393,
00162       CustomData10 = 16394,
00163       CustomData11 = 16395,
00164       CustomData12 = 16396,
00165       CustomData13 = 16397,
00166       CustomData14 = 16398,
00167       CustomData15 = 16399
00168     };
00169   } // end namespace
00170   enum DataOrigin {
00171     any,                 
00172     fileformatInput,     
00173     userInput,           
00174     perceived,           
00175     external,            
00176     local                
00177   };
00178 
00180   // Class introduction in generic.cpp
00181   // This base class declaration  has no dependence on mol.h
00182   class OBAPI OBGenericData
00183   {
00184   protected:
00185     std::string  _attr;  
00186     unsigned int _type;  
00187     DataOrigin   _source;
00188   public:
00189     OBGenericData(const std::string attr = "undefined",
00190                   const unsigned int type =  OBGenericDataType::UndefinedData,
00191                   const DataOrigin source = any);
00192     //Use default copy constructor and assignment operators
00193     //OBGenericData(const OBGenericData&);
00194                 
00195     /* Virtual constructors added. see 
00196        http://www.parashift.com/c++-faq-lite/abcs.html#faq-22.5
00197        to allow copying given only a base class OBGenericData pointer.
00198        It may be necessary to cast the return pointer to the derived class
00199        type, since we are doing without Covariant Return Types 
00200        http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.8
00201     
00202        A derived class may return NULL if copying is inappropriate */
00203     virtual OBGenericData* Clone(OBBase* /*parent*/) const
00204     { return NULL; } 
00205     virtual ~OBGenericData()    {}
00206     //Use default copy constructor and assignment operators
00207     //OBGenericData& operator=(const OBGenericData &src);
00208 
00210     void                      SetAttribute(const std::string &v)
00211     {        _attr = v;        }
00213     void SetOrigin(const DataOrigin s) { _source = s; }
00215     virtual const std::string &GetAttribute()  const
00216     {        return(_attr);    }
00218     unsigned int                GetDataType()    const
00219     {        return(_type);    }
00222     virtual const std::string &GetValue()  const
00223     {                   return _attr; }
00224     virtual DataOrigin GetOrigin() const
00225     {     return _source; }
00226   };
00227 
00229   typedef std::vector<OBGenericData*>::iterator OBDataIterator;
00230   
00232   // introduction in base.cpp
00233   class OBAPI OBBase
00234     {
00235     public:
00236       virtual ~OBBase()
00237         {
00238           if (!_vdata.empty())
00239             {
00240               std::vector<OBGenericData*>::iterator m;
00241               for (m = _vdata.begin();m != _vdata.end();m++)
00242                 delete *m;
00243               _vdata.clear();
00244             }
00245         }
00246 
00248       virtual bool Clear();
00249 
00255       virtual OBBase* DoTransformations(const std::map<std::string,std::string>* /*pOptions*/,
00256                                         OBConversion* /*pConv*/)
00257         {
00258           return this;
00259         }
00260 
00262       static const char* ClassDescription()
00263         {
00264           return "";
00265         } 
00266 
00268       template< class T >
00269       T* CastAndClear(bool clear=true)
00270         {
00271           T* pOb = dynamic_cast<T*>(this);
00272           if(pOb && clear)// Clear only if this is of target class
00273             Clear();
00274           return pOb;
00275         }
00276 
00279       //Currently no title data member in base class.
00280       virtual const char  *GetTitle(bool replaceNewlines = true) const { UNUSED(replaceNewlines); return "";}
00281       virtual void  SetTitle(const char *) {}
00282 
00284 
00285 
00286       bool                              HasData(const std::string &);
00288       bool                              HasData(const char *);
00290       bool                              HasData(const unsigned int type);
00292       void                              DeleteData(unsigned int type);
00294       void                              DeleteData(OBGenericData*);
00296       void                              DeleteData(std::vector<OBGenericData*>&);
00298       bool                              DeleteData(const std::string& s);
00300       void                              SetData(OBGenericData *d)
00301         {
00302           if(d) _vdata.push_back(d);
00303         }
00306       void                              CloneData(OBGenericData *d);
00308       size_t                      DataSize() const 
00309         { return(_vdata.size()); }
00312       OBGenericData                    *GetData(const unsigned int type);
00314       OBGenericData                    *GetData(const std::string&);
00316       OBGenericData                    *GetData(const char *);
00320       std::vector<OBGenericData*>       GetAllData(const unsigned int type);
00322       std::vector<OBGenericData*>      &GetData() { return(_vdata); }
00324       std::vector<OBGenericData*>      GetData(DataOrigin source);
00326       OBDataIterator  BeginData()
00327         { return(_vdata.begin());        }
00329       OBDataIterator  EndData()
00330         { return(_vdata.end());          }
00332     protected:
00333       std::vector<OBGenericData*> _vdata; 
00334 
00335     };
00336 
00337 } //namespace OpenBabel
00338 
00339 #endif // OB_BASE_H
00340