The Gnome Chemistry Utils  0.14.0
formula.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 
3 /*
4  * Gnome Chemistry Utils
5  * formula.h
6  *
7  * Copyright (C) 2005-2010 Jean Bréfort <jean.brefort@normalesup.org>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 3 of the
12  * License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
22  * USA
23  */
24 
25 #ifndef GCU_FORMULA_H
26 #define GCU_FORMULA_H
27 
28 #include <string>
29 #include <map>
30 #include <list>
31 #include <stdexcept>
32 #include "isotope.h"
33 #include "macros.h"
34 #include "value.h"
35 
37 namespace gcu
38 {
39 
52 typedef enum {
53  GCU_FORMULA_PARSE_GUESS,
54  GCU_FORMULA_PARSE_ATOM,
55  GCU_FORMULA_PARSE_RESIDUE,
56  GCU_FORMULA_PARSE_ASK,
57  GCU_FORMULA_PARSE_NO_CASE=8
59 
65 class parse_error: public std::exception
66 {
67 public:
71  explicit
72  parse_error (const std::string& __arg, int start, int length);
73 
74  virtual
75  ~parse_error () throw ();
76 
80  virtual const char*
81  what () const throw ();
85  const char*
86  what (int& start, int& length) const throw ();
87 
91  void add_offset (int offset) {m_start += offset;}
92 
93 private:
94  std::string m_msg;
95  int m_start, m_length;
96 
97 };
98 
104 {
105 public:
109  FormulaElt ();
113  virtual ~FormulaElt ();
117  virtual std::string Markup ();
121  virtual std::string Text ();
128  virtual void BuildRawFormula (std::map<int, int> &raw) = 0;
132  virtual int GetValence () = 0;
136  int stoich;
140  unsigned start;
144  unsigned end;
145 };
146 
151 class FormulaAtom: public FormulaElt
152 {
153 public:
157  FormulaAtom (int Z);
161  virtual ~FormulaAtom ();
165  std::string Markup ();
169  std::string Text ();
175  void BuildRawFormula (std::map<int, int> &raw);
179  int GetValence ();
182  int elt;
183 };
184 
190 {
191 public:
194  FormulaBlock ();
198  virtual ~FormulaBlock ();
202  std::string Markup ();
206  std::string Text ();
213  void BuildRawFormula (std::map<int, int> &raw);
217  int GetValence ();
221  std::list<FormulaElt *> children;
226 };
227 
228 class Residue;
229 
237 {
238 public:
244  FormulaResidue (Residue const *res, char const *symbol, int Z);
248  virtual ~FormulaResidue ();
252  std::string Markup ();
256  std::string Text ();
263  void BuildRawFormula (std::map<int, int> &raw);
267  int GetValence ();
271  Residue const *residue;
275  std::string Symbol;
279 GCU_RO_PROP (int, Z);
280 };
281 
282 
288 class Formula
289 {
290 public:
297  Formula (std::string entry, FormulaParseMode mode = GCU_FORMULA_PARSE_GUESS) throw (parse_error);
298 
302  virtual ~Formula ();
303 
307  char const *GetMarkup ();
311  std::map<int,int> &GetRawFormula ();
315  char const *GetRawMarkup ();
322  void SetFormula (std::string entry) throw (parse_error);
326  void Clear ();
332  DimensionalValue GetMolecularWeight (bool &artificial);
339 
343  std::list<FormulaElt *> const &GetElements () const {return Details;}
344 
345 private:
346  bool BuildConnectivity ();
347  void Parse (std::string &formula, std::list<FormulaElt *>&result) throw (parse_error);
348  bool AnalString (char *sz, std::list<FormulaElt *> &result, bool &ambiguous, int offset);
349  bool TryReplace (std::list<FormulaElt *> &result, std::list<FormulaElt *>::iterator it);
350 
351 private:
352  std::string Entry, Markup, RawMarkup;
353  std::map<int,int> Raw;
354  std::list<FormulaElt *> Details;
355  DimensionalValue m_Weight;
356  bool m_WeightCached;
357  bool m_Artificial;
358  bool m_ConnectivityCached;
359 
372 };
373 
374 }
375 
376 #endif // GCU_FORMULA_H