ReactionPickler.h

Go to the documentation of this file.
00001 //
00002 //  Copyright (C) 2009 Greg Landrum
00003 //
00004 //   @@ All Rights Reserved @@
00005 //  This file is part of the RDKit.
00006 //  The contents are covered by the terms of the BSD license
00007 //  which is included in the file license.txt, found at the root
00008 //  of the RDKit source tree.
00009 //
00010 #ifndef _RD_RXNPICKLE_H_2JUNE2009_
00011 #define _RD_RXNPICKLE_H_2JUNE2009_
00012 
00013 // Std stuff
00014 #include <iostream>
00015 #include <string>
00016 #include <exception>
00017 #ifdef WIN32
00018 #include <ios>
00019 #endif
00020 
00021 namespace RDKit{
00022   class ChemicalReaction;
00023 
00024   //! used to indicate exceptions whilst pickling (serializing) reactions
00025   class ReactionPicklerException : public std::exception {
00026     public :
00027       ReactionPicklerException(const char *msg) : _msg(msg) {};
00028       ReactionPicklerException(const std::string msg) : _msg(msg) {};
00029       const char *message () const { return _msg.c_str(); };
00030       ~ReactionPicklerException () throw () {};
00031     
00032     private :
00033       std::string _msg;
00034   };
00035 
00036   //! handles pickling (serializing) reactions
00037   class ReactionPickler{
00038   public:
00039     static const boost::int32_t versionMajor,versionMinor,versionPatch; //!< mark the pickle version
00040     static const boost::int32_t endianId;  //! mark the endian-ness of the pickle
00041 
00042     //! the pickle format is tagged using these tags:
00043     //! NOTE: if you add to this list, be sure to put new entries AT THE BOTTOM, otherwise
00044     //! you will break old pickles.
00045     typedef enum {
00046       VERSION=10000,
00047       BEGINREACTANTS,
00048       ENDREACTANTS,
00049       BEGINPRODUCTS,
00050       ENDPRODUCTS,
00051       ENDREACTION,
00052     } Tags;
00053 
00054     //! pickles a reaction and sends the results to stream \c ss
00055     static void pickleReaction(const ChemicalReaction *rxn,std::ostream &ss);
00056     static void pickleReaction(const ChemicalReaction &rxn,std::ostream &ss) {
00057       ReactionPickler::pickleReaction(&rxn,ss);
00058     };
00059     //! pickles a reaction and adds the results to string \c res
00060     static void pickleReaction(const ChemicalReaction *rxn,std::string &res);
00061     static void pickleReaction(const ChemicalReaction &rxn,std::string &res) {
00062       ReactionPickler::pickleReaction(&rxn,res);
00063     };
00064 
00065     //! constructs a reaction from a pickle stored in a string
00066     static void reactionFromPickle(const std::string &pickle,ChemicalReaction *rxn);
00067     static void reactionFromPickle(const std::string &pickle,ChemicalReaction &rxn) {
00068       ReactionPickler::reactionFromPickle(pickle,&rxn);
00069     };
00070 
00071     //! constructs a reaction from a pickle stored in a stream
00072     static void reactionFromPickle(std::istream &ss,ChemicalReaction *rxn);
00073     static void reactionFromPickle(std::istream &ss,ChemicalReaction &rxn) {
00074       ReactionPickler::reactionFromPickle(ss,&rxn);
00075     };
00076   private:
00077     //! do the actual work of pickling a reaction
00078     static void _pickle(const ChemicalReaction *rxn,std::ostream &ss);
00079 
00080     //! do the actual work of de-pickling a reaction
00081     static void _depickle(std::istream &ss,ChemicalReaction *rxn, int version);
00082 
00083   };  
00084   
00085 };
00086 
00087 
00088 #endif