# formula.h

Go to the documentation of this file.

    00001 // -*- C++ -*-
    00002
    00003 /*
    00004  * Gnome Chemistry Utils
    00005  * formula.h
    00006  *
    00007  * Copyright (C) 2005-2008 Jean Bréfort <jean.brefort@normalesup.org>
    00008  *
    00009  * This program is free software; you can redistribute it and/or
    00010  * modify it under the terms of the GNU General Public License as
    00011  * published by the Free Software Foundation; either version 2 of the
    00012  * License, or (at your option) any later version.
    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  * You should have received a copy of the GNU General Public License
    00020  * along with this program; if not, write to the Free Software
    00021  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
    00022  * USA
    00023  */
    00024
    00025 #ifndef GCU_FORMULA_H
    00026 #define GCU_FORMULA_H
    00027
    00028 #include <string>
    00029 #include <map>
    00030 #include <list>
    00031 #include <stdexcept>
    00032 #include "isotope.h"
    00033 #include "macros.h"
    00034 #include "value.h"
    00035
    00037 namespace gcu
    00038 {
    00039
    00052 typedef enum {
    00053         GCU_FORMULA_PARSE_GUESS,
    00054         GCU_FORMULA_PARSE_ATOM,
    00055         GCU_FORMULA_PARSE_RESIDUE,
    00056         GCU_FORMULA_PARSE_ASK,
    00057         GCU_FORMULA_PARSE_NO_CASE=8
    00058 } FormulaParseMode;
    00059
    00065 class parse_error: public std::exception
    00066 {
    00067 public:
    00071     explicit
    00072     parse_error (const std::string&  __arg, int start, int length);
    00073
    00074     virtual
    00075     ~parse_error () throw ();
    00076
    00080     virtual const char*
    00081     what () const throw ();
    00085     const char*
    00086     what (int& start, int& length) const throw ();
    00087
    00091         void add_offset (int offset) {m_start += offset;}
    00092
    00093 private:
    00094         std::string m_msg;
    00095         int m_start, m_length;
    00096
    00097 };
    00098
    00103 class FormulaElt
    00104 {
    00105 public:
    00109         FormulaElt ();
    00113         virtual ~FormulaElt ();
    00117         virtual std::string Markup ();
    00121         virtual std::string Text ();
    00128         virtual void BuildRawFormula (std::map<int, int> &raw) = 0;
    00132         virtual int GetValence () = 0;
    00136         int stoich;
    00140         unsigned start;
    00144         unsigned end;
    00145 };
    00146
    00151 class FormulaAtom: public FormulaElt
    00152 {
    00153 public:
    00157         FormulaAtom (int Z);
    00161         virtual ~FormulaAtom ();
    00165         std::string Markup ();
    00169         std::string Text ();
    00175         void BuildRawFormula (std::map<int, int> &raw);
    00179         int GetValence ();
    00182         int elt;
    00183 };
    00184
    00189 class FormulaBlock: public FormulaElt
    00190 {
    00191 public:
    00194         FormulaBlock ();
    00198         virtual ~FormulaBlock ();
    00202         std::string Markup ();
    00206         std::string Text ();
    00213         void BuildRawFormula (std::map<int, int> &raw);
    00217         int GetValence ();
    00221         std::list<FormulaElt *> children;
    00225         int parenthesis;
    00226 };
    00227
    00228 class Residue;
    00229
    00233 class FormulaResidue: public FormulaElt
    00234 {
    00235 public:
    00241         FormulaResidue (Residue const *res, char const *symbol, int Z);
    00245         virtual ~FormulaResidue ();
    00249         std::string Markup ();
    00253         std::string Text ();
    00260         void BuildRawFormula (std::map<int, int> &raw);
    00264         int GetValence ();
    00268         Residue const *residue;
    00272         std::string Symbol;
    00276 GCU_RO_PROP (int, Z);
    00277 };
    00278
    00279
    00285 class Formula
    00286 {
    00287 public:
    00294         Formula (std::string entry, FormulaParseMode mode = GCU_FORMULA_PARSE_GUESS) throw (parse_error);
    00295
    00299         virtual ~Formula ();
    00300
    00304         char const *GetMarkup ();
    00308         std::map<int,int> &GetRawFormula ();
    00312         char const *GetRawMarkup ();
    00319         void SetFormula (std::string entry) throw (parse_error);
    00323         void Clear ();
    00329         DimensionalValue GetMolecularWeight (bool &artificial);
    00335         void CalculateIsotopicPattern (IsotopicPattern &pattern);
    00336
    00340         std::list<FormulaElt *> const &GetElements () const {return Details;}
    00341
    00342 private:
    00343         bool BuildConnectivity ();
    00344         void Parse (std::string &formula, std::list<FormulaElt *>&result) throw (parse_error);
    00345         bool AnalString (char *sz, std::list<FormulaElt *> &result, bool &ambiguous, int offset);
    00346         bool TryReplace (std::list<FormulaElt *> &result, std::list<FormulaElt *>::iterator it);
    00347
    00348 private:
    00349         std::string Entry, Markup, RawMarkup;
    00350         std::map<int,int> Raw;
    00351         std::list<FormulaElt *> Details;
    00352         DimensionalValue m_Weight;
    00353         bool m_WeightCached;
    00354         bool m_Artificial;
    00355         bool m_ConnectivityCached;
    00356
    00368 GCU_PROP (FormulaParseMode, ParseMode);
    00369 };
    00370
    00371 }
    00372
    00373 #endif // GCU_FORMULA_H