Math Type Library (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
32namespace math {
33
34template<class> class TNode;
35template<class> class TLibrary;
36
40template<typename T>
41class TFunction {
42private:
43 std::string FName;
44 TNode<T> *FExpression;
45
46public:
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
66template<typename T>
67class TConstant {
68private:
69 std::string FName;
70 T FValue;
71
72public:
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
88class ELibraryLookup : public EMath {
89public:
90 ELibraryLookup(const std::string& AMsg) : EMath(AMsg) {}
91};
92
93
99template<typename T>
100class TLibrary {
101private:
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
110public:
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
147template<typename T> std::ostream& operator<<(std::ostream&, const math::TFunction<T>&);
148
149#include <math++/library.tcc>
150
151#endif
void insert(const TConstant< T > &, bool AReplaceIfExists=false)
inserts given constant into library, it throws if it's duplicated
void insert(const TFunction< T > &, bool AReplaceIfExists=false)
inserts given function into library, it throws if it's duplicated
T value(const std::string &AName) const
value() look for a constant AName and returns its value. It throws on lookup error.
unsigned constants() const
returns the number of constants stored in this library.
bool hasFunction(const std::string &AName) const
returns true, if function (AName) exists
void remove(const std::string &AName)
removes function or constant called AName
bool hasConstant(const std::string &AName) const
returns true, if constant (AName) exists
unsigned functions() const
returns the number of functions stored in this library.
T call(const std::string &AName, const T &AParam) const
call() looks for a function AName calls it using AParam and returns its result. It throws on lookup e...
TFunction< T > function(const std::string &AName) const
returns reference to requested function, throws if not found
TConstant< T > constant(const std::string &AName) const
returns reference to requested constant, throws if not fuond