MathTypeLibrary(libmath++)  0.0.3
library.h
1 
2 // Math Type Library
3 // $Id: library.h,v 1.6 2002/04/29 19:34:30 cparpart Exp $
4 // (module info here)
5 //
6 // Copyright (c) 2002 by Christian Parpart <cparpart@surakware.net>
7 //
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Library General Public
10 // License as published by the Free Software Foundation; either
11 // version 2 of the License, or (at your option) any later version.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Library General Public License for more details.
17 //
18 // You should have received a copy of the GNU Library General Public License
19 // along with this library; see the file COPYING.LIB. If not, write to
20 // the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 // Boston, MA 02111-1307, USA.
23 #ifndef libmath_library_h
24 #define libmath_library_h
25 
26 #include <math++/error.h>
27 
28 #include <list>
29 #include <map>
30 #include <string>
31 
32 namespace math {
33 
34 template<class> class TNode;
35 template<class> class TLibrary;
36 
40 template<typename T>
41 class TFunction {
42 private:
43  std::string FName;
44  TNode<T> *FExpression;
45 
46 public:
47  TFunction();
48  TFunction(const TFunction<T>&);
49  TFunction(const std::string& AName, const std::string& AExprStr = std::string());
50  TFunction(const std::string& AName, const TNode<T> *AExprTree);
51  ~TFunction();
52 
53  T call(const T& AParam, const TLibrary<T>& ALibrary, unsigned ALimit = 64) const;
54 
55  void name(const std::string&);
56  std::string name() const;
57 
58  void expression(const TNode<T> *ACopyOf);
59  void expression(const std::string& AExprStr);
60  TNode<T> *expression() const;
61 };
62 
66 template<typename T>
67 class TConstant {
68 private:
69  std::string FName;
70  T FValue;
71 
72 public:
73  TConstant();
74  TConstant(const TConstant<T>&);
75  TConstant(const std::string& AName, const T& AValue = T());
76 
77  void name(const std::string&);
78  std::string name() const;
79 
80  void value(const T&);
81  T value() const;
82 };
83 
88 class ELibraryLookup : public EMath {
89 public:
90  ELibraryLookup(const std::string& AMsg) : EMath(AMsg) {}
91 };
92 
93 
99 template<typename T>
100 class TLibrary {
101 private:
102  typedef std::list<TFunction<T> > TFunctionList;
103  typedef std::list<TConstant<T> > TConstantList;
104 
105  TFunctionList FFunctions;
106  TConstantList FConstants;
107 
108  void removeIf(const std::string& AName, bool AReplaceIfExists);
109 
110 public:
111  TLibrary();
112  TLibrary(const TLibrary<T>&);
113 
115  void insert(const TFunction<T>&, bool AReplaceIfExists = false);
117  void insert(const TConstant<T>&, bool AReplaceIfExists = false);
118 
120  void remove(const std::string& AName);
121 
123  TFunction<T> function(const std::string& AName) const;
124 
126  TConstant<T> constant(const std::string& AName) const;
127 
129  bool hasFunction(const std::string& AName) const;
130 
132  bool hasConstant(const std::string& AName) const;
133 
135  T call(const std::string& AName, const T& AParam) const;
137  T value(const std::string& AName) const;
138 
140  unsigned functions() const;
142  unsigned constants() const;
143 };
144 
145 } // namespace math
146 
147 template<typename T> std::ostream& operator<<(std::ostream&, const math::TFunction<T>&);
148 
149 #include <math++/library.tcc>
150 
151 #endif